Example usage for java.lang ClassLoader getParent

List of usage examples for java.lang ClassLoader getParent

Introduction

In this page you can find the example usage for java.lang ClassLoader getParent.

Prototype

@CallerSensitive
public final ClassLoader getParent() 

Source Link

Document

Returns the parent class loader for delegation.

Usage

From source file:com.adito.server.DefaultAditoServerFactory.java

public URL[] getContextLoaderClassPath() {
    List<URL> urlList = new ArrayList<URL>();
    ClassLoader webappContextClassLoader = webappContext.getClassLoader();
    while (webappContextClassLoader != null) {
        if (webappContextClassLoader instanceof URLClassLoader) {
            urlList.addAll(Arrays.asList(((URLClassLoader) webappContextClassLoader).getURLs()));
        }//from   www  .ja  va  2  s .c o  m
        webappContextClassLoader = webappContextClassLoader.getParent();
    }
    URL[] urls = urlList.toArray(new URL[urlList.size()]);
    return urls;
}

From source file:com.samples.platform.service.common.GetServiceStatusOperation.java

/**
 * @param message//ww w. j a  v  a  2 s  .  co m
 *            the {@link JAXBElement} containing a
 *            {@link GetServiceStatusRequestType}.
 * @return the {@link JAXBElement} with a
 *         {@link GetServiceStatusResponseType}.
 */
@InsightEndPoint
@ServiceActivator
public final JAXBElement<GetServiceStatusResponseType> getServiceStatus(
        final JAXBElement<GetServiceStatusRequestType> message) {
    this.logger.debug("+getServiceStatus");
    GetServiceStatusResponseType response = this.of.createGetServiceStatusResponseType();
    try {
        PropertyType p;
        ClassLoader cl;
        URL[] urls;
        ClassLoader sysCl = ClassLoader.getSystemClassLoader();

        response.setStatus("Service is available");

        /* System properties */
        p = new PropertyType();
        p.setName("System Properties");
        response.getDetails().add(p);
        TreeSet<String> propertyNames = new TreeSet<String>();
        propertyNames.addAll(System.getProperties().stringPropertyNames());
        for (String propertyName : propertyNames) {
            p.getValue().add(new StringBuffer(64).append(propertyName).append("=")
                    .append(System.getProperty(propertyName)).toString());
        }

        /* Application properties. */
        p = new PropertyType();
        p.setName("Application loaded properties");
        response.getDetails().add(p);
        propertyNames.clear();
        propertyNames.addAll(this.properties.stringPropertyNames());
        for (String propertyName : propertyNames) {
            p.getValue().add(new StringBuffer(64).append(propertyName).append("=")
                    .append(this.properties.getProperty(propertyName)).toString());
        }

        /* Current lass loader */
        cl = this.getClass().getClassLoader();
        p = new PropertyType();
        p.setName("This ClassLoader");
        response.getDetails().add(p);
        p.getValue().add(cl.getClass().getName());
        if (URLClassLoader.class.isInstance(cl)) {
            urls = ((URLClassLoader) cl).getURLs();
            p.getValue().add(new StringBuffer("Url: ").append(urls.length).toString());
            for (URL url : urls) {
                p.getValue().add(url.toString());
            }
        }
        cl = cl.getParent();
        while (cl != sysCl) {
            p = new PropertyType();
            p.setName("Parent Classloader");
            response.getDetails().add(p);
            p.getValue().add(cl.getClass().getName());
            if (URLClassLoader.class.isInstance(cl)) {
                urls = ((URLClassLoader) cl).getURLs();
                p.getValue().add(new StringBuffer("Url: ").append(urls.length).toString());
                for (URL url : urls) {
                    p.getValue().add(url.toString());
                }
            }
            cl = cl.getParent();
        }

        /* System class loader */
        cl = sysCl;
        p = new PropertyType();
        p.setName("SystemClassLoader");
        response.getDetails().add(p);
        p.getValue().add(cl.getClass().getName());
        if (URLClassLoader.class.isInstance(cl)) {
            urls = ((URLClassLoader) cl).getURLs();
            p.getValue().add(new StringBuffer("Url: ").append(urls.length).toString());
            for (URL url : urls) {
                p.getValue().add(url.toString());
            }
        }
    } catch (Throwable e) {
        this.logger.error(e.getMessage(), e);
    } finally {
        this.logger.debug("-getServiceStatus #{}, #f{}", response/* .get() */ != null ? 1 : 0,
                response.getFailure().size());
    }
    return this.of.createGetServiceStatusResponse(response);
}

