Decorator not decorating?
seacuke23 Jun 14, 2012 2:19 PMThis is my first foray into JEE decorators, and I'm not entirely sure if what I'm trying is supposed to work the way I'm expecting it to. I'm trying to use a decorator for one of my stateless bean and noticing that the only way the decoration actually takes place is if the bean is injected into an existing bean. It doesn't decorate if I use the @EJB annotation or if I look it up with contexts. Is this actually the way it's supposed to be or have I done something wrong or is this not the way it's supposed to be?
Example code and output follow.
package com.xxx.logging;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import org.apache.log4j.Logger;
/**
* Session Bean implementation class BasicBean
*/
@Stateless
@LocalBean
public class BasicBean implements BasicBeanRemote {
Logger l = Logger.getLogger(this.getClass());
@Override
public void doIt() {
l.debug("doing it");
}
}
package com.xxx.logging;
import javax.ejb.Remote;
@Remote
public interface BasicBeanRemote {
public void doIt();
}
package com.xxx.logging;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.enterprise.inject.Any;
import javax.inject.Inject;
import org.apache.log4j.Logger;
@Decorator
public class BasicDecorator implements BasicBeanRemote {
@Delegate
@Inject
@Any
BasicBeanRemote delegate;
Logger l = Logger.getLogger(this.getClass());
@Override
public void doIt() {
l.debug("decorated.");
delegate.doIt();
}
}
package com.xxx.logging;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.SessionContext;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.apache.log4j.Logger;
/**
* Session Bean implementation class DecoratorTest
*/
@Singleton
@LocalBean
@Startup
public class DecoratorTest {
Logger l = Logger.getLogger(this.getClass());
@Inject
BasicBeanRemote injectedBean;
@EJB
BasicBeanRemote ejbBean;
@Resource
SessionContext sc;
private static final String BEAN = "java:module/BasicBean!com.xxx.logging.BasicBeanRemote";
@PostConstruct
public void tryMe() {
l.debug("injectedBean");
injectedBean.doIt();
l.debug("ejbBean");
ejbBean.doIt();
try {
l.debug("InitialContext");
((BasicBeanRemote) new InitialContext().lookup(BEAN)).doIt();
}
catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
l.debug("SessionContext");
((BasicBeanRemote) sc.lookup(BEAN)).doIt();
}
}
12:06:25,333 DEBUG [com.xxx.logging.DecoratorTest] (MSC service thread 1-14) injectedBean 12:06:25,337 DEBUG [com.xxx.logging.BasicDecorator] (MSC service thread 1-14) decorated. 12:06:25,340 DEBUG [com.xxx.logging.BasicBean] (MSC service thread 1-14) doing it 12:06:25,341 DEBUG [com.xxx.logging.DecoratorTest] (MSC service thread 1-14) ejbBean 12:06:25,351 DEBUG [com.xxx.logging.BasicBean] (MSC service thread 1-14) doing it 12:06:25,352 DEBUG [com.xxx.logging.DecoratorTest] (MSC service thread 1-14) InitialContext 12:06:25,354 DEBUG [com.xxx.logging.BasicBean] (MSC service thread 1-14) doing it 12:06:25,356 DEBUG [com.xxx.logging.DecoratorTest] (MSC service thread 1-14) SessionContext 12:06:25,359 DEBUG [com.xxx.logging.BasicBean] (MSC service thread 1-14) doing it