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.apache.xml.security.utils.ClassLoaderUtils.java

/**
 * Load a given resource. <p/> This method will try to load the resource
 * using the following methods (in order):
 * <ul>/*from  ww w .  j  av  a 2 s.  c  o m*/
 * <li>From Thread.currentThread().getContextClassLoader()
 * <li>From ClassLoaderUtil.class.getClassLoader()
 * <li>callingClass.getClassLoader()
 * </ul>
 * 
 * @param resourceName The name of the resource to load
 * @param callingClass The Class object of the calling object
 */
public static URL getResource(String resourceName, Class<?> callingClass) {
    URL url = Thread.currentThread().getContextClassLoader().getResource(resourceName);
    if (url == null && resourceName.startsWith("/")) {
        //certain classloaders need it without the leading /
        url = Thread.currentThread().getContextClassLoader().getResource(resourceName.substring(1));
    }

    ClassLoader cluClassloader = ClassLoaderUtils.class.getClassLoader();
    if (cluClassloader == null) {
        cluClassloader = ClassLoader.getSystemClassLoader();
    }
    if (url == null) {
        url = cluClassloader.getResource(resourceName);
    }
    if (url == null && resourceName.startsWith("/")) {
        //certain classloaders need it without the leading /
        url = cluClassloader.getResource(resourceName.substring(1));
    }

    if (url == null) {
        ClassLoader cl = callingClass.getClassLoader();

        if (cl != null) {
            url = cl.getResource(resourceName);
        }
    }

    if (url == null) {
        url = callingClass.getResource(resourceName);
    }

    if ((url == null) && (resourceName != null) && (resourceName.charAt(0) != '/')) {
        return getResource('/' + resourceName, callingClass);
    }

    return url;
}

From source file:org.linqs.psl.config.Config.java

/**
 * (Re)create and populate the initial config.
 *///from  w  w  w . ja  v a2s . c o  m
public static void init() {
    config = new DataConfiguration(new BaseConfiguration());

    // Load maven project properties.
    InputStream stream = ClassLoader.getSystemClassLoader().getResourceAsStream(PROJECT_PROPS);
    if (stream != null) {
        loadResource(stream, PROJECT_PROPS);
    }

    // Load the configuration file directly if the path exists.
    String path = OptionConverter.getSystemProperty(PSL_CONFIG, PSL_CONFIG_DEFAULT);
    if ((new File(path)).isFile()) {
        loadResource(path);
        return;
    }

    // Try to get a resource URL from the system (if we have a property key instead of a path).
    stream = ClassLoader.getSystemClassLoader().getResourceAsStream(path);
    if (stream != null) {
        loadResource(stream, PSL_CONFIG);
        return;
    }

    log.debug("PSL configuration {} file not found."
            + " Only default values will be used unless additional properties are specified.", path);
}

From source file:com.apress.prospringintegration.customadapters.inbound.IntegrationTestUtils.java

public AbstractEndpoint createConsumer(MessageChannel messageChannel, MessageHandler messageHandler)
        throws Throwable {
    ConsumerEndpointFactoryBean consumerEndpointFactoryBean = new ConsumerEndpointFactoryBean();
    consumerEndpointFactoryBean.setInputChannel(messageChannel);
    consumerEndpointFactoryBean.setBeanName("MessageConsumer");
    consumerEndpointFactoryBean.setBeanFactory(beanFactory);
    consumerEndpointFactoryBean.setHandler(messageHandler);
    consumerEndpointFactoryBean.setBeanClassLoader(ClassLoader.getSystemClassLoader());
    consumerEndpointFactoryBean.afterPropertiesSet();

    AbstractEndpoint abstractEndpoint = consumerEndpointFactoryBean.getObject();
    abstractEndpoint.start();// w  w w.j  av  a2s.  c  om

    return abstractEndpoint;
}

From source file:com.nextep.designer.core.model.base.AbstractController.java

public Object load(String className, UID id) {
    try {//from w w  w . j a  v a 2s .  com
        Class<?> clazz = ClassLoader.getSystemClassLoader().loadClass(className);
        return CorePlugin.getIdentifiableDao().load(clazz, id);
    } catch (ClassNotFoundException e) {
        throw new ErrorException(e);
    }
}

From source file:org.wso2.extension.siddhi.io.jms.sink.util.JMSClient.java

