当前页面: 开发资料首页 → Netbeans 专题 → J2EE Application Clients in NetBeans IDE
摘要: J2EE Application Clients in NetBeans IDE
Due to resource constraints, NetBeans IDE 5.0 and earlier versions did not contain automatic support for application clients in J2EE 1.4 enterprise applications. This feature is now supported in NetBeans IDE 5.5 Beta and developement builds. You can create application clients in the New Enterprise Application wizard and add arbitrary J2SE projects to an enterprise application as an application client module. To download daily builds of NetBeans IDE 5.5, go to this page and select 5.5 as the release version and Daily as the build type.
If you are still using NetBeans 5.0 and earlier, some manual work is required. To make a regular Java application project to act like a J2EE application client, you have to manually create and configure some of the deployment descriptors and add some JAR files to the classpath.
The first question to ask is why use a full-fledged J2EE application client in the first place. After all, any Java application can access any EJB module through its remote interfaces. The main reason to register an application client in the enterprise application is that doing so gives the application access to services and functionality running on the enterprise application server.
The most common of these features is security. If your application client accesses methods that are guarded by security methods, you have to register the application client in the enterprise application and declare that it is part of the appropriate security role.
In this example, we will create an application client for the enterprise application from Building Secure Enterprise Beans in NetBeans IDE. You can download the finished enterprise application project here.
Before you begin, you have to install Sun Java System (SJS) Application Server Platform Edition 8.1 (download) on your computer. You cannot deploy enterprise applications to the bundled Tomcat server. You also have to register the application server in the IDE by choosing Tools > Server Manager.
In our example, only users from the bank_users can access our enterprise bean. We'll create the bank_users group's users in the file security realm on the SJS Application server.
In account manager is 250$
Our enterprise application consists of a simple session bean and a web client that attempts to access it. The application client will just attempt to invoke the getStatus method and pass it the username somename.
The Call Enterprise Bean command is not available in general Java application projects, so you have to enter the code that gets a reference to the enterprise bean's remote interfaces yourself.
... public static void main(String[] args) throws Exception { // TODO code application logic here Context ctx = new InitialContext(); Object objRef = ctx.lookup("java:comp/env/ejb/AccountStatusBean"); AccountStatusRemote account = ((AccountStatusRemoteHome)PortableRemoteObject.narrow(objRef, AccountStatusRemoteHome.class)).create(); System.out.println(account.getStatus("somename")); } ...
There are still a few steps we need to take to set up the project:
<?xml version="1.0" encoding="UTF-8"?> <application-client version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application-client_1_4.xsd"> <display-name>SecureClient</display-name> <ejb-ref> <ejb-ref-name>ejb/AccountStatusBean</ejb-ref-name> <ejb-ref-type>Session</ejb-ref-type> <home>ejb.AccountStatusRemoteHome</home> <remote>ejb.AccountStatusRemote</remote> </ejb-ref> </application-client>
This code just sets the display name of the module and registers the EJB reference for AccountStatusBean.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sun-application-client PUBLIC '-//Sun Microsystems, Inc.//DTD Application Server 8.1 Application Client 1.4//EN' 'http://www.sun.com/software/appserver/dtds/sun-application-client_1_4-1.dtd'> <sun-application-client> <ejb-ref> <ejb-ref-name>ejb/AccountStatusBean</ejb-ref-name> <jndi-name>ejb/AccountStatusBean</jndi-name> </ejb-ref> </sun-application-client>
This code just maps the EJB reference to an actual bean on the application server whose JNDI name is ejb/AccountStatusBean.
... </module> <module> <java>SecureClient.jar</java> </module> <security-role> ...
Now we have to make some changes to the build script to package the deployment descriptors and run it as a J2EE application client.
<!-- Makes sure the IDE can find the Application Server install directory and sets the appclient executable name depending on platform --> <target name="-pre-init"> <fail unless="com.sun.aas.installRoot"> J2EE home property - neither j2ee.home nor com.sun.aas.installRoot(NetBeans) is set. </fail> <property name="j2ee.home" value="${com.sun.aas.installRoot}" /> <condition property="appclient.name" value="appclient"> <and> <os family="unix"/> <os family="Linux"/> </and> </condition> <condition property="appclient.name" value="appclient.bat"> <and> <os family="Windows"/> </and> </condition> <condition property="package-appclient.name" value="package-appclient"> <and> <os family="unix"/> <os family="Linux"/> </and> </condition> <condition property="package-appclient.name" value="package-appclient.bat"> <and> <os family="Windows"/> </and> </condition> </target> <!-- Target that copies J2EE app client deployment descriptors to jar --> <target name="-pre-jar"> <!-- copy EJB interfaces --> <copy todir="${build.classes.dir}"> <fileset dir="${file.reference.build-jar}"> <include name="**/*.class"/> </fileset> </copy> <!-- copy configuration files --> <mkdir dir="${build.classes.dir}/META-INF" /> <copy todir="${build.classes.dir}/META-INF"> <fileset dir="${src.dir}/conf"> <include name="*.xml"/> </fileset> </copy> </target> <target name="run" depends="init,compile"> <exec executable="${j2ee.home}/bin/${appclient.name}"> <arg line="-client ${dist.jar}"/> </exec> </target> <target name="package-appclient" depends="init"> <exec executable="${j2ee.home}/bin/${package-appclient.name}" dir="${dist.dir}"/> </target>
In account somename is 250