![]() | ![]() | ![]() | ![]() | ![]() | ![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ![]() | ![]() | ![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ![]()
![]() ![]() |
Abstract Before we start Referencing EJBs in web application Installing OpenEJB's EJB factory in Tomcat Example application AbstractJava 2 Enterprise Edition (J2EE) provides several technologies that lets build J2EE-compliant applications that can be run in any J2EE-compliant application server. One of the several technologies is Java Naming and Directory Interface (JNDI). JNDI is a technology that provides a unified access to different naming and directory services. Regardless of the underlaying service, if it supports JNDI a client doesn't have to know what exactly application it's to talk to other than it adheres to JNDI concepts and interfaces. OpenEJB and Tomcat can complement each other in fullfiling J2EE principles. OpenEJB is an EJB container whereas Tomcat is a servlet container. Although they serve different clients, both products use JNDI extensively. Obviously, either container provides different JNDI "views" of its managed components. The question pertaining to the integration is how to tie the different naming systems so that when a client requests an object from Tomcat naming space, Tomcat will know that it needs to pass the request along to OpenEJB naming space. That's what the object factory is to solve out. While reading JNDI specification you can come across the interface: javax.naming.spi.ObjectFactory. The javadoc of the interface reads: This interface represents a factory for creating an object. The JNDI framework allows for object implementations to be loaded in dynamically via object factories. For example, when looking up a printer bound in the name space, if the print service binds printer names to References, the printer Reference could be used to create a printer object, so that the caller of lookup can directly operate on the printer object after the lookup. Theorically, it seems to be possible to create a web application that makes use of EJBs, deploy these EJBs in OpenEJB, and the web application in Tomcat, set up an object factory and finally boot it up. The following sections are to outline what exactly the steps are in OpenEJB and Tomcat with description of why they are required at all. Before we startBefore we start, ensure that OpenEJB and Tomcat are in appropriate releases.
Referencing EJBs in web applicationJ2EE 1.3 specification says (page 57):
It means that each time a web application needs to reference a bean, the bean has to be declared in the web application's deployment descriptor ( /WEB-INF/web.xml file). The element which does so is ejb-ref. Although most containers don't enforce that approach, it's always better to describe dependencies in a standard, J2EE-compliant way, in the deployment descriptor of the corresponding components.
Our example's web application declares the referenced bean in the deployment descriptor ( /WEB-INF/web.xml) as follows:
Here goes a JSP that uses the ejb-ref-name to get a reference to the declared bean.
As it's shown in the sample JSP above, there is no direct reference to the EJB container to be used. The application component provider doesn't have to know what container the EJB is to be deployed to. What it does have to ascertain is that the name the JSP uses is actually an ejb reference. Moreover, the reference should be the one to HelloHome home interface or otherwise the application will fail. Installing OpenEJB's EJB factory in Tomcat
The J2EE 1.3 specification says (page 56):
It means that after an Application Component Provider (in the document, it's you) has created a web application and specified an EJB reference in the deployment descriptor, a deployer (you, too) has to bind the reference in Tomcat. The task boils down to put appropriate entry in Tomcat's configuration file. Tomcat's default configuration file is server.xml in $CATALINA_HOME/conf directory. According to server.xml's documentation it's possible to create an appropriate definition of a factory and let the factory to handle requests for the objects it is registered to handle. That's what OpenEJB's EJB factory does. It intercepts messages for EJBs, passes it along to OpenEJB and returns requested bean. Tomcat registers resource factories in GlobalNamingResources or Context elements which demarcate the name space of different web applications.
The following snippet shows the elements necessary to register the EJB reference, which has been defined in the web application's deployment descriptor. The document doesn't describe the particular elements of server.xml file other than ResourceParams parameters. The reader shall read the Tomcat documentation to get acquainted with the file's structure.
ResourceParams parameters consitute a bean reference. They inform Tomcat about who will know how to handle
requests for the bean.
The following parameters are only required when
openejb.naming.factory.initial is set to
org.openejb.client.RemoteInitialContextFactory.
The factory parameter indicates the class which passes a request for a bean to OpenEJB instance. In order for the class to be instantiated by Tomcat, OpenEJB Loader has to be installed. The Loader takes care of loading necessary classes from OpenEJB directory. It finds the directory relying upon OPENEJB_HOME environament variable. Create a file named setenv.sh (or setenv.bat on MS Windows) in $CATALINA_HOME/bin directory with the following content:
Finally, add the OpenEJB Loader to Tomcat.
That's it. Tomcat is now fully configured to work with OpenEJB. Don't forget to start up OpenEJB instance if the factory's been configured with RemoteInitialContextFactory. Example applicationYou can use the OpenEJB Hello World as an example EJB to test things out. You won't need to do anything differently. Deploy the myHelloEjb.jar just as described in http://openejb.sourceforge.net/hello-world.html Register the factory in Tomcat by editing server.xml, i.e. add the above Ejb element declaration between Context's tags of Tomcat's default example web application. Place the JSP - openejb.jsp - in $CATALINA_HOME/webapps/examples directory. Start up Tomcat and enter http://localhost:8080/examples/openejb.jsp. As a result, you should see "Hello World!!!!!" on the screen. Tomcat should print out the following on its console (note OpenEJB messages about its startup):
| ![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | ![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |