Example usage for java.lang ClassLoader getSystemResourceAsStream

List of usage examples for java.lang ClassLoader getSystemResourceAsStream

Introduction

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

Prototype

public static InputStream getSystemResourceAsStream(String name) 

Source Link

Document

Open for reading, a resource of the specified name from the search path used to load classes.

Usage

From source file:de.unibi.techfak.bibiserv.BiBiTools.java

/**
 * publiv static helper method that search and load (if found) bibiserv
 * properties.//from  w  w  w  .  j av  a 2  s .c o m
 *
 * - Used by getProperty to load static properties variable with content,
 * during 1st time call. - Could be used to "reload" Properties content
 * during runtime (e.g. for reconfiguration).
 *
 * @return a Properties object
 *
 */
public static void loadBiBiProperties() throws BiBiToolsException {
    properties = new Properties();
    InputStream rin = null;

    /* first check if a System Property bibiserv2.property.location is set */
    log.info("Check for property 'bibiserv2.property.location'.");
    if (System.getProperty("de.unibi.techfak.bibiserv.config") != null) {
        try {
            rin = new FileInputStream(System.getProperty("de.unibi.techfak.bibiserv.config"));
        } catch (FileNotFoundException e) {
            // do nothing, in the case this exception occurs try second possibility
            log.warn(
                    "Property 'de.unibi.techfak.bibiserv.config' is set, but property value doesn't point to a xml configuration.");
        }
    }

    /* second, check if bibiserv property is located in domain root folder ${catalina.home}*/
    if (rin == null) {
        log.info("Check for ${catalina.home}/bibiserv_properties.xml");
        if (System.getProperty("catalina.home") != null) {
            try {
                rin = new FileInputStream(System.getProperty("catalina.home") + "/bibiserv_properties.xml");
            } catch (FileNotFoundException e) {
                // do nothing, in the case this exception occure try third possibility
            }
        }
    }

    /* third, check for bibiserv property in classpath (load as resource) */
    if (rin == null) {
        log.info("Check for bibiserv.properties in classpath!");
        rin = ClassLoader.getSystemResourceAsStream("bibiserv_properties.xml");
    }

    /* load properties from Inputstream */
    if (rin != null) {
        try {
            properties.loadFromXML(rin);
        } catch (IOException e) {
            log.fatal("Can't read BiBiServ Properties file!", e);
            throw new BiBiToolsException("Can't read BiBiServ Properties file!", e);
        }
    } else {
        throw new BiBiToolsException("BiBiServ Properties file not found!\n"
                + "1) set Java system property 'de.unibi.techfak.bibiserv.config'\n"
                + "2) place bibiserv_properties.xml in ${catalina.home} base folder\n"
                + "3) place bibiserv_properties.xml in Java classpath!");
    }

    // add Hostname of localhost to properties
    try {
        InetAddress addr = InetAddress.getLocalHost();
        properties.setProperty("hostname", addr.getHostName());
        log.info("Hostname is " + addr.getHostName() + " !");
    } catch (UnknownHostException e) {
        log.fatal("Fatal error occurred when detecting hostname of local machine\n" + e.getMessage());
        throw new RuntimeException(e);
    }

}

From source file:com.github.wshackle.java4cpp.J4CppMain.java

/**
 *
 * @param pw the value of pw/*ww w .ja v  a  2s .  c  om*/
 * @param map the value of map
 * @param resourceName the value of resourceName
 * @param tabs the value of tabs
 * @throws IOException
 */
private static void processTemplate(final PrintWriter pw, Map<String, String> map, String resourceName,
        String tabs) throws IOException {
    //                String arg = args[i];

    try (BufferedReader br = new BufferedReader(new InputStreamReader(
            ClassLoader.getSystemResourceAsStream(resourceName), StandardCharsets.UTF_8))) {

        String s = null;
        while (null != (s = br.readLine())) {
            if (!s.trim().startsWith(TEMPLATE_COMMENT_MARK)) {
                s = s.replace("\t", TAB_STRING);
                pw.println(tabs + replaceVars(map, s));
            }
        }
        //                    .filter(s -> !s.trim().startsWith(TEMPLATE_COMMENT_MARK))
        //                    .map(l -> l.replace("\t", TAB_STRING))
        //                    .forEach(l -> pw.println(tabs + replaceVars(map, l)));
    }
}