Search in sources :

Example 36 with NucleusContext

use of org.datanucleus.NucleusContext in project datanucleus-api-jdo by datanucleus.

the class MetaDataParserTest method testParseMetaDataURLnullhandler.

public void testParseMetaDataURLnullhandler() {
    NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
    MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
    try {
        parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/package2.jdo"), null);
        fail("expected JPOXException");
    } catch (NucleusException ex) {
    // expected
    }
}
Also used : NucleusContext(org.datanucleus.NucleusContext) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 37 with NucleusContext

use of org.datanucleus.NucleusContext in project datanucleus-api-jdo by datanucleus.

the class JDOMetaDataHandlerTest method testParseNamespace.

public void testParseNamespace() {
    NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
    MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
    MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/package2.jdo"), "jdo");
    assertNotNull(md);
}
Also used : MetaData(org.datanucleus.metadata.MetaData) NucleusContext(org.datanucleus.NucleusContext) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager)

Example 38 with NucleusContext

use of org.datanucleus.NucleusContext in project datanucleus-api-jdo by datanucleus.

the class PersistenceFileMetaDataHandlerTest method testParseDefaultNamespace.

public void testParseDefaultNamespace() {
    NucleusContext nucCtx = new PersistenceNucleusContextImpl("JDO", null);
    MetaDataParser parser = new MetaDataParser(new JDOMetaDataManager(nucCtx), nucCtx.getPluginManager(), true, true);
    MetaData md = parser.parseMetaDataURL(getClass().getResource("/org/datanucleus/api/jdo/metadata/xml/persistence1.xml"), "persistence");
    assertNotNull(md);
}
Also used : MetaData(org.datanucleus.metadata.MetaData) NucleusContext(org.datanucleus.NucleusContext) PersistenceNucleusContextImpl(org.datanucleus.PersistenceNucleusContextImpl) MetaDataParser(org.datanucleus.metadata.xml.MetaDataParser) JDOMetaDataManager(org.datanucleus.api.jdo.metadata.JDOMetaDataManager)

Example 39 with NucleusContext

use of org.datanucleus.NucleusContext 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)

Example 40 with NucleusContext

use of org.datanucleus.NucleusContext in project datanucleus-api-jdo by datanucleus.

the class JDOReplicationManager method replicate.

/**
 * Method to perform the replication for all objects of the specified types.
 * @param types Classes to replicate
 */
public void replicate(Class... types) {
    if (NucleusLogger.PERSISTENCE.isDebugEnabled()) {
        NucleusLogger.PERSISTENCE.debug(Localiser.msg("012052", pmfSource, pmfTarget, StringUtils.objectArrayToString(types)));
    }
    // Check if classes are detachable
    NucleusContext nucleusCtxSource = ((JDOPersistenceManagerFactory) pmfSource).getNucleusContext();
    MetaDataManager mmgr = nucleusCtxSource.getMetaDataManager();
    ClassLoaderResolver clr = nucleusCtxSource.getClassLoaderResolver(null);
    for (int i = 0; i < types.length; i++) {
        AbstractClassMetaData cmd = mmgr.getMetaDataForClass(types[i], clr);
        if (!cmd.isDetachable()) {
            throw new JDOUserException("Class " + types[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();
        ArrayList objects = new ArrayList();
        for (int i = 0; i < types.length; i++) {
            AbstractClassMetaData cmd = mmgr.getMetaDataForClass(types[i], clr);
            if (!cmd.isEmbeddedOnly()) {
                Extent ex = pm1.getExtent(types[i]);
                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

NucleusContext (org.datanucleus.NucleusContext)78 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)68 MetaDataManager (org.datanucleus.metadata.MetaDataManager)65 PersistenceNucleusContextImpl (org.datanucleus.PersistenceNucleusContextImpl)59 ClassMetaData (org.datanucleus.metadata.ClassMetaData)52 JPAMetaDataManager (org.datanucleus.api.jpa.metadata.JPAMetaDataManager)51 PersistenceUnitMetaData (org.datanucleus.metadata.PersistenceUnitMetaData)39 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)32 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)15 ClassLoaderResolverImpl (org.datanucleus.ClassLoaderResolverImpl)14 ElementMetaData (org.datanucleus.metadata.ElementMetaData)12 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)10 JoinMetaData (org.datanucleus.metadata.JoinMetaData)10 ArrayList (java.util.ArrayList)7 JDOMetaDataManager (org.datanucleus.api.jdo.metadata.JDOMetaDataManager)7 ColumnMetaData (org.datanucleus.metadata.ColumnMetaData)7 PackageMetaData (org.datanucleus.metadata.PackageMetaData)7 MetaDataParser (org.datanucleus.metadata.xml.MetaDataParser)7 DN2NamingFactory (org.datanucleus.store.schema.naming.DN2NamingFactory)7 IOException (java.io.IOException)4