use of org.datanucleus.NucleusContext in project hive by apache.
the class ObjectStore method clearOutPmfClassLoaderCache.
private static void clearOutPmfClassLoaderCache(PersistenceManagerFactory pmf) {
if ((pmf == null) || (!(pmf instanceof JDOPersistenceManagerFactory))) {
return;
}
// NOTE : This is hacky, and this section of code is fragile depending on DN code varnames
// so it's likely to stop working at some time in the future, especially if we upgrade DN
// versions, so we actively need to find a better way to make sure the leak doesn't happen
// instead of just clearing out the cache after every call.
JDOPersistenceManagerFactory jdoPmf = (JDOPersistenceManagerFactory) pmf;
NucleusContext nc = jdoPmf.getNucleusContext();
try {
Field pmCache = pmf.getClass().getDeclaredField("pmCache");
pmCache.setAccessible(true);
Set<JDOPersistenceManager> pmSet = (Set<JDOPersistenceManager>) pmCache.get(pmf);
for (JDOPersistenceManager pm : pmSet) {
org.datanucleus.ExecutionContext ec = pm.getExecutionContext();
if (ec instanceof org.datanucleus.ExecutionContextThreadedImpl) {
ClassLoaderResolver clr = ((org.datanucleus.ExecutionContextThreadedImpl) ec).getClassLoaderResolver();
clearClr(clr);
}
}
org.datanucleus.plugin.PluginManager pluginManager = jdoPmf.getNucleusContext().getPluginManager();
Field registryField = pluginManager.getClass().getDeclaredField("registry");
registryField.setAccessible(true);
org.datanucleus.plugin.PluginRegistry registry = (org.datanucleus.plugin.PluginRegistry) registryField.get(pluginManager);
if (registry instanceof org.datanucleus.plugin.NonManagedPluginRegistry) {
org.datanucleus.plugin.NonManagedPluginRegistry nRegistry = (org.datanucleus.plugin.NonManagedPluginRegistry) registry;
Field clrField = nRegistry.getClass().getDeclaredField("clr");
clrField.setAccessible(true);
ClassLoaderResolver clr = (ClassLoaderResolver) clrField.get(nRegistry);
clearClr(clr);
}
if (nc instanceof org.datanucleus.PersistenceNucleusContextImpl) {
org.datanucleus.PersistenceNucleusContextImpl pnc = (org.datanucleus.PersistenceNucleusContextImpl) nc;
org.datanucleus.store.types.TypeManagerImpl tm = (org.datanucleus.store.types.TypeManagerImpl) pnc.getTypeManager();
Field clrField = tm.getClass().getDeclaredField("clr");
clrField.setAccessible(true);
ClassLoaderResolver clr = (ClassLoaderResolver) clrField.get(tm);
clearClr(clr);
Field storeMgrField = pnc.getClass().getDeclaredField("storeMgr");
storeMgrField.setAccessible(true);
org.datanucleus.store.rdbms.RDBMSStoreManager storeMgr = (org.datanucleus.store.rdbms.RDBMSStoreManager) storeMgrField.get(pnc);
Field backingStoreField = storeMgr.getClass().getDeclaredField("backingStoreByMemberName");
backingStoreField.setAccessible(true);
Map<String, Store> backingStoreByMemberName = (Map<String, Store>) backingStoreField.get(storeMgr);
for (Store store : backingStoreByMemberName.values()) {
org.datanucleus.store.rdbms.scostore.BaseContainerStore baseStore = (org.datanucleus.store.rdbms.scostore.BaseContainerStore) store;
clrField = org.datanucleus.store.rdbms.scostore.BaseContainerStore.class.getDeclaredField("clr");
clrField.setAccessible(true);
clr = (ClassLoaderResolver) clrField.get(baseStore);
clearClr(clr);
}
}
Field classLoaderResolverMap = AbstractNucleusContext.class.getDeclaredField("classLoaderResolverMap");
classLoaderResolverMap.setAccessible(true);
Map<String, ClassLoaderResolver> loaderMap = (Map<String, ClassLoaderResolver>) classLoaderResolverMap.get(nc);
for (ClassLoaderResolver clr : loaderMap.values()) {
clearClr(clr);
}
classLoaderResolverMap.set(nc, new HashMap<String, ClassLoaderResolver>());
LOG.debug("Removed cached classloaders from DataNucleus NucleusContext");
} catch (Exception e) {
LOG.warn("Failed to remove cached classloaders from DataNucleus NucleusContext", e);
}
}
use of org.datanucleus.NucleusContext in project tests by datanucleus.
the class JDOTestBase method getEnhancedClassesFromFile.
/**
* Method to return a set of enhanced classes for all of those found in the specified JDO MetaData file.
* @param resourceName Name of the MetaData file (relative to the CLASSPATH).
* @return Set of enhanced classes
* @throws IllegalArgumentException if an error occurs reading the file resource
*/
public Class[] getEnhancedClassesFromFile(String resourceName) {
InputStream in = JDOTestBase.class.getClassLoader().getResourceAsStream(resourceName);
if (in == null) {
throw new IllegalArgumentException("Cannot load resource :" + resourceName);
}
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(in));
String buf = null;
while ((buf = br.readLine()) != null) {
sb.append(buf);
}
} catch (IOException e) {
throw new IllegalArgumentException("Error reading MetaData file " + resourceName + ": " + e.getMessage(), e);
} finally {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
String jdoXmlContents = sb.toString();
if (jdoXmlContents == null) {
throw new IllegalArgumentException("Contents of file is null");
}
NucleusContext context = new EnhancementNucleusContextImpl("JDO", null);
MetaDataManager mgr = new JDOMetaDataManager(context);
MetaDataParser parser = new MetaDataParser(mgr, context.getPluginManager(), true, true);
ClassLoaderResolverImpl clr = new ClassLoaderResolverImpl();
// Parse the MetaData
FileMetaData filemd = (FileMetaData) parser.parseMetaDataStream(new ByteArrayInputStream(jdoXmlContents.getBytes()), null, "JDO");
if (filemd == null) {
return null;
}
mgr.registerFile("EnhancerTestXMLFile", filemd, clr);
// Populate/Initialise the MetaData for the actual classes.
for (int i = 0; i < filemd.getNoOfPackages(); i++) {
PackageMetaData pmd = filemd.getPackage(i);
for (int j = 0; j < pmd.getNoOfClasses(); j++) {
ClassMetaData cmd = pmd.getClass(j);
cmd.populate(clr, null, mgr);
cmd.initialise(clr);
}
}
// Enhance the classes
TestClassLoader cl = new TestClassLoader();
Class[] result;
ArrayList<Class> resultList = new ArrayList<Class>();
for (int i = 0; i < filemd.getNoOfPackages(); i++) {
PackageMetaData pmd = filemd.getPackage(i);
for (int j = 0; j < pmd.getNoOfClasses(); j++) {
ClassMetaData cmd = (ClassMetaData) pmd.getClass(j);
// Enhance the class using the MetaData
ClassEnhancer enhancer = getClassEnhancer(cmd, mgr);
enhancer.enhance();
// Save the enhanced class
resultList.add(cl.getClass(cmd.getFullClassName(), enhancer.getClassBytes()));
}
}
result = (Class[]) resultList.toArray(new Class[resultList.size()]);
return result;
}
use of org.datanucleus.NucleusContext in project tests by datanucleus.
the class AnnotationPlusXMLTest method testOneToManyUniMapFK.
/**
* Test of JPA 1-N unidir Map relation
*/
public void testOneToManyUniMapFK() {
NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
metaDataMgr.loadPersistenceUnit(pumd, null);
// owner side
ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Person.class.getName(), clr);
AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("phoneNumbers");
assertNotNull("Department.phoneNumbers is null!", fmd1);
assertEquals("Department.phoneNumbers mapped-by is incorrect", fmd1.getMappedBy(), null);
assertEquals("Department.phoneNumbers relationType is incorrect", fmd1.getRelationType(clr), RelationType.ONE_TO_MANY_UNI);
assertEquals("Department.phoneNumbers jointable name is incorrect", fmd1.getTable(), null);
MapMetaData mmd = fmd1.getMap();
assertNotNull("Department.phoneNumbers has no Map metadata!", mmd);
KeyMetaData keymd = fmd1.getKeyMetaData();
assertNotNull("Department.phoneNumbers has no Key metadata!", keymd);
assertEquals("Department.phoneNumbers has incorrect key mapped-by", keymd.getMappedBy(), "name");
}
use of org.datanucleus.NucleusContext in project tests by datanucleus.
the class AnnotationPlusXMLTest method testOneToOneBi.
/**
* Test of 1-1 bi relation
*/
public void testOneToOneBi() {
NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
metaDataMgr.loadPersistenceUnit(pumd, null);
// non-owner side
ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Boiler.class.getName(), clr);
assertEquals("Boiler has incorrect table name", "JPA_AX_BOILER", cmd1.getTable());
AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("timer");
assertNotNull("Boiler.timer is null!", fmd1);
assertEquals("Boiler.timer mapped-by is incorrect", "boiler", fmd1.getMappedBy());
assertEquals("Boiler.timer relationType is incorrect", RelationType.ONE_TO_ONE_BI, fmd1.getRelationType(clr));
// owner side
ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Timer.class.getName(), clr);
assertEquals("Timer has incorrect table name", "JPA_AX_TIMER", cmd2.getTable());
AbstractMemberMetaData fmd2 = cmd2.getMetaDataForMember("boiler");
assertNotNull("Timer.boiler is null!", fmd2);
assertEquals("Timer.boiler mapped-by is incorrect", null, fmd2.getMappedBy());
assertEquals("Timer.boiler relationType is incorrect", RelationType.ONE_TO_ONE_BI, fmd2.getRelationType(clr));
assertNotNull("Timer.boiler has no column info", fmd2.getColumnMetaData());
assertEquals("Timer.boiler has incorrect number of columns", 1, fmd2.getColumnMetaData().length);
assertEquals("Timer.boiler column name is wrong", "BOILER_ID", fmd2.getColumnMetaData()[0].getName());
}
use of org.datanucleus.NucleusContext in project tests by datanucleus.
the class AnnotationPlusXMLTest method testOneToOneUni.
/**
* Test of 1-1 uni relation
*/
public void testOneToOneUni() {
NucleusContext nucleusCtx = new PersistenceNucleusContextImpl("JPA", null);
ClassLoaderResolver clr = nucleusCtx.getClassLoaderResolver(null);
MetaDataManager metaDataMgr = new JPAMetaDataManager(nucleusCtx);
PersistenceUnitMetaData pumd = getMetaDataForPersistenceUnit(nucleusCtx, "JPATest");
metaDataMgr.loadPersistenceUnit(pumd, null);
ClassMetaData cmd1 = (ClassMetaData) metaDataMgr.getMetaDataForClass(LoginAccount.class.getName(), clr);
assertEquals("LoginAccount has incorrect table name", cmd1.getTable(), "JPA_AX_LOGINACCOUNT");
AbstractMemberMetaData fmd1 = cmd1.getMetaDataForMember("login");
assertNotNull("LoginAccount.login is null!", fmd1);
assertEquals("LoginAccount.login mapped-by is incorrect", fmd1.getMappedBy(), null);
assertEquals("LoginAccount.login relationType is incorrect", fmd1.getRelationType(clr), RelationType.ONE_TO_ONE_UNI);
assertNotNull("LoginAccount.login has no column info", fmd1.getColumnMetaData());
assertEquals("LoginAccount.login has incorrect number of columns", fmd1.getColumnMetaData().length, 1);
assertEquals("LoginAccount.login column name is wrong", fmd1.getColumnMetaData()[0].getName(), "LOGIN_ID");
ClassMetaData cmd2 = (ClassMetaData) metaDataMgr.getMetaDataForClass(Login.class.getName(), clr);
assertEquals("LoginAccount has incorrect table name", cmd2.getTable(), "JPA_AX_LOGIN");
}
Aggregations