You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by mi...@apache.org on 2009/10/04 01:40:26 UTC

svn commit: r821449 - in /openjpa/branches/1.2.x: openjpa-kernel/src/main/java/org/apache/openjpa/datacache/ openjpa-kernel/src/main/java/org/apache/openjpa/meta/ openjpa-persistence/src/main/java/org/apache/openjpa/persistence/

Author: mikedd
Date: Sat Oct  3 23:40:26 2009
New Revision: 821449

URL: http://svn.apache.org/viewvc?rev=821449&view=rev
Log:
OPENJPA-1328:
Check cacheabiity in ClassMetaData instead of AbstractDataCache.
Submitted By: Jody Grassel

Modified:
    openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java
    openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java
    openjpa/branches/1.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java

Modified: openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java?rev=821449&r1=821448&r2=821449&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java (original)
+++ openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/datacache/AbstractDataCache.java Sat Oct  3 23:40:26 2009
@@ -489,42 +489,4 @@
             StringUtils.isEmpty(types) ? null : new HashSet<String>(Arrays
                 .asList(Strings.split(types, ";", 0)));
     }
-
-    /**
-     * Determine whether a provided class can be applied to this cache.
-     * 
-     * <P>
-     * The algorithm used to determine which types apply is as follows:
-     * <UL>
-     * <LI>If neither included nor excluded types are found all types will be
-     * used.</LI>
-     * <LI>If included types are specified and excluded types are not specified
-     * <b>only</b> the included types will be used.</LI>
-     * <LI>If included types are not specified and excluded types are specified
-     * all types will be used <b>except</b> those which are explicitly excluded.
-     * </LI>
-     * <LI>If both included types and excluded types are specified then
-     * <b>only</b> the included types will be used. If an included type is also
-     * an excluded type the <b>excluded</b> setting will take precedence (ie 
-     * the type will not be used).</LI>
-     * </UL>
-     * 
-     * @param className
-     *            A class which may be used by this plugin.
-     * @return True if the type should be used, otherwise false.
-     */
-    public boolean isCacheableType(String classname) {
-        boolean rval = true;
-        if (_includedTypes != null && ! _includedTypes.isEmpty()) { 
-            if(!_includedTypes.contains(classname)) {
-                rval = false;
-            }
-        }
-        if (_excludedTypes != null && ! _excludedTypes.isEmpty()) { 
-            if(_excludedTypes.contains(classname)) {
-                rval = false;
-            }
-        }
-        return rval;
-    }
 }

Modified: openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java?rev=821449&r1=821448&r2=821449&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java (original)
+++ openjpa/branches/1.2.x/openjpa-kernel/src/main/java/org/apache/openjpa/meta/ClassMetaData.java Sat Oct  3 23:40:26 2009
@@ -42,12 +42,14 @@
 import org.apache.openjpa.enhance.PCRegistry;
 import org.apache.openjpa.enhance.Reflection;
 import org.apache.openjpa.enhance.PersistenceCapable;
+import org.apache.openjpa.lib.conf.Configurations;
 import org.apache.openjpa.lib.conf.Value;
 import org.apache.openjpa.lib.conf.ValueListener;
 import org.apache.openjpa.lib.log.Log;
 import org.apache.openjpa.lib.meta.SourceTracker;
 import org.apache.openjpa.lib.util.J2DoPrivHelper;
 import org.apache.openjpa.lib.util.Localizer;
+import org.apache.openjpa.lib.util.Options;
 import org.apache.openjpa.lib.xml.Commentable;
 import org.apache.openjpa.util.BigDecimalId;
 import org.apache.openjpa.util.BigIntegerId;
@@ -196,6 +198,7 @@
     private boolean _abstract = false;
     private Boolean _hasAbstractPKField = null;
     private Boolean _hasPKFieldsFromAbstractClass = null;
+    private Boolean _isCacheable = null;
 
     /**
      * Constructor. Supply described type and repository.
@@ -1358,7 +1361,7 @@
             else {
                 _cacheName = DataCache.NAME_DEFAULT;
             }
-            if(!isCacheable(_cacheName)) { 
+            if(!isCacheable()) { 
                _cacheName = null; 
             }
         }
@@ -2410,27 +2413,106 @@
     		_cacheTimeout = Integer.MIN_VALUE;
     	}
     }
-
+    
     /**
      * Determine whether this Type should be included in the DataCache (if one
      * is provided) based on the DataCache's configuration.
      * 
      * @return true if the DataCache will accept this type, otherwise false.
      */
