当前页面: 开发资料首页 → Netbeans 专题 → 使用 NetBeans IDE 4.1 创建 J2EE 应用程序客户端
摘要: 使用 NetBeans IDE 4.1 创建 J2EE 应用程序客户端 John Jullion-Ceccarelli 和 Petr Blaha 由于资源限制,NetBeans IDE 4.1 没有包...
John Jullion-Ceccarelli 和 Petr Blaha
由于资源限制,NetBeans IDE 4.1 没有包含对企业应用程序中应用程序客户端的自动支持。该功能将在即将到来的版本中得到支持。若要将常规的 Java 应用程序项目用作 J2EE 应用程序客户端,必须手动创建并配置一些部署描述符,然后将一些 JAR 文件添加到类路径。
第一个要问的问题是,为什么首先要使用完全成熟的 J2EE 应用程序客户端。毕竟,任何 Java 应用程序可以通过其远程接口来访问任何 EJB 模块。在企业应用程序中注册应用程序客户端的主要原因是,这样做让应用程序可以访问运行在企业应用程序服务器上的服务和功能。
这些功能中最普通的就是安全性。如果应用程序客户端访问受安全方法保护的方法,则必须在企业应用程序中注册应用程序客户端,并声明它是适当安全角色的一部分。
在本例中,我们将为来自 Building Secure Enterprise Beans in NetBeans IDE 的企业应用程序创建应用程序客户端。您可以在这里下载已完成的企业应用程序项目。
开始之前,必须在计算机上安装 Sun Java System (SJS) Application Server Platform Edition 8.1(下载)。您无法将企业应用程序部署到绑定的 Tomcat 服务器。还必须在 IDE 中注册应用程序服务器,通过选择 Tools > Server Manager。
在我们的示例中,仅 bank_users 的用户可以访问我们的企业 bean。我们将在 SJS Application 服务器上的 file 安全领域中创建 bank_users 组的用户。
In account manager is 250$
我们企业应用程序包括一个简单的会话 bean 和尝试访问它的 web 客户端。应用程序客户端仅尝试调用 getStatus 方法,并向它发送用户名 somename。
Call Enterprise Bean 命令不能用于通用 Java 应用程序项目,因此您必须自己输入获得企业 bean 的远程接口参考的代码。
... 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")); } ...
需要执行一些步骤来设置项目:
<?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>
该代码仅设置了模块的显示名称,并注册 AccountStatusBean 的 EJB 参考。
<?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>
该代码仅将 EJB 参考(JNDI 名称为 ejb/AccountStatusBean)映射到应用程序服务器上的实际 bean。
... </module> <module> <java>SecureClient.jar</java> </module> <security-role> ...
现在必须对构建脚本做些修改来打包部署描述符,然后作为 J2EE 应用程序客户端运行。
<!-- 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