Example usage for java.lang ClassLoader getSystemClassLoader

List of usage examples for java.lang ClassLoader getSystemClassLoader

Introduction

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

Prototype

@CallerSensitive
public static ClassLoader getSystemClassLoader() 

Source Link

Document

Returns the system class loader.

Usage

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

/**
 * @return the {@link ClassLoader} that was used to load given {@link Class}. Always return not
 *         <code>null</code> value, even if {@link Class} was loaded using bootstrap
 *         {@link ClassLoader}./*w  w w  .  j  a  v a 2  s . c om*/
 */
public static ClassLoader getClassLoader(Class<?> clazz) {
    ClassLoader classLoader = clazz.getClassLoader();
    if (classLoader == null) {
        classLoader = ClassLoader.getSystemClassLoader();
    }
    return classLoader;
}

From source file:com.clican.pluto.orm.dynamic.impl.ClassLoaderUtilImpl.java

private void populateJars(Set<String> jars, ClassLoader loader) {
    if (!(loader instanceof URLClassLoader)) {
        if (log.isDebugEnabled()) {
            log.debug("The ClassLoader[" + loader.getClass().getName() + "] is ignored");
        }/*from   w  ww  .  j a v a  2  s  .  c om*/
    } else {
        URLClassLoader urlClassLoader = (URLClassLoader) loader;

        for (URL url : urlClassLoader.getURLs()) {
            jars.add(url.getPath());
        }

    }
    if (loader == ClassLoader.getSystemClassLoader()) {
        return;
    } else {
        populateJars(jars, loader.getParent());
    }
}

From source file:org.jdag.launcher.JDAGLauncher.java

/**
 * CTOR/*  w w w .  j a  v  a  2s .  c  o m*/
 */
@SuppressWarnings("static-access")
public JDAGLauncher(String[] args) {
    try {
        Option classPathFileOption = OptionBuilder.withDescription("<the file containing class path>").hasArg()
                .create(OPTION_JDAG_HOME);

        Options options = new Options();
        options.addOption(classPathFileOption);
        CommandLineParser commandLineParser = new GnuParser();
        CommandLine commandLine = commandLineParser.parse(options, args);

        if (!commandLine.hasOption(OPTION_JDAG_HOME)) {
            LOG.severe("The file containing class path is not specified" + " Hence aborting");
        }

        File libDir = new File(commandLine.getOptionValue(OPTION_JDAG_HOME) + File.separator + LIB_DIR);

        if (!libDir.exists()) {
            throw new RuntimeException("Not able to resolve the library directory" + " Please check "
                    + libDir.getAbsolutePath() + " exists");
        }

        File[] dependentLibraries = libDir.listFiles();
        if (dependentLibraries.length == 0) {
            throw new RuntimeException("Not able to locate dependent jars " + " in " + libDir);
        }

        StringBuilder builder = new StringBuilder();
        File confDir = new File(commandLine.getOptionValue(OPTION_JDAG_HOME) + File.separator + CONF_DIR);
        if (!confDir.exists()) {
            throw new RuntimeException(
                    "Unable to locate the conf directory" + "  " + confDir.getAbsolutePath());
        }
        builder.append(confDir.getAbsolutePath());
        for (File f : dependentLibraries) {
            builder.append(File.pathSeparator);
            builder.append(f.getAbsolutePath());
        }
        myClassPath = builder.toString();
        File f = new File(ClassLoader.getSystemClassLoader().getResource(TOPOLOGY_FILE).getFile());

        myTopologyConfiguration = new XMLConfiguration(f);

    } catch (ConfigurationException e) {
        LOG.log(Level.SEVERE, "Exception when loading the topology file", e);
        throw new RuntimeException(e);
    } catch (ParseException e) {
        LOG.log(Level.SEVERE, "Exception while parsing command line arguments", e);
        throw new RuntimeException(e);
    }
}

From source file:at.molindo.utils.reflect.ClassUtils.java

/**
 * @param thread//w w  w  .jav  a 2  s  .c  o  m
 *            {@link Thread} to use for
 *            {@link Thread#getContextClassLoader() context ClassLoader} or
 *            <code>null</code> for {@link Thread#currentThread() current
 *            thread}
 * @param fallback
 *            {@link ClassLoader} providing class if no context classloader
 *            or <code>null</code> for this class
 * @return never <code>null</code>
 */
public static ClassLoader getClassLoader(Thread thread, Class<?> fallback) {
    if (thread == null) {
        thread = Thread.currentThread();
    }
    ClassLoader loader = thread.getContextClassLoader();

    if (loader == null) {
        loader = (fallback != null ? fallback : ClassUtils.class).getClassLoader();
    }

    if (loader == null) {
        ClassLoader.getSystemClassLoader();
    }

    return loader;
}

From source file:com.ning.jetty.base.modules.JerseyBaseServerModule.java

