List of usage examples for java.lang ClassLoader getParent
@CallerSensitive public final ClassLoader getParent()
From source file:com.taobao.tddl.common.config.impl.DefaultConfigDataHandlerFactory.java
private static void findSpecifiedConfigHandlerClass() { ClassLoader currentCL = getBaseClassLoader(); InputStream resource;/*from ww w . j a va2 s. co m*/ for (;;) { if (currentCL != null) { resource = currentCL.getResourceAsStream(propertyFile); } else { resource = ClassLoader.getSystemResourceAsStream(propertyFile); break; } if (null != resource) { break; } else { currentCL = currentCL.getParent(); } } if (null != resource) { prop = new Properties(); try { prop.load(resource); handlerClassName = prop.getProperty(HANDLER_CLASS); if (null == handlerClassName || "".equals(handlerClassName)) { handlerClassName = DEFAULT_HANDLER_CLASS; } } catch (IOException e) { log.error("properties can not load " + propertyFile); } } else { handlerClassName = DEFAULT_HANDLER_CLASS; } }
From source file:MailHandlerDemo.java
/** * Gets the class loader list./*from w w w . ja v a2s . c o m*/ * * @param cl the class loader or null. * @return the class loader list. */ private static String toString(ClassLoader cl) { StringBuilder buf = new StringBuilder(); buf.append(cl); while (cl != null) { cl = cl.getParent(); buf.append("<-").append(cl); } return buf.toString(); }
From source file:com.gigaspaces.internal.utils.ClassLoaderCleaner.java
/** * Clear the {@link ResourceBundle} cache of any bundles loaded by this class loader or any * class loader where this loader is a parent class loader. * * The ResourceBundle is using WeakReferences so it shouldn't be pinning the class loader in * memory. However, it is. Therefore clear ou the references. *///from w w w .j a v a2 s . c o m private static void clearReferencesResourceBundles(ClassLoader classLoader) { // Get a reference to the cache try { Field cacheListField = ResourceBundle.class.getDeclaredField("cacheList"); cacheListField.setAccessible(true); // Java 6 uses ConcurrentMap // Java 5 uses SoftCache extends Abstract Map // So use Map and it *should* work with both Map<?, ?> cacheList = (Map<?, ?>) cacheListField.get(null); // Get the keys (loader references are in the key) Set<?> keys = cacheList.keySet(); Field loaderRefField = null; // Iterate over the keys looking at the loader instances Iterator<?> keysIter = keys.iterator(); int countRemoved = 0; while (keysIter.hasNext()) { Object key = keysIter.next(); if (loaderRefField == null) { loaderRefField = key.getClass().getDeclaredField("loaderRef"); loaderRefField.setAccessible(true); } WeakReference<?> loaderRef = (WeakReference<?>) loaderRefField.get(key); ClassLoader loader = (ClassLoader) loaderRef.get(); while (loader != null && loader != classLoader) { loader = loader.getParent(); } if (loader != null) { keysIter.remove(); countRemoved++; } } if (countRemoved > 0 && logger.isLoggable(Level.FINE)) { logger.fine("Removed [" + countRemoved + "] ResourceBundle references from the cache"); } } catch (Exception e) { logger.log(Level.WARNING, "Failed to clear ResourceBundle references", e); } }
From source file:io.fabric8.maven.core.util.ClassUtil.java
public static <T> Class<T> classForName(String className, List<ClassLoader> additionalClassLoaders) { ClassLoader[] classLoaders = mergeClassLoaders(additionalClassLoaders); Set<ClassLoader> tried = new HashSet<>(); for (ClassLoader loader : classLoaders) { // Go up the classloader stack to eventually find the server class. Sometimes the WebAppClassLoader // hide the server classes loaded by the parent class loader. while (loader != null) { try { if (!tried.contains(loader)) { return (Class<T>) Class.forName(className, true, loader); }/* w ww . j a va 2 s . c om*/ } catch (ClassNotFoundException ignored) { } tried.add(loader); loader = loader.getParent(); } } return null; }
From source file:com.google.gwt.dev.resource.impl.ResourceOracleImpl.java
private static void addAllClassPathEntries(TreeLogger logger, ClassLoader classLoader, List<ClassPathEntry> classPath) { // URL is expensive in collections, so we use URI instead // See:/* ww w .j a v a 2s . c o m*/ // http://michaelscharf.blogspot.com/2006/11/javaneturlequals-and-hashcode-make.html Set<URI> seenEntries = new HashSet<URI>(); for (; classLoader != null; classLoader = classLoader.getParent()) { if (classLoader instanceof URLClassLoader) { URLClassLoader urlClassLoader = (URLClassLoader) classLoader; URL[] urls = urlClassLoader.getURLs(); for (URL url : urls) { URI uri; try { uri = url.toURI(); } catch (URISyntaxException e) { logger.log(TreeLogger.WARN, "Error processing classpath URL '" + url + "'", e); continue; } if (seenEntries.contains(uri)) { continue; } seenEntries.add(uri); Throwable caught; try { ClassPathEntry entry = createEntryForUrl(logger, url); if (entry != null) { classPath.add(entry); } continue; } catch (AccessControlException e) { if (logger.isLoggable(TreeLogger.DEBUG)) { logger.log(TreeLogger.DEBUG, "Skipping URL due to access restrictions: " + url); } continue; } catch (URISyntaxException e) { caught = e; } catch (IOException e) { caught = e; } logger.log(TreeLogger.WARN, "Error processing classpath URL '" + url + "'", caught); } } } }
From source file:net.sf.jasperreports.engine.util.JRLoader.java
protected static void collectResources(String resourceName, ClassLoader classLoader, Map<URL, ClassLoaderResource> resources) { if (classLoader == null) { return;/*from w w w. j a v a2 s. c o m*/ } try { // creating a list of parent classloaders, with the highest in the // hierarchy first LinkedList<ClassLoader> classloaders = new LinkedList<ClassLoader>(); ClassLoader ancestorLoader = classLoader; while (ancestorLoader != null) { classloaders.addFirst(ancestorLoader); try { ancestorLoader = ancestorLoader.getParent(); } catch (SecurityException e) { // if we're not allowed to get the parent, stop here. // resources will be listed with the first classloader that // we're allowed to access. // one case when this happens is applets. // FIXME revisit logging on SecurityException for applet ancestorLoader = null; } } for (ClassLoader ancestor : classloaders) { Enumeration<URL> urls = ancestor.getResources(resourceName); if (urls != null) // class loaders should never return null on getResources, according to specs, but we've seen cases, so we protect our code here { while (urls.hasMoreElements()) { URL url = urls.nextElement(); // if this is the first time we see this resource, add it // with the current classloader. // this way a resource will be added with the most first // ancestor classloader that has it. if (!resources.containsKey(url)) { if (log.isDebugEnabled()) { log.debug("Found resource " + resourceName + " at " + url + " in classloader " + ancestor); } ClassLoaderResource resource = new ClassLoaderResource(url, ancestor); resources.put(url, resource); } } } } } catch (IOException e) { throw new JRRuntimeException(e); } }
From source file:azkaban.common.utils.Utils.java
public static List<String> getClassLoaderDescriptions(ClassLoader loader) { List<String> values = new ArrayList<String>(); while (loader != null) { if (loader instanceof URLClassLoader) { URLClassLoader urlLoader = (URLClassLoader) loader; for (URL url : urlLoader.getURLs()) values.add(url.toString()); } else {// w w w. j a v a2 s .com values.add(loader.getClass().getName()); } loader = loader.getParent(); } return values; }
From source file:com.seer.datacruncher.eventtrigger.DynamicClassLoader.java
private static String extractClasspath(ClassLoader classLoader) { StringBuffer classLoaderBuf = new StringBuffer(); try {//w w w. ja va 2s . c om while (classLoader != null) { if (classLoader instanceof URLClassLoader) { URL urls[] = ((URLClassLoader) classLoader).getURLs(); for (int i = 0; i < urls.length; i++) { if (classLoaderBuf.length() > 0) { classLoaderBuf.append(File.pathSeparatorChar); } classLoaderBuf.append(URLDecoder.decode(urls[i].getFile(), "UTF-8").toString()); } } classLoader = classLoader.getParent(); } } catch (UnsupportedEncodingException e) { logger.error("Exception:" + e.getMessage(), e); } return classLoaderBuf.toString(); }
From source file:de.alpharogroup.lang.ClassExtensions.java
/** * Compares the two given ClassLoader objects and returns true if compare is a derivate of * source.//from www.j a va2 s .com * * @param source * the source * @param compare * the compare * @return true, if compare is a derivate of source. */ public static boolean isDerivate(final ClassLoader source, ClassLoader compare) { if (source == compare) { return true; } if (compare == null) { return false; } if (source == null) { return true; } while (null != compare) { compare = compare.getParent(); if (source == compare) { return true; } } return false; }
From source file:com.jeeframework.util.classes.ClassUtils.java
/** * Check whether the given class is cache-safe in the given context, * i.e. whether it is loaded by the given ClassLoader or a parent of it. * @param clazz the class to analyze// w w w. j a v a 2 s .c o m * @param classLoader the ClassLoader to potentially cache metadata in */ public static boolean isCacheSafe(Class clazz, ClassLoader classLoader) { Assert.notNull(clazz, "Class must not be null"); ClassLoader target = clazz.getClassLoader(); if (target == null) { return false; } ClassLoader cur = classLoader; if (cur == target) { return true; } while (cur != null) { cur = cur.getParent(); if (cur == target) { return true; } } return false; }