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.obm.push.spushnik.service.CredentialsServiceTest.java

@Test
public void testCredentialsWithNiceCertificate() throws Exception {
    InputStream certificateInputStream = ClassLoader.getSystemClassLoader()
            .getResourceAsStream("pkcs_pwd_toto.p12");
    service.validate(Credentials.builder().loginAtDomain("user@domain").password("password".toCharArray())
            .pkcs12(ByteStreams.toByteArray(certificateInputStream)).pkcs12Password("toto").build());
}

From source file:ReflectUtils.java

/**
 * Scans all classes accessible from the context class loader which belong to the given package and subpackages.
 *
 * @param packageName The base package/*  w  w w .  j  av a  2  s  .c  om*/
 * @return The classes
 * @throws ClassNotFoundException
 * @throws IOException
 */
private static Class[] getClasses(String packageName) throws ClassNotFoundException, IOException {
    //        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    ClassLoader classLoader = ClassLoader.getSystemClassLoader();
    assert classLoader != null;
    String path = packageName.replace('.', '/');
    Enumeration<URL> resources = classLoader.getResources(path);
    List<File> dirs = new ArrayList<File>();
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        dirs.add(new File(resource.getFile()));
    }
    ArrayList<Class> classes = new ArrayList<Class>();
    for (File directory : dirs) {
        classes.addAll(findClasses(directory, packageName));
    }
    return classes.toArray(new Class[classes.size()]);
}

From source file:xc.mst.utils.SetupClasspath.java

@SuppressWarnings("unchecked")
public static void addURL(URL u) {

    URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class sysclass = URLClassLoader.class;

    for (URL u2 : sysloader.getURLs()) {
        // System.out.println("u: "+u2);
    }//from  w  w  w .jav a2  s. c  om

    try {
        Method method = sysclass.getDeclaredMethod("addURL", new Class[] { URL.class });
        method.setAccessible(true);
        method.invoke(sysloader, new Object[] { u });
    } catch (Throwable t) {
        t.printStackTrace();
        throw new RuntimeException("Error, could not add URL to system classloader");
    }

}

From source file:org.apache.ambari.server.stack.StackManagerTest.java

