Example usage for java.lang ClassLoader getResourceAsStream

List of usage examples for java.lang ClassLoader getResourceAsStream

Introduction

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

Prototype

public InputStream getResourceAsStream(String name) 

Source Link

Document

Returns an input stream for reading the specified resource.

Usage

From source file:jamm.tools.JammCleaner.java

/**
 * Load properties from jammCleaner.properties in the classpath.
 * Command line options override this.//  ww  w.ja  v a 2  s  .co m
 */
private static void loadProperties() {
    ClassLoader classLoader = JammCleaner.class.getClassLoader();
    InputStream is = classLoader.getResourceAsStream("jammCleaner.properties");
    if (is == null) {
        return;
    }

    Properties prop = new Properties();
    try {
        prop.load(is);
    } catch (IOException e) {
        LOG.error("Error reading properties file", e);
    }

    String tmp = prop.getProperty("jammCleaner.ldap.search_base");
    if (tmp != null) {
        JammCleanerOptions.setBaseDn(tmp);
    }

    tmp = prop.getProperty("jammCleaner.ldap.bind_dn");
    if (tmp != null) {
        JammCleanerOptions.setBindDn(tmp);
    }

    tmp = prop.getProperty("jammCleaner.ldap.password");
    if (tmp != null) {
        JammCleanerOptions.setPassword(tmp);
    }

    tmp = prop.getProperty("jammCleaner.ldap.host");
    if (tmp != null) {
        JammCleanerOptions.setHost(tmp);
    }

    tmp = prop.getProperty("jammCleaner.ldap.port");
    if (tmp != null) {
        JammCleanerOptions.setPort(Integer.parseInt(tmp));
    }

    tmp = prop.getProperty("jammCleaner.backup_dir");
    if (tmp != null) {
        JammCleanerOptions.setBackupDirectory(tmp);
    }

}

From source file:com.ibm.soatf.component.soap.builder.ResourceUtils.java

public static InputStream getResourceAsInputStream(Class<?> clazz, String resourcePath) {
    if (clazz == null) {
        throw new NullPointerException("clazz cannot be null");
    }//from ww w .ja  v a 2  s . c  o  m
    InputStream resource = clazz.getClass().getResourceAsStream(resourcePath);
    // second attempt - servlet container - inside application lib folder
    if (resource == null) {
        ClassLoader classLoader = clazz.getClass().getClassLoader();
        if (classLoader != null)
            resource = classLoader.getResourceAsStream(resourcePath);
    }
    if (resource == null) {
        throw new IllegalArgumentException(String.format("Resource [%s] loading failed", resourcePath));
    }
    return resource;
}

From source file:edu.wisc.nexus.auth.rut.realm.NexusSecurityTestCaseSupport.java

public static void copyFile(File destDir, String dir, String filename) throws IOException {
    final File confDir;
    if (dir != null && dir.length() > 0) {
        if (!dir.endsWith("/")) {
            dir = dir + "/";
        }//from w  ww.j  av a  2s.  co m

        confDir = new File(destDir, dir);
        confDir.mkdirs();
    } else {
        dir = "";
        confDir = destDir;
    }

    final Thread currentThread = Thread.currentThread();
    final ClassLoader contextClassLoader = currentThread.getContextClassLoader();
    final String src = dir + filename;
    final InputStream fileStream = contextClassLoader.getResourceAsStream(src);
    final File dest = new File(confDir, filename);
    try {
        FileUtils.copyStreamToFile(new RawInputStreamFacade(fileStream), dest);
    } catch (Exception e) {
        throw new IOException("Failed to copy " + src + " to " + dest, e);
    } finally {
        IOUtils.closeQuietly(fileStream);
    }
}

From source file:com.ibm.soatf.component.soap.builder.ResourceUtils.java

public static InputStream getResourceWithAbsolutePackagePathAsStream(Class<?> clazz, String absolutePackagePath,
        String resourceName) {/*w w w. ja  va  2  s.c  o  m*/
    if (clazz == null) {
        throw new NullPointerException("clazz cannot be null");
    }
    String resourcePath = getResourcePath(absolutePackagePath, resourceName);
    InputStream resource = null;
    // first attempt - outside/inside jar file
    resource = clazz.getClass().getResourceAsStream(resourcePath);
    // second attempt - servlet container - inside application lib folder
    if (resource == null) {
        ClassLoader classLoader = clazz.getClass().getClassLoader();
        if (classLoader != null)
            resource = classLoader.getResourceAsStream(resourcePath);
    }
    if (resource == null) {
        throw new IllegalArgumentException(String.format("Resource [%s] loading failed", resourcePath));
    }
    return resource;
}

From source file:com.feilong.core.util.PropertiesUtil.java

