List of usage examples for java.lang ClassLoader getParent
@CallerSensitive public final ClassLoader getParent()
From source file:com.hurence.logisland.classloading.PluginLoader.java
/** * Scan for plugins./*from www.j a v a 2 s.c o m*/ */ private static void scanAndRegisterPlugins() { Set<URL> urls = new HashSet<>(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); while (cl != null) { if (cl instanceof URLClassLoader) { urls.addAll(Arrays.asList(((URLClassLoader) cl).getURLs())); cl = cl.getParent(); } } for (URL url : urls) { try { Archive archive = null; try { archive = new JarFileArchive( new File(URLDecoder.decode(url.getFile(), Charset.defaultCharset().name())), url); } catch (Exception e) { //silently swallowing exception. just skip the archive since not an archive } if (archive == null) { continue; } Manifest manifest = archive.getManifest(); if (manifest != null) { String exportedPlugins = manifest.getMainAttributes() .getValue(ManifestAttributes.MODULE_EXPORTS); if (exportedPlugins != null) { String version = StringUtils.defaultIfEmpty( manifest.getMainAttributes().getValue(ManifestAttributes.MODULE_VERSION), "UNKNOWN"); logger.info("Loading components from module {}", archive.getUrl().toExternalForm()); final Archive arc = archive; if (StringUtils.isNotBlank(exportedPlugins)) { Arrays.stream(exportedPlugins.split(",")).map(String::trim).forEach(s -> { if (registry.putIfAbsent(s, PluginClassloaderBuilder.build(arc)) == null) { logger.info("Registered component '{}' version '{}'", s, version); } }); } } } } catch (Exception e) { logger.error("Unable to load components from " + url.toExternalForm(), e); } } }
From source file:com.springframework.beans.CachedIntrospectionResults.java
/** * Check whether the given ClassLoader is underneath the given parent, * that is, whether the parent is within the candidate's hierarchy. * @param candidate the candidate ClassLoader to check * @param parent the parent ClassLoader to check for *///from w w w.j a v a 2 s . c o m private static boolean isUnderneathClassLoader(ClassLoader candidate, ClassLoader parent) { if (candidate == parent) { return true; } if (candidate == null) { return false; } ClassLoader classLoaderToCheck = candidate; while (classLoaderToCheck != null) { classLoaderToCheck = classLoaderToCheck.getParent(); if (classLoaderToCheck == parent) { return true; } } return false; }
From source file:TypeUtil.java
public static void dump(ClassLoader cl) { System.err.println("Dump Loaders:"); while (cl != null) { System.err.println(" loader " + cl); cl = cl.getParent(); }/*from w w w . j a v a 2 s.com*/ }
From source file:Loader.java
/** Load a class. * /*from w ww .j a v a 2s.co m*/ * @param loadClass * @param name * @param checkParents If true, try loading directly from parent classloaders. * @return Class * @throws ClassNotFoundException */ public static Class loadClass(Class loadClass, String name, boolean checkParents) throws ClassNotFoundException { ClassNotFoundException ex = null; Class c = null; ClassLoader loader = Thread.currentThread().getContextClassLoader(); while (c == null && loader != null) { try { c = loader.loadClass(name); } catch (ClassNotFoundException e) { if (ex == null) ex = e; } loader = (c == null && checkParents) ? loader.getParent() : null; } loader = loadClass == null ? null : loadClass.getClassLoader(); while (c == null && loader != null) { try { c = loader.loadClass(name); } catch (ClassNotFoundException e) { if (ex == null) ex = e; } loader = (c == null && checkParents) ? loader.getParent() : null; } if (c == null) { try { c = Class.forName(name); } catch (ClassNotFoundException e) { if (ex == null) ex = e; } } if (c != null) return c; throw ex; }
From source file:com.npower.dm.util.DMUtil.java
/** * Format a string buffer containing the Class, Interfaces, CodeSource, and * ClassLoader information for the given object clazz. * //from w w w .jav a 2 s . c o m * @param clazz * the Class * @param results, * the buffer to write the info to */ public static void displayClassInfo(Class<?> clazz, StringBuffer results) { // Print out some codebase info for the ProbeHome ClassLoader cl = clazz.getClassLoader(); results.append("\n" + clazz.getName() + ".ClassLoader=" + cl); ClassLoader parent = cl; while (parent != null) { results.append("\n.." + parent); URL[] urls = getClassLoaderURLs(parent); int length = urls != null ? urls.length : 0; for (int u = 0; u < length; u++) { results.append("\n...." + urls[u]); } if (parent != null) parent = parent.getParent(); } CodeSource clazzCS = clazz.getProtectionDomain().getCodeSource(); if (clazzCS != null) results.append("\n++++CodeSource: " + clazzCS); else results.append("\n++++Null CodeSource"); results.append("\nImplemented Interfaces:"); Class<?>[] ifaces = clazz.getInterfaces(); for (int i = 0; i < ifaces.length; i++) { results.append("\n++" + ifaces[i]); ClassLoader loader = ifaces[i].getClassLoader(); results.append("\n++++ClassLoader: " + loader); ProtectionDomain pd = ifaces[i].getProtectionDomain(); CodeSource cs = pd.getCodeSource(); if (cs != null) results.append("\n++++CodeSource: " + cs); else results.append("\n++++Null CodeSource"); } }
From source file:Loader.java
public static ResourceBundle getResourceBundle(Class loadClass, String name, boolean checkParents, Locale locale) throws MissingResourceException { MissingResourceException ex = null; ResourceBundle bundle = null; ClassLoader loader = Thread.currentThread().getContextClassLoader(); while (bundle == null && loader != null) { try {//from w w w . j a v a2 s .co m bundle = ResourceBundle.getBundle(name, locale, loader); } catch (MissingResourceException e) { if (ex == null) ex = e; } loader = (bundle == null && checkParents) ? loader.getParent() : null; } loader = loadClass == null ? null : loadClass.getClassLoader(); while (bundle == null && loader != null) { try { bundle = ResourceBundle.getBundle(name, locale, loader); } catch (MissingResourceException e) { if (ex == null) ex = e; } loader = (bundle == null && checkParents) ? loader.getParent() : null; } if (bundle == null) { try { bundle = ResourceBundle.getBundle(name, locale); } catch (MissingResourceException e) { if (ex == null) ex = e; } } if (bundle != null) return bundle; throw ex; }
From source file:Main.java
/** * Format a string buffer containing the Class, Interfaces, CodeSource, and * ClassLoader information for the given object clazz. * //from w w w. ja v a 2 s .c om * @param clazz * the Class * @param results - * the buffer to write the info to */ public static void displayClassInfo(Class clazz, StringBuffer results) { // Print out some codebase info for the clazz ClassLoader cl = clazz.getClassLoader(); results.append("\n"); results.append(clazz.getName()); results.append("("); results.append(Integer.toHexString(clazz.hashCode())); results.append(").ClassLoader="); results.append(cl); ClassLoader parent = cl; while (parent != null) { results.append("\n.."); results.append(parent); URL[] urls = getClassLoaderURLs(parent); int length = urls != null ? urls.length : 0; for (int u = 0; u < length; u++) { results.append("\n...."); results.append(urls[u]); } if (parent != null) parent = parent.getParent(); } CodeSource clazzCS = clazz.getProtectionDomain().getCodeSource(); if (clazzCS != null) { results.append("\n++++CodeSource: "); results.append(clazzCS); } else results.append("\n++++Null CodeSource"); results.append("\nImplemented Interfaces:"); Class[] ifaces = clazz.getInterfaces(); for (int i = 0; i < ifaces.length; i++) { Class iface = ifaces[i]; results.append("\n++"); results.append(iface); results.append("("); results.append(Integer.toHexString(iface.hashCode())); results.append(")"); ClassLoader loader = ifaces[i].getClassLoader(); results.append("\n++++ClassLoader: "); results.append(loader); ProtectionDomain pd = ifaces[i].getProtectionDomain(); CodeSource cs = pd.getCodeSource(); if (cs != null) { results.append("\n++++CodeSource: "); results.append(cs); } else results.append("\n++++Null CodeSource"); } }
From source file:org.usergrid.rest.AbstractRestTest.java
public static void dumpClasspath(ClassLoader loader) { System.out.println("Classloader " + loader + ":"); if (loader instanceof URLClassLoader) { URLClassLoader ucl = (URLClassLoader) loader; System.out.println("\t" + Arrays.toString(ucl.getURLs())); } else {/*from w ww.j a v a 2 s . c o m*/ System.out.println("\t(cannot display components as not a URLClassLoader)"); } if (loader.getParent() != null) { dumpClasspath(loader.getParent()); } }
From source file:ClassLoaderUtils.java
/** * Show the class loader hierarchy for the given class loader. * @param cl class loader to analyze hierarchy for * @param lineBreak line break/* w ww . j av a 2 s . c om*/ * @param tabText text to use to set tabs * @param indent nesting level (from 0) of this loader; used in pretty printing * @return a String showing the class loader hierarchy for this class */ private static String showClassLoaderHierarchy(ClassLoader cl, String lineBreak, String tabText, int indent) { if (cl == null) { ClassLoader ccl = Thread.currentThread().getContextClassLoader(); return "context class loader=[" + ccl + "] hashCode=" + ccl.hashCode(); } StringBuffer buf = new StringBuffer(); for (int i = 0; i < indent; i++) { buf.append(tabText); } buf.append("[").append(cl).append("] hashCode=").append(cl.hashCode()).append(lineBreak); ClassLoader parent = cl.getParent(); return buf.toString() + showClassLoaderHierarchy(parent, lineBreak, tabText, indent + 1); }
From source file:net.sf.ufsc.ServiceLoader.java
/** * Creates a new service loader for the given service type, using the * extension class loader./*from w w w .ja va2s .c o m*/ * * <p> This convenience method simply locates the extension class loader, * call it <tt><i>extClassLoader</i></tt>, and then returns * * <blockquote><pre> * ServiceLoader.load(<i>service</i>, <i>extClassLoader</i>)</pre></blockquote> * * <p> If the extension class loader cannot be found then the system class * loader is used; if there is no system class loader then the bootstrap * class loader is used. * * <p> This method is intended for use when only installed providers are * desired. The resulting service will only find and load providers that * have been installed into the current Java virtual machine; providers on * the application's class path will be ignored. * * @param service * The interface or abstract class representing the service * * @return A new service loader */ public static <S> ServiceLoader<S> loadInstalled(Class<S> service) { ClassLoader cl = ClassLoader.getSystemClassLoader(); ClassLoader prev = null; while (cl != null) { prev = cl; cl = cl.getParent(); } return ServiceLoader.load(service, prev); }