public LogConfig() throws SecurityException, IOException { LogManager.getLogManager().readConfiguration(ResourceUtil.getResourceAsStream("CWLogging.properties")); } }
/** * Generic resource URL fetcher * * @param search * The name of the resource (without a leading /) to find * @return The requested resource * @throws IOException * if there is a problem reading the file * @throws MissingResourceException * if the resource can not be found */ public static InputStream getResourceAsStream(String search) throws IOException, MissingResourceException { return getResourceAsStream(CallContext.getCallingClass(), search); }
/** * @param versification the name of the versification maps to the expected .properties file * @throws IOException error reading the mapping files * @throws ConfigException error parsing the contents of the file */ public FileVersificationMapping(Versification versification) throws IOException, ConfigException { //TODO(CJB): deal with Missing Resource Exceptions InputStream s = ResourceUtil.getResourceAsStream(getClass(), versification.getName() + ".properties"); BufferedReader lineReader = new BufferedReader(new InputStreamReader(s)); String line; while ((line = lineReader.readLine()) != null) { if (line.length() == 0 || line.charAt(0) == '#') { continue; } int firstEqual = line.indexOf('='); if (firstEqual == -1) { this.addProperty(line, null); } else { this.addProperty(line.substring(0, firstEqual), line.substring(firstEqual + 1)); } } }
/** * Get and load an XML file from the classpath and a few other places into a * JDOM Document object. * * @param subject * The name of the desired resource (without any extension) * @return The requested resource * @throws IOException * if there is a problem reading the file * @throws JDOMException * If the resource is not valid XML */ public static Document getDocument(String subject) throws JDOMException, IOException { String resource = subject + FileUtil.EXTENSION_XML; InputStream in = ResourceUtil.getResourceAsStream(resource); log.debug("Loading {}.xml from classpath: [OK]", subject); // With JDom 1.x this passed true SAXBuilder builder = new SAXBuilder(XMLReaders.DTDVALIDATING); return builder.build(in); }
/** * Get and load a properties file from the writable area or if that fails * from the classpath (where a default ought to be stored) * * @param <T> the type of the resource * @param clazz * The name of the desired resource * @param subject * The name of the desired resource (without any extension) * @return The found and loaded properties file * @throws IOException * if the resource can not be loaded */ private static <T> PropertyMap getProperties(Class<T> clazz, String subject) throws IOException { try { String lookup = subject + FileUtil.EXTENSION_PROPERTIES; InputStream in = getResourceAsStream(clazz, lookup); PropertyMap prop = new PropertyMap(); prop.load(in); return prop; } catch (MissingResourceException e) { return new PropertyMap(); } } }
/** * Get and load a plugin file by looking it up as a resource. * * @param <T> the implementor's type * @param clazz * The name of the desired resource * @return The found and loaded plugin file * @throws IOException * if the resource can not be loaded * @throws MissingResourceException * if the resource can not be found */ public static <T> PropertyMap getPlugin(Class<T> clazz) throws IOException { String subject = ClassUtil.getShortClassName(clazz); try { String lookup = subject + PluginUtil.EXTENSION_PLUGIN; InputStream in = ResourceUtil.getResourceAsStream(clazz, lookup); PropertyMap prop = new PropertyMap(); prop.load(in); return prop; } catch (MissingResourceException e) { return new PropertyMap(); } }