@Override
protected void configureResources() {
    for (final String urlPattern : jaxRSServlets.keySet()) {
        serve(urlPattern).with(jaxRSServlets.get(urlPattern), JERSEY_PARAMS.build());
    }/*from   w w w  .ja v  a 2 s . c o m*/

    for (final String urlPattern : jaxRSServletsRegex.keySet()) {
        serveRegex(urlPattern).with(jaxRSServletsRegex.get(urlPattern), JERSEY_PARAMS.build());
    }

    // Catch-all resources
    if (jaxRSResources.size() != 0) {
        JERSEY_PARAMS.put("com.sun.jersey.config.property.packages", StringUtils.join(jaxRSResources, ";"));
        try {
            final Class servletKey = ClassLoader.getSystemClassLoader()
                    .loadClass("com.sun.jersey.guice.spi.container.servlet.GuiceContainer");
            serveRegex(jaxRSUriPattern).with(servletKey, JERSEY_PARAMS.build());
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException(e);
        }
    }
}

From source file:hudson.remoting.Launcher.java

@Option(name = "-cp", aliases = "-classpath", metaVar = "PATH", usage = "add the given classpath elements to the system classloader.")
public void addClasspath(String pathList) throws Exception {
    Method $addURL = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
    $addURL.setAccessible(true);//from w  ww. j  av a2s.  c om

    for (String token : pathList.split(File.pathSeparator))
        $addURL.invoke(ClassLoader.getSystemClassLoader(), new File(token).toURI().toURL());

    // fix up the system.class.path to pretend that those jar files
    // are given through CLASSPATH or something.
    // some tools like JAX-WS RI and Hadoop relies on this.
    System.setProperty("java.class.path",
            System.getProperty("java.class.path") + File.pathSeparatorChar + pathList);
}

From source file:de.alpharogroup.lang.ClassExtensions.java

/**
 * Gets the ClassLoader from the given object.
 *
 * @param obj//from   ww w .j ava 2  s  .c o m
 *            The object.
 * @return the ClassLoader from the given object.
 */
public static ClassLoader getClassLoader(final Object obj) {
    ClassLoader classLoader = null;
    if (null != obj) {
        if (isDerivate(Thread.currentThread().getContextClassLoader(), obj.getClass().getClassLoader())) {
            classLoader = obj.getClass().getClassLoader();
        } else {
            classLoader = Thread.currentThread().getContextClassLoader();
        }
        if (isDerivate(classLoader, ClassLoader.getSystemClassLoader())) {
            classLoader = ClassLoader.getSystemClassLoader();
        }
    } else {
        if (isDerivate(Thread.currentThread().getContextClassLoader(), ClassLoader.getSystemClassLoader())) {
            classLoader = ClassLoader.getSystemClassLoader();
        } else {
            classLoader = Thread.currentThread().getContextClassLoader();
        }
    }
    return classLoader;
}

From source file:de.lmu.ifi.dbs.jfeaturelib.utils.PackageScanner.java

public List<Class<T>> scanForClass(Package thePackage, Class<T> needle)
        throws IOException, UnsupportedEncodingException, URISyntaxException {
    List<String> binaryNames = getNames(thePackage);
    List<Class<T>> list = new ArrayList<>();

    ClassLoader loader = ClassLoader.getSystemClassLoader();
    for (String binaryName : binaryNames) {
        if (binaryName.contains("$") && !includeInnerClasses) {
            log.debug("Skipped inner class: " + binaryName);
            continue;
        }//from w  w  w  .  j av  a2  s.c  o m

        try {
            Class<?> clazz = loader.loadClass(binaryName);
            int modifiers = clazz.getModifiers();
            if ((modifiers & Modifier.INTERFACE) != 0 && !includeInterfaces) {
                log.debug("Skipped interface: " + binaryName);
                continue;
            }
            if ((modifiers & Modifier.ABSTRACT) != 0 && !includeAbstractClasses) {
                log.debug("Skipped abstract class: " + binaryName);
                continue;
            }
            if (needle.isAssignableFrom(clazz)) {
                log.debug("added class/interface/..: " + binaryName);
                list.add((Class<T>) clazz);
            }
        } catch (ClassNotFoundException e) {
            // This should never happen
            log.warn("couldn't find class (?!): " + binaryName, e);
        }
    }
    return list;
}

From source file:nz.co.senanque.madura.bundle.BundleRootImpl.java

private void dumpClassLoader(ClassLoader sysClassLoader) {
    //Get the System Classloader
    if (sysClassLoader == null) {
        sysClassLoader = ClassLoader.getSystemClassLoader();
    }//from   ww  w.j av a 2s.  c  o m

    //Get the URLs
    URL[] urls = ((URLClassLoader) sysClassLoader).getURLs();

    for (int i = 0; i < urls.length; i++) {
        m_logger.debug("{}", urls[i].getFile());
    }
}

From source file:gov.va.vinci.leo.tools.LeoUtils.java

/**
 * Load the config file(s).//w ww .j av a  2 s .c o m
 *
 * @param environment name of the environment to load
 * @param configFilePaths paths the config files to load, local or remote
 * @return ConfigObject with accumulated variables representing the environment
 * @throws IOException if there is a problem reading the config files
 */
public static ConfigObject loadConfigFile(String environment, String... configFilePaths) throws IOException {
    ConfigSlurper slurper = new ConfigSlurper(environment);
    ConfigObject config = new ConfigObject();
    ClassLoader cl = ClassLoader.getSystemClassLoader();
    for (String filePath : configFilePaths) {
        InputStream in = cl.getResourceAsStream(filePath);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IOUtils.copy(in, out);
        String resourceAsString = out.toString();
        config.merge(slurper.parse(resourceAsString));
    }
    return config;
}