Example usage for java.lang ClassLoader getResource

List of usage examples for java.lang ClassLoader getResource

Introduction

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

Prototype

public URL getResource(String name) 

Source Link

Document

Finds the resource with the given name.

Usage

From source file:Main.java

private static Templates loadTemplates(String name) throws TransformerException {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    return transfomerFactory().newTemplates(new StreamSource(cl.getResource(name).toString()));
}

From source file:org.jboss.as.test.integration.web.security.WebSecurityBASICTestCase.java

@Deployment
public static WebArchive deployment() {
    // FIXME hack to get things prepared before the deployment happens
    try {//from   w w w .  ja  va  2s .c o  m
        // create required security domains
        createSecurityDomain();
    } catch (Exception e) {
        // ignore
    }

    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    URL webxml = tccl.getResource("web-secure-basic.war/web.xml");
    WebArchive war = WebSecurityPasswordBasedBase.create("web-secure-basic.war", SecuredServlet.class, true,
            webxml);
    war.addAsWebInfResource("web-secure-basic.war/jboss-web.xml", "jboss-web.xml");
    WebSecurityPasswordBasedBase.printWar(war);
    return war;
}

From source file:com.shadwelldacunha.byteswipe.core.Utilities.java

public static File getResource(String resource) throws URISyntaxException {
    ClassLoader classLoader = Utilities.class.getClassLoader();
    URL fileURL = classLoader.getResource(resource);
    return new File(fileURL.toURI());
}

From source file:com.boundary.sdk.event.notification.AlarmNotification.java

public static AlarmNotification load(String resource) throws URISyntaxException {
    AlarmNotification instance = new AlarmNotification();

    ClassLoader classLoader = instance.getClass().getClassLoader();
    URL url = classLoader.getResource(resource);
    File file = new File(url.toURI());

    ObjectMapper mapper = new ObjectMapper();

    try {//from  w w w. j  a  va 2  s.co m
        instance = mapper.readValue(file, AlarmNotification.class);
    } catch (JsonParseException e) {
        e.printStackTrace();
    } catch (JsonMappingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return instance;
}

From source file:com.chiralBehaviors.autoconfigure.AutoConfigureService.java

public static URL getURL(String fileName) {
    URL url;/*from w w  w  . ja v a2 s.c o m*/
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    url = cl.getResource(fileName);
    if (url == null) {
        cl = AutoConfigureService.class.getClassLoader();
        url = cl.getResource(fileName);
    }
    return url;
}

From source file:com.basho.contact.parser.ContactParserTest.java

public static String loadResource(String name) throws Exception {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL url = loader.getResource(name);
    File f = new File(url.getPath());
    return org.apache.commons.io.FileUtils.readFileToString(f);
}

From source file:com.taobao.pushit.commons.ClientLoggerInit.java

public static void initLog() {

    if (initOK) {
        return;/*  www.  ja va 2 s  .c  o  m*/
    }

    URL url = null;
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    if (cl == null || (url = cl.getResource(PUSHIT_CLIENT_LOG4J_FILE)) == null) {
        cl = ClientLoggerInit.class.getClassLoader();
        if (cl == null || (url = cl.getResource(PUSHIT_CLIENT_LOG4J_FILE)) == null) {
            fallback();
            return;
        }
    }

    final ClassLoader pre = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(PropertyConfigurator.class.getClassLoader());
        PropertyConfigurator.configure(url);
    } finally {
        Thread.currentThread().setContextClassLoader(pre);
    }

    // root loggerfile appender, appenderpushit.log
    FileAppender rootFileAppender = getFileAppender(Logger.getRootLogger());
    if (rootFileAppender == null) {
        log.warn("root loggerfile appender!!!");
        rootFileAppender = new FileAppender();
        rootFileAppender.setFile(System.getProperty("user.home") + "/diamond/logs/pushit-client.log");
    }

    // pushit logger, log4jappenderlogger,
    // geckopushitappender
    setFileAppender(rootFileAppender, PUSHIT_CLIENT_LOGGER);

    initOK = true;
}

From source file:io.specto.hoverfly.junit.HoverflyRuleUtils.java

static URI findResourceOnClasspath(String resourceName) throws URISyntaxException {
    final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    final URL resource = classLoader.getResource(resourceName);
    if (resource == null) {
        throw new IllegalArgumentException("Resource not found with name: " + resourceName);
    }/*from w  w  w  . j  a v a2 s .c  o  m*/
    return resource.toURI();
}

From source file:org.jboss.as.test.spec.servlet3.WebSecurityProgrammaticLoginTestCase.java

@Deployment(testable = true)
public static WebArchive deployment() {
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    URL webxml = tccl.getResource("web-secure-programmatic-login.war/web.xml");
    WebArchive war = ShrinkWrap.create(WebArchive.class, "web-secure-programmatic-login.war");
    war.addAsResource(tccl.getResource("security/users.properties"), "users.properties");
    war.addAsResource(tccl.getResource("security/roles.properties"), "roles.properties");
    war.addAsManifestResource(tccl.getResource("web-secure-programmatic-login.war/MANIFEST.MF"), "MANIFEST.MF");
    war.addClass(LoginServlet.class);
    war.addClass(SecuredServlet.class);

    if (webxml != null) {
        war.setWebXML(webxml);//from  w  w w.  j  ava2s . co  m
    }

    printWar(war);

    return war;
}

From source file:com.github.cc007.headsinventory.locale.Translator.java

private static InputStream getTranslationsFileStream(String fileName, ClassLoader classLoader) {
    try {//  w w  w .j a v a  2  s  . c  om
        URL url = classLoader.getResource(fileName);

        if (url == null) {
            return null;
        }

        URLConnection connection = url.openConnection();
        connection.setUseCaches(false);
        return connection.getInputStream();
    } catch (IOException ex) {
        return null;
    }
}