001: package com.bm.creators;
002:
003: import java.util.ArrayList;
004: import java.util.Arrays;
005: import java.util.List;
006:
007: import javax.annotation.Resource;
008: import javax.ejb.EJB;
009: import javax.persistence.EntityManager;
010: import javax.persistence.PersistenceContext;
011:
012: import org.apache.log4j.Logger;
013:
014: import com.bm.cfg.Ejb3UnitCfg;
015: import com.bm.ejb3guice.inject.CreationListner;
016: import com.bm.ejb3guice.inject.Ejb3Guice;
017: import com.bm.ejb3guice.inject.Injector;
018: import com.bm.ejb3guice.inject.Module;
019: import com.bm.ejb3guice.inject.Stage;
020: import com.bm.ejb3metadata.annotations.metadata.MetaDataCache;
021: import com.bm.introspectors.AbstractIntrospector;
022: import com.bm.introspectors.Property;
023: import com.bm.utils.LifeCycleMethodExecuter;
024:
025: /**
026: * This class will create session bean instances without an application server.
027: * The dependency injection is done here
028: *
029: * @param <T> -
030: * the type of the session bean to create (class name)
031: * @author Daniel Wiese
032: * @since 18.09.2005
033: */
034: public final class SessionBeanFactory<T> {
035:
036: /** remember the current entity manager * */
037: private EntityManager manager = null;
038:
039: private static final Logger log = Logger
040: .getLogger(SessionBeanFactory.class);
041:
042: private final AbstractIntrospector<T> introspector;
043:
044: private final Ejb3UnitCfg configuration;
045:
046: private final LifeCycleMethodExecuter lifeCycleMethodExecuter = new LifeCycleMethodExecuter();
047:
048: /**
049: * Default constructor.
050: *
051: * @param intro -
052: * the introspector
053: *
054: * @param usedEntityBeans -
055: * the used entity beans for this test
056: */
057: public SessionBeanFactory(AbstractIntrospector<T> intro,
058: Class[] usedEntityBeans) {
059: final List<Class<? extends Object>> usedEntityBeansC = new ArrayList<Class<? extends Object>>();
060: if (usedEntityBeans != null) {
061:
062: for (Class<? extends Object> akt : usedEntityBeans) {
063: usedEntityBeansC.add(akt);
064: }
065: }
066: Ejb3UnitCfg.addEntytiesToTest(usedEntityBeansC);
067: this .configuration = Ejb3UnitCfg.getConfiguration();
068: this .introspector = intro;
069:
070: }
071:
072: /**
073: * Factory method to create stateless session beans.
074: *
075: * @author Daniel Wiese
076: * @since 18.09.2005
077: * @param toCreate -
078: * the class to create
079: * @return - the created session bean class for local usage
080: */
081: @SuppressWarnings("unchecked")
082: public T createSessionBean(Class<T> toCreate) {
083:
084: BeanCreationListener createdbeans = new BeanCreationListener();
085: Injector injector = getInjector(toCreate, createdbeans);
086: final T instance = injector.getInstance(toCreate);
087: // now inject other instances
088: for (Object created : createdbeans.getCreatedBeans()) {
089: lifeCycleMethodExecuter
090: .executeLifeCycleMethodsForCreate(created);
091: }
092: return instance;
093: }
094:
095: /**
096: * Creates the injector to create session beans and inject fields
097: *
098: * @param toCreate -
099: * the class to create
100: * @return - the injector
101: */
102: @SuppressWarnings("unchecked")
103: public Injector getInjector(Class<T> toCreate,
104: CreationListner creationListener) {
105: Module module = MetaDataCache.getDynamicModuleCreator(
106: configuration, this .getEntityManager(), toCreate);
107: Module[] mods = { module };
108: Injector injector = Ejb3Guice.createInjector(Stage.PRODUCTION,
109: Arrays.asList(mods), Ejb3Guice.markerToArray(EJB.class,
110: Resource.class, PersistenceContext.class),
111: creationListener);
112: return injector;
113:
114: }
115:
116: /**
117: * Factory method to close the entity manager in stateless session beans.
118: *
119: * @author Daniel Wiese
120: * @since 18.09.2005
121: * @param toClose -
122: * the session bean class with the entity manager to close
123: */
124: public void destroySessionBean(T toClose) {
125: try {
126: if (this .introspector != null
127: && this .introspector.hasEntityManager()) {
128: Property em = this .introspector.getEntityManagerField();
129: EntityManager toCloseEM = (EntityManager) em
130: .getField(toClose);
131: log.info("Closing EntityManager");
132: if (toCloseEM != null) {
133: toCloseEM.close();
134: this .manager = null;
135: }
136: }
137:
138: } catch (IllegalAccessException e) {
139: log.error("IllegalAccessException", e);
140: throw new RuntimeException(
141: "Could not close the Entyty-Manager in the session bean");
142: }
143: }
144:
145: /**
146: * Gets the instance of the entity manager assotiated with this factory.
147: *
148: * @author Daniel Wiese
149: * @since 18.09.2005
150: * @return - the entity manager
151: */
152: public EntityManager getEntityManager() {
153: if (manager == null) {
154: this.manager = this.configuration.getEntityManagerFactory()
155: .createEntityManager();
156: }
157: return this.manager;
158:
159: }
160:
161: }
|