David's notes on setting up Tomcat with OpenEJB

|  |  |  | OpenEJB OpenJMS OpenORB Castor Tyrex |

Main
Welcome!
Download
Mailing Lists
The Team

Users
Quickstart
Hello World!
CMP Example
CMP Guide
Deploy
Startup
Validation
Configuration
Support
Request Feature
FAQ

Servers
Local Server
Remote Server
Tomcat

Integrators
Why OpenEJB
Overview
Design
Specification
Presentation

Developers
Custom Services
Release Plan
Source Code
SourceForge

David's notes on setting up Tomcat with OpenEJB

Notes
Before we start
Installing Everything
Install Tomcat
Install OpenEJB
Hook them together
Example Servlet

## Notes

These are my notes from writing the Tomcat/OpenEJB integration. These are the minimum steps for integrating OpenEJB into Tomcat. The commands in this document are specifically for a bash shell in either Linux, Unix, Mac OS X or Windows with Cygwin.

The steps will be the same for the Windows Command Prompt, but the syntax of the commands themselves will obviously be different. Aside from that, there isn't a whole lot of information on what is going on behind the sences. So, I encourage *anyone* to feel completely welcome (perhaps obligated) to rewrite this document.

Anyone...anyone...Bueller...Bueller....

Regardless, this should help people get started. So here goes.

## Before we start

We always tell users to send us info on their OS and what version of OpenEJB, Java, or any other programs they are using when they submit support requests. So, I guess it's only fair I do the same.

Here is some information about my setup

    dblevins@Miles /home/j2ee
    $ pwd
    /home/j2ee

    dblevins@Miles /home/j2ee
    $ uname -a
    CYGWIN_NT-5.0 Miles 1.3.12(0.54/3/2) 2002-07-06 02:16 i686 unknown

    dblevins@Miles /home/j2ee
    $ java -version
    java version "1.3.1"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
    Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)

## Installing Everything

### Install Tomcat

If you don't already have Tomcat on your machine, download the Tomcat version of your choice from Apache we recommend the latest version as it has the most up-to-date libraries) In our example, we'll be using version 4.1.12.

http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/

Unpack Tomcat

    dblevins@Miles /home/j2ee
    $ tar xzvf jakarta-tomcat-4.1.12.tar.gz

### Install OpenEJB

You need OpenEJB 0.9.0 or higher, any older versions of OpenEJB will not work. You can get that here: http://openejb.sf.net/download.html

Unpack OpenEJB

    dblevins@Miles /home/j2ee
    $ tar xzvf openejb-0.9.0.tar.gz

### Hook them together

Go to the Tomcat Dir

    dblevins@Miles /home/j2ee
    $ cd jakarta-tomcat-4.1.12

Pass the OPENEJB_HOME dir into Tomcat using the CATALINA_OPTS environment variable

    dblevins@Miles /home/j2ee/jakarta-tomcat-4.1.12
    $ export CATALINA_OPTS='-Dopenejb.home=/home/j2ee/openejb-0.9.0'

Add the OpenEJB Loader to Tomcat

    dblevins@Miles /home/j2ee/jakarta-tomcat-4.1.12
    $ cp ../openejb-0.9.0/dist/openejb_loader-0.9.0.war webapps/

At this point, you're actually finished. All the libraries you need are in place. Your servlets should use the following InitialContextFactory when creating an InitialContext to lookup beans

    org.openejb.client.LocalInitialContextFactory

The openejb.home is taken care of for us with the CATALINA_OPTS variable, so no other JNDI options are needed to get an InitialContext from OpenEJB

## Example Servlet

You 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.

http://openejb.sourceforge.net/hello-world.html

Once deployed, the myHelloEjb.jar should be in the beans directory.

    openejb-0.9.0/beans/myHelloEjb.jar

You can use the HelloOpenEJB servlet below as an example of a servlet that looks up beans from OpenEJB.

It uses the HelloBean created in the OpenEJB Hello World document, so you'll need to run through that first. You can put the servlet in the following webapps directory

    jakarta-tomcat-4.1.12/webapps/examples/WEB-INF/classes

HelloOpenEJB.java

    import javax.servlet.http.*;
    import javax.naming.*;
    import javax.rmi.PortableRemoteObject;

    import org.acme.*;

    public class HelloOpenEJB extends HttpServlet {

        String factory = "org.openejb.client.LocalInitialContextFactory";

        public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
        {
        PrintWriter out = response.getWriter();

        try{
            Properties p = new Properties();
            p.put(Context.INITIAL_CONTEXT_FACTORY, factory );
            InitialContext ctx = new InitialContext( p );

            //Lookup the bean using it's deployment id
            Object obj = ctx.lookup("/Hello");

            HelloHome ejbHome = (HelloHome)
               PortableRemoteObject.narrow(obj, HelloHome.class);

            //Use the HelloHome to create a HelloObject
            HelloObject ejbObject = ejbHome.create();

            // Set content type.  If not done, only IE will be able
            // to interpret that it's HTML.  Otherwise, you'll
            // get nothing but tags and content, not HTML.
            response.setContentType("text/html");

            //The part we've all been wainting for...
            out.println("<html>");
            out.println("<body>");
            out.println("<head>");
            out.println("<title>Hello World!</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>"+ ejbObject.sayHello() +"</h1>");
            out.println("</body>");
            out.println("</html>");
            } catch (Exception e){
                response.setContentType("text/plain");
                e.printStackTrace(out);
            }
        }
    }

Now you can start Tomcat...

    dblevins@Miles /home/j2ee/jakarta-tomcat-4.1.12
    $ ./bin/startup.sh
    Using CATALINA_BASE:   C:\home\j2ee\jakarta-tomcat-4.1.12
    Using CATALINA_HOME:   C:\home\j2ee\jakarta-tomcat-4.1.12
    Using CATALINA_TMPDIR: /home/j2ee/jakarta-tomcat-4.1.12/temp
    Using JAVA_HOME:       C:\program_files\jdk1.3.1

Now, pop open your browser and go to the following URL.

    http://localhost:8080/examples/servlet/HelloOpenEJB

Should say "Hello World!!!" on the screen

Java, EJB, JDBC, JNDI, JTA, Sun, Sun Microsystems are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and in other countries. XML, XML Schema, XSLT and related standards are trademarks or registered trademarks of MIT, INRIA, Keio or others, and a product of the World Wide Web Consortium. All other product names mentioned herein are trademarks of their respective owners.