Download
FAQ History |
API
Search Feedback |
Using JAXR Clients in J2EE Applications
You can create J2EE applications that use JAXR clients to access registries. This section explains how to write, compile, package, deploy, and run a J2EE application that uses JAXR to publish an organization to a registry and then query the registry for that organization. The application in this section uses two components: an application client and a stateless session bean.
The section covers the following topics:
You will find the source files for this section in the directory
<
INSTALL
>/j2eetutorial14/examples/jaxr/clientsession
. Path names in this section are relative to this directory.The following directory contains a built version of this application:
If you run into difficulty at any time, you can open the EAR file in
deploytool
and compare that file to your own version. You may have to add a new version of the properties file that specifies a different Tomcat port. (The properties file in the EAR file sets the port to 8082.)Coding the Application Client: MyAppClient.java
The application client class,
src/MyAppClient.java
, obtains a handle to thePubQuery
enterprise bean's remote home interface, using the JNDI API naming contextjava:comp/env
. The program then creates an instance of the bean and calls the bean's two business methods:executePublish
andexecuteQuery
.Before you compile the application, edit the
PubQueryBeanExamples.properties
file in the same way you edited theJAXRExamples.properties
file to run the simple examples.
- If you are using the Java WSDP Registry Server, specify the correct host and port values for the
queryManagerURL
andlifeCycleManagerURL
entries. To use another registry, comment out the property that specifies the Registry Server, and remove the comment from the other registry.- If you are using a public registry, change the values for the r
egistry.username
andregistry.password
properties to specify the user name and password you obtained when you registered with the registry. Change the values for thehttp.proxyHost
andhttps.proxyHost
entries so that they specify the system on your network through which you access the Internet.Coding the PubQuery Session Bean
The
PubQuery
bean is a stateless session bean that has onecreate
method and two business methods. The bean uses remote interfaces rather than local interfaces because it is accessed from outside the EJB container.The remote home interface source file is
src/PubQueryHome.java
.The remote interface,
src/PubQueryRemote.java
, declares two business methods:executePublish
andexecuteQuery
. The bean class,src/PubQueryBean.java
, implements theexecutePublish
andexecuteQuery
methods and their helper methodsgetName
,getDescription
, andgetKey
. These methods are very similar to the methods of the same name in the simple examplesJAXRQuery.java
andJAXRPublish.java
. TheexecutePublish
method uses information in the filePubQueryBeanExample.properties
to create an organization named The Coffee Enterprise Bean Break. TheexecuteQuery
method uses the organization name, specified in the application client code, to locate this organization.The bean class also implements the required methods
ejbCreate
,setSessionContext
,ejbRemove
,ejbActivate
, andejbPassivate
.The
ejbCreate
method of the bean class allocates resources--in this case, by looking up theConnectionFactory
and creating theConnection
.The
ejbRemove
method must deallocate the resources that were allocated by theejbCreate
method. In this case, theejbRemove
method closes theConnection
.Compiling the Source Files
To compile the application source files, go to the directory
<
INSTALL
>/j2eetutorial14/examples/jaxr/clientsession
. Use the following command:The
compile
target places the properties file and the class files in thebuild
directory.Importing Certificates
If you will be using the Java WSDP Registry Server, skip this section.
In order to run the
ClientSessionApp
application against the Microsoft or IBM registry, you need to import certificates from your version of the Java 2, Standard Edition Software Development Kit (J2SE SDK) into the Application Server. The simple client programs use the J2SE SDK certificates, but the Application Server does not have these certificates, so running a J2EE application that uses JAXR against an external registry requires special steps.
- Verify the alias names of the Certificate Authorities (CA) you want to migrate by running the following command:
keytool -list -v -keystore
J2SE_SDK_truststore_file
The default location for
J2SE_SDK_truststore_file
is<
JAVA_HOME
>/jre/lib/security/cacerts
.To access the Microsoft registry, you need the CA with the alias name
verisignclass3ca
. To access the IBM registry, you need the CA with the alias nameverisignserverca
.- Export the CA with the desired alias name from the J2SE SDK truststore to a file in the current directory:
keytool -export -alias
alias_name
-keystore
J2SE_SDK_truststore_file
-file
export_CA_file
When you are asked for a password, type
changeit
.For example, you could type the following (all on one line) to export the Microsoft CA:
keytool -export -alias verisignclass3ca -keystore
C:\j2sdk1.4.2_04\jre\lib\security\cacerts -file ca_for_ms- Import the
export_CA_file
into the Application Server truststore:
keytool -import -alias
alias_name
-storepass changeit
-keystore <INSTALL
>/domains/domain1/config/cacerts.jks
-fileexport_CA_file
When you are asked, "Trust this certificate?", type
yes
.For example, you could type the following (all on one line) to import the CA you just exported:
keytool -import -alias verisignclass3ca -storepass changeit -keystore
C:\Sun\AppServer\domains\domain1\config\cacerts.jks
-file ca_for_ms- If the Application Server was running, stop and restart it.
Starting the Application Server
To run this example, you need to start the Application Server. Follow the instructions in Starting and Stopping the Application Server.
Creating JAXR Resources
To use JAXR in a J2EE application that uses the Application Server, you need to access the JAXR resource adapter (see Implementing a JAXR Client) through a connector connection pool and a connector resource. You can create these resources in the Admin Console.
If you have not done so, start the Admin Console as described in Starting the Admin Console.
To create the connector connection pool, perform the following steps:
To create the connector resource, perform the following steps:
If you are in a hurry, you can create these objects using the following
asant
target in thebuild.xml
file for this example:Creating and Packaging the Application
Creating and packaging this application involve four steps:
Starting deploytool and Creating the Application
- Start
deploytool
. On Windows systems, choose StartProgramsSun MicrosystemsJ2EE 1.4 SDKDeploytool. On UNIX systems, use thedeploytool
command.- Choose FileNewApplication.
- Click Browse (next to the Application File Name field), and use the file chooser to locate the directory
clientsession
.- In the File Name field, type
ClientSessionApp
.- Click New Application.
- Click OK.
Packaging the Session Bean
- Choose FileNewEnterprise Bean to start the Enterprise Bean wizard. Then click Next.
- In the EJB JAR General Settings screen:
- Select Create New JAR Module in Application, and make sure that the application is
ClientSessionApp
.- In the JAR Name field, type
PubQueryJAR
.- Click Edit Contents.
- In the dialog box, locate the
clientsession/build
directory. SelectPubQueryBean.class
,PubQueryHome.class
,PubQueryRemote.class
, andPubQueryBeanExample.properties
from the Available Files tree area. Click Add, and then OK.- In the Bean General Settings screen:
- From the Enterprise Bean Class menu, choose
PubQueryBean
.- Verify that the Enterprise Bean Name is
PubQueryBean
and that the Enterprise Bean Type is Stateless Session.- In the Remote Interfaces area, choose
PubQueryHome
from the Remote Home Interface menu, and choosePubQueryRemote
from the Remote Interface menu.After you finish the wizard, perform the following steps:
Packaging the Application Client
- Choose FileNewApplication Client to start the Application Client Wizard. Then click Next.
- In the JAR File Contents screen:
- Make sure that Create New AppClient Module in Application is selected and that the application is
ClientSessionApp
.- In the AppClient Name field, type
MyAppClient
.- Click Edit Contents.
- In the dialog box, locate the
clientsession/build
directory. SelectMyAppClient.class
from the Available Files tree area. Click Add, and then OK.- In the General screen, select
MyAppClient
in the Main Class combo box.After you finish the wizard, click the EJB Ref's tab, and then click Add in the inspector pane. In the dialog box, follow these steps:
- Type
ejb/remote/PubQuery
in the Coded Name field.- Choose Session from the EJB Type menu.
- Choose Remote from the Interfaces menu.
- Type
PubQueryHome
in the Home Interface field.- Type
PubQueryRemote
in the Local/Remote Interface field.- In the Target EJB area, select JNDI Name and type
PubQueryBean
in the field. The session bean uses remote interfaces, so the client accesses the bean through the JNDI name rather than the bean name.Checking the JNDI Names
Select the application, click Sun-specific Settings on the General page, and verify that the JNDI names for the application components are correct. They should appear as shown in Tables 10-3 and 10-4.
Table 10-3 Application Pane for ClientSessionApp Component Type Component JNDI Name EJB PubQueryBean PubQueryBean
Table 10-4 References Pane for ClientSessionApp Ref. Type Referenced By Reference Name JNDI Name EJB Ref MyAppClient ejb/remote/PubQuery PubQueryBean Resource PubQueryBean eis/JAXR eis/JAXR
Deploying the Application
- Save the application.
- Choose ToolsDeploy.
- In the dialog box, type your administrative user name and password (if they are not already filled in), and click OK.
- In the Application Client Stub Directory area, select the Return Client Jar checkbox, and make sure that the directory is
clientsession
.- Click OK.
- In the Distribute Module dialog box, click Close when the process completes. You will find a file named
ClientSessionAppClient.jar
in the specified directory.Running the Application Client
- If you are using the Java WSDP Registry Server, start it by starting Tomcat.
Windows systems: Choose StartProgramsJava(TM) Web Services Developer Pack 1.3Start Tomcat.
UNIX systems:
<
JWSDP_HOME
>/bin/startup.sh
Starting Tomcat takes some time.
- To run the client, use the following command:
appclient -client ClientSessionAppClient.jar
The program output in the terminal window looks like this:
In the server log, you will find the output from the
executePublish
andexecuteQuery
methods, wrapped in logging information.After you run the example using a public registry, use the
run-delete
target in thesimple
directory to delete the organization that was published.
Download
FAQ History |
API
Search Feedback |
All of the material in The J2EE(TM) 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.