001: package com.bm.jndi;
002:
003: import java.util.Hashtable;
004: import java.util.List;
005:
006: import javax.naming.Context;
007: import javax.naming.InitialContext;
008: import javax.naming.NamingException;
009:
010: import com.bm.cfg.Ejb3UnitCfg;
011: import com.bm.cfg.JndiProperty;
012: import com.bm.creators.BeanCreationListener;
013: import com.bm.creators.SessionBeanFactory;
014: import com.bm.ejb3guice.inject.Injector;
015: import com.bm.introspectors.AbstractIntrospector;
016: import com.bm.introspectors.SessionBeanIntrospector;
017: import com.bm.utils.LifeCycleMethodExecuter;
018:
019: /**
020: * This class binds all Objects specified in the Ejb3Unit config to the jndi
021: * tree.
022: *
023: * @author Daniel
024: *
025: */
026: public class Ejb3UnitJndiBinder {
027:
028: private static final org.apache.log4j.Logger log = org.apache.log4j.Logger
029: .getLogger(Ejb3UnitJndiBinder.class);
030:
031: private final InitialContext ctx;
032:
033: private final List<JndiProperty> toBind;
034:
035: private final Class[] usedEntityBeans;
036:
037: private final LifeCycleMethodExecuter lifeCycleMethodExecuter = new LifeCycleMethodExecuter();
038:
039: public Ejb3UnitJndiBinder(Class... usedEntityBeans) {
040: this .usedEntityBeans = usedEntityBeans;
041: try {
042: Hashtable<String, String> env = new Hashtable<String, String>();
043: env.put(Context.INITIAL_CONTEXT_FACTORY,
044: MemoryContextFactory.class.getName());
045: ctx = new InitialContext(env);
046: toBind = Ejb3UnitCfg.getJndiBindings();
047: } catch (NamingException e) {
048: throw new RuntimeException(
049: "Can't setup JNDI context for testing");
050: }
051: }
052:
053: public void bind() {
054: for (JndiProperty current : toBind) {
055: log.debug("Binding (" + current.getClassName()
056: + ") with name (" + current.getJndiName() + ")");
057: if (current.isSessionBean()) {
058: bindSessionBean(current);
059: } else {
060: bindPlainClass(current);
061: }
062:
063: }
064: }
065:
066: private void bindPlainClass(JndiProperty current) {
067: Class<Object> toCreate = loadClass(current);
068: try {
069: this .ctx
070: .bind(current.getJndiName(), toCreate.newInstance());
071: } catch (NamingException e) {
072: throw new IllegalArgumentException(e);
073: } catch (InstantiationException e) {
074: throw new IllegalArgumentException(
075: "Cant instantiate the class ("
076: + current.getClassName()
077: + ") for JNDI context binding");
078: } catch (IllegalAccessException e) {
079: throw new IllegalArgumentException(
080: "Cant instantiate the class ("
081: + current.getClassName()
082: + ") for JNDI context binding");
083: }
084: }
085:
086: private void bindSessionBean(JndiProperty current) {
087: Class<Object> sessionBean = loadClass(current);
088: final AbstractIntrospector<Object> intro = new SessionBeanIntrospector<Object>(
089: sessionBean);
090: final SessionBeanFactory<Object> sf = new SessionBeanFactory<Object>(
091: intro, usedEntityBeans);
092: try {
093: BeanCreationListener createdbeans = new BeanCreationListener();
094: Injector injector = sf.getInjector(sessionBean,
095: createdbeans);
096: final Object createdSessionBean = injector
097: .getInstance(sessionBean);
098: // now inject other instances
099: for (Object created : createdbeans.getCreatedBeans()) {
100: lifeCycleMethodExecuter
101: .executeLifeCycleMethodsForCreate(created);
102: }
103: this .ctx.bind(current.getJndiName(), createdSessionBean);
104: } catch (NamingException e) {
105: throw new IllegalArgumentException(e);
106: }
107: }
108:
109: @SuppressWarnings("unchecked")
110: private Class<Object> loadClass(JndiProperty current) {
111: Class<Object> classToLoad = null;
112: try {
113: classToLoad = (Class<Object>) Thread.currentThread()
114: .getContextClassLoader().loadClass(
115: current.getClassName());
116: } catch (ClassNotFoundException e) {
117: throw new IllegalArgumentException(
118: "The Class ("
119: + current.getClassName()
120: + ") you wold like to bind to JNDI tree can't be found");
121: }
122: return classToLoad;
123: }
124: }
|