Author: jgrassel
Date: Mon Nov 26 21:05:04 2012
New Revision: 1413841

URL: http://svn.apache.org/viewvc?rev=1413841&view=rev
Log:
OPENJPA-2288: MetaDataRepository should be able to filter classes from other 
app ClassLoaders in JEE Env

Modified:
    
openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
    
openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java

Modified: 
openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java?rev=1413841&r1=1413840&r2=1413841&view=diff
==============================================================================
--- 
openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
 (original)
+++ 
openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/conf/Compatibility.java
 Mon Nov 26 21:05:04 2012
@@ -71,6 +71,8 @@ public class Compatibility {
     private boolean _parseAnnotationsForQueryMode = true;  
     private boolean _resetFlushFlagForCascadePersist = false;//OPENJPA-2051
     private boolean _overrideContextClassloader = false; //OPENJPA-1993
+    private boolean _filterPCRegistryClasses = false; // OPENJPA-2288
+   
 
     /**
      * Whether to require exact identity value types when creating object
@@ -637,5 +639,21 @@ public class Compatibility {
     public void setOverrideContextClassloader(boolean 
overrideContextClassloader) {
         _overrideContextClassloader = overrideContextClassloader;
     }
+    
+    /**
+     * Whether the metadata processor should filter classes dispatched by the 
PCRegistry listener system.
+     * 
+     */
+    public boolean  getFilterPCRegistryClasses() {
+        return _filterPCRegistryClasses;
+    }
+    
+    /**
+     * Whether the metadata processor should filter classes dispatched by the 
PCRegistry listener system.
+     * 
+     */
+    public void setFilterPCRegistryClasses(boolean bool) {
+        _filterPCRegistryClasses = bool;
+    }
 }
 

Modified: 
openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
URL: 
http://svn.apache.org/viewvc/openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java?rev=1413841&r1=1413840&r2=1413841&view=diff
==============================================================================
--- 
openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
 (original)
+++ 
openjpa/branches/2.1.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
 Mon Nov 26 21:05:04 2012
@@ -159,7 +159,10 @@ public class MetaDataRepository implemen
     // A boolean used to decide whether or not we need to call to PCEnhancer 
to check whether we have any down level
     // Entities.
     private boolean _logEnhancementLevel = true;
-
+    
+    // A boolean used to decide whether to filter Class<?> objects submitted 
by the PCRegistry listener system
+    private boolean _filterRegisteredClasses = false;
+    
     /**
      * Default constructor. Configure via {@link Configurable}.
      */
@@ -1632,11 +1635,43 @@ public class MetaDataRepository implemen
 
         Collection<String> pcNames = getPersistentTypeNames(false, envLoader);
         Collection<Class<?>> failed = null;
+        
         for (int i = 0; i < reg.length; i++) {
-            // don't process types that aren't listed by the user; may belong
-            // to a different persistence unit
-            if (pcNames != null && !pcNames.isEmpty() && 
!pcNames.contains(reg[i].getName()))
+            // Don't process types that aren't listed by the user; it may 
belong to a different persistence unit.
+            if (pcNames != null && !pcNames.isEmpty() && 
!pcNames.contains(reg[i].getName())) {
                 continue;
+            }
+            
+            // If the compatibility option "filterPCRegistryClasses" is 
enabled, then verify that the type is 
+            // accessible to the envLoader/Thread Context ClassLoader
+            if (_filterRegisteredClasses) {
+                Log log = (_conf == null) ? null : 
_conf.getLog(OpenJPAConfiguration.LOG_RUNTIME);                
+                ClassLoader loadCL = (envLoader != null) ?
+                        envLoader :
+                        
AccessController.doPrivileged(J2DoPrivHelper.getContextClassLoaderAction());
+                
+                try {
+                    Class<?> classFromAppClassLoader = 
Class.forName(reg[i].getName(), true, loadCL);
+                    
+                    if (!reg[i].equals(classFromAppClassLoader)) {
+                        // This is a class that belongs to a ClassLoader not 
associated with the Application,
+                        // so it should be processed.
+                        if (log != null && log.isTraceEnabled()) {
+                            log.trace(
+                                "Metadata Repository will ignore Class " + 
reg[i].getName() + 
+                                ", since it originated from a ClassLoader not 
associated with the application.");
+                        }
+                        continue;
+                    }
+                } catch (ClassNotFoundException cnfe) {
+                    // Catch exception and log its occurrence, and permit MDR 
processing to continue to preserve
+                    // original behavior.
+                    if (log != null && log.isTraceEnabled()) {
+                        log.trace("The Class " + reg[i].getName() + " was 
identified as a persistent class " + 
+                             "by configuration, but the Class could not be 
found.");
+                    }
+                }                              
+            }
 
             checkEnhancementLevel(reg[i]);
             try {
@@ -1876,6 +1911,7 @@ public class MetaDataRepository implemen
     public void setConfiguration(Configuration conf) {
         _conf = (OpenJPAConfiguration) conf;
         _log = _conf.getLog(OpenJPAConfiguration.LOG_METADATA);
+        _filterRegisteredClasses = 
_conf.getCompatibilityInstance().getFilterPCRegistryClasses();
     }
 
     public void startConfiguration() {


Reply via email to