openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r433811 - in /incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta: MetaDataRepository.java PersistenceAwareClass.java
Date Wed, 23 Aug 2006 00:11:30 GMT
Author: ppoddar
Date: Tue Aug 22 17:11:29 2006
New Revision: 433811

URL: http://svn.apache.org/viewvc?rev=433811&view=rev
Log:
Added support for PersistenceAwareClass -- wraps java.lang.Class thinly with SourceTracker.
Modified MetaDataRepository to add a container for PersistenceAwareClasses.


Added:
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/PersistenceAwareClass.java
Modified:
    incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java

Modified: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java?rev=433811&r1=433810&r2=433811&view=diff
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
(original)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/MetaDataRepository.java
Tue Aug 22 17:11:29 2006
@@ -20,6 +20,7 @@
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
@@ -85,6 +86,8 @@
     protected final ClassMetaData[] EMPTY_METAS;
     protected final FieldMetaData[] EMPTY_FIELDS;
     protected final Order[] EMPTY_ORDERS;
+    protected static final PersistenceAwareClass[] EMPTY_PAWARE_CLASSES = 
+    	new PersistenceAwareClass[0];
 
     private static final Localizer _loc = Localizer.forPackage
         (MetaDataRepository.class);
@@ -105,7 +108,8 @@
     private final Map _queries = new HashMap();
     private final Map _seqs = new HashMap();
     private final Map _aliases = Collections.synchronizedMap(new HashMap());
-
+    private final Map _pawares = Collections.synchronizedMap(new HashMap());
+    
     // map of classes to lists of their subclasses
     private final Map _subs = Collections.synchronizedMap(new HashMap());
 
@@ -697,6 +701,19 @@
     }
 
     /**
+     * Gets all the registered persistence-aware classes.
+     * 
+     * @return empty array if no class has been registered.
+     * 
+     */
+    public PersistenceAwareClass[] getPersistenceAwareClasses() {
+    	if (_pawares.isEmpty())
+    		return EMPTY_PAWARE_CLASSES;
+    	return (PersistenceAwareClass[])_pawares.values().toArray
+    		(new PersistenceAwareClass[_pawares.size()]);
+    }
+    
+    /**
      * Create a new metadata, populate it with default information, add it to
      * the repository, and return it. Use the default access type.
      */
@@ -727,11 +744,36 @@
     }
 
     /**
+     * Add the given class as persitence-aware.
+     * 
+     * @param cls non-null and must not alreaddy be added as persitence-capable.
+     */
+    public PersistenceAwareClass addPersistenceAware(Class cls) {
+    	if (cls == null)
+    		return null;
+    	if (_pawares.containsKey(cls))
+    		return (PersistenceAwareClass)_pawares.get(cls);
+    	if (getCachedMetaData(cls) == null) {
+    		synchronized(this) {
+	    		PersistenceAwareClass result = newPersistenceAwareClass(cls); 
+	    		_pawares.put(cls,result);
+	    		return result;
+    		}
+    	}
+    	else
+    		throw new MetaDataException(_loc.get("pc-and-aware", cls));
+    }
+    
+    /**
      * Create a new class metadata instance.
      */
     protected ClassMetaData newClassMetaData(Class type) {
         return new ClassMetaData(type, this);
     }
+    
+    protected PersistenceAwareClass newPersistenceAwareClass(Class type) {
+    	return new PersistenceAwareClass(type, this);
+    }
 
     /**
      * Create a new array of the proper class metadata subclass.
@@ -844,8 +886,27 @@
         }
         return false;
     }
+    
+    /**
+     * Remove a persitence-aware class from this receiver.
+     * 
+     * @param cls a class possibly added earlier as persitence-aware.
+     * 
+     * @return true if removed, false if not contained in this receiver
+     */
+    public synchronized boolean removePersistenceAware(Class cls) {
+    	return _pawares.remove(cls) != null;
+    }
 
     /**
+     * Removes all persitence-aware classes from this receiver.
+     *
+     */
+    public synchronized void removeAllPersistenceAware() {
+    	_pawares.clear();
+    }
+    
+    /**
      * Return the least-derived class metadata for the given application
      * identity object.
      *
@@ -1499,6 +1560,18 @@
             (new SequenceMetaData[_seqs.size()]);
     }
 
+    /**
+     * Gets the persistence-aware class corresponding to the given class. Can
+     * be null, if the given class is not registered as persistence-aware with
+     * this receiver.
+     * 
+     * @param cls a Java class possibly registered as persistence-aware earlier
+     * with this receiver.
+     */
+    public synchronized PersistenceAwareClass getPersistenceAware(Class cls) {
+    	return (PersistenceAwareClass)_pawares.get(cls);
+    }
+    
     /**
      * Return the cached a sequence metadata for the given name.
      */

