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

/**
 * This method will search for <code>resource</code> in different
 * places. The search order is as follows:
 * <ol>//from  ww w  .  j  a  va 2 s . com
 * <p><li>Search for <code>resource</code> using the thread context
 * class loader under Java2.
 * <p><li>Try one last time with
 * <code>ClassLoader.getSystemResource(resource)</code>, that is is
 * using the system class loader in JDK 1.2 and virtual machine's
 * built-in class loader in JDK 1.1.
 * </ol>
 * <p/>
 *
 * @param resource
 * @return TODO
 */
public static URL getResource(String resource) {
    ClassLoader classLoader = null;
    URL url = null;
    try {
        classLoader = getTCL();
        if (classLoader != null) {
            url = classLoader.getResource(resource);
            if (url != null) {
                return url;
            }
        }
    } catch (Throwable t) {

    }

    // Last ditch attempt: get the resource from the class path. It
    // may be the case that clazz was loaded by the Extension class
    // loader which the parent of the system class loader. Hence the
    // code below.

    return ClassLoader.getSystemResource(resource);
}

From source file:com.dfki.av.sudplan.Configuration.java

/**
 * Initialize the {@link #SUDPLAN_3D_IMAGE}.
 *
 * @return the {@link Image} to return or null.
 *//*from   w  ww  . j a  v a  2  s  .c  o  m*/
private static Image initImage() {
    log.info("Init suplan3D icon...");
    Image image = null;
    try {
        ClassLoader loader = Configuration.class.getClassLoader();
        URL iconURL = loader.getResource("icons/sudplan3D.png");
        image = ImageIO.read(iconURL);
    } catch (IOException ex) {
        log.error(ex.toString());
    }

    return image;
}

From source file:org.mule.tools.rhinodo.impl.NodeModuleImplBuilder.java

private static URI getRoot(Class<?> klass, String rootDirectory) {
    ClassLoader classLoader = klass.getClassLoader();
    URI root;/* www  . j a v a2 s. com*/
    try {
        URL resource = classLoader.getResource(rootDirectory);
        if (resource == null) {
            throw new IllegalArgumentException("Invalid resource at: " + rootDirectory);
        }
        root = resource.toURI();
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }

    if (root == null) {
        throw new IllegalStateException("Error: path not found.");
    }

    try {
        root = new URI(root.toString() + "/");
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }

    return root;
}

From source file:com.autentia.tnt.xml.UtilitiesXML.java

/**
 * Este metodo busca un fichero de tipo XML en el classpath crea un objeto 
 * de tipo org.w3c.dom.Document.//from  w w w  .  j  a  v a  2s.  c o m
 * @param fichero: El nombre del fichero a procesar.
 * @return
 * @throws Exception
 */
public static Document file2Document(String fichero) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL urlfichero = loader.getResource(fichero);
    Document XMLDoc = factory.newDocumentBuilder().parse(new InputSource(urlfichero.openStream()));
    return XMLDoc;
}

From source file:io.apiman.test.common.util.TestUtil.java

/**
 * Loads a test plan from a classpath resource.
 * @param resourcePath/*from w w w  .j ava  2s.com*/
 * @param cl
 */
