Search in sources :

Example 51 with MetaDataManager

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;
            }
        }
    }
}
Also used : ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) NucleusException(org.datanucleus.exceptions.NucleusException) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 52 with MetaDataManager

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());
}
Also used : JDOMetadataImpl(org.datanucleus.api.jdo.metadata.JDOMetadataImpl) MetaDataManager(org.datanucleus.metadata.MetaDataManager) FileMetaData(org.datanucleus.metadata.FileMetaData)

Example 53 with MetaDataManager

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);
}
Also used : MetaDataManager(org.datanucleus.metadata.MetaDataManager) ClassMetadataImpl(org.datanucleus.api.jdo.metadata.ClassMetadataImpl) InterfaceMetadataImpl(org.datanucleus.api.jdo.metadata.InterfaceMetadataImpl) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) ClassMetaData(org.datanucleus.metadata.ClassMetaData)

Example 54 with MetaDataManager

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;
}
Also used : ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) MetaDataManager(org.datanucleus.metadata.MetaDataManager) ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) HashSet(java.util.HashSet)

Example 55 with MetaDataManager

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);
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Extent(javax.jdo.Extent) NucleusContext(org.datanucleus.NucleusContext) ClassLoaderResolver(org.datanucleus.ClassLoaderResolver) ArrayList(java.util.ArrayList) MetaDataManager(org.datanucleus.metadata.MetaDataManager) JDOUserException(javax.jdo.JDOUserException) AbstractClassMetaData(org.datanucleus.metadata.AbstractClassMetaData) Transaction(javax.jdo.Transaction) Iterator(java.util.Iterator) Collection(java.util.Collection)

Aggregations

MetaDataManager (org.datanucleus.metadata.MetaDataManager)102 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)72 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)69 NucleusContext (org.datanucleus.NucleusContext)65 ClassMetaData (org.datanucleus.metadata.ClassMetaData)55 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)51 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)41 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)40 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)38 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)31 JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)19 ElementMetaData (org.datanucleus.metadata.ElementMetaData)12 ArrayList (java.util.ArrayList)11 JoinMetaData (org.datanucleus.metadata.JoinMetaData)10 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)9 PackageMetaData (org.datanucleus.metadata.PackageMetaData)8 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)7 DN2NamingFactory (org.datanucleus.store.schema.naming.DN2NamingFactory)7 FetchPlanForClass (org.datanucleus.FetchPlanForClass)6 FileMetaData (org.datanucleus.metadata.FileMetaData)6