Added support for PersistenceAwareClass -- wraps java.lang.Class thinly with SourceTracker.

Modified MetaDataRepository to add a container for PersistenceAwareClasses.



git-svn-id: https://svn.apache.org/repos/asf/incubator/openjpa/trunk@433811 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Pinaki Poddar 2006-08-23 00:11:29 +00:00
parent ac98ba5c80
commit 1aec5d3dde
2 changed files with 196 additions and 1 deletions

View File

@ -20,6 +20,7 @@ import java.util.Arrays;
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 @@ public class MetaDataRepository
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 @@ public class MetaDataRepository
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());
@ -696,6 +700,19 @@ public class MetaDataRepository
return (ClassMetaData) _metas.get(cls);
}
/**
* 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.
@ -726,12 +743,37 @@ public class MetaDataRepository
return meta;
}
/**
* 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,7 +886,26 @@ public class MetaDataRepository
}
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 @@ public class MetaDataRepository
(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.
*/

View File

@ -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());
}
}