From source file:com.adito.server.Main.java

public URL[] getContextLoaderClassPath() {
    List<URL> urlList = new ArrayList<URL>();
    ClassLoader webappContextClassLoader = webappContext.getClassLoader();
    while (webappContextClassLoader != null) {
        if (webappContextClassLoader != null && webappContextClassLoader instanceof URLClassLoader) {
            urlList.addAll(Arrays.asList(((URLClassLoader) webappContextClassLoader).getURLs()));
        }//  www. jav  a2 s .c o  m
        webappContextClassLoader = webappContextClassLoader.getParent();
    }
    URL[] urls = (URL[]) urlList.toArray(new URL[urlList.size()]);
    return urls;
}

From source file:net.lightbody.bmp.proxy.jetty.http.HttpContext.java

/** Get the file classpath of the context.
 * This method makes a best effort to return a complete file
 * classpath for the context.//w w w  . j a  v  a2  s . c o m
 * It is obtained by walking the classloader hierarchy and looking for
 * URLClassLoaders.  The system property java.class.path is also checked for
 * file elements not already found in the loader hierarchy.
 * @return Path of files and directories for loading classes.
 * @exception IllegalStateException HttpContext.initClassLoader
 * has not been called.
 */
public String getFileClassPath() throws IllegalStateException {

    ClassLoader loader = getClassLoader();
    if (loader == null)
        throw new IllegalStateException("Context classloader not initialized");

    LinkedList paths = new LinkedList();
    LinkedList loaders = new LinkedList();

    // Walk the loader hierarchy
    while (loader != null) {
        loaders.add(0, loader);
        loader = loader.getParent();
    }

    // Try to handle java2compliant modes
    loader = getClassLoader();
    if (loader instanceof ContextLoader && !((ContextLoader) loader).isJava2Compliant()) {
        loaders.remove(loader);
        loaders.add(0, loader);
    }

    for (int i = 0; i < loaders.size(); i++) {
        loader = (ClassLoader) loaders.get(i);

        if (log.isDebugEnabled())
            log.debug("extract paths from " + loader);
        if (loader instanceof URLClassLoader) {
            URL[] urls = ((URLClassLoader) loader).getURLs();
            for (int j = 0; urls != null && j < urls.length; j++) {
                try {
                    Resource path = Resource.newResource(urls[j]);
                    if (log.isTraceEnabled())
                        log.trace("path " + path);
                    File file = path.getFile();
                    if (file != null)
                        paths.add(file.getAbsolutePath());
                } catch (Exception e) {
                    LogSupport.ignore(log, e);
                }
            }
        }
    }

    // Add the system classpath elements from property.
    String jcp = System.getProperty("java.class.path");
    if (jcp != null) {
        StringTokenizer tok = new StringTokenizer(jcp, File.pathSeparator);
        while (tok.hasMoreTokens()) {
            String path = tok.nextToken();
            if (!paths.contains(path)) {
                if (log.isTraceEnabled())
                    log.trace("PATH=" + path);
                paths.add(path);
            } else if (log.isTraceEnabled())
                log.trace("done=" + path);
        }
    }

    StringBuffer buf = new StringBuffer();
    Iterator iter = paths.iterator();
    while (iter.hasNext()) {
        if (buf.length() > 0)
            buf.append(File.pathSeparator);
        buf.append(iter.next().toString());
    }

    if (log.isDebugEnabled())
        log.debug("fileClassPath=" + buf);
    return buf.toString();
}

From source file:org.allcolor.yahp.converter.CClassLoader.java

/**
 * calculate the loader path/*w  w  w .  ja v  a2s.co  m*/
 * 
 * @return the calculated path
 */
private final String nGetLoaderPath() {
    ClassLoader currentLoader = this;
    final StringBuffer buffer = new StringBuffer();
    buffer.append(this.name);

    while ((currentLoader = currentLoader.getParent()) != null) {
        if (currentLoader.getClass() == CClassLoader.class) {
            buffer.insert(0, "/");
            buffer.insert(0, ((CClassLoader) currentLoader).name);
        } else {
            break;
        }
    }

    return buffer.toString();
}