public static final TestPlan loadTestPlan(String resourcePath, ClassLoader cl) {
    try {
        URL url = cl.getResource(resourcePath);
        if (url == null)
            throw new RuntimeException("Test Plan not found: " + resourcePath); //$NON-NLS-1$
        JAXBContext jaxbContext = JAXBContext.newInstance(TestPlan.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        TestPlan plan = (TestPlan) jaxbUnmarshaller.unmarshal(url.openStream());
        return plan;
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:net.erdfelt.android.sdkfido.Build.java

public static String getVersion() {
    if (version == null) {
        ClassLoader cl = Thread.currentThread().getContextClassLoader();
        String resource = String.format("META-INF/maven/%s/%s/pom.properties", GROUP_ID, ARTIFACT_ID);
        URL url = cl.getResource(resource);
        if (url == null) {
            version = "[DEV]";
        } else {/*from  w  ww .  j ava 2  s. co m*/
            InputStream in = null;
            try {
                in = url.openStream();
                Properties props = new Properties();
                props.load(in);
                version = props.getProperty("version");
            } catch (IOException e) {
                LOG.log(Level.WARNING, "Unable to read: " + url, e);
                version = "[UNKNOWN]";
            } finally {
                IOUtils.closeQuietly(in);
            }
        }
    }
    return version;
}

From source file:com.baidu.rigel.biplatform.parser.util.PropertiesUtil.java

public static Properties loadPropertiesFromFile(String file) throws IOException {
    String location = file;//www .  j a  v a  2  s .  c om
    if (StringUtils.isBlank(location)) {
        throw new IllegalArgumentException("file can not be blank.");
    }
    Properties properties = new Properties();

    ClassLoader classLoaderToUse = Thread.currentThread().getContextClassLoader();

    URL url = classLoaderToUse != null ? classLoaderToUse.getResource(location)
            : ClassLoader.getSystemResource(location);

    URLConnection con = url.openConnection();
    InputStream is = con.getInputStream();
    try {
        if (location != null && location.endsWith(".xml")) {
            properties.loadFromXML(is);
        } else {
            properties.load(is);
        }
    } finally {
        IOUtils.close(con);
        IOUtils.closeQuietly(is);
    }

    return properties;
}

From source file:com.baidu.rigel.biplatform.parser.util.PropertiesUtil.java

/** 
 * loadPropertiesFromCurrent/*from   ww  w . ja  v  a2s . co  m*/
 * @param resource
 * @return
 * @throws IOException
 */
public static Properties loadPropertiesFromPath(String resource) throws IOException {
    String location = resource;
    if (StringUtils.isBlank(location)) {
        location = "conf/default.properties";
    }
    Properties properties = new Properties();

    ClassLoader classLoaderToUse = Thread.currentThread().getContextClassLoader();

    URL url = classLoaderToUse != null ? classLoaderToUse.getResource(location)
            : ClassLoader.getSystemResource(location);

    URLConnection con = url.openConnection();
    InputStream is = con.getInputStream();
    try {
        if (location != null && location.endsWith(".xml")) {
            properties.loadFromXML(is);
        } else {
            properties.load(is);
        }
    } finally {
        IOUtils.close(con);
        IOUtils.closeQuietly(is);
    }

    return properties;
}

From source file:org.apache.hadoop.hive.hwi.util.HadoopUtil.java

public static String getDataNodeURL(String path) {
    Configuration conf = new Configuration();
    conf.addResource("hdfs-default.xml");
    conf.addResource("hdfs-site.xml");

    String nnHttp = conf.get("dfs.http.address");
    String dnHttp = conf.get("dfs.datanode.http.address");

    String host = "";
    try {/*from  w  w w . j a v  a 2 s  .  co  m*/
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        BufferedReader in = new BufferedReader(
                new InputStreamReader(classLoader.getResource("slaves").openStream()));
        host = in.readLine();
    } catch (IOException e) {
        e.printStackTrace();
        l4j.error(e.getMessage());
    }

    String nnPort = "";
    if (nnHttp.contains(":")) {
        nnPort = nnHttp.split(":")[1];
    }

    String dnPort = "";
    if (dnHttp.contains(":")) {
        dnPort = dnHttp.split(":")[1];
    }

    return "http://" + host + ":" + dnPort + "/browseDirectory.jsp?namenodeInfoPort=" + nnPort + "&dir=" + path;
}

From source file:org.apache.flink.streaming.connectors.fs.bucketing.RollingSinkMigrationTest.java

private static String getResourceFilename(String filename) {
    ClassLoader cl = RollingSinkMigrationTest.class.getClassLoader();
    URL resource = cl.getResource(filename);
    return resource.getFile();
}