Added: incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/PersistenceAwareClass.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/PersistenceAwareClass.java?rev=433811&view=auto
==============================================================================
--- incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/PersistenceAwareClass.java
(added)
+++ incubator/openjpa/trunk/openjpa-kernel/src/main/java/org/apache/openjpa/meta/PersistenceAwareClass.java
Tue Aug 22 17:11:29 2006
@@ -0,0 +1,122 @@
+package org.apache.openjpa.meta;
+
+import java.io.File;
+
+import org.apache.openjpa.lib.meta.SourceTracker;
+import org.apache.openjpa.lib.xml.Commentable;
+
+public class PersistenceAwareClass 
+	implements Comparable, SourceTracker,Commentable,MetaDataContext,
+	MetaDataModes {
+
+    private final MetaDataRepository _repos;
+	private final Class _class;
+	
+    private File _srcFile = null;
+    private int _srcType = SRC_OTHER;
+    private String[] _comments = null;
+    private int _listIndex = -1;
+    private int _srcMode = MODE_META | MODE_MAPPING;
+	
+	public PersistenceAwareClass(Class cls, MetaDataRepository repos) {
+		_repos = repos;
+		_class = cls;
+	}
+	
+	public String getName() {
+		return _class.getName();
+	}
+	
+	public MetaDataRepository getRepository() {
+		return _repos;
+	}
+	
+	public Class getDescribedType() {
+		return _class;
+	}
+	
+    public File getSourceFile() {
+        return _srcFile;
+    }
+
+    public Object getSourceScope() {
+        return null;
+    }
+
+    public int getSourceType() {
+        return _srcType;
+    }
+
+    public void setSource(File file, int srcType) {
+        _srcFile = file;
+        _srcType = srcType;
+    }
+
+    public String getResourceName() {
+        return _class.getName();
+    }
+
+    /**
+     * The source mode this metadata has been loaded under.
+     */
+    public int getSourceMode() {
+        return _srcMode;
+    }
+
+    /**
+     * The source mode this metadata has been loaded under.
+     */
+    public void setSourceMode(int mode) {
+        _srcMode = mode;
+    }
+
+    /**
+     * The source mode this metadata has been loaded under.
+     */
+    public void setSourceMode(int mode, boolean on) {
+        if (mode == MODE_NONE)
+            _srcMode = mode;
+        else if (on)
+            _srcMode |= mode;
+        else
+            _srcMode &= ~mode;
+    }
+
+    /**
+     * The index in which this class was listed in the metadata. Defaults to
+     * <code>-1</code> if this class was not listed in the metadata.
+     */
+    public int getListingIndex() {
+        return _listIndex;
+    }
+
+    /**
+     * The index in which this field was listed in the metadata. Defaults to
+     * <code>-1</code> if this class was not listed in the metadata.
+     */
+    public void setListingIndex(int index) {
+        _listIndex = index;
+    }
+
+    ///////////////
+    // Commentable
+    ///////////////
+
+    public String[] getComments() {
+        return (_comments == null) ? ClassMetaData.EMPTY_COMMENTS : _comments;
+    }
+
+    public void setComments(String[] comments) {
+        _comments = comments;
+    }
+    
+    public int compareTo(Object other) {
+        if (other == this)
+            return 0;
+        if (other instanceof PersistenceAwareClass)
+        	return 1;
+        return _class.getName().compareTo(((ClassMetaData) other).
+            getDescribedType().getName());
+    }
+
+}



Mime
View raw message