public static StackManager createTestStackManager(String stackRoot) throws Exception {
    try {//w w  w .j a v a 2 s  . c o  m
        //todo: dao , actionMetaData expectations
        dao = createNiceMock(MetainfoDAO.class);
        actionMetadata = createNiceMock(ActionMetadata.class);
        Configuration config = createNiceMock(Configuration.class);
        expect(config.getSharedResourcesDirPath())
                .andReturn(ClassLoader.getSystemClassLoader().getResource("").getPath()).anyTimes();
        replay(config);
        osFamily = new OsFamily(config);

        replay(dao, actionMetadata);
        StackManager stackManager = new StackManager(new File(stackRoot), null,
                new StackContext(dao, actionMetadata, osFamily));
        return stackManager;
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
}

From source file:de.kaixo.mubi.lists.MubiListsReaderTest.java

private InputStream getResourceAsStream(String fileName) {
    return ClassLoader.getSystemClassLoader()
            .getResourceAsStream(getClass().getPackage().getName().replace('.', '/') + "/" + fileName);
}

From source file:org.atm.mvn.run.JavaBootstrap.java

/**
 * Run the main method in the class using the specified class path.
 * //from   www  .  ja  v a 2s.c o  m
 * @throws Exception
 *             Any exceptions that might occur.
 */
public void run() throws Exception {
    Thread currentThread = Thread.currentThread();

    ClassLoader existingClassLoader = currentThread.getContextClassLoader();
    try {
        /*
         * Replace the current class loader with one with all of the needed
         * dependencies.
         */
        ClassLoader bootstrapClassLoader = createClassLoader(ClassLoader.getSystemClassLoader(), false);

        currentThread.setContextClassLoader(bootstrapClassLoader);

        // find the java class
        Class<?> mainClass = resolveClass(bootstrapClassLoader);

        // find the main method
        Method mainMethod = resolveMainMethod(mainClass);

        // invoke the main method
        invokeMain(mainMethod);
    } finally {
        currentThread.setContextClassLoader(existingClassLoader);
    }
}

From source file:org.netxilia.api.impl.utils.DynamicClassLoader.java

public void addURL(URL u) throws IOException {

    URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
    Class<?> sysclass = URLClassLoader.class;

    try {//from   w ww  .  ja  v a2 s .c o m
        Method method = sysclass.getDeclaredMethod("addURL", parameters);
        method.setAccessible(true);
        method.invoke(sysloader, new Object[] { u });
    } catch (Throwable t) {
        t.printStackTrace();
        throw new IOException("Error, could not add URL to system classloader");
    } // end try catch

}

From source file:org.apache.ambari.server.bootstrap.BootStrapTest.java

@Test
public void testRun() throws Exception {
    Properties properties = new Properties();
    String bootdir = temp.newFolder("bootdir").toString();
    String metadetadir = temp.newFolder("metadetadir").toString();
    String serverVersionFilePath = temp.newFolder("serverVersionFilePath").toString();
    LOG.info("Bootdir is " + bootdir);
    LOG.info("Metadetadir is " + metadetadir);
    LOG.info("ServerVersionFilePath is " + serverVersionFilePath);

    String sharedResourcesDir = "src/test/resources/";
    if (System.getProperty("os.name").contains("Windows")) {
        sharedResourcesDir = ClassLoader.getSystemClassLoader().getResource("").getPath();
    }/*from   w ww. ja  v  a 2s.c  o m*/

    properties.setProperty(Configuration.BOOTSTRAP_DIR, bootdir);
    properties.setProperty(Configuration.BOOTSTRAP_SCRIPT, "echo");
    properties.setProperty(Configuration.SRVR_KSTR_DIR_KEY, "target" + File.separator + "classes");
    properties.setProperty(Configuration.METADETA_DIR_PATH, metadetadir);
    properties.setProperty(Configuration.SERVER_VERSION_FILE, serverVersionFilePath);
    properties.setProperty(Configuration.SHARED_RESOURCES_DIR_KEY, sharedResourcesDir);

    Configuration conf = new Configuration(properties);
    AmbariMetaInfo ambariMetaInfo = new AmbariMetaInfo(conf);
    BootStrapImpl impl = new BootStrapImpl(conf, ambariMetaInfo);
    impl.init();
    SshHostInfo info = new SshHostInfo();
    info.setSshKey("xyz");
    ArrayList<String> hosts = new ArrayList<String>();
    hosts.add("host1");
    hosts.add("host2");
    info.setUserRunAs("root");
    info.setHosts(hosts);
    info.setUser("user");
    info.setPassword("passwd");
    BSResponse response = impl.runBootStrap(info);
    LOG.info("Response id from bootstrap " + response.getRequestId());
    /* do a query */
    BootStrapStatus status = impl.getStatus(response.getRequestId());
    LOG.info("Status " + status.getStatus());
    int num = 0;
    while ((status.getStatus() == BSStat.RUNNING) && (num < 500)) {
        status = impl.getStatus(response.getRequestId());
        Thread.sleep(100);
        num++;
    }
    LOG.info("Status: log " + status.getLog() + " status=" + status.getStatus());
    /* Note its an echo command so it should echo host1,host2 */
    Assert.assertTrue(status.getLog().contains("host1,host2"));
    Assert.assertEquals(BSStat.SUCCESS, status.getStatus());
    Assert.assertFalse(new File(bootdir + File.separator + "1" + File.separator + "sshKey").exists());
    Assert.assertFalse(new File(bootdir + File.separator + "1" + File.separator + "host_pass").exists());
}

From source file:org.apache.axis2.classloader.MultiParentClassLoader.java

/**
 * Creates a named class loader with no parents.
 *
 * @param urls the urls from which this class loader will classes and resources
 *//*  w  w w .j  ava2  s .c  om*/
public MultiParentClassLoader(URL[] urls) {
    super(urls);
    parents = new ClassLoader[] { ClassLoader.getSystemClassLoader() };
    inverseClassLoading = false;
    hiddenClasses = new String[0];
    nonOverridableClasses = new String[0];
    hiddenResources = new String[0];
    nonOverridableResources = new String[0];
}

From source file:dk.statsbiblioteket.util.qa.PackageScanner.java

/**
 * Create a new PackageScanner scanning a specific file or a recursively
 * through a directory if {@code target} is an empty string.
 *
 * @param finalReport The {@link Report} to submit collected data to.
 * @param baseDir     The root directory from which {@code .class} files should
 *                    be scanned. This is typically directory containing the
 *                    root of your compiled classes.
 * @param className   The name of the {@code .class} file to scan or an empty
 *                    string to scan recursively through {@code baseDir}.
 *//*ww  w. j ava 2  s.c  om*/
public PackageScanner(Report finalReport, File baseDir, String className) {
    loader = new DynamicClassLoader(ClassLoader.getSystemClassLoader());
    this.report = finalReport;
    this.baseSource = baseDir;
    this.target = className;
    log = LogFactory.getLog(PackageScanner.class);
}