[jboss-cvs] JBossAS SVN: r94561 - in projects/jpa/trunk: deployers/src/main/java/org/jboss/jpa and 10 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Fri Oct 9 05:59:00 EDT 2009
Author: wolfc
Date: 2009-10-09 05:59:00 -0400 (Fri, 09 Oct 2009)
New Revision: 94561
Added:
projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/deployment/
projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/deployment/PersistenceUnitInfoImpl.java
projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/injection/
projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/injection/InjectedEntityManagerFactory.java
projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/remote/
projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/remote/RemotelyInjectEntityManagerFactory.java
Removed:
projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/PersistenceUnitInfoImpl.java
projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/spi/
Modified:
projects/jpa/trunk/deployers/.project
projects/jpa/trunk/deployers/pom.xml
projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/ManagedEntityManagerFactory.java
projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/PersistenceUnitDeployment.java
projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/injection/InjectedEntityManagerFactory.java
projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/remote/RemotelyInjectEntityManagerFactory.java
projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/tx/TransactionScopedEntityManager.java
projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/deployers/test/common/MockRegionFactory.java
projects/jpa/trunk/impl/pom.xml
Log:
JBJPA-15: added more impls and use them
Modified: projects/jpa/trunk/deployers/.project
===================================================================
--- projects/jpa/trunk/deployers/.project 2009-10-09 09:56:42 UTC (rev 94560)
+++ projects/jpa/trunk/deployers/.project 2009-10-09 09:59:00 UTC (rev 94561)
@@ -7,9 +7,11 @@
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
</buildCommand>
+ <!--
<buildCommand>
<name>org.maven.ide.eclipse.maven2Builder</name>
</buildCommand>
+ -->
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
Modified: projects/jpa/trunk/deployers/pom.xml
===================================================================
--- projects/jpa/trunk/deployers/pom.xml 2009-10-09 09:56:42 UTC (rev 94560)
+++ projects/jpa/trunk/deployers/pom.xml 2009-10-09 09:59:00 UTC (rev 94561)
@@ -1,3 +1,6 @@
+<!--
+ vi:ts=2:sw=2:expandtab
+-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<groupId>org.jboss.jpa</groupId>
@@ -3,5 +6,5 @@
<artifactId>jboss-jpa-build</artifactId>
<relativePath>../build/pom.xml</relativePath>
- <version>1.0.0</version>
+ <version>1.0.1-SNAPSHOT</version>
</parent>
@@ -45,7 +48,7 @@
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
- <version>3.4.0.GA</version>
+ <version>${version.org.hibernate.hibernate-entitymanager}</version>
</dependency>
<dependency>
<groupId>org.jboss.integration</groupId>
@@ -69,9 +72,32 @@
<version>${version.org.jboss.deployers}</version>
</dependency>
<dependency>
+ <groupId>org.jboss.jpa</groupId>
+ <artifactId>jboss-jpa-impl</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ </dependency>
+ <dependency>
+ <groupId>org.jboss.jpa</groupId>
+ <artifactId>jboss-jpa-spi</artifactId>
+ <version>1.0.0-SNAPSHOT</version>
+ <!-- TODO: we should really use the right jpa-spi -->
+ <exclusions>
+ <exclusion>
+ <groupId>org.hibernate</groupId>
+ <artifactId>ejb3-persistence</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
<groupId>org.jboss.metadata</groupId>
<artifactId>jboss-metadata</artifactId>
<version>1.0.0.CR17</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.hibernate</groupId>
+ <artifactId>ejb3-persistence</artifactId>
+ </exclusion>
+ </exclusions>
</dependency>
<!-- For @JMX -->
<dependency>
Modified: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/ManagedEntityManagerFactory.java
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/ManagedEntityManagerFactory.java 2009-10-09 09:56:42 UTC (rev 94560)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/ManagedEntityManagerFactory.java 2009-10-09 09:59:00 UTC (rev 94561)
@@ -32,22 +32,25 @@
import javax.transaction.SystemException;
import javax.transaction.Transaction;
+import org.jboss.jpa.spi.PersistenceUnit;
import org.jboss.jpa.util.ThreadLocalStack;
import org.jboss.logging.Logger;
import org.jboss.tm.TransactionLocal;
import org.jboss.tm.TxUtils;
/**
+ *
* @author <a href="mailto:gavine at hibernate.org">Gavin King</a>
* @version $Revision$
*/
+// TODO: move functionality to PersistenceUnitDeployment
+ at Deprecated
public class ManagedEntityManagerFactory
{
private static final Logger log = Logger.getLogger(ManagedEntityManagerFactory.class);
- protected EntityManagerFactory entityManagerFactory;
protected TransactionLocal session = new TransactionLocal();
- protected String kernelName;
+ private PersistenceUnit persistenceUnit;
public static ThreadLocalStack<Map<ManagedEntityManagerFactory, EntityManager>> nonTxStack = new ThreadLocalStack<Map<ManagedEntityManagerFactory, EntityManager>>();
@@ -66,31 +69,30 @@
if (em == null)
{
- em = entityManagerFactory.createEntityManager();
+ em = getEntityManagerFactory().createEntityManager();
map.put(this, em);
}
return em;
}
- public ManagedEntityManagerFactory(EntityManagerFactory sf, String kernelName)
+ protected ManagedEntityManagerFactory(PersistenceUnit persistenceUnit)
{
- this.entityManagerFactory = sf;
- this.kernelName = kernelName;
+ this.persistenceUnit = persistenceUnit;
}
public EntityManagerFactory getEntityManagerFactory()
{
- return entityManagerFactory;
+ return persistenceUnit.getContainerEntityManagerFactory();
}
public String getKernelName()
{
- return kernelName;
+ return persistenceUnit.getName();
}
public void destroy()
{
- entityManagerFactory.close();
+ throw new RuntimeException("JPA-15: do not call");
}
private static class SessionSynchronization implements Synchronization
@@ -192,8 +194,11 @@
public EntityManager createEntityManager()
{
- return entityManagerFactory.createEntityManager();
+ return getEntityManagerFactory().createEntityManager();
}
-
+ public PersistenceUnit getPersistenceUnit()
+ {
+ return persistenceUnit;
+ }
}
Modified: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/PersistenceUnitDeployment.java
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/PersistenceUnitDeployment.java 2009-10-09 09:56:42 UTC (rev 94560)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/PersistenceUnitDeployment.java 2009-10-09 09:59:00 UTC (rev 94561)
@@ -40,6 +40,7 @@
import org.hibernate.ejb.HibernatePersistence;
import org.jboss.beans.metadata.api.annotations.Inject;
import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
+import org.jboss.jpa.impl.deployment.PersistenceUnitInfoImpl;
import org.jboss.jpa.injection.InjectedEntityManagerFactory;
import org.jboss.jpa.spi.PersistenceUnit;
import org.jboss.jpa.spi.PersistenceUnitRegistry;
@@ -164,7 +165,7 @@
public EntityManagerFactory getContainerEntityManagerFactory()
{
- return getManagedFactory().getEntityManagerFactory();
+ return actualFactory;
}
public String getEntityManagerName()
@@ -210,12 +211,22 @@
}
}
+ public EntityManager getTransactionScopedEntityManager()
+ {
+ return managedFactory.getTransactionScopedEntityManager();
+ }
+
public XPCResolver getXPCResolver()
{
assert xpcResolver != null : "xpcResolver is null in " + this;
return xpcResolver;
}
+ public boolean isInTx()
+ {
+ return managedFactory.isInTx();
+ }
+
@Inject
public void setXPCResolver(XPCResolver xpcResolver)
{
@@ -300,7 +311,7 @@
PersistenceProvider pp = (PersistenceProvider) providerClass.newInstance();
actualFactory = pp.createContainerEntityManagerFactory(pi, null);
- managedFactory = new ManagedEntityManagerFactory(actualFactory, kernelName);
+ managedFactory = new ManagedEntityManagerFactory(this);
String entityManagerJndiName = (String) props.get("jboss.entity.manager.jndi.name");
if (entityManagerJndiName != null)
@@ -330,7 +341,7 @@
{
unbind(entityManagerFactoryJndiName);
}
- managedFactory.destroy();
+ actualFactory.close();
}
private void unbind(String name) throws NamingException
@@ -338,4 +349,9 @@
NonSerializableFactory.unbind(name);
initialContext.unbind(name);
}
+
+ public void verifyInTx()
+ {
+ managedFactory.verifyInTx();
+ }
}
Deleted: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/PersistenceUnitInfoImpl.java
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/PersistenceUnitInfoImpl.java 2009-10-09 09:56:42 UTC (rev 94560)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/PersistenceUnitInfoImpl.java 2009-10-09 09:59:00 UTC (rev 94561)
@@ -1,298 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * Copyright 2006, Red Hat Middleware LLC, and individual contributors
- * as indicated by the @author tags. See the copyright.txt file in the
- * distribution for a full listing of individual contributors.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
- */
-package org.jboss.jpa.deployment;
-
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-
-import javax.naming.Context;
-import javax.naming.NamingException;
-import javax.persistence.spi.ClassTransformer;
-import javax.persistence.spi.PersistenceUnitInfo;
-import javax.persistence.spi.PersistenceUnitTransactionType;
-import javax.sql.DataSource;
-
-import org.hibernate.ejb.HibernatePersistence;
-import org.jboss.logging.Logger;
-import org.jboss.metadata.jpa.spec.PersistenceUnitMetaData;
-import org.jboss.metadata.jpa.spec.TransactionType;
-
-/**
- * Comment
- *
- * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
- * @version $Revision$
- */
-public class PersistenceUnitInfoImpl implements PersistenceUnitInfo
-{
- private static final Logger log = Logger.getLogger(PersistenceUnitInfoImpl.class);
-
- private String entityManagerName;
- private DataSource jtaDataSource;
- private DataSource nonJtaDataSource;
- private List<String> mappingFileNames;
- private List<URL> jarFiles;
- private List<String> entityclassNames;
- private Properties properties;
- private ClassLoader classLoader;
- private String persistenceProviderClassName;
- private PersistenceUnitTransactionType transactionType;
- private URL persistenceUnitRootUrl;
- private boolean excludeClasses;
-
- public PersistenceUnitInfoImpl()
- {
- }
-
- /**
- * Note that the jarFiles in metaData are ignore and should be
- * specified in the jarFiles argument.
- *
- * @param metaData the persistence unit meta data
- * @param props properties for the persistence provider
- * @param classLoader the class loader used for entity class loading
- * @param persistenceUnitRootUrl a jar or JarInputStream where the entities are packaged
- * @param jarFiles a list of URLs pointing to jar or JarInputStreams where entities are packaged
- * @param ctx naming context for looking up data sources
- * @throws NamingException when a data source can't be located
- */
- public PersistenceUnitInfoImpl(PersistenceUnitMetaData metaData, Properties props, ClassLoader classLoader, URL persistenceUnitRootUrl, List<URL> jarFiles, Context ctx) throws NamingException
- {
- log.debug("Using class loader " + classLoader);
- this.setClassLoader(classLoader);
-
- this.setJarFiles(jarFiles);
- this.setPersistenceProviderClassName(HibernatePersistence.class.getName());
- log.debug("Found persistence.xml file in EJB3 jar");
- this.setManagedClassnames(safeList(metaData.getClasses()));
- this.setPersistenceUnitName(metaData.getName());
- this.setMappingFileNames(safeList(metaData.getMappingFiles()));
- this.setExcludeUnlistedClasses(metaData.isExcludeUnlistedClasses());
- this.setPersistenceUnitRootUrl(persistenceUnitRootUrl);
- PersistenceUnitTransactionType transactionType = getJPATransactionType(metaData);
- this.setTransactionType(transactionType);
-
- if (metaData.getProvider() != null) this.setPersistenceProviderClassName(metaData.getProvider());
- /*
- if (explicitEntityClasses.size() > 0)
- {
- List<String> classes = this.getManagedClassNames();
- if (classes == null) classes = explicitEntityClasses;
- else classes.addAll(explicitEntityClasses);
- this.setManagedClassnames(classes);
- }
- */
- if (metaData.getJtaDataSource() != null)
- {
- this.setJtaDataSource((javax.sql.DataSource) ctx.lookup(metaData.getJtaDataSource()));
- }
- else if (transactionType == PersistenceUnitTransactionType.JTA)
- {
- throw new RuntimeException("Specification violation [EJB3 JPA 6.2.1.2] - "
- + "You have not defined a jta-data-source for a JTA enabled persistence context named: " + metaData.getName());
- }
- if (metaData.getNonJtaDataSource() != null)
- {
- this.setNonJtaDataSource((javax.sql.DataSource) ctx.lookup(metaData.getNonJtaDataSource()));
- }
- else if (transactionType == PersistenceUnitTransactionType.RESOURCE_LOCAL)
- {
- throw new RuntimeException("Specification violation [EJB3 JPA 6.2.1.2] - "
- + "You have not defined a non-jta-data-source for a RESOURCE_LOCAL enabled persistence context named: "
- + metaData.getName());
- }
- props.putAll(getProperties(metaData));
- this.setProperties(props);
-
- if (this.getPersistenceUnitName() == null)
- {
- throw new RuntimeException("you must specify a name in persistence.xml");
- }
-
- // EJBTHREE-893
- /* TODO: can this work remotely?
- if(!this.getProperties().containsKey("hibernate.session_factory_name"))
- {
- this.getProperties().put("hibernate.session_factory_name", kernelName);
- }
- */
- }
-
- public void addTransformer(ClassTransformer transformer)
- {
- //throw new RuntimeException("NOT IMPLEMENTED");
- }
-
- private static List<String> safeList(Set<String> set)
- {
- return (set == null || set.isEmpty()) ? Collections.<String>emptyList() : new ArrayList<String>(set);
- }
-
- public ClassLoader getNewTempClassLoader()
- {
- return null;
- }
-
- public String getPersistenceProviderClassName()
- {
- return persistenceProviderClassName;
- }
-
- public void setPersistenceProviderClassName(String persistenceProviderClassName)
- {
- this.persistenceProviderClassName = persistenceProviderClassName;
- }
-
- public String getPersistenceUnitName()
- {
- return entityManagerName;
- }
-
- public void setPersistenceUnitName(String entityManagerName)
- {
- this.entityManagerName = entityManagerName;
- }
-
- public DataSource getJtaDataSource()
- {
- return jtaDataSource;
- }
-
- public void setJtaDataSource(DataSource jtaDataSource)
- {
- this.jtaDataSource = jtaDataSource;
- }
-
- protected static PersistenceUnitTransactionType getJPATransactionType(PersistenceUnitMetaData metaData)
- {
- TransactionType type = metaData.getTransactionType();
- if (type == TransactionType.RESOURCE_LOCAL)
- return PersistenceUnitTransactionType.RESOURCE_LOCAL;
- else // default or actually being JTA
- return PersistenceUnitTransactionType.JTA;
- }
-
- public DataSource getNonJtaDataSource()
- {
- return nonJtaDataSource;
- }
-
- public void setNonJtaDataSource(DataSource nonJtaDataSource)
- {
- this.nonJtaDataSource = nonJtaDataSource;
- }
-
- public List<String> getMappingFileNames()
- {
- return mappingFileNames;
- }
-
- public void setMappingFileNames(List<String> mappingFileNames)
- {
- this.mappingFileNames = mappingFileNames;
- }
-
- public List<URL> getJarFileUrls()
- {
- return jarFiles;
- }
-
- public void setJarFiles(List<URL> jarFiles)
- {
- // Hibernate EM 3.3.2.GA LogHelper at 49
- assert jarFiles != null : "jarFiles is null";
-
- this.jarFiles = jarFiles;
- }
-
- public List<String> getManagedClassNames()
- {
- return entityclassNames;
- }
-
- public void setManagedClassnames(List<String> entityclassNames)
- {
- this.entityclassNames = entityclassNames;
- }
-
- public Properties getProperties()
- {
- return properties;
- }
-
- protected static Map<String, String> getProperties(PersistenceUnitMetaData metaData)
- {
- Map<String, String> properties = metaData.getProperties();
- return (properties != null) ? properties : Collections.<String, String>emptyMap();
- }
-
- public void setProperties(Properties properties)
- {
- this.properties = properties;
- }
-
- public ClassLoader getClassLoader()
- {
- return classLoader;
- }
-
- public void setClassLoader(ClassLoader classLoader)
- {
- this.classLoader = classLoader;
- }
-
- public PersistenceUnitTransactionType getTransactionType()
- {
- return transactionType;
- }
-
- public void setTransactionType(PersistenceUnitTransactionType transactionType)
- {
- this.transactionType = transactionType;
- }
-
- public URL getPersistenceUnitRootUrl()
- {
- return persistenceUnitRootUrl;
- }
-
- public void setPersistenceUnitRootUrl(URL persistenceUnitRootUrl)
- {
- this.persistenceUnitRootUrl = persistenceUnitRootUrl;
- }
-
- public boolean excludeUnlistedClasses()
- {
- return excludeClasses;
- }
-
- public void setExcludeUnlistedClasses(boolean excludeClasses)
- {
- this.excludeClasses = excludeClasses;
- }
-
-}
Modified: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/injection/InjectedEntityManagerFactory.java
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/injection/InjectedEntityManagerFactory.java 2009-10-09 09:56:42 UTC (rev 94560)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/injection/InjectedEntityManagerFactory.java 2009-10-09 09:59:00 UTC (rev 94561)
@@ -21,18 +21,7 @@
*/
package org.jboss.jpa.injection;
-import java.io.Externalizable;
-import java.io.IOException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
-import java.util.Map;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-
import org.jboss.jpa.deployment.ManagedEntityManagerFactory;
-import org.jboss.jpa.tx.TransactionScopedEntityManager;
-import org.jboss.jpa.util.ManagedEntityManagerFactoryHelper;
/**
* Comment
@@ -40,66 +29,15 @@
* @author <a href="mailto:bill at jboss.org">Bill Burke</a>
* @version $Revision$
*/
-public class InjectedEntityManagerFactory implements EntityManagerFactory, Externalizable
+ at Deprecated
+public class InjectedEntityManagerFactory extends org.jboss.jpa.impl.injection.InjectedEntityManagerFactory
{
private static final long serialVersionUID = -3734435119658196788L;
- private transient EntityManagerFactory delegate;
- private transient ManagedEntityManagerFactory managedFactory;
-
public InjectedEntityManagerFactory() {}
public InjectedEntityManagerFactory(ManagedEntityManagerFactory managedFactory)
{
- assert managedFactory != null : "managedFactory is null";
-
- this.delegate = managedFactory.getEntityManagerFactory();
- this.managedFactory = managedFactory;
+ super(managedFactory.getPersistenceUnit());
}
-
-
- public EntityManagerFactory getDelegate()
- {
- return delegate;
- }
-
- public void writeExternal(ObjectOutput out) throws IOException
- {
- out.writeUTF(managedFactory.getKernelName());
- }
-
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
- {
- String kernelName = in.readUTF();
- managedFactory = ManagedEntityManagerFactoryHelper.getManagedEntityManagerFactory(kernelName);
- if(managedFactory == null)
- throw new IOException("Unable to find persistence unit in registry: " + kernelName);
- delegate = managedFactory.getEntityManagerFactory();
- }
-
- public EntityManager createEntityManager()
- {
- return getDelegate().createEntityManager();
- }
-
- public EntityManager createEntityManager(Map map)
- {
- return delegate.createEntityManager(map);
- }
-
-
- public EntityManager getEntityManager()
- {
- return new TransactionScopedEntityManager(managedFactory);
- }
-
- public void close()
- {
- throw new IllegalStateException("It is illegal to close an injected EntityManagerFactory");
- }
-
- public boolean isOpen()
- {
- return getDelegate().isOpen();
- }
}
Modified: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/remote/RemotelyInjectEntityManagerFactory.java
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/remote/RemotelyInjectEntityManagerFactory.java 2009-10-09 09:56:42 UTC (rev 94560)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/remote/RemotelyInjectEntityManagerFactory.java 2009-10-09 09:59:00 UTC (rev 94561)
@@ -21,30 +21,7 @@
*/
package org.jboss.jpa.remote;
-import java.io.IOException;
-import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
-import java.io.Serializable;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Enumeration;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-
-import javax.naming.InitialContext;
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.spi.PersistenceProvider;
-import javax.persistence.spi.PersistenceUnitTransactionType;
-
-import org.hibernate.ejb.HibernatePersistence;
-import org.jboss.jpa.deployment.PersistenceUnitInfoImpl;
-import org.jboss.logging.Logger;
import org.jboss.metadata.jpa.spec.PersistenceUnitMetaData;
-import org.jboss.metadata.jpa.spec.TransactionType;
/**
* EXPERIMENTAL
@@ -56,159 +33,16 @@
* @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
* @version $Revision: $
*/
-public class RemotelyInjectEntityManagerFactory implements EntityManagerFactory, Serializable
+ at Deprecated
+public class RemotelyInjectEntityManagerFactory extends org.jboss.jpa.impl.remote.RemotelyInjectEntityManagerFactory
{
- private static final Logger log = Logger.getLogger(RemotelyInjectEntityManagerFactory.class);
-
private static final long serialVersionUID = 1L;
-
- private PersistenceUnitMetaData metaData;
- private EntityManagerFactory actualFactory;
- private Properties defaultPersistenceProperties = new Properties();
- private List<String> explicitEntityClasses = new ArrayList<String>();
- private String jaccContextId;
-
/**
* Assume the data source name has been changed to a remote data source name.
*/
public RemotelyInjectEntityManagerFactory(PersistenceUnitMetaData metaData, String jaccContextId)
{
- assert metaData != null : "metaData is null";
- this.metaData = metaData;
- this.jaccContextId = jaccContextId;
+ super(metaData, jaccContextId);
}
-
- public void close()
- {
- throw new RuntimeException("NYI");
- }
-
- private void createEntityManagerFactory() throws Exception
- {
- log.debug("Booting up the entity manager factory");
-
- Properties props = new Properties();
- props.putAll(defaultPersistenceProperties);
- props.put(HibernatePersistence.JACC_CONTEXT_ID, getJaccContextId());
-
- ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
-
- // TODO:
- List<URL> jarFiles = new ArrayList<URL>();
-
- InitialContext ctx = new InitialContext();
-
- PersistenceUnitInfoImpl pi = new PersistenceUnitInfoImpl(metaData, props, classLoader, getPersistenceUnitRoot(), jarFiles, ctx);
-
- Class<?> providerClass = Thread.currentThread().getContextClassLoader().loadClass(pi.getPersistenceProviderClassName());
-
- // EJBTHREE-893
- /* TODO: can this work remotely?
- if(!pi.getProperties().containsKey("hibernate.session_factory_name"))
- {
- pi.getProperties().put("hibernate.session_factory_name", kernelName);
- }
- */
-
- PersistenceProvider pp = (PersistenceProvider) providerClass.newInstance();
- actualFactory = pp.createContainerEntityManagerFactory(pi, null);
- }
-
- public EntityManager createEntityManager()
- {
- return actualFactory.createEntityManager();
- }
-
- public EntityManager createEntityManager(@SuppressWarnings("unchecked") Map map)
- {
- return actualFactory.createEntityManager(map);
- }
-
- protected String getJaccContextId()
- {
- //return di.getSimpleName();
- return jaccContextId;
- }
-
- protected PersistenceUnitTransactionType getJPATransactionType()
- {
- TransactionType type = metaData.getTransactionType();
- if (type == TransactionType.RESOURCE_LOCAL)
- return PersistenceUnitTransactionType.RESOURCE_LOCAL;
- else // default or actually being JTA
- return PersistenceUnitTransactionType.JTA;
- }
-
- /**
- * Find the persistence unit root, which can be the root of a jar
- * or WEB-INF/classes of a war.
- * @return the persistence unit root
- */
- protected URL getPersistenceUnitRoot() throws IOException
- {
- // FIXME: What is the correct way to find the persistence unit root?
- List<URL> list = new ArrayList<URL>();
- Enumeration<URL> e = Thread.currentThread().getContextClassLoader().getResources("META-INF/persistence.xml");
- while(e.hasMoreElements())
- list.add(e.nextElement());
-// if(list.size() > 1)
-// throw new RuntimeException("Can't handle more than 1 persistence unit on the class path, found " + list);
- if(list.size() > 1)
- log.warn("Found multiple persistence units on the classpath, will use the first one of " + list);
- if(list.size() == 0)
- throw new IllegalStateException("Can't find META-INF/persistence.xml");
- URL url = list.get(0);
- String spec = url.toExternalForm();
- spec = spec.substring(0, spec.length() - "META-INF/persistence.xml".length());
- return new URL(spec);
- /*
- try
- {
- VirtualFile metaData = di.getMetaDataFile("persistence.xml");
- assert metaData != null : "Can't find persistence.xml in " + di;
- return metaData.getParent().getParent();
- }
- catch(IOException e)
- {
- throw new RuntimeException(e);
- }
- */
- }
-
- protected Map<String, String> getProperties()
- {
- Map<String, String> properties = metaData.getProperties();
- return (properties != null) ? properties : Collections.<String, String>emptyMap();
- }
-
- public boolean isOpen()
- {
- throw new RuntimeException("NYI");
- }
-
- private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
- {
- in.defaultReadObject();
-
- try
- {
- createEntityManagerFactory();
- }
- catch (Exception e)
- {
- throw new RuntimeException(e);
- }
- }
-
- private static List<String> safeList(Set<String> set)
- {
- return (set == null || set.isEmpty()) ? Collections.<String>emptyList() : new ArrayList<String>(set);
- }
-
- private void writeObject(ObjectOutputStream out) throws IOException
- {
- log.trace("writeObject");
- out.defaultWriteObject();
- }
}
Modified: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/tx/TransactionScopedEntityManager.java
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/tx/TransactionScopedEntityManager.java 2009-10-09 09:56:42 UTC (rev 94560)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/tx/TransactionScopedEntityManager.java 2009-10-09 09:59:00 UTC (rev 94561)
@@ -21,241 +21,23 @@
*/
package org.jboss.jpa.tx;
-import java.io.Externalizable;
-import java.io.IOException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityTransaction;
-import javax.persistence.FlushModeType;
-import javax.persistence.LockModeType;
-import javax.persistence.Query;
-
-import org.hibernate.Session;
-import org.hibernate.ejb.HibernateEntityManager;
import org.jboss.jpa.deployment.ManagedEntityManagerFactory;
-import org.jboss.jpa.spi.PersistenceUnit;
-import org.jboss.jpa.spi.PersistenceUnitRegistry;
-import org.jboss.jpa.spi.XPCResolver;
-import org.jboss.jpa.util.ManagedEntityManagerFactoryHelper;
-import org.jboss.logging.Logger;
/**
* @author <a href="mailto:bill at jboss.org">Bill Burke</a>
* @version $Revision$
*/
-public class TransactionScopedEntityManager implements EntityManager, Externalizable
+ at Deprecated
+public class TransactionScopedEntityManager extends org.jboss.jpa.impl.tx.TransactionScopedEntityManager
{
private static final long serialVersionUID = 4260828563883650376L;
- private static final Logger log = Logger.getLogger(TransactionScopedEntityManager.class);
-
- private transient ManagedEntityManagerFactory factory;
-
- public Session getHibernateSession()
- {
- EntityManager em = factory.getTransactionScopedEntityManager();
- if (em instanceof HibernateEntityManager)
- {
- return ((HibernateEntityManager) em).getSession();
- }
- throw new RuntimeException("ILLEGAL ACTION: Not a Hibernate pe" +
- "rsistence provider");
- }
-
public TransactionScopedEntityManager(ManagedEntityManagerFactory factory)
{
- if (factory == null) throw new NullPointerException("factory must not be null");
- this.factory = factory;
+ super(factory.getPersistenceUnit());
}
public TransactionScopedEntityManager()
{
}
-
- public void writeExternal(ObjectOutput out) throws IOException
- {
- out.writeUTF(factory.getKernelName());
- }
-
- public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
- {
- String kernelName = in.readUTF();
- factory = ManagedEntityManagerFactoryHelper.getManagedEntityManagerFactory(kernelName);
- if ( factory == null ) throw new IOException( "Unable to find persistence unit in registry: " + kernelName );
- }
-
- public Object getDelegate()
- {
- return getEntityManager().getDelegate();
- }
-
- public void joinTransaction()
- {
- factory.verifyInTx();
- getEntityManager().joinTransaction();
- }
-
- public void clear()
- {
- getEntityManager().clear();
- }
-
- public FlushModeType getFlushMode()
- {
- return getEntityManager().getFlushMode();
- }
-
- public void lock(Object entity, LockModeType lockMode)
- {
- factory.verifyInTx();
- getEntityManager().lock(entity, lockMode);
- }
-
- public <T> T getReference(Class<T> entityClass, Object primaryKey)
- {
- EntityManager em = getEntityManager();
- if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
- try
- {
- return em.getReference(entityClass, primaryKey);
- }
- finally
- {
- if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
- }
- }
-
- public void setFlushMode(FlushModeType flushMode)
- {
- getEntityManager().setFlushMode(flushMode);
- }
-
- public Query createQuery(String ejbqlString)
- {
- EntityManager em = getEntityManager();
- if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
- return em.createQuery(ejbqlString);
- }
-
- public Query createNamedQuery(String name)
- {
- EntityManager em = getEntityManager();
- if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
- return em.createNamedQuery(name);
- }
-
- public Query createNativeQuery(String sqlString)
- {
- EntityManager em = getEntityManager();
- if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
- return em.createNativeQuery(sqlString);
- }
-
- public Query createNativeQuery(String sqlString, Class resultClass)
- {
- EntityManager em = getEntityManager();
- if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
- return em.createNativeQuery(sqlString, resultClass);
- }
-
- public Query createNativeQuery(String sqlString, String resultSetMapping)
- {
- EntityManager em = getEntityManager();
- if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
- return em.createNativeQuery(sqlString, resultSetMapping);
- }
-
- public <A> A find(Class<A> entityClass, Object primaryKey)
- {
- EntityManager em = getEntityManager();
- if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
- try
- {
- return em.find(entityClass, primaryKey);
- }
- finally
- {
- if (!factory.isInTx()) em.clear(); // em will be closed by interceptor
- }
- }
-
- public void persist(Object entity)
- {
- factory.verifyInTx();
- getEntityManager().persist(entity);
- }
-
- public <A> A merge(A entity)
- {
- factory.verifyInTx();
- return (A) getEntityManager().merge(entity);
- }
-
- public void remove(Object entity)
- {
- factory.verifyInTx();
- getEntityManager().remove(entity);
- }
-
- public void refresh(Object entity)
- {
- factory.verifyInTx();
- getEntityManager().refresh(entity);
- }
-
- public boolean contains(Object entity)
- {
- return getEntityManager().contains(entity);
- }
-
- public void flush()
- {
- factory.verifyInTx();
- getEntityManager().flush();
- }
-
- public void close()
- {
- throw new IllegalStateException("Illegal to call this method from injected, managed EntityManager");
- }
-
- public boolean isOpen()
- {
- throw new IllegalStateException("Illegal to call this method from injected, managed EntityManager");
- }
-
- public EntityTransaction getTransaction()
- {
- throw new IllegalStateException("Illegal to call this method from injected, managed EntityManager");
- }
-
- protected EntityManager getEntityManager()
- {
- /* legacy
- StatefulBeanContext beanContext = StatefulBeanContext.currentBean.get();
-
- EntityManager em;
- if (beanContext != null)
- {
- List<StatefulBeanContext> beanContexts = StatefulBeanContext.currentBean.getList();
- for( StatefulBeanContext bc : beanContexts)
- {
- em = bc.getExtendedPersistenceContext(factory.getKernelName());
- if (em != null)
- return em;
- }
- }
- */
- String kernelName = factory.getKernelName();
- PersistenceUnit pu = PersistenceUnitRegistry.getPersistenceUnit(kernelName);
- XPCResolver xpcResolver = pu.getXPCResolver();
- EntityManager em = xpcResolver.getExtendedPersistenceContext(kernelName);
- if(em != null)
- return em;
-
- return factory.getTransactionScopedEntityManager();
- }
-
}
Modified: projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/deployers/test/common/MockRegionFactory.java
===================================================================
--- projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/deployers/test/common/MockRegionFactory.java 2009-10-09 09:56:42 UTC (rev 94560)
+++ projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/deployers/test/common/MockRegionFactory.java 2009-10-09 09:59:00 UTC (rev 94561)
@@ -115,6 +115,13 @@
this.name = name;
}
+ // new since JPA-2
+ public boolean contains(Object arg0)
+ {
+ // no-op
+ return false;
+ }
+
public void evict(Object arg0) throws CacheException
{
// no-op
Modified: projects/jpa/trunk/impl/pom.xml
===================================================================
--- projects/jpa/trunk/impl/pom.xml 2009-10-09 09:56:42 UTC (rev 94560)
+++ projects/jpa/trunk/impl/pom.xml 2009-10-09 09:59:00 UTC (rev 94561)
@@ -60,5 +60,10 @@
<artifactId>jboss-logging-spi</artifactId>
<version>2.1.0.GA</version>
</dependency>
+ <dependency>
+ <groupId>org.jboss.metadata</groupId>
+ <artifactId>jboss-metadata</artifactId>
+ <version>1.0.1.GA</version>
+ </dependency>
</dependencies>
</project>
Copied: projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/deployment/PersistenceUnitInfoImpl.java (from rev 94507, projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/PersistenceUnitInfoImpl.java)
===================================================================
--- projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/deployment/PersistenceUnitInfoImpl.java (rev 0)
+++ projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/deployment/PersistenceUnitInfoImpl.java 2009-10-09 09:59:00 UTC (rev 94561)
@@ -0,0 +1,298 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.impl.deployment;
+
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+import javax.persistence.spi.ClassTransformer;
+import javax.persistence.spi.PersistenceUnitInfo;
+import javax.persistence.spi.PersistenceUnitTransactionType;
+import javax.sql.DataSource;
+
+import org.hibernate.ejb.HibernatePersistence;
+import org.jboss.logging.Logger;
+import org.jboss.metadata.jpa.spec.PersistenceUnitMetaData;
+import org.jboss.metadata.jpa.spec.TransactionType;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @version $Revision$
+ */
+public class PersistenceUnitInfoImpl implements PersistenceUnitInfo
+{
+ private static final Logger log = Logger.getLogger(PersistenceUnitInfoImpl.class);
+
+ private String entityManagerName;
+ private DataSource jtaDataSource;
+ private DataSource nonJtaDataSource;
+ private List<String> mappingFileNames;
+ private List<URL> jarFiles;
+ private List<String> entityclassNames;
+ private Properties properties;
+ private ClassLoader classLoader;
+ private String persistenceProviderClassName;
+ private PersistenceUnitTransactionType transactionType;
+ private URL persistenceUnitRootUrl;
+ private boolean excludeClasses;
+
+ public PersistenceUnitInfoImpl()
+ {
+ }
+
+ /**
+ * Note that the jarFiles in metaData are ignore and should be
+ * specified in the jarFiles argument.
+ *
+ * @param metaData the persistence unit meta data
+ * @param props properties for the persistence provider
+ * @param classLoader the class loader used for entity class loading
+ * @param persistenceUnitRootUrl a jar or JarInputStream where the entities are packaged
+ * @param jarFiles a list of URLs pointing to jar or JarInputStreams where entities are packaged
+ * @param ctx naming context for looking up data sources
+ * @throws NamingException when a data source can't be located
+ */
+ public PersistenceUnitInfoImpl(PersistenceUnitMetaData metaData, Properties props, ClassLoader classLoader, URL persistenceUnitRootUrl, List<URL> jarFiles, Context ctx) throws NamingException
+ {
+ log.debug("Using class loader " + classLoader);
+ this.setClassLoader(classLoader);
+
+ this.setJarFiles(jarFiles);
+ this.setPersistenceProviderClassName(HibernatePersistence.class.getName());
+ log.debug("Found persistence.xml file in EJB3 jar");
+ this.setManagedClassnames(safeList(metaData.getClasses()));
+ this.setPersistenceUnitName(metaData.getName());
+ this.setMappingFileNames(safeList(metaData.getMappingFiles()));
+ this.setExcludeUnlistedClasses(metaData.isExcludeUnlistedClasses());
+ this.setPersistenceUnitRootUrl(persistenceUnitRootUrl);
+ PersistenceUnitTransactionType transactionType = getJPATransactionType(metaData);
+ this.setTransactionType(transactionType);
+
+ if (metaData.getProvider() != null) this.setPersistenceProviderClassName(metaData.getProvider());
+ /*
+ if (explicitEntityClasses.size() > 0)
+ {
+ List<String> classes = this.getManagedClassNames();
+ if (classes == null) classes = explicitEntityClasses;
+ else classes.addAll(explicitEntityClasses);
+ this.setManagedClassnames(classes);
+ }
+ */
+ if (metaData.getJtaDataSource() != null)
+ {
+ this.setJtaDataSource((javax.sql.DataSource) ctx.lookup(metaData.getJtaDataSource()));
+ }
+ else if (transactionType == PersistenceUnitTransactionType.JTA)
+ {
+ throw new RuntimeException("Specification violation [EJB3 JPA 6.2.1.2] - "
+ + "You have not defined a jta-data-source for a JTA enabled persistence context named: " + metaData.getName());
+ }
+ if (metaData.getNonJtaDataSource() != null)
+ {
+ this.setNonJtaDataSource((javax.sql.DataSource) ctx.lookup(metaData.getNonJtaDataSource()));
+ }
+ else if (transactionType == PersistenceUnitTransactionType.RESOURCE_LOCAL)
+ {
+ throw new RuntimeException("Specification violation [EJB3 JPA 6.2.1.2] - "
+ + "You have not defined a non-jta-data-source for a RESOURCE_LOCAL enabled persistence context named: "
+ + metaData.getName());
+ }
+ props.putAll(getProperties(metaData));
+ this.setProperties(props);
+
+ if (this.getPersistenceUnitName() == null)
+ {
+ throw new RuntimeException("you must specify a name in persistence.xml");
+ }
+
+ // EJBTHREE-893
+ /* TODO: can this work remotely?
+ if(!this.getProperties().containsKey("hibernate.session_factory_name"))
+ {
+ this.getProperties().put("hibernate.session_factory_name", kernelName);
+ }
+ */
+ }
+
+ public void addTransformer(ClassTransformer transformer)
+ {
+ //throw new RuntimeException("NOT IMPLEMENTED");
+ }
+
+ private static List<String> safeList(Set<String> set)
+ {
+ return (set == null || set.isEmpty()) ? Collections.<String>emptyList() : new ArrayList<String>(set);
+ }
+
+ public ClassLoader getNewTempClassLoader()
+ {
+ return null;
+ }
+
+ public String getPersistenceProviderClassName()
+ {
+ return persistenceProviderClassName;
+ }
+
+ public void setPersistenceProviderClassName(String persistenceProviderClassName)
+ {
+ this.persistenceProviderClassName = persistenceProviderClassName;
+ }
+
+ public String getPersistenceUnitName()
+ {
+ return entityManagerName;
+ }
+
+ public void setPersistenceUnitName(String entityManagerName)
+ {
+ this.entityManagerName = entityManagerName;
+ }
+
+ public DataSource getJtaDataSource()
+ {
+ return jtaDataSource;
+ }
+
+ public void setJtaDataSource(DataSource jtaDataSource)
+ {
+ this.jtaDataSource = jtaDataSource;
+ }
+
+ protected static PersistenceUnitTransactionType getJPATransactionType(PersistenceUnitMetaData metaData)
+ {
+ TransactionType type = metaData.getTransactionType();
+ if (type == TransactionType.RESOURCE_LOCAL)
+ return PersistenceUnitTransactionType.RESOURCE_LOCAL;
+ else // default or actually being JTA
+ return PersistenceUnitTransactionType.JTA;
+ }
+
+ public DataSource getNonJtaDataSource()
+ {
+ return nonJtaDataSource;
+ }
+
+ public void setNonJtaDataSource(DataSource nonJtaDataSource)
+ {
+ this.nonJtaDataSource = nonJtaDataSource;
+ }
+
+ public List<String> getMappingFileNames()
+ {
+ return mappingFileNames;
+ }
+
+ public void setMappingFileNames(List<String> mappingFileNames)
+ {
+ this.mappingFileNames = mappingFileNames;
+ }
+
+ public List<URL> getJarFileUrls()
+ {
+ return jarFiles;
+ }
+
+ public void setJarFiles(List<URL> jarFiles)
+ {
+ // Hibernate EM 3.3.2.GA LogHelper at 49
+ assert jarFiles != null : "jarFiles is null";
+
+ this.jarFiles = jarFiles;
+ }
+
+ public List<String> getManagedClassNames()
+ {
+ return entityclassNames;
+ }
+
+ public void setManagedClassnames(List<String> entityclassNames)
+ {
+ this.entityclassNames = entityclassNames;
+ }
+
+ public Properties getProperties()
+ {
+ return properties;
+ }
+
+ protected static Map<String, String> getProperties(PersistenceUnitMetaData metaData)
+ {
+ Map<String, String> properties = metaData.getProperties();
+ return (properties != null) ? properties : Collections.<String, String>emptyMap();
+ }
+
+ public void setProperties(Properties properties)
+ {
+ this.properties = properties;
+ }
+
+ public ClassLoader getClassLoader()
+ {
+ return classLoader;
+ }
+
+ public void setClassLoader(ClassLoader classLoader)
+ {
+ this.classLoader = classLoader;
+ }
+
+ public PersistenceUnitTransactionType getTransactionType()
+ {
+ return transactionType;
+ }
+
+ public void setTransactionType(PersistenceUnitTransactionType transactionType)
+ {
+ this.transactionType = transactionType;
+ }
+
+ public URL getPersistenceUnitRootUrl()
+ {
+ return persistenceUnitRootUrl;
+ }
+
+ public void setPersistenceUnitRootUrl(URL persistenceUnitRootUrl)
+ {
+ this.persistenceUnitRootUrl = persistenceUnitRootUrl;
+ }
+
+ public boolean excludeUnlistedClasses()
+ {
+ return excludeClasses;
+ }
+
+ public void setExcludeUnlistedClasses(boolean excludeClasses)
+ {
+ this.excludeClasses = excludeClasses;
+ }
+
+}
Copied: projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/injection/InjectedEntityManagerFactory.java (from rev 94507, projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/injection/InjectedEntityManagerFactory.java)
===================================================================
--- projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/injection/InjectedEntityManagerFactory.java (rev 0)
+++ projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/injection/InjectedEntityManagerFactory.java 2009-10-09 09:59:00 UTC (rev 94561)
@@ -0,0 +1,105 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.impl.injection;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.jboss.jpa.impl.tx.TransactionScopedEntityManager;
+import org.jboss.jpa.spi.PersistenceUnit;
+import org.jboss.jpa.spi.PersistenceUnitRegistry;
+
+/**
+ * Comment
+ *
+ * @author <a href="mailto:bill at jboss.org">Bill Burke</a>
+ * @version $Revision$
+ */
+public class InjectedEntityManagerFactory implements EntityManagerFactory, Externalizable
+{
+ private static final long serialVersionUID = 1L;
+
+ private transient EntityManagerFactory delegate;
+ private transient PersistenceUnit persistenceUnit;
+
+ public InjectedEntityManagerFactory() {}
+
+ public InjectedEntityManagerFactory(PersistenceUnit persistenceUnit)
+ {
+ assert persistenceUnit != null : "persistenceUnit is null";
+
+ this.delegate = persistenceUnit.getContainerEntityManagerFactory();
+ this.persistenceUnit = persistenceUnit;
+ }
+
+
+ public EntityManagerFactory getDelegate()
+ {
+ return delegate;
+ }
+
+ public void writeExternal(ObjectOutput out) throws IOException
+ {
+ out.writeUTF(persistenceUnit.getName());
+ }
+
+ public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
+ {
+ String kernelName = in.readUTF();
+ persistenceUnit = PersistenceUnitRegistry.getPersistenceUnit(kernelName);
+ if(persistenceUnit == null)
+ throw new IOException("Unable to find persistence unit in registry: " + kernelName);
+ delegate = persistenceUnit.getContainerEntityManagerFactory();
+ }
+
+ public EntityManager createEntityManager()
+ {
+ return getDelegate().createEntityManager();
+ }
+
+ @SuppressWarnings("unchecked")
+ public EntityManager createEntityManager(Map map)
+ {
+ return delegate.createEntityManager(map);
+ }
+
+ public EntityManager getEntityManager()
+ {
+ return new TransactionScopedEntityManager(persistenceUnit);
+ }
+
+ public void close()
+ {
+ throw new IllegalStateException("It is illegal to close an injected EntityManagerFactory");
+ }
+
+ public boolean isOpen()
+ {
+ return getDelegate().isOpen();
+ }
+}
Copied: projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/remote/RemotelyInjectEntityManagerFactory.java (from rev 94507, projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/remote/RemotelyInjectEntityManagerFactory.java)
===================================================================
--- projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/remote/RemotelyInjectEntityManagerFactory.java (rev 0)
+++ projects/jpa/trunk/impl/src/main/java/org/jboss/jpa/impl/remote/RemotelyInjectEntityManagerFactory.java 2009-10-09 09:59:00 UTC (rev 94561)
@@ -0,0 +1,208 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.jpa.impl.remote;
+
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+
+import javax.naming.InitialContext;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.spi.PersistenceProvider;
+import javax.persistence.spi.PersistenceUnitTransactionType;
+
+import org.hibernate.ejb.HibernatePersistence;
+import org.jboss.jpa.impl.deployment.PersistenceUnitInfoImpl;
+import org.jboss.logging.Logger;
+import org.jboss.metadata.jpa.spec.PersistenceUnitMetaData;
+import org.jboss.metadata.jpa.spec.TransactionType;
+
+/**
+ * EXPERIMENTAL
+ *
+ * Allows a persistence unit to go over the wire. It is assumed that the same
+ * persistence unit jar is available locally. The jar is then used to boot
+ * up JPA locally.
+ *
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class RemotelyInjectEntityManagerFactory implements EntityManagerFactory, Serializable
+{
+ private static final Logger log = Logger.getLogger(RemotelyInjectEntityManagerFactory.class);
+
+ private static final long serialVersionUID = 1L;
+
+ private PersistenceUnitMetaData metaData;
+
+ private EntityManagerFactory actualFactory;
+ private Properties defaultPersistenceProperties = new Properties();
+ private List<String> explicitEntityClasses = new ArrayList<String>();
+ private String jaccContextId;
+
+ /**
+ * Assume the data source name has been changed to a remote data source name.
+ */
+ public RemotelyInjectEntityManagerFactory(PersistenceUnitMetaData metaData, String jaccContextId)
+ {
+ assert metaData != null : "metaData is null";
+ this.metaData = metaData;
+ this.jaccContextId = jaccContextId;
+ }
+
+ public void close()
+ {
+ throw new RuntimeException("NYI");
+ }
+
+ private void createEntityManagerFactory() throws Exception
+ {
+ log.debug("Booting up the entity manager factory");
+
+ Properties props = new Properties();
+ props.putAll(defaultPersistenceProperties);
+ props.put(HibernatePersistence.JACC_CONTEXT_ID, getJaccContextId());
+
+ ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+
+ // TODO:
+ List<URL> jarFiles = new ArrayList<URL>();
+
+ InitialContext ctx = new InitialContext();
+
+ PersistenceUnitInfoImpl pi = new PersistenceUnitInfoImpl(metaData, props, classLoader, getPersistenceUnitRoot(), jarFiles, ctx);
+
+ Class<?> providerClass = Thread.currentThread().getContextClassLoader().loadClass(pi.getPersistenceProviderClassName());
+
+ // EJBTHREE-893
+ /* TODO: can this work remotely?
+ if(!pi.getProperties().containsKey("hibernate.session_factory_name"))
+ {
+ pi.getProperties().put("hibernate.session_factory_name", kernelName);
+ }
+ */
+
+ PersistenceProvider pp = (PersistenceProvider) providerClass.newInstance();
+ actualFactory = pp.createContainerEntityManagerFactory(pi, null);
+ }
+
+ public EntityManager createEntityManager()
+ {
+ return actualFactory.createEntityManager();
+ }
+
+ public EntityManager createEntityManager(@SuppressWarnings("unchecked") Map map)
+ {
+ return actualFactory.createEntityManager(map);
+ }
+
+ protected String getJaccContextId()
+ {
+ //return di.getSimpleName();
+ return jaccContextId;
+ }
+
+ protected PersistenceUnitTransactionType getJPATransactionType()
+ {
+ TransactionType type = metaData.getTransactionType();
+ if (type == TransactionType.RESOURCE_LOCAL)
+ return PersistenceUnitTransactionType.RESOURCE_LOCAL;
+ else // default or actually being JTA
+ return PersistenceUnitTransactionType.JTA;
+ }
+
+ /**
+ * Find the persistence unit root, which can be the root of a jar
+ * or WEB-INF/classes of a war.
+ * @return the persistence unit root
+ */
+ protected URL getPersistenceUnitRoot() throws IOException
+ {
+ // FIXME: What is the correct way to find the persistence unit root?
+ List<URL> list = new ArrayList<URL>();
+ Enumeration<URL> e = Thread.currentThread().getContextClassLoader().getResources("META-INF/persistence.xml");
+ while(e.hasMoreElements())
+ list.add(e.nextElement());
+// if(list.size() > 1)
+// throw new RuntimeException("Can't handle more than 1 persistence unit on the class path, found " + list);
+ if(list.size() > 1)
+ log.warn("Found multiple persistence units on the classpath, will use the first one of " + list);
+ if(list.size() == 0)
+ throw new IllegalStateException("Can't find META-INF/persistence.xml");
+ URL url = list.get(0);
+ String spec = url.toExternalForm();
+ spec = spec.substring(0, spec.length() - "META-INF/persistence.xml".length());
+ return new URL(spec);
+ /*
+ try
+ {
+ VirtualFile metaData = di.getMetaDataFile("persistence.xml");
+ assert metaData != null : "Can't find persistence.xml in " + di;
+ return metaData.getParent().getParent();
+ }
+ catch(IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+ */
+ }
+
+ public boolean isOpen()
+ {
+ throw new RuntimeException("NYI");
+ }
+
+ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
+ {
+ in.defaultReadObject();
+
+ try
+ {
+ createEntityManagerFactory();
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private static List<String> safeList(Set<String> set)
+ {
+ return (set == null || set.isEmpty()) ? Collections.<String>emptyList() : new ArrayList<String>(set);
+ }
+
+ private void writeObject(ObjectOutputStream out) throws IOException
+ {
+ log.trace("writeObject");
+ out.defaultWriteObject();
+ }
+}
More information about the jboss-cvs-commits
mailing list