[jboss-cvs] JBossAS SVN: r78416 - in projects/jpa/trunk/deployers/src: main/java/org/jboss/jpa/deployment and 7 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Sep 11 06:18:50 EDT 2008


Author: wolfc
Date: 2008-09-11 06:18:50 -0400 (Thu, 11 Sep 2008)
New Revision: 78416

Added:
   projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/remote/
   projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/remote/RemotelyInjectEntityManagerFactory.java
   projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/remote/
   projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/remote/test/
   projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/remote/test/remote/
   projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/remote/test/remote/Person.java
   projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/remote/test/remote/RemoteTestCase.java
   projects/jpa/trunk/deployers/src/test/resources/META-INF/
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/PersistenceUnitInfoImpl.java
   projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/deployers/test/common/DerbyService.java
Log:
JBAS-5940: Remotely injectable entity manager factory

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	2008-09-11 10:16:51 UTC (rev 78415)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/PersistenceUnitDeployment.java	2008-09-11 10:18:50 UTC (rev 78416)
@@ -34,7 +34,6 @@
 import javax.naming.InitialContext;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.spi.PersistenceProvider;
-import javax.persistence.spi.PersistenceUnitTransactionType;
 
 import org.hibernate.ejb.HibernatePersistence;
 import org.jboss.deployers.vfs.spi.structure.VFSDeploymentUnit;
@@ -42,7 +41,6 @@
 import org.jboss.jpa.spi.PersistenceUnitRegistry;
 import org.jboss.logging.Logger;
 import org.jboss.metadata.jpa.spec.PersistenceUnitMetaData;
-import org.jboss.metadata.jpa.spec.TransactionType;
 import org.jboss.virtual.VFSUtils;
 import org.jboss.virtual.VirtualFile;
 
@@ -224,20 +222,6 @@
 //
 //   }
 
-   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;
-   }
-
-   private List<String> safeList(Set<String> set)
-   {
-      return (set == null || set.isEmpty()) ? Collections.<String>emptyList() : new ArrayList<String>(set);
-   }
-
    public void start() throws Exception
    {
       log.info("Starting persistence unit " + kernelName);
@@ -246,19 +230,16 @@
       props.putAll(defaultPersistenceProperties);
       props.put(HibernatePersistence.JACC_CONTEXT_ID, getJaccContextId());
 
-      PersistenceUnitInfoImpl pi = new PersistenceUnitInfoImpl();
-      log.debug("Using class loader " + di.getClassLoader());
-      pi.setClassLoader(di.getClassLoader());
+      List<URL> jarFiles = new ArrayList<URL>();
+      Set<String> files = metaData.getJarFiles();
+      if (files != null)
+      {
+         for (String jar : files)
+         {
+            jarFiles.add(getRelativeURL(jar));
+         }
+      }
 
-      ArrayList<URL> jarFiles = new ArrayList<URL>();
-      pi.setJarFiles(jarFiles);
-      pi.setPersistenceProviderClassName(HibernatePersistence.class.getName());
-      log.debug("Found persistence.xml file in EJB3 jar");
-      props.putAll(getProperties());
-      pi.setManagedClassnames(safeList(metaData.getClasses()));
-      pi.setPersistenceUnitName(metaData.getName());
-      pi.setMappingFileNames(safeList(metaData.getMappingFiles()));
-      pi.setExcludeUnlistedClasses(metaData.isExcludeUnlistedClasses());
       VirtualFile root = getPersistenceUnitRoot();
       log.debug("Persistence root: " + root);
       // TODO - update this with VFSUtils helper method
@@ -277,20 +258,9 @@
                url = new URL(urlString.substring(3)); // (vfs)file and (vfs)jar are ok
          }
       }
-      pi.setPersistenceUnitRootUrl(url);
-      PersistenceUnitTransactionType transactionType = getJPATransactionType();
-      pi.setTransactionType(transactionType);
+      
+      PersistenceUnitInfoImpl pi = new PersistenceUnitInfoImpl(metaData, props, di.getClassLoader(), url, jarFiles, initialContext);
 
