站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Netbeans 专题J2EE Application Clients in NetBeans IDE

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.

Getting Started

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.

Configuring the User and Group on the Application Server

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.

  1. Start the application server by right-clicking its node in the Runtime window and choosing Start/Stop Server.
  2. Right-click the application server node and choose Show Admin Console. Log into the admin console and select Configuration > Security > Realms > file in the left navigation bar.
  3. Click the Manage Users button in the center frame of the admin console. Under Current Users, click the New button.
  4. Type manager for the User ID, password for the Password, and bank_users for the Group List. Then click OK.

File Users panel in admin console

Opening the Enterprise Application

  1. Download the completed enterprise application and unzip it somewhere on your system.
  2. Press Ctrl-Shift-O to open the Secure project. Make sure the Open Required Projects checkbox is selected. The IDE should open three projects:
    • Secure
    • Secure-EJBModule
    • Secure-WebModule
  3. Right-click the Secure project and choose Run Project. The IDE builds and deploys the project and opens the AccountState servlet in the external browser.
  4. Enter the user name (manager) and password (password) in the dialog box and click OK. The servlet should return the following output:
    In account manager is 250$

Coding the Application Client

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.

Setting Up the Application Client Project

  1. Choose File > New Project (Ctrl-Shift-N) and select the Java Application template from the General category. Click Next.
  2. Name the application SecureClient and specify a location for the project. Leave the rest of the settings at their default values and click Finish. The IDE creates the project and opens secureclient.Main.java in the Source Editor.
  3. Right-click the SecureClient project's Libraries node and choose Add JAR/Folder to add the following to the classpath:
    • The j2ee.jar file from your application server's lib directory
    • The dist/jar directory from the Secure-EJBModule project

Coding the Application Client

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.

  1. In Main.java, add the following code in bold to the main method:
    ...
        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"));
        }
    ...
  2. Press Alt-Shift-F to generate any needed import statements.

Configuring the Deployment Descriptors and Build Script

There are still a few steps we need to take to set up the project:

Creating the Application Client Deployment Descriptors

  1. Create a folder called conf in the SecureClient/src folder.
  2. Create an XML file called application-client.xml in the SecureClient/src/conf folder. (If you create the file using the New XML wizard, use the Well-Formed XML Document option.)
  3. Change the body of application-client.xml to the following:
    <?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.

  4. Create an XML file called sun-application-client.xml in the SecureClient/src/conf folder.
  5. Change the body of sun-application-client.xml to the following:
    <?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.

  6. Save the files.

Configuring the Enterprise Application Deployment Descriptor

  1. In the Projects window, expand the Configuration Files node for the Secure project, right-click application.xml, and choose Edit.
  2. Add the following code in bold to the root application element:
    ...
      </module>
      <module>
        <java>SecureClient.jar</java>
      </module>
      <security-role>
    ...
  3. Save the file.

Packaging the Application Client JAR File in the Enterprise Application

  1. Right-click the Secure project node and choose Properties.
  2. Select Build > Packaging in the left panel of the dialog box.
  3. In Additional Content to Include, click Add Project and add the project directory for the SecureClient project.

Configuring the Build Script

Now we have to make some changes to the build script to package the deployment descriptors and run it as a J2EE application client.

  1. In the Files window, expand the SecureClient node and double-click build.xml.
  2. Add the following targets to build.xml:
        <!--
          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>
    
  3. Save the file.

Running the Application

  1. Clean and build the Secure enterprise application, then deploy it to the application server.
  2. Right-click the SecureClient project node and choose Run project.
  3. Enter the user name (manager) and password (password) in the dialog box and click OK. The Output window should display the following output:
    In account somename is 250

↑返回目录
前一篇: Integrating NetBeans with other J2EE Server Vendors
后一篇: J2EE BluePrints in NetBeans IDE