use of org.datanucleus.metadata.MetaDataManager in project datanucleus-rdbms by datanucleus.
the class DynamicSchemaFieldManager method processInterfaceMappingForValue.
protected void processInterfaceMappingForValue(InterfaceMapping intfMapping, Object value, AbstractMemberMetaData mmd, ExecutionContext ec) {
if (intfMapping.getMappingStrategy() == ReferenceMapping.PER_IMPLEMENTATION_MAPPING) {
int intfImplMappingNumber = intfMapping.getMappingNumberForValue(ec, value);
if (intfImplMappingNumber == -1) {
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug("Dynamic schema updates : field=" + mmd.getFullFieldName() + " has an interface mapping yet " + StringUtils.toJVMIDString(value) + " is not a known implementation - trying to update the schema ...");
}
// Make sure the metadata for this value class is loaded (may be first encounter)
MetaDataManager mmgr = ec.getNucleusContext().getMetaDataManager();
ClassLoaderResolver clr = ec.getClassLoaderResolver();
mmgr.getMetaDataForClass(value.getClass(), clr);
String[] impls = ec.getMetaDataManager().getClassesImplementingInterface(intfMapping.getType(), clr);
if (ClassUtils.stringArrayContainsValue(impls, value.getClass().getName())) {
// Value is a valid implementation yet there is no mapping so re-initialize the mapping
try {
if (NucleusLogger.DATASTORE_SCHEMA.isDebugEnabled()) {
NucleusLogger.DATASTORE_SCHEMA.debug("Dynamic schema updates : field=" + mmd.getFullFieldName() + " has a new implementation available so reinitialising its mapping");
}
intfMapping.initialize(mmd, intfMapping.getTable(), clr);
intfMapping.getStoreManager().validateTable((TableImpl) intfMapping.getTable(), clr);
} catch (Exception e) {
NucleusLogger.DATASTORE_SCHEMA.debug("Exception thrown trying to create missing columns for implementation", e);
throw new NucleusException("Exception thrown performing dynamic update of schema", e);
}
schemaUpdatesPerformed = true;
}
}
}
}
use of org.datanucleus.metadata.MetaDataManager in project datanucleus-api-jdo by datanucleus.
the class JDOEnhancer method registerMetadata.
/* (non-Javadoc)
* @see javax.jdo.JDOEnhancer#registerMetadata(javax.jdo.metadata.JDOMetadata)
*/
public void registerMetadata(javax.jdo.metadata.JDOMetadata metadata) {
MetaDataManager mmgr = enhancer.getMetaDataManager();
FileMetaData filemd = ((JDOMetadataImpl) metadata).getInternal();
mmgr.loadUserMetaData(filemd, enhancer.getClassLoader());
}
use of org.datanucleus.metadata.MetaDataManager in project datanucleus-api-jdo by datanucleus.
the class JDOPersistenceManagerFactory method getMetadata.
/**
* Method to return the (class) metadata object for the specified class, if there is
* metadata defined for that class.
* @param className Name of the class that we want metadata for
* @return The metadata
*/
public javax.jdo.metadata.TypeMetadata getMetadata(String className) {
MetaDataManager mmgr = nucleusContext.getMetaDataManager();
AbstractClassMetaData acmd = mmgr.getMetaDataForClass(className, nucleusContext.getClassLoaderResolver(null));
if (acmd == null) {
return null;
}
if (acmd instanceof ClassMetaData) {
return new ClassMetadataImpl((ClassMetaData) acmd);
}
return new InterfaceMetadataImpl((InterfaceMetaData) acmd);
}
use of org.datanucleus.metadata.MetaDataManager in project datanucleus-api-jdo by datanucleus.
the class JDOPersistenceManagerFactory method getManagedClasses.
/**
* Accessor for the classes that are managed (have metadata loaded).
* @return Collection of classes
*/
public Collection<Class> getManagedClasses() {
checkJDOPermission(JDOPermission.GET_METADATA);
MetaDataManager mmgr = nucleusContext.getMetaDataManager();
Collection<String> classNames = mmgr.getClassesWithMetaData();
Collection<Class> classes = new HashSet<Class>();
if (classNames != null) {
ClassLoaderResolver clr = nucleusContext.getClassLoaderResolver(null);
Iterator<String> iter = classNames.iterator();
while (iter.hasNext()) {
try {
Class cls = clr.classForName(iter.next());
classes.add(cls);
} catch (ClassNotResolvedException cnre) {
// Do nothing
}
}
}
return classes;
}
use of org.datanucleus.metadata.MetaDataManager in project datanucleus-api-jdo by datanucleus.
the class JDOReplicationManager method replicate.
/**
* Method to perform the replication for all objects of the specified class names.
* @param classNames Classes to replicate
*/
public void replicate(String... classNames) {
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("012052", pmfSource, pmfTarget, StringUtils.objectArrayToString(classNames)));
}
// Check if classes are detachable
NucleusContext nucleusCtxSource = ((JDOPersistenceManagerFactory) pmfSource).getNucleusContext();
MetaDataManager mmgr = nucleusCtxSource.getMetaDataManager();
ClassLoaderResolver clr = nucleusCtxSource.getClassLoaderResolver(null);
for (int i = 0; i < classNames.length; i++) {
AbstractClassMetaData cmd = mmgr.getMetaDataForClass(classNames[i], clr);
if (!cmd.isDetachable()) {
throw new JDOUserException("Class " + classNames[i] + " is not detachable so cannot replicate");
}
}
Object[] detachedObjects = null;
// Detach from datastore 1
if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
NucleusLogger.PERSISTENCE.debug(Localiser.msg("012053"));
}
PersistenceManager pm1 = pmfSource.getPersistenceManager();
Transaction tx1 = pm1.currentTransaction();
if (getBooleanProperty("datanucleus.replicateObjectGraph")) {
pm1.getFetchPlan().setGroup(javax.jdo.FetchPlan.ALL);
pm1.getFetchPlan().setMaxFetchDepth(-1);
}
try {
tx1.begin();
clr = ((JDOPersistenceManager) pm1).getExecutionContext().getClassLoaderResolver();
ArrayList objects = new ArrayList();
for (int i = 0; i < classNames.length; i++) {
Class cls = clr.classForName(classNames[i]);
AbstractClassMetaData cmd = mmgr.getMetaDataForClass(cls, clr);
if (!cmd.isEmbeddedOnly()) {
Extent ex = pm1.getExtent(cls);
Iterator iter = ex.iterator();
while (iter.hasNext()) {
objects.add(iter.next());
}
}
}
Collection detachedColl = pm1.detachCopyAll(objects);
detachedObjects = detachedColl.toArray();
tx1.commit();
} finally {
if (tx1.isActive()) {
tx1.rollback();
}
pm1.close();
}
replicateInTarget(detachedObjects);
}
Aggregations