List of usage examples for java.lang ClassLoader getParent
@CallerSensitive public final ClassLoader getParent()
From source file:org.nextframework.controller.CachedIntrospectionResults.java
/** * Check whether the given class is cache-safe, * i.e. whether it is loaded by the same class loader as the * CachedIntrospectionResults class or a parent of it. * <p>Many thanks to Guillaume Poirier for pointing out the * garbage collection issues and for suggesting this solution. * @param clazz the class to analyze/*from w ww .j a va 2 s . com*/ * @return whether the given class is thread-safe */ private static boolean isCacheSafe(Class<?> clazz) { ClassLoader cur = CachedIntrospectionResults.class.getClassLoader(); ClassLoader target = clazz.getClassLoader(); if (target == null || cur == target) { return true; } while (cur != null) { cur = cur.getParent(); if (cur == target) { return true; } } return false; }
From source file:org.openmrs.logic.CompilingClassLoader.java
private List<File> getCompilerClasspath() { List<File> files = new ArrayList<File>(); Collection<ModuleClassLoader> moduleClassLoaders = ModuleFactory.getModuleClassLoaders(); //check module dependencies for (ModuleClassLoader moduleClassLoader : moduleClassLoaders) { URL[] urls = moduleClassLoader.getURLs(); for (URL url : urls) files.add(new File(url.getFile())); }/*from w w w . ja va 2s . c om*/ // check current class loader and all its parents ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); while (classLoader != null) { if (classLoader instanceof URLClassLoader) { URLClassLoader urlClassLoader = (URLClassLoader) classLoader; URL[] urls = urlClassLoader.getURLs(); for (URL url : urls) files.add(new File(url.getFile())); } classLoader = classLoader.getParent(); } return files; }
From source file:org.openmrs.util.OpenmrsClassLoader.java
public static void onShutdown() { //Since we are shutting down, stop all threads that reference the openmrs class loader. Set<Thread> threadSet = Thread.getAllStackTraces().keySet(); Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]); for (Thread thread : threadArray) { ClassLoader classLoader = thread.getContextClassLoader(); //Threads like Finalizer, Reference Handler, etc have null class loader reference. if (classLoader == null) { continue; }// w ww . jav a2s . c o m if (classLoader instanceof OpenmrsClassLoader) { try { //Set to WebappClassLoader just in case stopping fails. thread.setContextClassLoader(classLoader.getParent()); //Stopping the current thread will halt all current cleanup. //So do not ever ever even attempt stopping it. :) if (thread == Thread.currentThread()) { continue; } log.info("onShutdown Stopping thread: " + thread.getName()); thread.stop(); } catch (Exception ex) { log.error(ex.getMessage(), ex); } } } clearReferences(); }
From source file:org.openspaces.remoting.SpaceRemotingServiceExporter.java
@Override public void onApplicationEvent(ApplicationEvent applicationEvent) { if (applicationEvent instanceof ContextRefreshedEvent) { Assert.notNull(services, "services property is required"); ClassLoader origClassLoader = Thread.currentThread().getContextClassLoader(); if (origClassLoader instanceof ServiceClassLoader && origClassLoader.getParent() instanceof ServiceClassLoader) { // Since the refreshable context causes the ContextRefreshEvent as well, under its own new class loader Thread.currentThread().setContextClassLoader(origClassLoader.getParent()); }//from w w w.j a v a 2 s. c o m try { // go over the services and create the interface to service lookup int naCounter = 0; for (Object service : services) { if (service instanceof ServiceRef) { String ref = ((ServiceRef) service).getRef(); service = applicationContext.getBean(ref); this.servicesInfo.add(new ServiceInfo(ref, service.getClass().getName(), service)); } else { this.servicesInfo .add(new ServiceInfo("NA" + (++naCounter), service.getClass().getName(), service)); } } methodInvocationLookup = new HashMap<String, Map<RemotingUtils.MethodHash, IMethod>>(); for (ServiceInfo serviceInfo : servicesInfo) { Set<Class> interfaces = ReflectionUtil .getAllInterfacesForClassAsSet(serviceInfo.getService().getClass()); for (Class<?> anInterface : interfaces) { interfaceToService.put(anInterface.getName(), serviceInfo.getService()); methodInvocationLookup.put(anInterface.getName(), RemotingUtils.buildHashToMethodLookupForInterface(anInterface, useFastReflection)); // for backward comp methodInvocationCache.addService(anInterface, serviceInfo.getService(), useFastReflection); } serviceToServiceInfoMap.put(serviceInfo.getService(), serviceInfo); } initialized = true; initializationLatch.countDown(); } finally { Thread.currentThread().setContextClassLoader(origClassLoader); } } }
From source file:org.pepstock.jem.node.tasks.jndi.AbsoluteHashMap.java
/** * This is a singleton. If instance is not null, means that local map is already loaded. * If null and classload is ANT classloader, then uses the proxy to load the instance from parent classloader, * otherwise it creates a new instance.//www . ja v a2s. c om * @return shared HashMap. */ @SuppressWarnings({ "rawtypes", "unchecked" }) static synchronized Map<String, Object> getInstance() { ClassLoader myClassLoader = AbsoluteHashMap.class.getClassLoader(); if (instance == null) { // The root classloader is sun.misc.Launcher package. If we are not in a sun package, // we need to get hold of the instance of ourself from the class in the root classloader. // checks is ANT classloader if (myClassLoader.getClass().getName().startsWith("org.apache.tools.ant.loader.AntClassLoader") || myClassLoader.getClass().getName().startsWith(ReverseURLClassLoader.class.getName())) { try { // So we find our parent classloader ClassLoader parentClassLoader = myClassLoader.getParent(); // And get the other version of our current class Class otherClassInstance = parentClassLoader.loadClass(AbsoluteHashMap.class.getName()); // And call its getInstance method - this gives the correct instance of ourself Method getInstanceMethod = otherClassInstance.getDeclaredMethod("getInstance", new Class[] { String.class }); String internalKey = createKey(); Object otherAbsoluteSingleton = getInstanceMethod.invoke(null, new Object[] { internalKey }); // But, we can't cast it to our own interface directly because classes loaded from // different classloaders implement different versions of an interface. // So instead, we use java.lang.reflect.Proxy to wrap it in an object that // supports our interface, and the proxy will use reflection to pass through all calls // to the object. instance = (Map<String, Object>) Proxy.newProxyInstance(myClassLoader, new Class[] { Map.class }, new DelegateInvocationHandler(otherAbsoluteSingleton)); } catch (SecurityException e) { LogAppl.getInstance().debug(e.getMessage(), e); } catch (IllegalArgumentException e) { LogAppl.getInstance().debug(e.getMessage(), e); } catch (ClassNotFoundException e) { LogAppl.getInstance().debug(e.getMessage(), e); } catch (NoSuchMethodException e) { LogAppl.getInstance().debug(e.getMessage(), e); } catch (IOException e) { LogAppl.getInstance().debug(e.getMessage(), e); } catch (IllegalAccessException e) { LogAppl.getInstance().debug(e.getMessage(), e); } catch (InvocationTargetException e) { LogAppl.getInstance().debug(e.getMessage(), e); } // We're in the root classloader, so the instance we have here is the correct one } else { instance = new AbsoluteHashMap(); } } return instance; }
From source file:org.psikeds.common.threadlocal.ThreadLocalHelper.java
/** * Check whether an Object was loaded by the same ClassLoader (or one of its Childs) * as this Class, i.e. whether Object was created by our Application. * /*from w w w . ja v a 2 s . c om*/ * @param obj * Object to be checked. * @return True if same ClassLoader was used, false else. */ private static boolean loadedByOurClassloaderOrChild(final Object obj) { boolean ret = false; try { LOGGER.trace("--> loadedByOurClassloaderOrChild()"); // no object, nothing to do if (obj == null) { LOGGER.trace("Object is null."); ret = false; return ret; } // object loaded by same classloader as ourself? final ClassLoader ownCL = getOwnClassLoader(); ClassLoader objCL = getClassLoader(obj); while ((ownCL != null) && (objCL != null)) { if (ownCL == objCL) { LOGGER.trace("Object was loaded by our ClassLoader: {}", obj); ret = true; return ret; } objCL = objCL.getParent(); } // is object a collection? then check its contents if (obj instanceof Collection<?>) { final Iterator<?> iter = ((Collection<?>) obj).iterator(); try { while (iter.hasNext()) { final Object entry = iter.next(); if (loadedByOurClassloaderOrChild(entry)) { LOGGER.trace("Object is Collection and contains Entry loaded by our ClassLoader: {}", entry); ret = true; return ret; } } } catch (final ConcurrentModificationException cme) { LOGGER.warn("loadedByOurClassloaderOrChild: Concurrent modification of " + String.valueOf(obj), cme); } } LOGGER.trace("Object was not loaded by our ClassLoader/Application: {}", obj); ret = false; return ret; } finally { LOGGER.trace("<-- loadedByOurClassloaderOrChild(); ret = {}", ret); } }
From source file:org.sakaiproject.kernel.component.core.ComponentClassLoader.java
/** * {@inheritDoc}// w w w . jav a2s . c o m * * @see java.lang.Object#toString() */ @Override public String toString() { String t = spacing.get(); try { String bl = t + " : "; StringBuilder sb = new StringBuilder(); sb.append(DependencyImpl.toString(artifact)).append("(").append(super.toString()).append(")\n"); sb.append(bl).append("Contents :"); for (URL u : getURLs()) { sb.append("\n").append(bl).append(u); } ClassLoader parent = getParent(); Map<ClassLoader, ClassLoader> parents = new LinkedHashMap<ClassLoader, ClassLoader>(); while (parent != null && !parents.containsKey(parent)) { parents.put(parent, parent); parent = parent.getParent(); } if (t.equals("1")) { sb.append("\n").append(bl).append("Classloaders :"); int i = 1; for (ClassLoader p : parents.keySet()) { String l = t + "." + i; spacing.set(l); sb.append("\n").append(l).append(" :").append(p); i++; } } return sb.toString(); } finally { spacing.set(t); } }
From source file:org.sakaiproject.kernel.loader.server.jetty.SakaiWebAppContext.java
@Override @SuppressWarnings(value = "DC_DOUBLECHECK", justification = "Double checking in this instance avoids excessive use of the sunchronized lock as the variable is immutable once set") public ClassLoader getClassLoader() { if (webappClassLoader == null) { synchronized (lock) { if (webappClassLoader == null) { try { CommonObjectManager com = new CommonObjectManager("sharedclassloader"); final ClassLoader cl = com.getManagedObject(); parentClassloader = AccessController .doPrivileged(new PrivilegedAction<SwitchedClassLoader>() { public SwitchedClassLoader run() { return new SwitchedClassLoader(new URL[] {}, cl, containerClassLoader); }// www.j a va2s . co m }); LOG.info("Got Classloader fromm JMX as " + parentClassloader + "(" + parentClassloader.getClass() + ")"); if (debug) { LOG.debug("Thread Context class loader is: " + parentClassloader); ClassLoader loader = parentClassloader.getParent(); while (loader != null) { LOG.debug("Parent class loader is: " + loader); loader = loader.getParent(); } } webappClassLoader = AccessController .doPrivileged(new PrivilegedAction<WebAppClassLoader>() { public WebAppClassLoader run() { try { return new WebAppClassLoader(parentClassloader, SakaiWebAppContext.this); } catch (IOException e) { throw new AccessControlException( "Unable to start classloader cause:" + e.getMessage()); } } }); super.setClassLoader(webappClassLoader); } catch (CommonObjectConfigurationException e) { LOG.error(e); } } } } return webappClassLoader; }
From source file:org.shelloid.netverif.test.suite.VerificationTestCase.java
private boolean fromSuite() { final String cl = this.getClass().getClassLoader().getClass().getName(); ClassLoader loader = this.getClass().getClassLoader(); int i = 0;/*from w w w .j a v a 2 s . c om*/ while (loader != null && i < 10) { System.out.println("Loader: " + loader.getClass().getName()); loader = loader.getParent(); } return cl.contains("ClassTransformerClassLoader"); }
From source file:org.shelloid.netverif.TestRunnable.java
public TestRunnable() { ClassLoader loader = this.getClass().getClassLoader(); int i = 0;/*from w w w. ja v a 2 s . com*/ while (loader != null && i < 10) { System.out.println("Loader: " + loader.getClass().getName()); loader = loader.getParent(); } }