From source file:org.apache.accumulo.minicluster.impl.MiniAccumuloClusterImpl.java

private String getClasspath() throws IOException {

    try {/*from   w ww .  j  av a 2 s  .c  om*/
        ArrayList<ClassLoader> classloaders = new ArrayList<ClassLoader>();

        ClassLoader cl = this.getClass().getClassLoader();

        while (cl != null) {
            classloaders.add(cl);
            cl = cl.getParent();
        }

        Collections.reverse(classloaders);

        StringBuilder classpathBuilder = new StringBuilder();
        classpathBuilder.append(config.getConfDir().getAbsolutePath());

        if (config.getHadoopConfDir() != null)
            classpathBuilder.append(File.pathSeparator).append(config.getHadoopConfDir().getAbsolutePath());

        if (config.getClasspathItems() == null) {

            // 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 if (classLoader instanceof VFSClassLoader) {

                    VFSClassLoader vcl = (VFSClassLoader) classLoader;
                    for (FileObject f : vcl.getFileObjects()) {
                        append(classpathBuilder, f.getURL());
                    }
                } else {
                    throw new IllegalArgumentException(
                            "Unknown classloader type : " + classLoader.getClass().getName());
                }
            }
        } else {
            for (String s : config.getClasspathItems())
                classpathBuilder.append(File.pathSeparator).append(s);
        }

        return classpathBuilder.toString();

    } catch (URISyntaxException e) {
        throw new IOException(e);
    }
}

From source file:org.apache.accumulo.minicluster.MiniAccumuloCluster.java

private String getClasspath() throws IOException {

    try {//w  w  w  .  jav a 2s  .c  o  m
        ArrayList<ClassLoader> classloaders = new ArrayList<ClassLoader>();

        ClassLoader cl = this.getClass().getClassLoader();

        while (cl != null) {
            classloaders.add(cl);
            cl = cl.getParent();
        }

        Collections.reverse(classloaders);

        StringBuilder classpathBuilder = new StringBuilder();
        classpathBuilder.append(config.getConfDir().getAbsolutePath());

        if (config.getClasspathItems() == null) {

            // 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) {

                    URLClassLoader ucl = (URLClassLoader) classLoader;

                    for (URL u : ucl.getURLs()) {
                        append(classpathBuilder, u);
                    }

                } else if (classLoader instanceof VFSClassLoader) {

                    VFSClassLoader vcl = (VFSClassLoader) classLoader;
                    for (FileObject f : vcl.getFileObjects()) {
                        append(classpathBuilder, f.getURL());
                    }
                } else {
                    throw new IllegalArgumentException(
                            "Unknown classloader type : " + classLoader.getClass().getName());
                }
            }
        } else {
            for (String s : config.getClasspathItems())
                classpathBuilder.append(File.pathSeparator).append(s);
        }

        return classpathBuilder.toString();

    } catch (URISyntaxException e) {
        throw new IOException(e);
    }
}

From source file:org.apache.accumulo.start.classloader.vfs.AccumuloVFSClassLoader.java

public static void printClassPath(Printer out) {
    try {//from w w  w .  j av a 2  s .  c  om
        ClassLoader cl = getClassLoader();
        ArrayList<ClassLoader> classloaders = new ArrayList<ClassLoader>();

        while (cl != null) {
            classloaders.add(cl);
            cl = cl.getParent();
        }

        Collections.reverse(classloaders);

        int level = 0;

        for (ClassLoader classLoader : classloaders) {
            if (level > 0)
                out.print("");
            level++;

            String classLoaderDescription;

            switch (level) {
            case 1:
                classLoaderDescription = level + ": Java System Classloader (loads Java system resources)";
                break;
            case 2:
                classLoaderDescription = level
                        + ": Java Classloader (loads everything defined by java classpath)";
                break;
            case 3:
                classLoaderDescription = level
                        + ": Accumulo Classloader (loads everything defined by general.classpaths)";
                break;
            case 4:
                classLoaderDescription = level
                        + ": Accumulo Dynamic Classloader (loads everything defined by general.dynamic.classpaths)";
                break;
            default:
                classLoaderDescription = level
                        + ": Mystery Classloader (someone probably added a classloader and didn't update the switch statement in "
                        + AccumuloVFSClassLoader.class.getName() + ")";
                break;
            }

            if (classLoader instanceof URLClassLoader) {
                // If VFS class loader enabled, but no contexts defined.
                out.print("Level " + classLoaderDescription + " URL classpath items are:");

                for (URL u : ((URLClassLoader) classLoader).getURLs()) {
                    out.print("\t" + u.toExternalForm());
                }

            } else if (classLoader instanceof VFSClassLoader) {
                out.print("Level " + classLoaderDescription + " VFS classpaths items are:");
                VFSClassLoader vcl = (VFSClassLoader) classLoader;
                for (FileObject f : vcl.getFileObjects()) {
                    out.print("\t" + f.getURL().toExternalForm());
                }
            } else {
                out.print("Unknown classloader configuration " + classLoader.getClass());
            }
        }

    } catch (Throwable t) {
        throw new RuntimeException(t);
    }
}

