OpenEJB

Thursday June 18, 2009

Screencast: EJB Unit Testing with Eclipse and OpenEJB

We've put together the first of hopefully many screencasts to walk you through various aspects of using OpenEJB. This screencast shows how you can turn the plain, non-javaee version of Eclipse into an EJB testing machine. The tutorial walks you through installing Eclipse, adding OpenEJB to your project's classpath, and creating a simple EJB with a JUnit unit test.

EJB Unit Testing with Eclipse and OpenEJB

Enjoy!

Monday June 01, 2009

OpenEJB 3.1.1 Coming Soon

The OpenEJB 3.1.1 binaries have been spun have gone through some rigorous testing and should be available in the next day or two. Check back tomorrow for some nice new OpenEJB 3.1.1 binaries and some great new features like alternative deployment descriptors for testing, test case injection, secure webservices, global openejb namespace, several new examples and more.

Monday May 04, 2009

Mapped name with OpenEJB

Recently we added the facility to use the mappedName attribute from a @Stateful, @Stateless or @Singleton annotation to add a global JNDI name for your bean. This is pretty simple to use, simply add the mapped name to your bean like so:

@Stateless(mappedName="MyCalculatorBean")
public class CalculatorImpl implements CalculatorRemote, CalculatorLocal {

	public int sum(int add1, int add2) {
		return add1+add2;
	}

	public int multiply(int mul1, int mul2) {
		return mul1*mul2;
	}
}
When this is deployed you will see:
Apache OpenEJB 3.1.1-SNAPSHOT    build: 20090425-09:05
http://openejb.apache.org/
INFO - openejb.home = /home/jgallimore/Sync/OpenEJB/src/openejb3/examples/simple-stateless
INFO - openejb.base = /home/jgallimore/Sync/OpenEJB/src/openejb3/examples/simple-stateless
INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
INFO - Found EjbModule in classpath: /home/jgallimore/Sync/OpenEJB/src/openejb3/examples/simple-stateless/target/classes
INFO - Beginning load: /home/jgallimore/Sync/OpenEJB/src/openejb3/examples/simple-stateless/target/classes
INFO - Configuring enterprise application: classpath.ear
INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
INFO - Auto-creating a container for bean CalculatorImpl: Container(type=STATELESS, id=Default Stateless Container)
INFO - Enterprise application "classpath.ear" loaded.
INFO - Assembling app: classpath.ear
INFO - Jndi(name=CalculatorImplLocal) --> Ejb(deployment-id=CalculatorImpl)
INFO - Jndi(name=MyCalculatorBean) --> Ejb(deployment-id=CalculatorImpl)
INFO - Jndi(name=MyCalculatorBean) --> Ejb(deployment-id=CalculatorImpl)
INFO - Created Ejb(deployment-id=CalculatorImpl, ejb-name=CalculatorImpl, container=Default Stateless Container)
INFO - Deployed Application(path=classpath.ear)
You will now be able to lookup your bean by doing:
Properties properties = new Properties();
properties.setProperty (Context.INITIAL_CONTEXT_FACTORY,"org.apache.openejb.client.LocalInitialContextFactory");

