Author: rmannibucau
Date: Sat Nov 10 10:29:18 2012
New Revision: 1407745

URL: http://svn.apache.org/viewvc?rev=1407745&view=rev
Log:
TOMEE-566 scanning of entities

Added:
    
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/EntitiesScanningTest.java
      - copied, changed from r1406996, 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionGlobalJPATest.java
Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceModule.java
    
openejb/trunk/openejb/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/unit/PersistenceUnit.java

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java?rev=1407745&r1=1407744&r2=1407745&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/ReloadableEntityManagerFactory.java
 Sat Nov 10 10:29:18 2012
@@ -386,6 +386,14 @@ public class ReloadableEntityManagerFact
         return entityManagerFactoryCallable.getUnitInfo();
     }
 
+    public void setExcludeUnlistedClasses(boolean excludeUnlistedClasses) {
+        
entityManagerFactoryCallable.getUnitInfo().setExcludeUnlistedClasses(excludeUnlistedClasses);
+    }
+
+    public boolean getExcludeUnlistedClasses() {
+        return 
entityManagerFactoryCallable.getUnitInfo().excludeUnlistedClasses();
+    }
+
     @MBean
     @Internal
     @Description("represents a persistence unit managed by OpenEJB")
@@ -479,6 +487,12 @@ public class ReloadableEntityManagerFact
         }
 
         @ManagedOperation
+        @Description("exclude or not unlisted entities")
+        public void setExcludeUnlistedClasses(boolean value) {
+            reloadableEntityManagerFactory.setExcludeUnlistedClasses(value);
+        }
+
+        @ManagedOperation
         @Description("change the validation mode if possible (value is ok)")
         public void setValidationMode(String value) {
             try {

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java?rev=1407745&r1=1407744&r2=1407745&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
 Sat Nov 10 10:29:18 2012
@@ -77,6 +77,7 @@ import org.apache.openejb.jee.MethodPerm
 import org.apache.openejb.jee.NamedMethod;
 import org.apache.openejb.jee.OutboundResourceAdapter;
 import org.apache.openejb.jee.ParamValue;
+import org.apache.openejb.jee.Persistence;
 import org.apache.openejb.jee.PersistenceContextRef;
 import org.apache.openejb.jee.PersistenceContextType;
 import org.apache.openejb.jee.PersistenceUnitRef;
@@ -180,6 +181,7 @@ import javax.interceptor.Interceptors;
 import javax.jms.Queue;
 import javax.jws.HandlerChain;
 import javax.jws.WebService;
+import javax.persistence.Entity;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.PersistenceContext;
@@ -239,6 +241,9 @@ import static org.apache.openejb.util.Jo
 public class AnnotationDeployer implements DynamicDeployer {
     public static final Logger logger = 
Logger.getInstance(LogCategory.OPENEJB_STARTUP, 
AnnotationDeployer.class.getPackage().getName());
     public static final Logger startupLogger = 
Logger.getInstance(LogCategory.OPENEJB_STARTUP_CONFIG, 
"org.apache.openejb.util.resources");
+
+    public static final String OPENEJB_JPA_AUTO_SCAN = "openejb.jpa.auto-scan";
+
     private static final ThreadLocal<DeploymentModule> currentModule = new 
ThreadLocal<DeploymentModule>();
     private static final Set<String> lookupMissing = new HashSet<String>(2);
     private static final String[] JSF_CLASSES = new String[] {
@@ -1203,7 +1208,7 @@ public class AnnotationDeployer implemen
                         } catch (ClassNotFoundException e) {
                             // todo log debug warning
                         } catch (java.lang.NoClassDefFoundError e) {
-
+                            // no-op
                         }
                     }
 
@@ -1239,6 +1244,7 @@ public class AnnotationDeployer implemen
                     final Class<?> clazz = 
ejbModule.getClassLoader().loadClass(bean.getEjbClass());
                     sessionBean.setSessionType(getSessionType(clazz));
                 } catch (Throwable handledInValidation) {
+                    // no-op
                 }
             }
 
@@ -1471,6 +1477,24 @@ public class AnnotationDeployer implemen
                 addJaxRsProviders(finder, 
ejbModule.getAppModule().getJaxRsProviders(), Provider.class);
             }
 