public void listen() throws InterruptedException {
    Properties properties = new Properties();
    try {//w w w  .  j a v  a  2s. co  m
        if ("qpid".equalsIgnoreCase(broker)) {
            properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("qpid.properties"));
        } else if ("activemq".equalsIgnoreCase(broker)) {
            properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("activemq.properties"));
        } else if ("mb".equalsIgnoreCase(broker)) {
            properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("mb.properties"));
        } else {
            log.error("Entered broker is invalid! ");
        }
        if (topic != null && topic.isEmpty() || topic.equals("\"\"")) {
            topic = null;
        }
        if (queue != null && queue.isEmpty() || queue.equals("\"\"")) {
            queue = null;
        }
        if (topic == null && queue == null) {
            log.error("Enter topic value or queue value! ");
        } else if (topic != null) {
            Context context = new InitialContext(properties);
            TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) context
                    .lookup("ConnectionFactory");
            topicConsumer = new TopicConsumer(topicConnectionFactory, topic, resultContainer);
            Thread consumerThread = new Thread(topicConsumer);
            log.info("Starting" + broker + "consumerTopic thread...");
            consumerThread.start();
        } else {
            Context context = new InitialContext(properties);
            QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) context
                    .lookup("ConnectionFactory");
            queueConsumer = new QueueConsumer(queueConnectionFactory, queue, resultContainer);
            Thread consumerThread = new Thread(queueConsumer);
            log.info("Starting" + broker + "consumerQueue thread...");
            consumerThread.start();
        }
    } catch (IOException e) {
        log.error("Cannot read properties file from resources. " + e.getMessage(), e);
    } catch (NamingException e) {
        log.error("Invalid properties in the properties " + e.getMessage(), e);
    }
}

From source file:org.openmrs.util.MemoryLeakUtil.java

public static void shutdownKeepAliveTimer() {
    try {//from w w w  . j  av  a2  s  .  c o m
        final Field kac = HttpClient.class.getDeclaredField("kac");

        kac.setAccessible(true);
        final Field keepAliveTimer = KeepAliveCache.class.getDeclaredField("keepAliveTimer");

        keepAliveTimer.setAccessible(true);

        final Thread thread = (Thread) keepAliveTimer.get(kac.get(null));

        if (thread.getContextClassLoader() == OpenmrsClassLoader.getInstance()) {
            //Set to system class loader such that we can be garbage collected.
            thread.setContextClassLoader(ClassLoader.getSystemClassLoader());
        }
    } catch (final Exception e) {
        log.error(e.getMessage(), e);
    }
}

From source file:de.fhg.fokus.odp.middleware.unittest.xTestCKANGateway.java

/** Setup the test case. */
@Override/*  w w  w.j  av a2 s  .com*/
@Before
protected void setUp() {

    // prepare the CKAN version
    String filename = "testConfig.properties";
    InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream(filename);
    Properties prop = new Properties();
    try {
        prop.load(is);
    } catch (IOException e) {
        log.log(Level.SEVERE, "Failed to load property file: " + filename);
    }

    ckanGW = new CKANGateway(prop.getProperty("ckan.uri"), prop.getProperty("ckan.apiKey"));
}

From source file:com.krawler.runtime.utils.URLClassLoaderUtil.java

public void addJarURL(URL u) throws Exception {
    try {/*  w  w  w . j a  v  a2  s  .c  o m*/

        URLClassLoader sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        URL urls[] = sysLoader.getURLs();
        for (int i = 0; i < urls.length; i++) {
            if (StringUtils.equalsIgnoreCase(urls[i].toString(), u.toString())) {
                if (log.isDebugEnabled()) {
                    log.debug("URL " + u + " is already in the CLASSPATH");
                }
                return;
            }
        }
        Class sysclass = URLClassLoader.class;
        Method method = sysclass.getDeclaredMethod("addURL", parameters);
        method.setAccessible(true);
        method.invoke(sysLoader, new Object[] { u });
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Error, could not add URL to system classloader" + e.getMessage());
    }

}

From source file:net.servicefixture.fitnesse.FixtureTemplateCreator.java

public void generateTemplate(String fixtureClass) throws Exception {
    try {/*  w w  w. jav  a2s  .  co  m*/
        Class clazz = ClassLoader.getSystemClassLoader().loadClass(fixtureClass);
        if (ServiceFixture.class.isAssignableFrom(clazz)) {
            writeLine("!|" + fixtureClass + "|");
            generateServiceFixtureTemplate(clazz);
        } else {
            // Support fitnesse built in fixtures.
            super.run(fixtureClass);
        }
    } catch (ClassNotFoundException e) {
        writeLine("# Could not find " + fixtureClass + " in the classpath. #");
    }
}

From source file:ninja.eivind.hotsreplayuploader.providers.hotslogs.HotSLogsProviderTest.java

@Before
public void setUp() throws IOException {
    URL resource = ClassLoader.getSystemClassLoader().getResource("test.StormReplay");
    assertNotNull("Could not load test resource", resource);
    String fileName = resource.getFile();
    parsedReplay = new StormParser().apply(new File(fileName));
    SimpleHttpClient mock = mock(SimpleHttpClient.class);
    when(mock.simpleRequest(anyString())).thenReturn("Duplicate");
    provider.setHttpClient(mock);//w ww.  ja  va2  s  .  c  om
    replayFile = new ReplayFile(new File(fileName));
}