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:uk.dsxt.voting.common.utils.PropertiesHelper.java

private static URL getResource(String fileName) {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    return loader.getResource(fileName);
}

From source file:org.apache.hadoop.yarn.applications.unmanagedamlauncher.TestUnmanagedAMLauncher.java

private static String getTestRuntimeClasspath() {
    LOG.info("Trying to generate classpath for app master from current thread's classpath");
    String envClassPath = "";
    String cp = System.getProperty("java.class.path");
    if (cp != null) {
        envClassPath += cp.trim() + File.pathSeparator;
    }/*from  w ww.  j a v  a2  s.c o m*/
    // yarn-site.xml at this location contains proper config for mini cluster
    ClassLoader thisClassLoader = Thread.currentThread().getContextClassLoader();
    URL url = thisClassLoader.getResource("yarn-site.xml");
    envClassPath += new File(url.getFile()).getParent();
    return envClassPath;
}

From source file:Main.java

public static <T> Set<Class<T>> findClassesAssignableFrom(String packageName, Class<T> assignableFrom)
        throws IOException, ClassNotFoundException {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    Set<Class<T>> classes = new HashSet<Class<T>>();
    String path = packageName.replace('.', '/');
    URL resource = loader.getResource(path);
    if (resource != null) {
        String filePath = resource.getFile();
        if (filePath != null && new File(filePath).isDirectory()) {
            for (String file : new File(filePath).list()) {
                if (file.endsWith(".class")) {
                    String name = packageName + '.' + file.substring(0, file.indexOf(".class"));
                    Class<T> clazz = (Class<T>) Class.forName(name);
                    if (assignableFrom.isAssignableFrom(clazz))
                        classes.add(clazz);
                }/*w w  w  .  j  a  v  a2  s .com*/
            }
        }
    }
    return classes;
}

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

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

From source file:com.boundary.sdk.metric.MeasurementTest.java

public static Measurement read(String resource) throws URISyntaxException {
    Measurement instance = new Measurement();

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

    ObjectMapper mapper = new ObjectMapper();

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

From source file:com.cloud.utils.PropertiesUtil.java

public static InputStream openStreamFromURL(String path) {
    ClassLoader cl = PropertiesUtil.class.getClassLoader();
    URL url = cl.getResource(path);
    if (url != null) {
        try {//w ww .  j  ava2 s. c o m
            InputStream stream = url.openStream();
            return stream;
        } catch (IOException ioex) {
            return null;
        }
    }
    return null;
}

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

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

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

From source file:info.extensiblecatalog.OAIToolkit.utils.ConfigUtil.java

public static PropertiesConfiguration load(String configFileName) throws Exception {
    prglog.info("[PRG] ConfigUtil::load(" + configFileName + ")");
    ClassLoader cloader = ConfigUtil.class.getClassLoader();
    URL configFile = cloader.getResource(configFileName);
    if (null == configFile) {
        File f = new File(configFileName);
        prglog.info("[PRG] load from file: " + f.getAbsolutePath());
        configFile = new URL("file", "", f.getAbsolutePath());
    }//from  www  .j  a  v a  2 s .c  om
    prglog.info("config file: " + configFile.getPath());
    //System.out.println("The config file is " + configFile);
    //System.out.println("The config file name is " + configFileName);

    if (!(new File(configFile.getPath()).exists())) {
        prglog.error("[PRG] Inexistent configuration file: " + configFileName);
        throw new Exception("Inexistent configuration file: " + configFileName);
    }

    BufferedReader re = new BufferedReader(new FileReader(configFile.getPath()));
    /*        String tmpfile = "tmpfile.txt";
            // Create new file
            File temp = new File(tmpfile);
            
            boolean success = temp.createNewFile();
            if (success) {
     System.out.println("Its success");
    //File did not exist and was created
            } else {
     System.out.println("File already exists");
     //File already exists
            }
            
            
             if (!temp.exists())
    throw new IllegalArgumentException("no such file or directory: " + temp);
            
            if (!temp.canWrite())
    throw new IllegalArgumentException("Write protected: " + temp);
            
            System.out.println("Temporary file created. File is " + temp);
            //System.out.println("Temporary file created. Path is " + temp.getPath());
            
            BufferedWriter out = new BufferedWriter(new FileWriter(temp));
            while(true)
            {
    String s = re.readLine();
    //System.out.println(s);
    if(s!=null) {
        s = s.replaceAll("\\\\", "/");
        //System.out.println(s);
        out.write(s + System.getProperty("line.separator"));
        out.flush();
    }
    else
        break;
            }
            out.close();
    */
    try {
        //PropertiesConfiguration prop = new PropertiesConfiguration(temp);
        PropertiesConfiguration prop = new PropertiesConfiguration(configFile.getPath());
        prglog.info("[PRG] successful ConfigUtil::load");
        //temp.deleteOnExit();
        /*boolean dsuccess = temp.delete();
        if (!dsuccess)
        throw new IllegalArgumentException("Delete: deletion failed");*/
        return prop;
    } catch (ConfigurationException e) {
        prglog.error("[PRG] Unable to load properties from configuration file: " + configFileName + ". "
                + e.getMessage());
        throw new Exception(
                "Unable to load properties from configuration file: " + configFileName + ". " + e.getMessage());
    }
}

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

/**
 * Save the configuration file {@code config/sudplan3D.xml} from the
 * resources to the the file {@code file}. Adding the additional properties
 * {@code sudplan3D.user.dir} and {@code sudplan3D.working.dir}.
 *
 * @param file the configuration {@link File}
 * @param userHomeDir the user home directory
 * @param workingDir the working directory
 *//*from ww w .  ja v a 2s  .  c  o  m*/
private static void installConfigFile(File file, String userHomeDir, String workingDir) {
    log.debug("Installing configuration to {}...", file.getAbsoluteFile());
    try {
        ClassLoader loader = Configuration.class.getClassLoader();
        URL url = loader.getResource("config/sudplan3D.xml");
        XMLConfiguration xmlInitialConfig = new XMLConfiguration(url);
        xmlInitialConfig.addProperty("sudplan3D.user.dir", userHomeDir);
        xmlInitialConfig.addProperty("sudplan3D.working.dir", workingDir);
        xmlInitialConfig.save(file);
    } catch (ConfigurationException ex) {
        log.error(ex.toString());
    }
}

From source file:net.fabricmc.installer.installer.MultiMCInstaller.java

private static String readBaseJson() throws IOException {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    String string = Resources.toString(classLoader.getResource("multimcPatch.json"), StandardCharsets.UTF_8);
    return string;
}