Sun Java System Application Server Platform Edition 2004Q4 Beta Developer's Guide |
Chapter 9
Developing Java ClientsThis chapter describes how to develop, assemble, and deploy J2EE Application Clients in the following sections:
Introducing the Application Client ContainerThe Application Client Container (ACC) includes a set of Java classes, libraries, and other files that are required for and distributed with Java client programs that execute in their own Java Virtual Machine (JVM). The ACC manages the execution of J2EE application client components, which are used to access a variety of J2EE services (such as JMS resources, EJB components, web services, security, and so on.) from a JVM outside the Sun Java System Application Server.
The ACC communicates with the Application Server using RMI-IIOP protocol and manages the details of RMI-IIOP communication using the client ORB that is bundled with it. Compared to other J2EE containers, the ACC is lightweight.
Security
The ACC is responsible for collecting authentication data such as the username and password and sending the collected data to the Application Server. The server then processes the authentication data using the configured JavaTM Authentication and Authorization Service (JAAS) module. See “Authenticating an Application Client Using the JAAS Module” on page 59.
Authentication techniques are provided by the client container, and are not under the control of the application client component. The container integrates with the platform’s authentication system. When you execute a client application, it displays a login window and collects authentication data from the user. It also supports SSL (Secure Socket Layer)/IIOP if configured and when necessary.
Naming
The client container enables the application clients to use the Java Naming and Directory Interface (JNDI) to look up J2EE services (such as JMS resources, EJB components, web services, security, and so on.) and to reference configurable parameters set at the time of deployment.
Developing Clients Using the ACCThis section describes the procedure to develop, assemble, and deploy client applications using the ACC. This section describes the following topics:
For information about Java-based clients that are not packaged using the ACC, see Developing Clients Without the ACC.
Using an Application Client to Access an EJB Component
To access an EJB component from an application client, perform the following steps:
- In your client code, instantiate the InitialContext using the default (no argument) constructor:
InitialContext ctx = new InitialContext();
It is not necessary to explicitly instantiate a naming context that points to the CosNaming service.
- In your client code, look up the home object by specifying the JNDI name of the home object as specified in the ejb-jar.xml file. For example:
Object ref = ctx.lookup("java:comp/env/ejb-ref-name");
BeanAHome = (BeanAHome)PortableRemoteObject.narrow(ref,BeanAHome.class);For more information about naming and lookups, see “Accessing the Naming Context” on page 343.
- Define the ejb-ref elements in the application-client.xml file and the corresponding sun-application-client.xml file.
For more information on the sun-application-client.xml file, see “The sun-application-client.xml file” on page 138. For a general explanation of how to map JNDI names using reference elements, see “Mapping References” on page 347.
- Deploy the application client and EJB component together in an application. For more information on deployment, see “Tools for Deployment” on page 92. To get the client JAR file, use the --retrieve option.
To retrieve the stubs and ties whether or not you requested their generation during deployment, use the asadmin get-client-stubs command. See the Sun Java System Application Server Utility Reference.
- Ensure that the client JAR file includes the following files:
- a Java class to access the bean
- application-client.xml - J2EE 1.4 application client deployment descriptor
- sun-application-client.xml - Sun Java System Application Server specific client deployment descriptor. For information on the sun-application-client.xml file, see “The sun-application-client.xml file” on page 138.
- The MANIFEST.MF file. This file contains the main class, which states the complete package prefix and classname of the Java client.
You can package the application client using the package-appclient script. This is optional. See Packaging an Application Client Using the ACC.
- Copy the following JAR files to the client machine and include them in the classpath on the client side:
- To access EJB components that are residing in a remote system other than the system where the application client is being developed, make the following changes to the sun-acc.xml file:
- Define the target-server element’s address attribute to reference the remote server machine.
- Define the target-server element’s port attribute to reference the ORB port on the remote server.
This information can be obtained from the domain.xml file on the remote system. For more information on domain.xml file, see the Sun Java System Application Server Reference.
- Run the application client. See Running an Application Client Using the ACC.
Using an Application Client to Access a JMS Resource
To access a JMS resource from an application client, perform the following steps:
- Create a JMS client. For detailed instructions on developing a JMS client, see the J2EE tutorial:
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JMS.html#wp84181
- Next, configure a JMS resource on the Sun Java System Application Server. For information on configuring JMS resources, see “Creating JMS Resources: Destinations and Connection Factories” on page 355.
- Define the resource-ref elements in the application-client.xml file and the corresponding sun-application-client.xml file.
For more information on the sun-application-client.xml file, see “The sun-application-client.xml file” on page 138. For a general explanation of how to map JNDI names using reference elements, see “Mapping References” on page 347.
- Ensure that the client JAR file includes the following files:
- A Java class to access the resource.
- application-client.xml - J2EE 1.4 application client deployment descriptor.
- sun-application-client.xml - Sun Java System Application Server specific client deployment descriptor. For information on the sun-application-client.xml file, see “The sun-application-client.xml file” on page 138.
- The MANIFEST.MF file. This file contains the main class, which states the complete package prefix and classname of the Java client.
You can package the application client using the package-appclient script. This is optional. See Packaging an Application Client Using the ACC.
- Copy the following JAR files to the client machine and include them in the classpath on the client side:
- Run the application client. See Running an Application Client Using the ACC.
Running an Application Client Using the ACC
To run an application client, launch the ACC using the appclient script.
appclient -client client_application_jar [-mainclass client_application_main_class_name|-name display_name][-xml sun-acc.xml] [-textauth] [-user user_name] [-password password] [app_args]
- -client: Specifies the name and location of the client JAR file. This is a required parameter.
- -mainclass: Specifies the class name whose main() method is to be invoked. By default, the class specified in the client JAR’s Main-class attribute of the MANIFEST file is used. This is optional.
- -name: Specifies the display name that is located in the client JAR file. By default, the display name is specified in the client JAR application-client.xml file and identified by the display-name attribute. This is optional.
Note
The -mainclass and -name parameters are optional for a single client application. For multiple client applications, you must use either the -classname option or the -name option.
- -xml: Specifies the name and location of the ACC configuration XML file, which is required if you are not using the default domain. By default, the ACC uses domain_dir/config/sun-acc.xml for clients running on the application server, or install_dir/lib/appclient/sun-acc.xml for clients that are packaged using the package-applclient script.
- -textauth: Specifies authentication using the text format. This is optional.
- -user: Specifies the user.
- -password: Specifies the user’s password.
- app_args: Specifies the arguments passed to the client’s main() method. This is optional.
The following example shows how to run the sample application client, rmiConverter:
appclient -client rmi-simpleClient.jar
Packaging an Application Client Using the ACC
The package-appclient script, located in the install_dir/bin directory, is used to package a client application into a single appclient.jar file. Packaging an application client involves the following main steps:
Editing the Configuration File
Modify the environment variables in asenv.conf file located in the default-config_dir directory as shown below:
- $AS_INSTALL to reference the location where the package was un-jared plus /appclient. For example: $AS_INSTALL=/install_dir/appclient.
- $AS_NSS to reference the location of the nss libs. For example:
- $AS_JAVA to reference the location where the JDK is installed.
- $AS_ACC_CONFIG to reference the configuration xml (sun-acc.xml). The sun-acc.xml is located at install_dir/config.
- $AS_IMQ_LIB to reference the imq home. Use domain_dir/imq/lib.
Editing the appclient Script
Modify the appclient script file as follows:
UNIX:
Change $CONFIG_HOME/asenv.conf to your_ACC_dir/config/asenv.conf.
Windows:
Change %CONFIG_HOME%\config\asenv.bat to your_ACC_dir\config\asenv.bat
Editing the sun-acc.xml File
Modify sun-acc.xml file to set the following attributes:
- Ensure that the DOCTYPE references %%%SERVER_ROOT%%%/lib/dtds to your_ACC_dir/lib/dtds.
- Ensure that the <target-server> address attribute references the remote server machine.
- Ensure that the <target-server> port attribute references the ORB port on the remote server.
- To log the messages in a file, specify a file name for the log-service element’s file attribute. You can also set the log level. For example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE client-container SYSTEM "file:{Your installed server root}/lib/dtds/sun-application-client-container_1_0.dtd">
<client-container>
<target-server name="qasol-e1" address="qasol-e1" port="3700">
<log-service level="WARNING"/>
</client-container>
For more information on the sun-acc.xml file, see “The sun-acc.xml File” on page 139.
Setting Security Options
You can run the application client using SSL with certificate authentication. To set the security options, modify the sun-acc.xml file as shown in the code illustration below. For more information on the sun-acc.xml file, see “The sun-acc.xml File” on page 139.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE client-container SYSTEM "file:////opt/SUNWappserver/lib/dtds/sun-application-client-container_1_0.dtd">
<client-container>
<target-server name="qasol-e1" address="qasol-e1" port="3700">
<security>
<ssl cert-nickname="cts"
ssl2-enabled="false"
ssl2-ciphers="-rc4,-rc4export,-rc2,-rc2export,-des,-desede3"
ssl3-enabled="true"
ssl3-tls-ciphers="+rsa_rc4_128_md5,-rsa_rc4_40_md5,+rsa3_des_sha,+rsa_des_sha,-rsa_rc2_40 _md5,-rsa_null_md5,-rsa_des_56_sha,-rsa_rc4_56_sha"
tls-enabled="true"
tls-rollback-enabled="true"/>
<cert-db path="ignored" password="ignored"/> <!-- not used -->
</security>
</target-server>
<client-credential user-name="j2ee" password="j2ee"/>
<log-service level="WARNING"/>
</client-container>
Using the package-appclient Script
The following steps describe the procedure to use the package-appclient script that is bundled with Sun Java System Application Server:
- Under install_dir/bin directory, run the package-appclient script. This creates an appclient.jar file and stores it under install_dir/lib/appclient/ directory.
- Copy the install_dir/lib/appclient/appclient.jar file to the desired location. The appclient.jar file contains the following files:
- appclient/bin - contains the appclient script used to launch the ACC.
- appclient/lib - contains the JAR and runtime shared library files.
- appclient/lib/appclient - contains the following files:
- sun-acc.xml - the ACC configuration file.
- client.policy file- the security manager policy file for the ACC.
- appclientlogin.conf file - the login configuration file.
- client.jar file - is created during the deployment of the client application.
- appclient/lib/dtds - contains sun-application_client-container_1_0.dtd which is the DTD corresponding to sun-acc.xml.
client.policy
client.policy file is the J2SE policy file used by the application client. Each application client has a client.policy file. The default policy file limits the permissions of J2EE deployed application clients to the minimal set of permissions required for these applications to operate correctly. If an application client requires more than this default set of permissions, edit the client.policy file to add the custom permissions that your application client needs. Use the J2SE standard policy tool or any text editor to edit this file.
For more information on using the J2SE policy tool, visit the following URL:
http://java.sun.com/docs/books/tutorial/security1.2/tour2/index.html
For more information about the permissions you can set in the client.policy file, visit the following URL:
http://java.sun.com/j2se/1.4/docs/guide/security/permissions.html
Developing Clients Without the ACCThis section describes the procedure to create, assemble, and deploy a Java-based client that is not packaged using the Application Client Container (ACC). This section describes the following topics:
For information about using the ACC, see Developing Clients Using the ACC.
Using a Stand-Alone Client to Access an EJB Component
To access an EJB component from a stand-alone client, perform the following steps:
- In your client code, instantiate the InitialContext:
InitialContext ctx = new InitialContext();
It is not necessary to explicitly instantiate a naming context that points to the CosNaming service.
- In the client code, look up the home object by specifying the interoperable name of the home object. For example:
Object ref = ctx.lookup("beanA_interop_name");
BeanAHome = (BeanAHome)PortableRemoteObject.narrow(ref,BeanAHome.class);For more information about naming and lookups, see “Accessing the Naming Context” on page 343.
- Deploy the EJB component to be accessed. For more information on deployment, see “Tools for Deployment” on page 92.
- Copy the following JAR files to the client machine and include them in the classpath on the client side:
- To access EJB components that are residing in a remote system other than the system where the application client is being developed, set the values for the Java Virtual Machine startup options:
jvmarg value = "-Dorg.omg.CORBA.ORBInitialHost=${ORBhost}"
jvmarg value = "-Dorg.omg.CORBA.ORBInitialPort=${ORBport}"Here ORBhost is the Application Server hostname and ORBport is the ORB port number (default is 3700).
This information can be obtained from the domain.xml file on the remote system. For more information on domain.xml file, see the Sun Java System Application Server Reference.
- Run the stand-alone client. As long as the client environment is set appropriately and the JVM is compatible, you merely need to run the main class.
Using a Stand-Alone Client to Access a JMS Resource
To access a JMS resource from a stand-alone client, perform the following steps:
- Create a JMS client. For detailed instructions on developing a JMS client, see the J2EE tutorial:
http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JMS.html#wp84181
- Next, configure a JMS resource on the Sun Java System Application Server. For information on configuring JMS resources, see “Creating JMS Resources: Destinations and Connection Factories” on page 355.
- Copy the following JAR files to the client machine and include them in the classpath on the client side:
- Set the values for the Java Virtual Machine startup options:
jvmarg value = "-Dorg.omg.CORBA.ORBInitialHost=${ORBhost}"
jvmarg value = "-Dorg.omg.CORBA.ORBInitialPort=${ORBport}"Here ORBhost is the Application Server hostname and ORBport is the ORB port number (default is 3700).
This information can be obtained from the domain.xml file. For more information on domain.xml file, see the Sun Java System Application Server Reference.
- Run the stand-alone client. As long as the client environment is set appropriately and the JVM is compatible, you merely need to run the main class.