Author: dain
Date: Mon Oct 15 22:05:17 2007
New Revision: 585041
URL: http://svn.apache.org/viewvc?rev=585041&view=rev
Log:
Simplified CmpContainer and JpaCmpEngine code
Modified:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/CmpContainer.java
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/CmpEngine.java
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/jpa/JpaCmpEngine.java
Modified:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/CmpContainer.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/CmpContainer.java?rev=585041&r1=585040&r2=585041&view=diff
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/CmpContainer.java
(original)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/CmpContainer.java
Mon Oct 15 22:05:17 2007
@@ -74,10 +74,6 @@
protected final Object containerID;
protected final TransactionManager transactionManager;
protected final SecurityService securityService;
- protected final String cmpEngineFactory;
- protected final String connectorName;
- protected final String engine;
- protected final CmpCallback cmpCallback;
/**
* Index used for getDeployments() and getDeploymentInfo(deploymentId).
@@ -92,9 +88,9 @@
protected final Map<Class, DeploymentInfo> deploymentsByClass = new
HashMap<Class, DeploymentInfo>();
/**
- * There is one cmpEngine per ejb jar and they are keyed by either the jar
path or class loader when there is not jar.
+ * The CmpEngine which performs the actual persistence operations
*/
- protected final Map<Object, CmpEngine> cmpEngines = new HashMap<Object,
CmpEngine>();
+ protected final CmpEngine cmpEngine;
/**
* Tracks entity instances that have been "entered" so we can throw
reentrancy exceptions.
@@ -107,17 +103,30 @@
}
};
- public CmpContainer(Object id, TransactionManager transactionManager,
SecurityService securityService, String cmpEngineFactory, String engine, String
connectorName) {
+ public CmpContainer(Object id, TransactionManager transactionManager,
SecurityService securityService, String cmpEngineFactory, String engine, String
connectorName) throws OpenEJBException {
this.transactionManager = transactionManager;
this.securityService = securityService;
this.containerID = id;
- this.cmpEngineFactory = cmpEngineFactory;
- this.connectorName = connectorName;
- this.engine = engine;
synchronizationRegistry =
SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class);
entrancyTracker = new EntrancyTracker(synchronizationRegistry);
- cmpCallback = new ContainerCmpCallback();
+ // create the cmp engine instance
+ ClassLoader classLoader =
Thread.currentThread().getContextClassLoader();
+ if (classLoader == null) classLoader = getClass().getClassLoader();
+
+ CmpEngineFactory factory = null;
+ try {
+ Class<?> cmpEngineFactoryClass =
classLoader.loadClass(cmpEngineFactory);
+ factory = (CmpEngineFactory) cmpEngineFactoryClass.newInstance();
+ } catch (Exception e) {
+ throw new OpenEJBException("Unable to create cmp engine factory "
+ cmpEngineFactory, e);
+ }
+ factory.setTransactionManager(transactionManager);
+ factory.setConnectorName(connectorName);
+ factory.setCmpCallback(new ContainerCmpCallback());
+ factory.setEngine(engine);
+ factory.setClassLoader(classLoader);
+ cmpEngine = factory.create();
}
public Object getContainerID() {
@@ -154,16 +163,6 @@
synchronized (this) {
Object deploymentId = deploymentInfo.getDeploymentID();
- Object cmpEngineKey = deploymentInfo.getJarPath();
- if (cmpEngineKey == null) {
- cmpEngineKey = deploymentInfo.getClassLoader();
- }
-
- CmpEngine cmpEngine = cmpEngines.get(cmpEngineKey);
- if (cmpEngine == null) {
- cmpEngine = createCmpEngine(deploymentInfo.getJarPath(),
deploymentInfo.getClassLoader());
- cmpEngines.put(cmpEngineKey, cmpEngine);
- }
cmpEngine.deploy(deploymentInfo);
deploymentInfo.setContainerData(cmpEngine);
@@ -207,23 +206,12 @@
// ignore
}
- CmpEngine cmpEngine = (CmpEngine)
deploymentInfo.getContainerData();
- if (cmpEngine != null){
- cmpEngine.undeploy(deploymentInfo);
-
- if (cmpEngine.isEmpty()){
- cmpEngines.remove(deploymentInfo.getJarPath());
- cmpEngines.remove(deploymentInfo.getClassLoader());
- }
- }
deploymentInfo.setContainer(null);
deploymentInfo.setContainerData(null);
}
}
public Object getEjbInstance(CoreDeploymentInfo deployInfo, Object
primaryKey) {
- CmpEngine cmpEngine = getCmpEngine(deployInfo);
-
ThreadContext callContext = new ThreadContext(deployInfo, primaryKey);
ThreadContext oldCallContext = ThreadContext.enter(callContext);
@@ -235,32 +223,6 @@
}
}
- private CmpEngine getCmpEngine(CoreDeploymentInfo deployInfo) {
- CmpEngine cmpEngine = (CmpEngine) deployInfo.getContainerData();
- if (cmpEngine == null) {
- throw new IllegalArgumentException("Deployment does not contain a
CmpEngine " + deployInfo);
- }
- return cmpEngine;
- }
-
- private CmpEngine createCmpEngine(String jarPath, ClassLoader classLoader)
throws OpenEJBException {
- CmpEngineFactory factory = null;
- try {
- Class<?> cmpEngineFactoryClass =
classLoader.loadClass(cmpEngineFactory);
- factory = (CmpEngineFactory) cmpEngineFactoryClass.newInstance();
- } catch (Exception e) {
- throw new OpenEJBException("Unable to create cmp engine factory "
+ cmpEngineFactory, e);
- }
- factory.setTransactionManager(transactionManager);
- factory.setJarPath(jarPath);
- factory.setConnectorName(connectorName);
- factory.setCmpCallback(cmpCallback);
- factory.setEngine(engine);
- factory.setClassLoader(classLoader);
- CmpEngine cmpEngine = factory.create();
- return cmpEngine;
- }
-
/**
* @deprecated use invoke signature without 'securityIdentity' argument.
*/
@@ -521,7 +483,6 @@
entrancyTracker.enter(deploymentInfo, callContext.getPrimaryKey());
try {
- CmpEngine cmpEngine = getCmpEngine(deploymentInfo);
bean = (EntityBean) cmpEngine.loadBean(callContext,
callContext.getPrimaryKey());
if (bean == null) {
throw new
NoSuchObjectException(deploymentInfo.getDeploymentID() + " : " +
callContext.getPrimaryKey());
@@ -634,7 +595,6 @@
ejbCreateMethod.invoke(bean, args);
// create the new bean
- CmpEngine cmpEngine = getCmpEngine(deploymentInfo);
primaryKey = cmpEngine.createBean(bean, callContext);
// determine post create callback method
@@ -691,7 +651,6 @@
txPolicy.beforeInvoke(null, txContext);
try {
- CmpEngine cmpEngine = getCmpEngine(deploymentInfo);
EntityBean bean = (EntityBean) cmpEngine.loadBean(callContext,
args[0]);
if (bean == null) {
throw new
ObjectNotFoundException(deploymentInfo.getDeploymentID() + " : " + args[0]);
@@ -722,7 +681,6 @@
txPolicy.beforeInvoke(null, txContext);
try {
- CmpEngine cmpEngine = getCmpEngine(deploymentInfo);
List<Object> results = cmpEngine.queryBeans(callContext,
callMethod, args);
KeyGenerator kg = deploymentInfo.getKeyGenerator();
@@ -781,7 +739,6 @@
try {
// exectue the select query
- CmpEngine cmpEngine = getCmpEngine(deploymentInfo);
Collection<Object> results = cmpEngine.queryBeans(deploymentInfo,
signature, args);
//
@@ -852,7 +809,6 @@
txPolicy.beforeInvoke(null, txContext);
try {
- CmpEngine cmpEngine = getCmpEngine(deploymentInfo);
EntityBean entityBean = (EntityBean)
cmpEngine.loadBean(callContext, callContext.getPrimaryKey());
if (entityBean == null) {
throw new
NoSuchObjectException(callContext.getDeploymentInfo().getDeploymentID() + " " +
callContext.getPrimaryKey());
Modified:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/CmpEngine.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/CmpEngine.java?rev=585041&r1=585040&r2=585041&view=diff
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/CmpEngine.java
(original)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/CmpEngine.java
Mon Oct 15 22:05:17 2007
@@ -17,18 +17,15 @@
*/
package org.apache.openejb.core.cmp;
-import org.apache.openejb.core.ThreadContext;
-import org.apache.openejb.core.CoreDeploymentInfo;
-import org.apache.openejb.SystemException;
import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.core.CoreDeploymentInfo;
+import org.apache.openejb.core.ThreadContext;
-import javax.persistence.EntityTransaction;
-import javax.ejb.EntityBean;
import javax.ejb.CreateException;
+import javax.ejb.EntityBean;
import javax.ejb.FinderException;
-import javax.transaction.TransactionManager;
-import java.util.List;
import java.lang.reflect.Method;
+import java.util.List;
public interface CmpEngine {
@@ -47,6 +44,4 @@
void deploy(CoreDeploymentInfo deploymentInfo) throws OpenEJBException;
void undeploy(CoreDeploymentInfo deploymentInfo) throws OpenEJBException;
-
- boolean isEmpty();
}
Modified:
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/jpa/JpaCmpEngine.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/jpa/JpaCmpEngine.java?rev=585041&r1=585040&r2=585041&view=diff
==============================================================================
---
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/jpa/JpaCmpEngine.java
(original)
+++
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/core/cmp/jpa/JpaCmpEngine.java
Mon Oct 15 22:05:17 2007
@@ -17,30 +17,7 @@
*/
package org.apache.openejb.core.cmp.jpa;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import java.util.WeakHashMap;
-import java.util.Map;
-import java.util.HashMap;
-import java.lang.reflect.Method;
-import javax.ejb.CreateException;
-import javax.ejb.EJBException;
-import javax.ejb.EJBObject;
-import javax.ejb.EntityBean;
-import javax.ejb.FinderException;
-import javax.ejb.RemoveException;
-import javax.ejb.EJBLocalObject;
-import javax.naming.NamingException;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceException;
-import javax.persistence.Query;
-import javax.transaction.TransactionManager;
-import javax.transaction.Status;
-import javax.transaction.TransactionSynchronizationRegistry;
-
import org.apache.openejb.OpenEJBException;
-import org.apache.openejb.loader.SystemInstance;
import org.apache.openejb.core.CoreDeploymentInfo;
import org.apache.openejb.core.ThreadContext;
import org.apache.openejb.core.cmp.CmpCallback;
@@ -50,10 +27,33 @@
import org.apache.openejb.core.cmp.SimpleKeyGenerator;
import org.apache.openejb.core.cmp.cmp2.Cmp2KeyGenerator;
import org.apache.openejb.core.cmp.cmp2.Cmp2Util;
+import org.apache.openejb.loader.SystemInstance;
import org.apache.openjpa.event.AbstractLifecycleListener;
import org.apache.openjpa.event.LifecycleEvent;
import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
+import javax.ejb.CreateException;
+import javax.ejb.EJBException;
+import javax.ejb.EJBLocalObject;
+import javax.ejb.EJBObject;
+import javax.ejb.EntityBean;
+import javax.ejb.FinderException;
+import javax.ejb.RemoveException;
+import javax.naming.NamingException;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceException;
+import javax.persistence.Query;
+import javax.transaction.Status;
+import javax.transaction.TransactionManager;
+import javax.transaction.TransactionSynchronizationRegistry;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.WeakHashMap;
+
public class JpaCmpEngine implements CmpEngine {
private static final Object[] NO_ARGS = new Object[0];
public static final String CMP_PERSISTENCE_CONTEXT_REF_NAME =
"openejb/cmp";
@@ -61,9 +61,12 @@
private final CmpCallback cmpCallback;
private final TransactionManager transactionManager;
private final TransactionSynchronizationRegistry synchronizationRegistry =
SystemInstance.get().getComponent(TransactionSynchronizationRegistry.class);
+
+ /**
+ * Used to track which entity managers have had the live cycle listener
registered
+ */
private final WeakHashMap<EntityManager,Object> entityManagerListeners =
new WeakHashMap<EntityManager,Object>();
- private final Map<Object, CoreDeploymentInfo> deployments = new
HashMap<Object, CoreDeploymentInfo>();
private final ThreadLocal<Set<EntityBean>> creating = new
ThreadLocal<Set<EntityBean>>() {
protected Set<EntityBean> initialValue() {
return new HashSet<EntityBean>();
@@ -75,24 +78,12 @@
this.transactionManager = transactionManager;
}
- public void deploy(CoreDeploymentInfo deploymentInfo) throws
OpenEJBException {
- deployments.put(deploymentInfo.getDeploymentID(), deploymentInfo);
- if (deploymentInfo.getCmpImplClass() == null) {
- throw new OpenEJBException("Deployment info does not define a CMP
implementation class " + deploymentInfo.getDeploymentID());
- }
+ public synchronized void deploy(CoreDeploymentInfo deploymentInfo) throws
OpenEJBException {
configureKeyGenerator(deploymentInfo);
}
- public void undeploy(CoreDeploymentInfo deploymentInfo) throws
OpenEJBException {
+ public synchronized void undeploy(CoreDeploymentInfo deploymentInfo)
throws OpenEJBException {
deploymentInfo.setKeyGenerator(null);
- deployments.remove(deploymentInfo.getDeploymentID());
- if (deployments.size() == 0){
- entityManagerListeners.clear();
- }
- }
-
- public boolean isEmpty() {
- return deployments.size() == 0;
}
private EntityManager getEntityManager(CoreDeploymentInfo deploymentInfo) {
@@ -121,7 +112,7 @@
OpenJPAEntityManagerSPI openjpaEM = (OpenJPAEntityManagerSPI)
entityManager;
OpenJPALifecycleListener listener = new OpenJPALifecycleListener();
openjpaEM.addLifecycleListener(listener, (Class[])null);
- entityManagerListeners.put(entityManager, listener);
+ entityManagerListeners.put(entityManager, null);
return;
}