-      Set<String> files = metaData.getJarFiles();
-      if (files != null)
-      {
-         for (String jar : files)
-         {
-            jarFiles.add(getRelativeURL(jar));
-         }
-      }
-
-      if (metaData.getProvider() != null) pi.setPersistenceProviderClassName(metaData.getProvider());
       if (explicitEntityClasses.size() > 0)
       {
          List<String> classes = pi.getManagedClassNames();
@@ -298,32 +268,7 @@
          else classes.addAll(explicitEntityClasses);
          pi.setManagedClassnames(classes);
       }
-      if (metaData.getJtaDataSource() != null)
-      {
-         pi.setJtaDataSource((javax.sql.DataSource) initialContext.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)
-      {
-         pi.setNonJtaDataSource((javax.sql.DataSource) initialContext.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());
-      }
-      pi.setProperties(props);
-
-      if (pi.getPersistenceUnitName() == null)
-      {
-         throw new RuntimeException("you must specify a name in persistence.xml");
-      }
-
+      
       Class<?> providerClass = Thread.currentThread().getContextClassLoader().loadClass(pi.getPersistenceProviderClassName());
 
       // EJBTHREE-893

Modified: 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	2008-09-11 10:16:51 UTC (rev 78415)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/deployment/PersistenceUnitInfoImpl.java	2008-09-11 10:18:50 UTC (rev 78416)
@@ -22,14 +22,25 @@
 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
  *
@@ -38,6 +49,8 @@
  */
 public class PersistenceUnitInfoImpl implements PersistenceUnitInfo
 {
+   private static final Logger log = Logger.getLogger(PersistenceUnitInfoImpl.class);
+   
    private String entityManagerName;
    private DataSource jtaDataSource;
    private DataSource nonJtaDataSource;
@@ -55,11 +68,90 @@
    {
    }
 
+   /**
+    * 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;
@@ -95,6 +187,15 @@
       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;
@@ -122,6 +223,9 @@
 
    public void setJarFiles(List<URL> jarFiles)
    {
+      // Hibernate EM 3.3.2.GA LogHelper at 49
+      assert jarFiles != null : "jarFiles is null";
+      
       this.jarFiles = jarFiles;
    }
 
@@ -140,6 +244,12 @@
       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;

Copied: projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/remote/RemotelyInjectEntityManagerFactory.java (from rev 78404, projects/jpa/trunk/sandbox/src/main/java/org/jboss/jpa/sandbox/remote/RemotelyInjectEntityManagerFactory.java)
===================================================================
--- projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/remote/RemotelyInjectEntityManagerFactory.java	                        (rev 0)
+++ projects/jpa/trunk/deployers/src/main/java/org/jboss/jpa/remote/RemotelyInjectEntityManagerFactory.java	2008-09-11 10:18:50 UTC (rev 78416)
@@ -0,0 +1,214 @@
+/*
+ * 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.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
+ * 
+ * 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);
+      }
+      */
+   }
+   
+   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/test/java/org/jboss/jpa/deployers/test/common/DerbyService.java
===================================================================
--- projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/deployers/test/common/DerbyService.java	2008-09-11 10:16:51 UTC (rev 78415)
+++ projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/deployers/test/common/DerbyService.java	2008-09-11 10:18:50 UTC (rev 78416)
@@ -68,6 +68,11 @@
       ds = null;
    }
    