/**
 * klassClassLoader,ClassLoader ?Properties.
 * //from   www .java 2s  . c o m
 * <h3> <code>propertiesPath</code>:</h3>
 * <blockquote>
 * ? class {@link PropertiesUtil},? src/main/resources?, messages/feilong-core-message_en_US.properties<br>
 * 
 * <p>
 * ?
 * <code>getPropertiesWithClassLoader(PropertiesUtil.class,<b>"messages/feilong-core-message_en_US.properties"</b>)</code>
 * <br>
 * ??? <b>"messages/feilong-core-message_en_US.properties"</b> (??"/"),ClassLoader
 * JVMBootstrapLoader?.?
 * </p>
 * 
 * <p>
 * ? {@link #getProperties(Class, String)} 
 * </p>
 * </blockquote>
 *
 * @param klass
 *             klass  ClassLoader,? getResourceAsStream
 * @param propertiesPath
 *            the properties path
 * @return  <code>klass</code> null, {@link NullPointerException}<br>
 *          <code>propertiesPath</code> null, {@link NullPointerException}<br>
 *          <code>propertiesPath</code> blank, {@link IllegalArgumentException}<br>
 * @see ClassLoaderUtil#getClassLoaderByClass(Class)
 * @see java.lang.ClassLoader#getResourceAsStream(String)
 * @see #getProperties(InputStream)
 * @see #getProperties(Class, String)
 */
public static Properties getPropertiesWithClassLoader(Class<?> klass, String propertiesPath) {
    Validate.notBlank(propertiesPath, "propertiesPath can't be blank!");
    ClassLoader classLoader = null;

    //ClassLoaderUtil.getClassLoaderByClass(klass);
    return getProperties(classLoader.getResourceAsStream(propertiesPath));
}

From source file:edu.harvard.i2b2.fhir.Utils.java

public static InputStream getInputStream(String fileName) {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    return classLoader.getResourceAsStream(fileName);

}

From source file:org.apache.falcon.ExtensionHandler.java

public static List<Entity> prepare(String extensionName, String jobName, InputStream configStream,
        List<URL> urls) throws IOException, FalconException {
    ClassLoader extensionClassLoader = ExtensionClassLoader.load(urls);
    if (extensionClassLoader.getResourceAsStream(EXTENSION_BUILDER_INTERFACE_SERVICE_FILE) == null) {
        throw new FalconCLIException(
                "The extension build time jars do not contain " + EXTENSION_BUILDER_INTERFACE_SERVICE_FILE);
    }//from ww  w  . j  av  a 2  s . c  o  m
    ExtensionHandler extensionHandler = new ExtensionHandler();

    return extensionHandler.getEntities(extensionClassLoader, extensionName, jobName, configStream);
}

From source file:abfab3d.opencl.ProgramLoader.java

public static InputStream getStreamFor(String filename) {
    InputStream is = ProgramLoader.class.getResourceAsStream(filename);
    if (is != null) {
        if (DEBUG)
            printf("abfab3d.opencl.ProgramLoader.getStreamFor(%s) found in resources returns: %s \n", filename,
                    is);/*from  ww  w.j a  v  a 2 s . c o  m*/
        return is;
    }

    // resources not found     
    String path = "classes" + File.separator + filename;
    //printf("Loading openCL Script: %s\n", path);
    try {

        FileInputStream fis = new FileInputStream(path);
        if (DEBUG)
            printf("abfab3d.opencl.ProgramLoader.getStreamFor(%s) found in files returns: %s\n", filename, fis);

        return fis;
    } catch (IOException ioe) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        InputStream input = classLoader.getResourceAsStream(filename);

        if (DEBUG)
            printf("abfab3d.opencl.ProgramLoader.getStreamFor(%s) found in classLoader returns: %s \n",
                    filename, input);

        return input;
    }

}

From source file:com.coky.user.util.ConfigHelper.java

/**
 * ???Properties/*from   ww w  .  java 2 s  .com*/
 * @param filename ??
 */
public static void loadProperties(String filename) {
    InputStream in = null;
    ClassLoader threadContextClassLoader = Thread.currentThread().getContextClassLoader();
    properties = new Properties();
    if (threadContextClassLoader != null) {
        in = threadContextClassLoader.getResourceAsStream(filename);
    }
    if (in == null) {
        in = ConfigHelper.class.getResourceAsStream(filename);
        if (in == null) {
            log.warn("No properties file found in the classpath by filename " + filename);
        }
    } else {
        try {
            properties.load(in);
            log.info("Properties read " + properties);
        } catch (Exception e) {
            log.error("Error reading from " + filename, e);
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                log.warn("IOException while closing InputStream: " + e.getMessage());
            }
        }
    }
}

From source file:com.jagornet.dhcp.db.DbSchemaManager.java

public static List<String> getSchemaDDL(String schemaFilename) throws IOException {
    List<String> schema = new ArrayList<String>();
    FileReader fr = null;// w  w w  .j ava 2 s .  com
    BufferedReader br = null;
    try {
        StringBuilder ddl = new StringBuilder();
        if (JagornetDhcpServer.springBootStrategy) {
            String cpFileName = schemaFilename.substring(schemaFilename.lastIndexOf("/") + 1,
                    schemaFilename.length());
            ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
            InputStream res = classLoader.getResourceAsStream(cpFileName);
            br = new BufferedReader(new InputStreamReader(res, "UTF-8"));
        } else {
            fr = new FileReader(schemaFilename);
            br = new BufferedReader(fr);
        }
        String line = br.readLine();
        while (line != null) {
            if (!line.startsWith("-- ")) {
                ddl.append(line);
            }
            if (ddl.toString().endsWith(";")) {
                ddl.setLength(ddl.length() - 1);
                schema.add(ddl.toString());
                ddl.setLength(0);
            }
            line = br.readLine();
        }
    } finally {
        if (br != null) {
            br.close();
        }
        if (fr != null) {
            fr.close();
        }
    }
    return schema;
}