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:com.networknt.audit.ServerInfoDisabledTest.java

static void addURL(URL url) throws Exception {
    URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class clazz = URLClassLoader.class;
    // Use reflection
    Method method = clazz.getDeclaredMethod("addURL", new Class[] { URL.class });
    method.setAccessible(true);// ww  w . j  a  v  a2s .co  m
    method.invoke(classLoader, new Object[] { url });
}

From source file:org.kineticsystem.commons.util.ResourceLoader.java

/** 
 * Return a resource as an input stream.
 * @param path The resource path i.e/* w  w  w.  j  a  va  2  s.  c o  m*/
 *     <tt>org/kineticsystem/commons/mymodule/bundle/test.txt</tt>.
 * @return The input stream representing the given resource.
 */
public static InputStream getStream(String path) {
    return ClassLoader.getSystemClassLoader().getResourceAsStream(path);
}

From source file:com.olacabs.fabric.compute.builder.impl.JarScanner.java

private URL[] genUrls(URL[] jarFileURLs) {
    URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    URL[] originalURLs = loader.getURLs();
    Set<URL> mergedJarURLs = new HashSet<URL>(originalURLs.length + jarFileURLs.length);
    mergedJarURLs.addAll(Arrays.asList(originalURLs));
    mergedJarURLs.addAll(Arrays.asList(jarFileURLs));
    return mergedJarURLs.toArray(new URL[mergedJarURLs.size()]);
}

From source file:com.indexoutofbounds.util.ClassPathInputStreamUtils.java

/**
 * Trys to locate a file from the classpath.
 * @param path/*from w  w w . jav a  2  s .co m*/
 * @return
 */
private static final URL locateAsResource(final String path) {

    URL url = null;
    // First, try to locate this resource through the current
    // context classloader.
    url = Thread.currentThread().getContextClassLoader().getResource(path);
    if (url != null)
        return url;

    // Next, try to locate this resource through this class's classloader
    url = null;
    if (url != null)
        return url;

    // Next, try to locate this resource through the system classloader
    url = ClassLoader.getSystemClassLoader().getResource(path);

    // Anywhere else we should look?
    return url;
}

From source file:io.trivium.Registry.java

public void reload() {
    final String PREFIX = "META-INF/services/";
    ClassLoader tvmLoader = ClassLoader.getSystemClassLoader();
    //types/*from  ww w.j  a  v a 2s  . c  o  m*/
    try {
        Enumeration<URL> resUrl = tvmLoader.getResources(PREFIX + "io.trivium.extension.Fact");
        while (resUrl.hasMoreElements()) {
            URL url = resUrl.nextElement();
            URLConnection connection = url.openConnection();
            connection.connect();
            InputStream is = connection.getInputStream();
            List<String> lines = IOUtils.readLines(is, "UTF-8");
            is.close();
            for (String line : lines) {
                Class<? extends Fact> clazz = (Class<? extends Fact>) Class.forName(line);
                Fact prototype = clazz.newInstance();
                if (!types.containsKey(prototype.getTypeRef())) {
                    types.put(prototype.getTypeRef(), clazz);
                }
                logger.log(Level.FINE, "registered type {0}", prototype.getFactName());
            }
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "dynamically loading types failed", ex);
    }

    //bindings
    try {
        Enumeration<URL> resUrl = tvmLoader.getResources(PREFIX + "io.trivium.extension.Binding");
        while (resUrl.hasMoreElements()) {
            URL url = resUrl.nextElement();
            URLConnection connection = url.openConnection();
            connection.connect();
            InputStream is = connection.getInputStream();
            List<String> lines = IOUtils.readLines(is, "UTF-8");
            is.close();
            for (String line : lines) {
                Class<? extends Binding> clazz = (Class<? extends Binding>) Class.forName(line);
                Binding prototype = clazz.newInstance();
                if (!bindings.containsKey(prototype.getTypeRef())) {
                    bindings.put(prototype.getTypeRef(), clazz);
                    //register prototype
                    bindingInstances.put(prototype.getTypeRef(), prototype);
                }
                logger.log(Level.FINE, "registered binding {0}", prototype.getName());
            }
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "dynamically loading bindings failed", ex);
    }

    //tasks
    try {
        Enumeration<URL> resUrl = tvmLoader.getResources(PREFIX + "io.trivium.extension.Task");
        while (resUrl.hasMoreElements()) {
            URL url = resUrl.nextElement();
            URLConnection connection = url.openConnection();
            connection.connect();
            InputStream is = connection.getInputStream();
            List<String> lines = IOUtils.readLines(is, "UTF-8");
            is.close();
            for (String line : lines) {
                Class<? extends Task> clazz = (Class<? extends Task>) Class.forName(line);
                Task prototype = clazz.newInstance();
                if (!tasks.containsKey(prototype.getTypeRef())) {
                    tasks.put(prototype.getTypeRef(), clazz);
                }
                logger.log(Level.FINE, "registered binding {0}", prototype.getName());
            }
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "dynamically loading bindings failed", ex);
    }

    //testcases
    try {
        Enumeration<URL> resUrl = tvmLoader.getResources(PREFIX + "io.trivium.test.TestCase");
        while (resUrl.hasMoreElements()) {
            URL url = resUrl.nextElement();
            URLConnection connection = url.openConnection();
            connection.connect();
            InputStream is = connection.getInputStream();
            List<String> lines = IOUtils.readLines(is, "UTF-8");
            is.close();
            for (String line : lines) {
                Class<? extends TestCase> clazz = (Class<? extends TestCase>) Class.forName(line);
                TestCase prototype = clazz.newInstance();
                if (!testcases.containsKey(prototype.getTypeRef())) {
                    testcases.put(prototype.getTypeRef(), prototype);
                }
                logger.log(Level.FINE, "registered testcase {0}", prototype.getTypeRef());
            }
        }
    } catch (Exception ex) {
        logger.log(Level.SEVERE, "dynamically loading test cases failed", ex);
    }
}

