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