+   public void setJndiName(String jndiName)
+   {
+      this.jndiName = jndiName;
+   }
+   
    public void start() throws Exception
    {
       log.info("Starting derby service");

Copied: projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/remote/test/remote/Person.java (from rev 78404, projects/jpa/trunk/sandbox/src/test/java/org/jboss/jpa/sandbox/test/remote/Person.java)
===================================================================
--- projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/remote/test/remote/Person.java	                        (rev 0)
+++ projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/remote/test/remote/Person.java	2008-09-11 10:18:50 UTC (rev 78416)
@@ -0,0 +1,36 @@
+/*
+ * 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.remote.test.remote;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+/**
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+ at Entity
+public class Person
+{
+   @Id
+   private int id;
+}

Copied: projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/remote/test/remote/RemoteTestCase.java (from rev 78404, projects/jpa/trunk/sandbox/src/test/java/org/jboss/jpa/sandbox/test/remote/RemoteTestCase.java)
===================================================================
--- projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/remote/test/remote/RemoteTestCase.java	                        (rev 0)
+++ projects/jpa/trunk/deployers/src/test/java/org/jboss/jpa/remote/test/remote/RemoteTestCase.java	2008-09-11 10:18:50 UTC (rev 78416)
@@ -0,0 +1,127 @@
+/*
+ * 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.remote.test.remote;
+
+import java.io.IOException;
+import java.rmi.MarshalledObject;
+
+import javax.naming.InitialContext;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.jboss.jpa.deployers.test.common.DerbyService;
+import org.jboss.jpa.remote.RemotelyInjectEntityManagerFactory;
+import org.jboss.metadata.jpa.spec.PersistenceUnitMetaData;
+import org.jboss.metadata.jpa.spec.TransactionType;
+import org.jnp.server.SingletonNamingServer;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Note that this test doesn't really do remote, it uses serialization.
+ * After deserialization care has to be taking not to use any of the
+ * facilities that are already in the VM.
+ * 
+ * @author <a href="mailto:cdewolf at redhat.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class RemoteTestCase
+{
+   private static SingletonNamingServer namingServer;
+
+   private static DerbyService derbyService;
+   
+   // not generic in Java 5
+   @SuppressWarnings("unchecked")
+   private MarshalledObject mo;
+   
+   private InitialContext ctx;
+   
+   @AfterClass
+   public static void afterClass() throws Exception
+   {
+      if(derbyService != null)
+      {
+         derbyService.stop();
+         derbyService.destroy();
+         derbyService = null;
+      }
+      
+      if(namingServer != null)
+      {
+         namingServer.destroy();
+         namingServer = null;
+      }
+   }
+
+   @BeforeClass
+   public static void beforeClass() throws Exception
+   {
+      namingServer = new SingletonNamingServer();
+      
+      derbyService = new DerbyService();
+      derbyService.setJndiName("remoteDS");
+      
+      derbyService.create();
+      derbyService.start();
+   }
+
+   @SuppressWarnings("unchecked")
+   @Before
+   public void setup() throws IOException
+   {
+      PersistenceUnitMetaData metaData = new PersistenceUnitMetaData();
+      metaData.setName("remotePU");
+      metaData.setNonJtaDataSource("remoteDS");
+      metaData.setTransactionType(TransactionType.RESOURCE_LOCAL);
+      
+      RemotelyInjectEntityManagerFactory factory = new RemotelyInjectEntityManagerFactory(metaData, "dummy");
+      
+      mo = new MarshalledObject(factory);
+   }
+   
+   @Test
+   public void test1() throws IOException, ClassNotFoundException
+   {
+      EntityManagerFactory factory = (EntityManagerFactory) mo.get();
+      
+      EntityManager em = factory.createEntityManager();
+      
+      em.close();
+   }
+
+   
+   @Test
+   public void test2() throws IOException, ClassNotFoundException
+   {
+      EntityManagerFactory factory = (EntityManagerFactory) mo.get();
+      
+      EntityManager em = factory.createEntityManager();
+      
+      Person person = new Person();
+      em.persist(person);
+      
+      em.close();
+   }
+}

Copied: projects/jpa/trunk/deployers/src/test/resources/META-INF (from rev 78404, projects/jpa/trunk/sandbox/src/test/resources/META-INF)




More information about the jboss-cvs-commits mailing list