From source file:org.apache.accumulo.start.classloader.vfs.AccumuloVFSClassLoaderTest.java

@Test
public void testDefaultConfig() throws Exception {

    Whitebox.setInternalState(AccumuloVFSClassLoader.class, "loader", (AccumuloReloadingVFSClassLoader) null);

    File conf = folder1.newFile("accumulo-site.xml");
    FileWriter out = new FileWriter(conf);
    out.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    out.append("<configuration>\n");
    out.append("<property>\n");
    out.append("<name>general.classpaths</name>\n");
    out.append("<value></value>\n");
    out.append("</property>\n");
    out.append("<property>\n");
    out.append("<name>general.vfs.classpaths</name>\n");
    out.append("<value></value>\n");
    out.append("</property>\n");
    out.append("</configuration>\n");
    out.close();/*from ww w  . j  a  v  a  2  s .c  om*/

    Whitebox.setInternalState(AccumuloClassLoader.class, "SITE_CONF", conf.toURI().toURL().toString());
    Whitebox.setInternalState(AccumuloVFSClassLoader.class, "lock", new Object());
    ClassLoader acl = AccumuloVFSClassLoader.getClassLoader();
    Assert.assertTrue((acl instanceof VFSClassLoader));
    Assert.assertTrue((acl.getParent() instanceof URLClassLoader));
}

From source file:org.apache.accumulo.start.classloader.vfs.AccumuloVFSClassLoaderTest.java

@Test
public void testDefaultContextConfigured() throws Exception {

    Whitebox.setInternalState(AccumuloVFSClassLoader.class, "loader", (AccumuloReloadingVFSClassLoader) null);

    // Copy jar file to TEST_DIR
    FileUtils.copyURLToFile(this.getClass().getResource("/HelloWorld.jar"), folder1.newFile("HelloWorld.jar"));

    File conf = folder1.newFile("accumulo-site.xml");
    FileWriter out = new FileWriter(conf);
    out.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
    out.append("<configuration>\n");
    out.append("<property>\n");
    out.append("<name>general.classpaths</name>\n");
    out.append("<value></value>\n");
    out.append("</property>\n");
    out.append("<property>\n");
    out.append("<name>general.vfs.classpaths</name>\n");
    out.append("<value>" + new File(folder1.getRoot(), "HelloWorld.jar").toURI() + "</value>\n");
    out.append("</property>\n");
    out.append("</configuration>\n");
    out.close();//from w w  w .ja v a  2 s  . c  o m

    Whitebox.setInternalState(AccumuloClassLoader.class, "SITE_CONF", conf.toURI().toURL().toString());
    Whitebox.setInternalState(AccumuloVFSClassLoader.class, "lock", new Object());
    ClassLoader acl = AccumuloVFSClassLoader.getClassLoader();
    Assert.assertTrue((acl instanceof VFSClassLoader));
    Assert.assertTrue((acl.getParent() instanceof VFSClassLoader));
    VFSClassLoader arvcl = (VFSClassLoader) acl.getParent();
    Assert.assertEquals(1, arvcl.getFileObjects().length);
    // We can't be sure what the authority/host will be due to FQDN mappings, so just check the path
    Assert.assertTrue(arvcl.getFileObjects()[0].getURL().toString().contains("HelloWorld.jar"));
    Class<?> clazz1 = arvcl.loadClass("test.HelloWorld");
    Object o1 = clazz1.newInstance();
    Assert.assertEquals("Hello World!", o1.toString());
    Whitebox.setInternalState(AccumuloVFSClassLoader.class, "loader", (AccumuloReloadingVFSClassLoader) null);
}