Dashboard > Stripes > ... > How To's > EJB3 with Stripes
EJB3 with Stripes Log In | Sign Up   View a printable version of the current page.

Added by Samuel Santos , last edited by Samuel Santos on Jul 21, 2008  (view change)

Stripes already integrates with Spring to provide your ActionBean classes access to Spring resources in the form of configured Spring beans.

This extension is very similar to the Spring one, in fact I was inspired by it.

Installing and Configuring

All you have to do is to add EJBInterceptor and EJBBean files to your project.

Configuring Stripes for EJB3 Interceptor

Like with the Spring you need to configure your StripesFilter. In your web.xml file locate the initialization parameters for the Stripes Filter. If you do not already have the Interceptor.Classes parameter defined, add the following:

Using the EJB Interceptor with Stripes 1.4.3
<init-param>
    <param-name>Interceptor.Classes</param-name>
    <param-value>
        com.samaxes.stripes.integration.ejb.EJBInterceptor,
        net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor
    </param-value>
</init-param>
Using the EJB Interceptor with Stripes 1.5.0
<init-param>
    <param-name>Interceptor.Classes</param-name>
    <param-value>
        com.samaxes.stripes.integration.ejb.EJBInterceptor
    </param-value>
</init-param>

The parameter tells Stripes to use the EJB interceptor, and also not to stop using the Before/After method interceptor (which is configured by default). That's all, we're done with configurations.

Accessing EJB Beans in your ActionBean

Stripes uses the EJBInterceptor to inject EJB beans into ActionBeans after the ActionBean is instantiated. To do this, it must be told how to inject beans and what to inject. As opposed to pushing the linkage out to an XML file, a simple annotation is used. The standard case would look something like this:

Injecting a EJB bean into an ActionBean
// Inject any properties that are annotated
@EJBBean("bugManager")
private BugManager bugManager;

// Inject any methods that are annotated
@EJBBean("bugManager")
public void setBugManager(BugManager bugManager) {
    this.bugManager  = bugManager;
}

This extension also supports auto-wire by name. If you omit the value of the @EJBBean annotation thusly:

Auto-wiring of Spring beans in an ActionBean
// Inject any properties that are annotated
@EJBBean
private BugManager bugManager;

// Inject any methods that are annotated
@EJBBean
public void setBugManager(BugManager bugManager) {
    this.bugManager  = bugManager;
}

then Stripes will attempt to auto-wire by name. The name of the desired bean is derived from the property name or the method name - if the method name starts with 'set' then it is removed and the next character down-cased (following standard JavaBean rules), otherwise the entire method name is taken verbatim. In the example above this yields bugManager. The initial context is then queried to see if it contains a bean called bugManager.

Private and Protected Access

Stripes can inject EJB beans through both method access and field access.
If the JVM's security manager will permit it, Stripes will even inject EJB beans, when requested, into protected, package access and private fields and methods. If the JVM's security manager does not permit this, an exception will be raised explaining the problem.

JBoss and JNDI binding

If you are having trouble with JNDI binding do the following:

Changing the bean JNDI binding
// your interface
public interface BugManager {
}

// your implementation
@Stateless
@Local(BugManager.class)
@LocalBinding(jndiBinding = "bugManager")
public class BugManagerBean implements BugManager {
}

Glassfish and @Local EJB

If you are having trouble with injecting @Local EJB's into your ActionBean use the following:

Injecting a @Local EJB bean into an ActionBean
// your bean implementation:
@Stateless(name = "bugManager", mappedName = "bugManager")
public class BugManager implements com.Manager {
}

// action bean:
@EJB(name = "bugManager", beanInterface = "com.Manager")
public class BugActionBean {
    @EJBBean("java:comp/env/bugManager")
    private BugManager bugManager;
}

Apache Geronimo 2.x and Local interface

It requires additional configuration of a context in the EJBInterceptor.findEJB() method.

EJBInterceptor.findEJB
protected static Object findEJB(String name) {
    // Try to lookup using the name provided
    try {
        if (ctx == null) {
            Properties p = new Properties();
            p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.openejb.client.LocalInitialContextFactory");
            ctx = new InitialContext(p);
        }
        Object ejb = ctx.lookup(name);
        logger.info("Found EJB bean with name [" + name + "]");
        return ejb;
    } catch (NamingException e) {
        throw new StripesRuntimeException("Unable to find EJBBean with name [" + name +
            "] in the initial context.");
    }
}
Injecting a @Local EJB bean into an ActionBean
@EJBBean("BugManagerLocal")
private BugManagerLocal bugManager;
Hosting for this site is donated by the wonderful people at Contegix
Site running on a free Atlassian Confluence Open Source Project License granted to Stripes Framework. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.2 Build:#919 Nov 26, 2007) - Bug/feature request - Contact Administrators