List of usage examples for java.lang ClassLoader getParent
@CallerSensitive public final ClassLoader getParent()
From source file:org.apache.naming.modules.java.ContextBindings.java
/** * Retrieves the naming context bound to a class loader. *///from w w w . j a va 2 s . c o m public static Context getClassLoader() throws NamingException { ClassLoader cl = Thread.currentThread().getContextClassLoader(); Context context = null; do { context = (Context) clBindings.get(cl); log.info("Context=getClassLoader: " + context + " " + cl); if (context != null) { return context; } } while ((cl = cl.getParent()) != null); throw new NamingException(sm.getString("contextBindings.noContextBoundToCL")); }
From source file:org.apache.naming.modules.java.ContextBindings.java
/** * Retrieves the naming context name bound to a class loader. *//* w w w . java 2s .c om*/ static Object getClassLoaderName() throws NamingException { ClassLoader cl = Thread.currentThread().getContextClassLoader(); Object name = null; do { name = clNameBindings.get(cl); if (name != null) { return name; } } while ((cl = cl.getParent()) != null); throw new NamingException(sm.getString("contextBindings.noContextBoundToCL")); }
From source file:org.apache.naming.modules.java.ContextBindings.java
/** * Tests if current class loader is bound to a context. *///from w w w . ja v a2 s . c o m public static boolean isClassLoaderBound() { ClassLoader cl = Thread.currentThread().getContextClassLoader(); do { if (clBindings.containsKey(cl)) { return true; } } while ((cl = cl.getParent()) != null); return false; }
From source file:org.apache.nifi.web.server.JettyServer.java
/** * Loads the WARs in the specified NAR working directories. A WAR file must * have a ".war" extension./*from w w w . j a va 2s. c om*/ * * @param narWorkingDirectories dirs */ private void loadWars(final Set<File> narWorkingDirectories) { // load WARs Map<File, File> warToNarWorkingDirectoryLookup = findWars(narWorkingDirectories); // locate each war being deployed File webUiWar = null; File webApiWar = null; File webErrorWar = null; File webDocsWar = null; File webContentViewerWar = null; List<File> otherWars = new ArrayList<>(); for (File war : warToNarWorkingDirectoryLookup.keySet()) { if (war.getName().toLowerCase().startsWith("nifi-web-api")) { webApiWar = war; } else if (war.getName().toLowerCase().startsWith("nifi-web-error")) { webErrorWar = war; } else if (war.getName().toLowerCase().startsWith("nifi-web-docs")) { webDocsWar = war; } else if (war.getName().toLowerCase().startsWith("nifi-web-content-viewer")) { webContentViewerWar = war; } else if (war.getName().toLowerCase().startsWith("nifi-web")) { webUiWar = war; } else { otherWars.add(war); } } // ensure the required wars were found if (webUiWar == null) { throw new RuntimeException("Unable to load nifi-web WAR"); } else if (webApiWar == null) { throw new RuntimeException("Unable to load nifi-web-api WAR"); } else if (webDocsWar == null) { throw new RuntimeException("Unable to load nifi-web-docs WAR"); } else if (webErrorWar == null) { throw new RuntimeException("Unable to load nifi-web-error WAR"); } else if (webContentViewerWar == null) { throw new RuntimeException("Unable to load nifi-web-content-viewer WAR"); } // handlers for each war and init params for the web api final HandlerCollection handlers = new HandlerCollection(); final Map<String, String> mimeMappings = new HashMap<>(); final ClassLoader frameworkClassLoader = getClass().getClassLoader(); final ClassLoader jettyClassLoader = frameworkClassLoader.getParent(); // deploy the other wars if (CollectionUtils.isNotEmpty(otherWars)) { // hold onto to the web contexts for all ui extensions componentUiExtensionWebContexts = new ArrayList<>(); contentViewerWebContexts = new ArrayList<>(); // ui extension organized by component type final Map<String, List<UiExtension>> componentUiExtensionsByType = new HashMap<>(); for (File war : otherWars) { // identify all known extension types in the war final Map<UiExtensionType, List<String>> uiExtensionInWar = new HashMap<>(); identifyUiExtensionsForComponents(uiExtensionInWar, war); // only include wars that are for custom processor ui's if (!uiExtensionInWar.isEmpty()) { // get the context path String warName = StringUtils.substringBeforeLast(war.getName(), "."); String warContextPath = String.format("/%s", warName); // attempt to locate the nar class loader for this war ClassLoader narClassLoaderForWar = NarClassLoaders.getInstance() .getExtensionClassLoader(warToNarWorkingDirectoryLookup.get(war)); // this should never be null if (narClassLoaderForWar == null) { narClassLoaderForWar = jettyClassLoader; } // create the extension web app context WebAppContext extensionUiContext = loadWar(war, warContextPath, narClassLoaderForWar); // create the ui extensions for (final Map.Entry<UiExtensionType, List<String>> entry : uiExtensionInWar.entrySet()) { final UiExtensionType extensionType = entry.getKey(); final List<String> types = entry.getValue(); if (UiExtensionType.ContentViewer.equals(extensionType)) { // consider each content type identified for (final String contentType : types) { // map the content type to the context path mimeMappings.put(contentType, warContextPath); } // this ui extension provides a content viewer contentViewerWebContexts.add(extensionUiContext); } else { // consider each component type identified for (final String componentType : types) { logger.info(String.format("Loading UI extension [%s, %s] for %s", extensionType, warContextPath, types)); // record the extension definition final UiExtension uiExtension = new UiExtension(extensionType, warContextPath); // create if this is the first extension for this component type List<UiExtension> componentUiExtensionsForType = componentUiExtensionsByType .get(componentType); if (componentUiExtensionsForType == null) { componentUiExtensionsForType = new ArrayList<>(); componentUiExtensionsByType.put(componentType, componentUiExtensionsForType); } // record this extension componentUiExtensionsForType.add(uiExtension); } // this ui extension provides a component custom ui componentUiExtensionWebContexts.add(extensionUiContext); } } // include custom ui web context in the handlers handlers.addHandler(extensionUiContext); } } // record all ui extensions to give to the web api componentUiExtensions = new UiExtensionMapping(componentUiExtensionsByType); } else { componentUiExtensions = new UiExtensionMapping(Collections.EMPTY_MAP); } // load the web ui app handlers.addHandler(loadWar(webUiWar, "/nifi", frameworkClassLoader)); // load the web api app webApiContext = loadWar(webApiWar, "/nifi-api", frameworkClassLoader); handlers.addHandler(webApiContext); // load the content viewer app webContentViewerContext = loadWar(webContentViewerWar, "/nifi-content-viewer", frameworkClassLoader); webContentViewerContext.getInitParams().putAll(mimeMappings); handlers.addHandler(webContentViewerContext); // create a web app for the docs final String docsContextPath = "/nifi-docs"; // load the documentation war webDocsContext = loadWar(webDocsWar, docsContextPath, frameworkClassLoader); // overlay the actual documentation final ContextHandlerCollection documentationHandlers = new ContextHandlerCollection(); documentationHandlers.addHandler(createDocsWebApp(docsContextPath)); documentationHandlers.addHandler(webDocsContext); handlers.addHandler(documentationHandlers); // load the web error app handlers.addHandler(loadWar(webErrorWar, "/", frameworkClassLoader)); // deploy the web apps server.setHandler(gzip(handlers)); }
From source file:org.apache.solr.core.TestCoreContainer.java
@Test public void testClassLoaderHierarchy() throws Exception { final CoreContainer cc = init(CONFIGSETS_SOLR_XML); try {// w ww . ja va 2 s .c o m ClassLoader sharedLoader = cc.loader.getClassLoader(); ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); assertSame(contextLoader, sharedLoader.getParent()); SolrCore core1 = cc.create("core1", ImmutableMap.of("configSet", "minimal")); ClassLoader coreLoader = core1.getResourceLoader().getClassLoader(); assertSame(sharedLoader, coreLoader.getParent()); } finally { cc.shutdown(); } }
From source file:org.apache.sqoop.accumulo.AccumuloTestCase.java
protected static String getClasspath(File confDir) throws URISyntaxException { // Mostly lifted from MiniAccumuloConfigImpl#getClasspath ArrayList<ClassLoader> classloaders = new ArrayList<ClassLoader>(); ClassLoader cl = AccumuloTestCase.class.getClassLoader(); while (cl != null) { classloaders.add(cl);//w w w .ja v a 2 s. com cl = cl.getParent(); } Collections.reverse(classloaders); StringBuilder classpathBuilder = new StringBuilder(64); classpathBuilder.append(confDir.getAbsolutePath()); // assume 0 is the system classloader and skip it for (int i = 1; i < classloaders.size(); i++) { ClassLoader classLoader = classloaders.get(i); if (classLoader instanceof URLClassLoader) { for (URL u : ((URLClassLoader) classLoader).getURLs()) { append(classpathBuilder, u); } } else { throw new IllegalArgumentException( "Unknown classloader type : " + classLoader.getClass().getName()); } } return classpathBuilder.toString(); }
From source file:org.apache.struts2.convention.PackageBasedActionConfigBuilder.java
private UrlSet buildUrlSet(List<URL> resourceUrls) throws IOException { ClassLoaderInterface classLoaderInterface = getClassLoaderInterface(); UrlSet urlSet = new UrlSet(resourceUrls); urlSet = urlSet.include(new UrlSet(classLoaderInterface, this.fileProtocols)); //excluding the urls found by the parent class loader is desired, but fails in JBoss (all urls are removed) if (excludeParentClassLoader) { //exclude parent of classloaders ClassLoaderInterface parent = classLoaderInterface.getParent(); //if reload is enabled, we need to step up one level, otherwise the UrlSet will be empty //this happens because the parent of the realoding class loader is the web app classloader if (parent != null && isReloadEnabled()) parent = parent.getParent(); if (parent != null) urlSet = urlSet.exclude(parent); try {/*w w w. j a va 2 s . c om*/ // This may fail in some sandboxes, ie GAE ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); urlSet = urlSet.exclude(new ClassLoaderInterfaceDelegate(systemClassLoader.getParent())); } catch (SecurityException e) { if (LOG.isWarnEnabled()) LOG.warn( "Could not get the system classloader due to security constraints, there may be improper urls left to scan"); } } //try to find classes dirs inside war files urlSet = urlSet.includeClassesUrl(classLoaderInterface, new UrlSet.FileProtocolNormalizer() { public URL normalizeToFileProtocol(URL url) { return fileManager.normalizeToFileProtocol(url); } }); urlSet = urlSet.excludeJavaExtDirs(); urlSet = urlSet.excludeJavaEndorsedDirs(); try { urlSet = urlSet.excludeJavaHome(); } catch (NullPointerException e) { // This happens in GAE since the sandbox contains no java.home directory if (LOG.isWarnEnabled()) LOG.warn("Could not exclude JAVA_HOME, is this a sandbox jvm?"); } urlSet = urlSet.excludePaths(System.getProperty("sun.boot.class.path", "")); urlSet = urlSet.exclude(".*/JavaVM.framework/.*"); if (includeJars == null) { urlSet = urlSet.exclude(".*?\\.jar(!/|/)?"); } else { //jar urls regexes were specified List<URL> rawIncludedUrls = urlSet.getUrls(); Set<URL> includeUrls = new HashSet<URL>(); boolean[] patternUsed = new boolean[includeJars.length]; for (URL url : rawIncludedUrls) { if (fileProtocols.contains(url.getProtocol())) { //it is a jar file, make sure it macthes at least a url regex for (int i = 0; i < includeJars.length; i++) { String includeJar = includeJars[i]; if (Pattern.matches(includeJar, url.toExternalForm())) { includeUrls.add(url); patternUsed[i] = true; break; } } } else { //it is not a jar includeUrls.add(url); } } if (LOG.isWarnEnabled()) { for (int i = 0; i < patternUsed.length; i++) { if (!patternUsed[i]) { LOG.warn("The includeJars pattern [#0] did not match any jars in the classpath", includeJars[i]); } } } return new UrlSet(includeUrls); } return urlSet; }
From source file:org.apache.struts2.jasper.compiler.TldLocationsCache.java
private void scanJars() throws Exception { ClassLoader webappLoader = Thread.currentThread().getContextClassLoader(); ClassLoader loader = webappLoader; while (loader != null) { if (loader instanceof URLClassLoader) { URL[] urls = ((URLClassLoader) loader).getURLs(); for (int i = 0; i < urls.length; i++) { URLConnection conn = urls[i].openConnection(); if (conn instanceof JarURLConnection) { if (needScanJar(loader, webappLoader, ((JarURLConnection) conn).getJarFile().getName())) { scanJar((JarURLConnection) conn, true); }//from w w w . jav a 2 s .co m } else { String urlStr = urls[i].toString(); if (urlStr.startsWith(FILE_PROTOCOL) && urlStr.endsWith(JAR_FILE_SUFFIX) && needScanJar(loader, webappLoader, urlStr)) { URL jarURL = new URL("jar:" + urlStr + "!/"); scanJar((JarURLConnection) jarURL.openConnection(), true); } } } } loader = loader.getParent(); } }
From source file:org.apdplat.platform.struts.APDPlatPackageBasedActionConfigBuilder.java
private UrlSet buildUrlSet(List<URL> resourceUrls) throws IOException { ClassLoaderInterface classLoaderInterface = getClassLoaderInterface(); UrlSet urlSet = new UrlSet(resourceUrls); urlSet = urlSet.include(new UrlSet(classLoaderInterface, this.fileProtocols)); //excluding the urls found by the parent class loader is desired, but fails in JBoss (all urls are removed) if (excludeParentClassLoader) { //exclude parent of classloaders ClassLoaderInterface parent = classLoaderInterface.getParent(); //if reload is enabled, we need to step up one level, otherwise the UrlSet will be empty //this happens because the parent of the realoding class loader is the web app classloader if (parent != null && isReloadEnabled()) parent = parent.getParent(); if (parent != null) urlSet = urlSet.exclude(parent); try {//from w w w. j a va 2s . c o m // This may fail in some sandboxes, ie GAE ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader(); urlSet = urlSet.exclude(new ClassLoaderInterfaceDelegate(systemClassLoader.getParent())); } catch (SecurityException e) { if (LOG.isWarnEnabled()) LOG.warn( "Could not get the system classloader due to security constraints, there may be improper urls left to scan"); } } //try to find classes dirs inside war files urlSet = urlSet.includeClassesUrl(classLoaderInterface, new UrlSet.FileProtocolNormalizer() { public URL normalizeToFileProtocol(URL url) { return fileManager.normalizeToFileProtocol(url); } }); urlSet = urlSet.excludeJavaExtDirs(); urlSet = urlSet.excludeJavaEndorsedDirs(); try { urlSet = urlSet.excludeJavaHome(); } catch (NullPointerException e) { // This happens in GAE since the sandbox contains no java.home directory if (LOG.isWarnEnabled()) LOG.warn("Could not exclude JAVA_HOME, is this a sandbox jvm?"); } urlSet = urlSet.excludePaths(System.getProperty("sun.boot.class.path", "")); urlSet = urlSet.exclude(".*/JavaVM.framework/.*"); if (includeJars == null) { urlSet = urlSet.exclude(".*?\\.jar(!/|/)?"); } else { //jar urls regexes were specified List<URL> rawIncludedUrls = urlSet.getUrls(); Set<URL> includeUrls = new HashSet<URL>(); boolean[] patternUsed = new boolean[includeJars.length]; for (URL url : rawIncludedUrls) { if (fileProtocols.contains(url.getProtocol())) { //it is a jar file, make sure it macthes at least a url regex for (int i = 0; i < includeJars.length; i++) { String includeJar = includeJars[i]; if (url.toExternalForm().contains(includeJar)) { includeUrls.add(url); patternUsed[i] = true; break; } } } else { //it is not a jar includeUrls.add(url); } } if (LOG.isWarnEnabled()) { for (int i = 0; i < patternUsed.length; i++) { if (!patternUsed[i]) { LOG.warn("The includeJars pattern [#0] did not match any jars in the classpath", includeJars[i]); } } } return new UrlSet(includeUrls); } return urlSet; }
From source file:org.beangle.model.persist.hibernate.internal.ClassUtils.java
/** * Special static method used during the class initialization. * //from w w w .j ava2s . co m * @param classLoader * non OSGi class loader */ private static void addNonOsgiClassLoader(ClassLoader classLoader, List<ClassLoader> list, Map<ClassLoader, Boolean> map) { while (classLoader != null) { synchronized (list) { if (!map.containsKey(classLoader)) { list.add(classLoader); map.put(classLoader, Boolean.TRUE); } } classLoader = classLoader.getParent(); } }