Accessing EJBs from Servlets -- OpenEJB embedded in Tomcat

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

Accessing EJBs from Servlets OpenEJB embedded in Tomcat

Introduction
Accessing EJBs locally from Tomcat
Accessing EJBs remotely from Tomcat
OpenEJB-Tomcat FAQ
How does the Local (IntraVM) Server work?
What Security Service do I get?

## Introduction

Servlets can access beans from OpenEJB using either the Local (IntraVM) Server or the default Remote Server

This document is a starting point for using OpenEJB in Tomcat and will evolve based on user contributions. If you wish to contribute to this document, please email the text to the OpenEJB User list.

## Accessing EJBs locally from Tomcat

Local Server access:

your servlet

      ...
      try{
        Properties properties = new Properties();

        properties.put(Context.INITIAL_CONTEXT_FACTORY,
            "org.openejb.client.LocalInitialContextFactory");

        InitialContext ctx = new InitialContext(properties);

        Object obj = ctx.lookup("my/bean/Foo");

        FooHome ejbHome = (FooHome)
            PortableRemoteObject.narrow(obj, FooHome.class);
      } catch (Exception e){
        e.printStackTRace();
      }
      ...

Or simply

your servlet

      ...
      try{

        FooHome ejbHome = (FooHome)new InitialContext().lookup(
                                "java:openejb/ejb/my/bean/Foo");

      } catch (Exception e){
        e.printStackTRace();
      }
      ...

Now keep in mind, that is not J2EE spec compliant. Also keep in mind that we provide it as a convenience, so if there is something you don't like or think should be changed, send code.

## Accessing EJBs remotely from Tomcat

Servlets can access beans from OpenEJB using either the Local (IntraVM) Server or the default Remote Server

Remote Server access:

your servlet

      ...
      try{
        Properties p = new Properties();
        p.put("java.naming.factory.initial",
        "org.openejb.client.RemoteInitialContextFactory");
        p.put("java.naming.provider.url", "25.14.3.92:4201");
        p.put("java.naming.security.principal", "myuser");
        p.put("java.naming.security.credentials", "mypass");

        InitialContext ctx = new InitialContext(p);

        Object obj = ctx.lookup("my/bean/Foo");

        FooHome ejbHome = (FooHome)
            PortableRemoteObject.narrow(obj, FooHome.class);
      } catch (Exception e){
        e.printStackTRace();
      }
      ...

The java.naming.security.principal and java.naming.security.credentials parameters are not used at the moment, but it is a good idea to use them as it will reduce the amount of patching you need to do when they are activated.

## OpenEJB-Tomcat FAQ

### How does the Local (IntraVM) Server work?

When OpenEJB runs in the same VM as Tomcat and Servlets are accessing OpenEJB, you're really using OpenEJB as an embedded EJB Server inside Tomcat. This is identical to using embedded database servers like InstantDB and Cloudscape inside Tomcat.

OpenEJB is the only EJB server that I know of that you can run as an embedded library, so the fact that you can even do it is a real feather in our cap. If anyone knows of another, please tell me.

In fact, anyone already using InstantDB or Cloudscape as embedded database servers in Tomcat could just as easily use OpenEJB as an embedded EJB Server and add instant EJB support as well. OpenEJB can easily play with InstantDB or Cloudscape.

### What Security Service do I get?

A special adapter needs to be written so that OpenEJB can use the Security capabilities of Tomcat and get the security credintials from calls by servlets. If you are interested in writing this, send mail to the OpenEJB development list.

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.