-    private boolean isCacheable(String candidateCacheName) {
-        boolean rval = true;
-        DataCache cache =
-            getRepository().getConfiguration().getDataCacheManagerInstance()
-                .getDataCache(candidateCacheName);
-        if (cache != null && (cache instanceof AbstractDataCache)) {
-            AbstractDataCache adc = (AbstractDataCache) cache;
-            if (!adc.isCacheableType(getDescribedType().getName()))
-                rval = false;
-        }
-        return rval;
+    private boolean isCacheable() {
+    	if (_isCacheable != null) {
+    		return _isCacheable.booleanValue();
+    	}
+    	    	
+    	setIsCacheable(true, false);
+    	return _isCacheable.booleanValue();
     }
 
     /**
+     * <p>
+     * Set whether or not the class represented by this ClassMetaData object should be included in the datacache. The
+     * arguments provided are *hints* as to whether the class should be included in the datacache, and can be overridden
+     * by the configuration set in openjpa.Datacache.
+     * </p>
+     * 
+     * <p>
+     * Rules for this determination are:
+     * </p>
+     * <ol>
+     * <li>If the class shows up in the list of excluded types, it does not get cached, period.</li>
+     * <li>If the class does not show up in the excluded types, but the included types field is set (ie, has at least
+     * one class), then:
+     * <ol>
+     * <li>If the class is listed in the include list, then it gets cached</li>
+     * <li>If the class is set as cacheable by the @Datacache annotation, it gets cached</li>
+     * <li>If neither a or b are true, then the class does not get cached</li>
+     * </ol>
+     * </li>
+     * <li>If neither the include or exclude lists are defined, then go along with the value passed into the argument,
+     * which is either the default value (true) or whatever was set with the @Datacache annotation</li>
+     * </ol>
+     * 
+     * @param isCacheable
+     *            Hint whether this class should be included in the datacache. Default behavior is yes, though the
+     *            @Datacache annotation can specify if it should not be cached.
+     * @param annotationOverride
+     *            Whether this hint originated from the @Datacache annotation or whether this is the default "yes" hint.
+     *            The origination of the hint influences the decision making process in rule #2b.
+     * 
+     */
+    public void setIsCacheable(boolean isCacheable, boolean annotationOverride) {
+    	Options dataCacheOptions = getDataCacheOptions();
+    	Set excludedTypes = extractDataCacheClassListing(dataCacheOptions.getProperty("ExcludedTypes", null));
+    	Set types = extractDataCacheClassListing(dataCacheOptions.getProperty("Types", null));
+    	
+    	String className = getDescribedType().getName();
+    	if (excludedTypes != null && excludedTypes.contains(className)) {
+    		// Rule #1
+    		_isCacheable = Boolean.FALSE;
+    	} else if (types != null) {
+    		// Rule #2
+    		if ((annotationOverride && isCacheable) || (types.contains(className))) {
+    			_isCacheable = Boolean.TRUE;
+    		} else {
+    			_isCacheable = Boolean.FALSE;
+    		}
+    	} else {
+    		// Rule #3
+    		_isCacheable = isCacheable ? Boolean.TRUE : Boolean.FALSE;
+    	}
+    }
+    
+    /**
+     * Extract all of the DataCache plugin options from the configuration
+     * 
+     */
+    private Options getDataCacheOptions() {
+    	String dataCacheConfig = getRepository().getConfiguration().getDataCache();
+    	Options dataCacheOptions = 
+    		Configurations.parseProperties(Configurations.getProperties(dataCacheConfig));
+    	return dataCacheOptions;    
+    }
+    
+    /**
+     * Tool to extract classes defined in the datacache include and exclude list into
+     * individual entries in a Set.
+     * 
+     */
+    private final Set extractDataCacheClassListing(String classList) {
+    	if (classList == null || classList.length() == 0)
+    		return null;
+    	
+    	HashSet returnSet = new HashSet();
+    	String[] entries = classList.split(";");
+    	for (int index = 0; index < entries.length; index++) {
+    		returnSet.add(entries[index]);
+    	}
+    	
+    	return returnSet;
+    }
+    
+    /**
      * Returns true if the pcType modeled by this ClassMetaData
      * object is abstract (ie, a MappedSuperclass in JPA terms.)
      *

Modified: openjpa/branches/1.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java
URL: http://svn.apache.org/viewvc/openjpa/branches/1.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java?rev=821449&r1=821448&r2=821449&view=diff
==============================================================================
--- openjpa/branches/1.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java (original)
+++ openjpa/branches/1.2.x/openjpa-persistence/src/main/java/org/apache/openjpa/persistence/AnnotationPersistenceMetaDataParser.java Sat Oct  3 23:40:26 2009
@@ -757,6 +757,8 @@
                 org.apache.openjpa.datacache.DataCache.NAME_DEFAULT);
         else
             meta.setDataCacheName(null);
+        
+        meta.setIsCacheable(cache.enabled(), true);
     }
 
     private void parseManagedInterface(ClassMetaData meta,