From source file:org.tinymediamanager.TmmOsUtils.java

/**
 * need to do add path to Classpath with reflection since the URLClassLoader.addURL(URL url) method is protected:
 * //from  w  w  w . jav  a  2  s  .com
 * @param s
 *          the path to be set
 * @throws Exception
 */
public static void addPath(String s) throws Exception {
    File f = new File(s);
    URI u = f.toURI();
    URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class<URLClassLoader> urlClass = URLClassLoader.class;
    Method method = urlClass.getDeclaredMethod("addURL", new Class[] { URL.class });
    method.setAccessible(true);
    method.invoke(urlClassLoader, new Object[] { u.toURL() });
}

From source file:com.web.server.XMLDeploymentScanner.java

public XMLDeploymentScanner(String scanDirectory, String userLibDir) {
    super(scanDirectory);
    this.userLibDir = userLibDir;
    File userLibJars = new File(userLibDir);
    CopyOnWriteArrayList<String> jarList = new CopyOnWriteArrayList();
    getUsersJars(userLibJars, jarList);/*from   ww  w.  j  a va  2s . co m*/
    URLClassLoader loader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    userLibJarLoader = new WebClassLoader(loader.getURLs());
    for (String jarFilePath : jarList) {
        try {
            userLibJarLoader.addURL(new URL("file:/" + jarFilePath.replace("\\", "/")));
        } catch (MalformedURLException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();
        }
    }

    DigesterLoader xmlDigesterLoader = DigesterLoader.newLoader(new FromXmlRulesModule() {

        protected void loadRules() {
            // TODO Auto-generated method stub
            try {
                loadXMLRules(new InputSource(new FileInputStream("./config/datasource-rules.xml")));
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    });
    xmlDigester = xmlDigesterLoader.newDigester();
    try {
        ic = new InitialContext();
        ic.createSubcontext("java:");
    } catch (NamingException e1) {
        // TODO Auto-generated catch block
    }

    File[] xmlFiles = new File(scanDirectory).listFiles();

    if (xmlFiles != null && xmlFiles.length > 0) {
        for (File xmlFile : xmlFiles) {
            if (xmlFile.getName().toLowerCase().endsWith("datasource.xml")) {
                installDataSource(xmlFile);
            }
        }
    }

    // TODO Auto-generated constructor stub
}

From source file:org.eclipse.winery.common.TestUtil.java

@Test
public void testGetChecksumOfFile() throws Exception {
    File file = new File(
            ClassLoader.getSystemClassLoader().getResource("org/eclipse/winery/common/invalid.xml").getFile());
    assertEquals("4406bff97249955ef46ea3ae590f9813fd44dcd769b8204cbb702ee6767173b0",
            HashingUtil.getChecksum(file, "SHA-256"));
}

From source file:com.otiliouine.configurablefactory.ConfigurableFactory.java

public ConfigurableFactory(String path) {
    InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(path);
    loadConfiguration(in);
}

From source file:org.apache.james.utils.GuiceProtocolHandlerLoader.java

private ProtocolHandler createProtocolHandler(String name) throws LoadingException {
    try {//from www . ja  v  a 2s  .  c om
        @SuppressWarnings("unchecked")
        Class<ProtocolHandler> clazz = (Class<ProtocolHandler>) ClassLoader.getSystemClassLoader()
                .loadClass(name);
        return injector.getInstance(clazz);
    } catch (ClassNotFoundException e) {
        throw new LoadingException("Can not load " + name);
    }
}