+            for (PersistenceModule pm : 
ejbModule.getAppModule().getPersistenceModules()) {
+                for (org.apache.openejb.jee.jpa.unit.PersistenceUnit pu : 
pm.getPersistence().getPersistenceUnit()) {
+                    if ((pu.isExcludeUnlistedClasses() == null || 
!pu.isExcludeUnlistedClasses())
+                            && 
"true".equalsIgnoreCase(pu.getProperties().getProperty(OPENEJB_JPA_AUTO_SCAN))) 
{
+                        // no need of meta currently since JPA providers 
doesn't support it
+                        final List<Class<?>> classes = 
finder.findAnnotatedClasses(Entity.class);
+                        final List<String> existingClasses = pu.getClazz();
+                        for (Class<?> clazz : classes) {
+                            final String name = clazz.getName();
+                            if (!existingClasses.contains(name)) {
+                                pu.getClazz().add(name);
+                            }
+                        }
+                        pu.setScanned(true);
+                    }
+                }
+            }
+
             return ejbModule;
         }
 

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java?rev=1407745&r1=1407744&r2=1407745&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/AppInfoBuilder.java
 Sat Nov 10 10:29:18 2012
@@ -603,7 +603,7 @@ class AppInfoBuilder {
                 info.transactionType = 
persistenceUnit.getTransactionType().toString();
 
                 Boolean excludeUnlistedClasses = 
persistenceUnit.isExcludeUnlistedClasses();
-                info.excludeUnlistedClasses = excludeUnlistedClasses != null 
&& excludeUnlistedClasses;
+                info.excludeUnlistedClasses = persistenceUnit.isScanned() || 
(excludeUnlistedClasses != null && excludeUnlistedClasses);
 
                 info.jtaDataSource = persistenceUnit.getJtaDataSource();
                 info.nonJtaDataSource = persistenceUnit.getNonJtaDataSource();

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceModule.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceModule.java?rev=1407745&r1=1407744&r2=1407745&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceModule.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/PersistenceModule.java
 Sat Nov 10 10:29:18 2012
@@ -16,17 +16,14 @@
  */
 package org.apache.openejb.config;
 
-import java.util.HashMap;
-import java.util.List;
 import org.apache.openejb.jee.jpa.unit.Persistence;
 
 import java.io.File;
 import java.net.URI;
+import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 import java.util.TreeSet;
-import java.util.Map;
-import org.apache.openejb.jee.jpa.unit.TransactionType;
 
 public class PersistenceModule implements DeploymentModule {
     private String rootUrl;

Copied: 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/EntitiesScanningTest.java
 (from r1406996, 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionGlobalJPATest.java)
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/EntitiesScanningTest.java?p2=openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/EntitiesScanningTest.java&p1=openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionGlobalJPATest.java&r1=1406996&r2=1407745&rev=1407745&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/assembler/classic/DataSourceDefinitionGlobalJPATest.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/config/EntitiesScanningTest.java
 Sat Nov 10 10:29:18 2012
@@ -14,90 +14,61 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.openejb.assembler.classic;
+package org.apache.openejb.config;
 
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.util.Properties;
-import javax.annotation.sql.DataSourceDefinition;
-import javax.ejb.EJB;
-import javax.ejb.Stateless;
-import javax.persistence.Entity;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.GeneratedValue;
-import javax.persistence.Id;
-import javax.persistence.PersistenceUnit;
-import javax.sql.DataSource;
-import org.apache.commons.dbcp.DelegatingConnection;
-import org.apache.commons.dbcp.managed.ManagedConnection;
+import org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory;
 import org.apache.openejb.jee.jpa.unit.Persistence;
 import org.apache.openejb.junit.ApplicationComposer;
 import org.apache.openejb.junit.Configuration;
 import org.apache.openejb.junit.Module;
-import org.apache.openejb.resource.jdbc.dbcp.BasicManagedDataSource;
-import org.hsqldb.jdbc.JDBCConnection;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
+import javax.ejb.Stateless;
+import javax.persistence.Entity;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.PersistenceUnit;
+import java.util.Properties;
+
 import static junit.framework.Assert.assertEquals;
-import static org.hamcrest.core.IsInstanceOf.instanceOf;
-import static org.junit.Assert.assertThat;
 
 @RunWith(ApplicationComposer.class)
-public class DataSourceDefinitionGlobalJPATest {
-
-    @EJB
-    private EmfHolder holder;
+public class EntitiesScanningTest {
 
     @Module
     public Class<?>[] app() throws Exception {
-        return new Class<?>[]{EmfHolder.class};
+        return new Class<?>[]{ SomeSimpleEJb.class, SomeSimpleEntity.class };
     }
 
-    @Configuration // useless but add another datasource so resolution should 
be more tricky, name before d
+    @Configuration
     public Properties config() {
         final Properties p = new Properties();
-        p.put("cczczcz", "new://Resource?type=DataSource");
-        p.put("cczczcz.JdbcDriver", "org.hsqldb.jdbcDriver");
-        p.put("cczczcz.JdbcUrl", "jdbc:hsqldb:mem:cczczcz");
+        p.put("cczczczedc", "new://Resource?type=DataSource");
+        p.put("cczczczedc.JdbcDriver", "org.hsqldb.jdbcDriver");
+        p.put("cczczczedc.JdbcUrl", "jdbc:hsqldb:mem:cczccdzcz");
         return p;
     }
 
     @Module
     public Persistence persistence() {
-        org.apache.openejb.jee.jpa.unit.PersistenceUnit unit = new 
org.apache.openejb.jee.jpa.unit.PersistenceUnit("jpa-global-dsdef-unit");
-        unit.addClass(IdEntity.class);
+        final org.apache.openejb.jee.jpa.unit.PersistenceUnit unit = new 
org.apache.openejb.jee.jpa.unit.PersistenceUnit("jpa-global-dsdef-unit");
         unit.setProperty("openjpa.jdbc.SynchronizeMappings", 
"buildSchema(ForeignKeys=true)");
         unit.getProperties().setProperty("openjpa.RuntimeUnenhancedClasses", 
"supported");
-        unit.setJtaDataSource("java:app/foo");
-        unit.setExcludeUnlistedClasses(true);
+        unit.getProperties().setProperty("openejb.jpa.auto-scan", "true");
 
-        Persistence persistence = new Persistence(unit);
+        final Persistence persistence = new Persistence(unit);
         persistence.setVersion("2.0");
         return persistence;
     }
 
-    @DataSourceDefinition(
-            name = "java:app/foo",
-            className = "org.hsqldb.jdbc.JDBCDataSource",
-            user = "sa",
-            password = "",
-            url = "jdbc:hsqldb:mem:dsdjpa"
-    )
     @Stateless
-    public static class EmfHolder {
-        @PersistenceUnit
-        private EntityManagerFactory emf;
-
-        public EntityManagerFactory getEmf() {
-            return emf;
-        }
+    public static class SomeSimpleEJb {
     }
 
     @Entity
-    public static class IdEntity {
+    public static class SomeSimpleEntity {
         @Id
         @GeneratedValue
         private long id;
@@ -111,28 +82,14 @@ public class DataSourceDefinitionGlobalJ
         }
     }
 
+    @PersistenceUnit
+    private EntityManagerFactory emf;
+
     @Test
     public void check() throws Exception {
-        final EntityManagerFactory emf = holder.getEmf();
-        assertThat(emf, instanceOf(ReloadableEntityManagerFactory.class));
-        final ReloadableEntityManagerFactory remf = 
(ReloadableEntityManagerFactory) emf;
-        final DataSource ds = remf.info().getJtaDataSource();
-        check(ds, "dsdjpa");
-    }
-
-    private void check(final DataSource ds, final String name) throws 
SQLException, NoSuchMethodException, InvocationTargetException, 
IllegalAccessException {
-        // the first "cast part" is not important, we just want to check the 
jdbc url is ok
-        assertThat(ds, instanceOf(BasicManagedDataSource.class));
-        final BasicManagedDataSource dbcp = (BasicManagedDataSource) ds;
-        final Connection connection = dbcp.getConnection();
-        assertThat(connection, instanceOf(ManagedConnection.class));
-        final ManagedConnection mc = (ManagedConnection) connection;
-        final Method getInnermostDelegateInternal = 
DelegatingConnection.class.getDeclaredMethod("getInnermostDelegateInternal");
-        getInnermostDelegateInternal.setAccessible(true);
-        final Connection delegate = (Connection) 
getInnermostDelegateInternal.invoke(mc);
-        assertThat(delegate, instanceOf(JDBCConnection.class));
-        final Method getURL = JDBCConnection.class.getDeclaredMethod("getURL");
-        getURL.setAccessible(true);
-        assertEquals("jdbc:hsqldb:mem:" + name, getURL.invoke(delegate));
+        final ReloadableEntityManagerFactory remf = 
((ReloadableEntityManagerFactory) emf);
+        assertEquals(1, remf.getManagedClasses().size());
+        assertEquals(SomeSimpleEntity.class.getName(), 
remf.getManagedClasses().iterator().next());
+        assertEquals(true, remf.getExcludeUnlistedClasses());
     }
 }

Modified: 
openejb/trunk/openejb/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/unit/PersistenceUnit.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/unit/PersistenceUnit.java?rev=1407745&r1=1407744&r2=1407745&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/unit/PersistenceUnit.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-jee/src/main/java/org/apache/openejb/jee/jpa/unit/PersistenceUnit.java
 Sat Nov 10 10:29:18 2012
@@ -125,6 +125,17 @@ public class PersistenceUnit {
     protected String name;
     @XmlAttribute(name = "transaction-type")
     protected TransactionType transactionType;
+    @XmlTransient
+    protected boolean scanned = false;
+
+    public boolean isScanned() {
+        return scanned;
+    }
+
+    public void setScanned(boolean scanned) {
+        this.scanned = scanned;
+    }
+
 
     public PersistenceUnit(String name, String provider) {
         this.name = name;


Reply via email to