InitialContext initialContext = new InitialContext(properties);
Calculator calc = initialContext.lookup("MyCalculatorBean");
You might also be aware that with OpenEJB you can also specify how global JNDI names are assigned by the container (when you haven't specified a mappedName). You do this by setting a property with a template: For example, setting the following system property (for an embedded OpenEJB instance in a unit test)
System.setProperty("openejb.jndiname.format", "{deploymentId}/{interfaceClass}");
Would result in these JNDI names:
INFO - Jndi(name=CalculatorImpl/org.superbiz.calculator.CalculatorLocal) --> Ejb(deployment-id=CalculatorImpl)
INFO - Jndi(name=CalculatorImpl/org.superbiz.calculator.CalculatorRemote) --> Ejb(deployment-id=CalculatorImpl)
You can see more details on these templates and the variables available at: http://openejb.apache.org/3.0/jndi-names.html

We can also use this template mechanism with the @Stateful/@Stateless/@Singleton mappedName attribute. So this code:

@Stateless(mappedName="{deploymentId}/{interfaceClass}")
public class CalculatorImpl implements CalculatorRemote, CalculatorLocal {

	public int sum(int add1, int add2) {
		return add1+add2;
	}

	public int multiply(int mul1, int mul2) {
		return mul1*mul2;
	}

}
would yield the following JNDI names being set up:
INFO - Enterprise application "classpath.ear" loaded.
INFO - Assembling app: classpath.ear
INFO - Jndi(name=CalculatorImplLocal) --> Ejb(deployment-id=CalculatorImpl)
INFO - Jndi(name=CalculatorImpl/org.superbiz.calculator.CalculatorRemote) --> Ejb(deployment-id=CalculatorImpl)
INFO - Jndi(name=CalculatorImpl/org.superbiz.calculator.CalculatorRemote) --> Ejb(deployment-id=CalculatorImpl)

Wednesday April 29, 2009

Lookups into the openejb namespace

To ease the burden in looking up things from OpenEJB's internal JNDI tree, we've added a new "openejb" JNDI URL prefix which can be used in any context to do lookups.

The following will work in a TestCase or EJB or any code running in the same VM as the EJB Container.

InitialContext context = new InitialContext();

TransactionManager tm = (TransactionManager) context.lookup("openejb:TransactionManager");
DataSource dataSource = (DataSource) context.lookup("openejb:Resource/MyDataSource");

Tuesday April 28, 2009

Alternate descriptors for testing

Inspired by some user blog posts, we've added a new feature so support for altDD (alternative deployment descriptors) can be done in a test or other environment without much work.

Basically, just set the new "openejb.altdd.prefix" system property or initialcontext property to something like "test", then any descriptors in your META-INF/ directory that start with "test." will override the regular descriptor. So for example with an app like this:

  • META-INF/ejb-jar.xml
  • META-INF/test.ejb-jar.xml
  • META-INF/persistence.xml
  • META-INF/test.env-entry.properties

    Just initialize your test case like so:

     Properties properties = new Properties();
     properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
          "org.apache.openejb.client.LocalInitialContextFactory");
     properties.setProperty("openejb.altdd.prefix", "test");
    
     InitialContext initialContext = new InitialContext(properties);
    
    The logical result will be:

  • META-INF/ejb-jar.xml (via test.ejb-jar.xml)
  • META-INF/persistence.xml
  • META-INF/env-entry.properties (via test.env-entry.properties)

    This will work in any environment in which OpenEJB works (embedded, standalone, tomcat, geronimo, etc.).

  • Monday April 27, 2009

    EJBContext.getCallerPrincipal() improvements

    The getCallerPrincipal() method has been improved to always return the username used to login.

    Now any LoginModule implementation can use the new @CallerPrincipal annotation to flag the Principal object they want to be the one returned from getCallerPrincipal() method. The security service will iterate over the Principal objects in the current Subject and return the first one that is annotated as @CallerPrincipal. If no Principal is found with the @CallerPrincipal annotation, the security service will return the first Principal in the Subject as before. All the OpenEJB supplied LoginModules have been updated to use this new annotation.

    If you have implemented your own LoginModule, here's an example of a Principal implementation that uses the new annotation:

    import org.apache.openejb.spi.CallerPrincipal;
    
    @CallerPrincipal
    public class UserPrincipal implements java.security.Principal {
    
        private final String name;
    
        public UserPrincipal(String name) {
            if (name == null) throw new IllegalArgumentException("name cannot be null");
            this.name = name;
        }
    
        public String getName() {
            return name;
        }
    
        // don't forget to implement equals() and hashCode()
    }
    

    Calendar

    Search

    Hot Blogs (today's hits)

    Tag Cloud

    Categories

    Feeds

    Links

    Navigation