Search in sources :

Example 11 with PersistenceNucleusContext

use of org.datanucleus.PersistenceNucleusContext in project datanucleus-core by datanucleus.

the class MetaDataManagerImpl method getClassesImplementingInterface.

/* (non-Javadoc)
     * @see org.datanucleus.metadata.MetaDataManager#getClassesImplementingInterface(java.lang.String, org.datanucleus.ClassLoaderResolver)
     */
@Override
public String[] getClassesImplementingInterface(String interfaceName, ClassLoaderResolver clr) {
    Collection<Class> classes = new HashSet<>();
    Class intfClass = clr.classForName(interfaceName);
    Collection<String> generatedClassNames = new HashSet<>();
    // Loop through all known classes and find the implementations
    Collection<AbstractClassMetaData> cmds = classMetaDataByClass.values();
    boolean isPersistentInterface = false;
    for (AbstractClassMetaData acmd : cmds) {
        Class implClass = null;
        try {
            implClass = clr.classForName(acmd.getFullClassName());
        } catch (ClassNotResolvedException cnre) {
        // Implementation class not yet generated
        }
        if (implClass != null) {
            if (acmd instanceof ClassMetaData) {
                // Make sure that we are initialised since implementsMetaData wont be set
                initialiseAbstractClassMetaData(acmd, clr);
                if (intfClass.isAssignableFrom(implClass)) {
                    if (!((ClassMetaData) acmd).isAbstract()) {
                        classes.add(implClass);
                    }
                }
            } else if (acmd instanceof InterfaceMetaData) {
                if (intfClass.isAssignableFrom(implClass)) {
                    isPersistentInterface = true;
                }
            }
        } else {
            if (isPersistentInterfaceImplementation(interfaceName, acmd.getFullClassName())) {
                isPersistentInterface = true;
                generatedClassNames.add(acmd.getFullClassName());
            }
        }
    }
    if (isPersistentInterface && nucleusContext instanceof PersistenceNucleusContext && ((PersistenceNucleusContext) nucleusContext).getImplementationCreator() != null) {
        // JDO "persistent interfaces" - deliberately kept separate from normal persistence since it is largely undocumented and best left alone
        // TODO this is very time consuming. got to do some cache
        classes.add(((PersistenceNucleusContext) nucleusContext).getImplementationCreator().newInstance(intfClass, clr).getClass());
        int numClasses = classes.size() + generatedClassNames.size();
        String[] classNames = new String[numClasses];
        int i = 0;
        Iterator<Class> classIter = classes.iterator();
        while (classIter.hasNext()) {
            classNames[i++] = classIter.next().getName();
        }
        Iterator<String> classNameIter = generatedClassNames.iterator();
        while (classNameIter.hasNext()) {
            classNames[i++] = classNameIter.next();
        }
        return classNames;
    } else if (!classes.isEmpty()) {
        // Normal persistence
        // Put the classes into a sorter so we make sure we get the initial implementations first followed by any subclasses of these implementations.
        // This is needed because when generating the schema we require the subclass implementations to already have their datastore column created
        Collection<Class> classesSorted = new TreeSet(new InterfaceClassComparator());
        Iterator<Class> classesIter = classes.iterator();
        while (classesIter.hasNext()) {
            classesSorted.add(classesIter.next());
        }
        // Return the class names (in the same order)
        String[] classNames = new String[classesSorted.size()];
        Iterator<Class> iter = classesSorted.iterator();
        int i = 0;
        while (iter.hasNext()) {
            classNames[i++] = iter.next().getName();
        }
        return classNames;
    }
    return null;
}
Also used : ClassNotResolvedException(org.datanucleus.exceptions.ClassNotResolvedException) TreeSet(java.util.TreeSet) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) Iterator(java.util.Iterator) Collection(java.util.Collection) HashSet(java.util.HashSet)

Example 12 with PersistenceNucleusContext

use of org.datanucleus.PersistenceNucleusContext in project tests by datanucleus.

the class TestRunListener method cleanupDatastore.

private void cleanupDatastore(int number) {
    String datastoreProtocol = TestHelper.getDatastorePluginProtocol(number);
    if (// Avoid creating PMF when clean up may not be supported
    datastoreProtocol != null && !datastoreProtocol.equals("jdbc")) {
        LOG.info("Datastore clean up not supported for datastore=" + datastoreProtocol);
        return;
    }
    JDOPersistenceManagerFactory pmf = (JDOPersistenceManagerFactory) JDOPersistenceTestCase.getPMF(number, null);
    PersistenceNucleusContext ctx = pmf.getNucleusContext();
    if (ctx.getStoreManager() instanceof RDBMSStoreManager) {
        PersistenceManager pm = null;
        JDOConnection jdoConnection = null;
        Connection nativeConnection = null;
        try {
            pm = pmf.getPersistenceManager();
            jdoConnection = pm.getDataStoreConnection();
            // Obtain the connection via PM instead of using the PMF properties so we support DataSource too
            nativeConnection = (Connection) jdoConnection.getNativeConnection();
            cleanupRDBMSdatastore(nativeConnection);
        } catch (SQLException e) {
            LOG.error("Error during datastore clean up", e);
        } finally {
            if (jdoConnection != null) {
                jdoConnection.close();
            }
            if (pm != null) {
                pm.close();
            }
        }
    } else {
        // TODO Support cleanup of mongodb, Cassandra, Excel, ODF etc
        LOG.info("Datastore clean up not supported");
    }
    pmf.close();
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) SQLException(java.sql.SQLException) PersistenceNucleusContext(org.datanucleus.PersistenceNucleusContext) Connection(java.sql.Connection) JDOConnection(javax.jdo.datastore.JDOConnection) JDOConnection(javax.jdo.datastore.JDOConnection) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) RDBMSStoreManager(org.datanucleus.store.rdbms.RDBMSStoreManager)

Aggregations

PersistenceNucleusContext (org.datanucleus.PersistenceNucleusContext)12 ClassLoaderResolver (org.datanucleus.ClassLoaderResolver)7 AttributeConverter (javax.jdo.AttributeConverter)4 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)4 JDOTypeConverter (org.datanucleus.api.jdo.JDOTypeConverter)4 AbstractMemberMetaData (org.datanucleus.metadata.AbstractMemberMetaData)4 HashSet (java.util.HashSet)3 Iterator (java.util.Iterator)3 Map (java.util.Map)3 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)3 AbstractClassMetaData (org.datanucleus.metadata.AbstractClassMetaData)3 FieldMetaData (org.datanucleus.metadata.FieldMetaData)3 FieldPersistenceModifier (org.datanucleus.metadata.FieldPersistenceModifier)3 TypeManager (org.datanucleus.store.types.TypeManager)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 TreeSet (java.util.TreeSet)2 ClassNotResolvedException (org.datanucleus.exceptions.ClassNotResolvedException)2 ArrayMetaData (org.datanucleus.metadata.ArrayMetaData)2 CollectionMetaData (org.datanucleus.metadata.CollectionMetaData)2