List of usage examples for java.lang ClassLoader getResourceAsStream
public InputStream getResourceAsStream(String name)
From source file:org.apache.roller.weblogger.business.jpa.JPAPersistenceStrategy.java
/** * Loads properties from given resourceName using given class loader * @param resourceName The name of the resource containing properties * @param cl Classloeder to be used to locate the resouce * @return A properties object/*from ww w . ja va 2 s . c o m*/ * @throws WebloggerException */ private static Properties loadPropertiesFromResourceName(String resourceName, ClassLoader cl) throws WebloggerException { Properties props = new Properties(); InputStream in; in = cl.getResourceAsStream(resourceName); if (in == null) { //TODO: Check how i18n is done in roller throw new WebloggerException("Could not locate properties to load " + resourceName); } try { props.load(in); } catch (IOException ioe) { throw new WebloggerException("Could not load properties from " + resourceName); } finally { try { in.close(); } catch (IOException ioe) { } } return props; }
From source file:WarUtil.java
/** * ?????????????????//from w ww . j ava2 s. c o m * * @param path * @param caller * @return */ public static InputStream getResourceAsStream(String path, Class<?> caller) { String resource = path; if (resource.startsWith(SLASH)) { resource = resource.substring(1); } InputStream is = null; File file = new File(path); if (file.exists() && file.isFile()) { try { is = new FileInputStream(file); } catch (FileNotFoundException e) { } } if (is == null) { ClassLoader tcl = Thread.currentThread().getContextClassLoader(); if (tcl != null) { is = tcl.getResourceAsStream(resource); } } if (is == null) { is = caller.getResourceAsStream(path); } if (is == null) { is = ClassLoader.class.getResourceAsStream(path); } if (is == null) { is = ClassLoader.getSystemResourceAsStream(resource); } return is; }
From source file:org.apache.fop.hyphenation.Hyphenator.java
private static InputStream getResourceStream(String key) { InputStream is = null;// ww w . j a v a2 s. com // Try to use Context Class Loader to load the properties file. try { java.lang.reflect.Method getCCL = Thread.class.getMethod("getContextClassLoader", new Class[0]); if (getCCL != null) { ClassLoader contextClassLoader = (ClassLoader) getCCL.invoke(Thread.currentThread(), new Object[0]); is = contextClassLoader.getResourceAsStream("hyph/" + key + ".hyp"); } } catch (Exception e) { //ignore, fallback further down } if (is == null) { is = Hyphenator.class.getResourceAsStream("/hyph/" + key + ".hyp"); } return is; }
From source file:org.apache.roller.planet.business.jpa.JPAPersistenceStrategy.java
/** * Loads properties from given resourceName using given class loader * @param resourceName The name of the resource containing properties * @param cl Classloeder to be used to locate the resouce * @return A properties object//from w ww . j a v a2s . c om * @throws PlanetException */ protected static Properties loadPropertiesFromResourceName(String resourceName, ClassLoader cl) throws PlanetException { Properties props = new Properties(); InputStream in = null; in = cl.getResourceAsStream(resourceName); if (in == null) { //TODO: Check how i18n is done in roller throw new PlanetException("Could not locate properties to load " + resourceName); } try { props.load(in); } catch (IOException ioe) { throw new PlanetException("Could not load properties from " + resourceName); } finally { if (in != null) { try { in.close(); } catch (IOException ioe) { } } } return props; }
From source file:org.apache.bval.jsr.xml.ValidationParser.java
protected static InputStream getInputStream(final String path) throws IOException { final ClassLoader loader = Reflection.getClassLoader(ValidationParser.class); final InputStream inputStream = loader.getResourceAsStream(path); if (inputStream != null) { // spec says: If more than one META-INF/validation.xml file // is found in the classpath, a ValidationException is raised. final Enumeration<URL> urls = loader.getResources(path); if (urls.hasMoreElements()) { final String url = urls.nextElement().toString(); while (urls.hasMoreElements()) { if (!url.equals(urls.nextElement().toString())) { // complain when first duplicate found throw new ValidationException("More than one " + path + " is found in the classpath"); }// w w w . java2 s . c om } } } return IOs.convertToMarkableInputStream(inputStream); }
From source file:be.fedict.eid.applet.service.signer.time.TSPTimeStampService.java
private static X509Certificate loadCertificate(String resourceName) { LOG.debug("loading certificate: " + resourceName); Thread currentThread = Thread.currentThread(); ClassLoader classLoader = currentThread.getContextClassLoader(); InputStream certificateInputStream = classLoader.getResourceAsStream(resourceName); if (null == certificateInputStream) { throw new IllegalArgumentException("resource not found: " + resourceName); }/*from w ww. j a va2 s. c o m*/ try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); return (X509Certificate) certificateFactory.generateCertificate(certificateInputStream); } catch (CertificateException e) { throw new RuntimeException("X509 error: " + e.getMessage(), e); } }
From source file:com.knockturnmc.api.util.ConfigurationUtils.java
/** * Loads a plain file./*from w ww . j a v a 2 s . c om*/ * If the desired file is not found, a file with the same name will be copied from the classpath to the datafolder. * If no default file was found in the classloader's classpath, an empty file will be created. * * @param classLoader the classloader to use for the default file * @param file the filename to create/load * @param datafolder the datafolder to use * @return the loaded/created file * @throws IOException if something went wrong */ public static File getConfigFile(ClassLoader classLoader, String file, File datafolder) throws IOException { File config = new File(datafolder, file); datafolder.mkdirs(); if (!config.exists()) { logger.info("No configuration file found. Copying default configuration..."); try (InputStream in = classLoader.getResourceAsStream(file)) { if (in != null) { if (!config.createNewFile()) { logger.error("Failed creating default file."); throw new RuntimeException("Failed to create default file"); } try (OutputStream out = new FileOutputStream(config)) { IOUtils.copy(in, out); out.flush(); } } else { config.createNewFile(); } } } return config; }
From source file:org.apache.synapse.transport.amqp.AMQPTransportUtils.java
private static Properties loadProperties(String filePath) { Properties properties = new Properties(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (log.isDebugEnabled()) { log.debug("Loading a file '" + filePath + "' from classpath"); }//from w w w. j a va 2s.c o m InputStream in = cl.getResourceAsStream(filePath); if (in == null) { if (log.isDebugEnabled()) { log.debug("Unable to load file ' " + filePath + " '"); } filePath = "repository/conf" + File.separatorChar + filePath; if (log.isDebugEnabled()) { log.debug("Loading a file '" + filePath + "' from classpath"); } in = cl.getResourceAsStream(filePath); if (in == null) { if (log.isDebugEnabled()) { log.debug("Unable to load file ' " + filePath + " '"); } } } if (in != null) { try { properties.load(in); } catch (IOException e) { String msg = "Error loading properties from a file at :" + filePath; log.error(msg, e); } } return properties; }
From source file:com.flozano.socialauth.AuthProviderFactory.java
private static AuthProvider getProvider(final String id, final String fileName, final ClassLoader classLoader) throws Exception { Properties props = new Properties(); AuthProvider provider;/*from ww w. ja va2 s. c om*/ ClassLoader loader = null; if (classLoader != null) { loader = classLoader; } else { loader = AuthProviderFactory.class.getClassLoader(); } try { InputStream in = loader.getResourceAsStream(fileName); props.load(in); for (Object key : props.keySet()) { String str = key.toString(); if (str.startsWith("socialauth.")) { String val = str.substring("socialauth.".length()); registerProvider(val, Class.forName(props.get(str).toString())); } } } catch (NullPointerException ne) { throw new FileNotFoundException(fileName + " file is not found in your class path"); } catch (IOException ie) { throw new IOException("Could not load configuration from " + fileName); } provider = loadProvider(id, props); return provider; }
From source file:nl.ru.cmbi.vase.tools.util.ToolBox.java
static public List<String> getInsideContents(String fileName) { ClassLoader loader = ToolBox.class.getClassLoader(); InputStream stream = loader.getResourceAsStream(fileName); if (stream == null) { loader = ClassLoader.getSystemClassLoader(); stream = loader.getResourceAsStream(fileName); }// w w w . j a v a2s . c o m // stream = loader.getResourceAsStream("spring.xml"); StringBuffer contents = new StringBuffer(); List<String> lines = new ArrayList<String>(); BufferedReader input = null; try { input = new BufferedReader(new InputStreamReader(stream)); String line = null; // not declared within while loop while ((line = input.readLine()) != null) { lines.add(line); contents.append(line); contents.append(System.getProperty("line.separator")); } } catch (Exception e) { LOG.fatal(e); } finally { try { if (input != null) { input.close(); } } catch (Exception e) { LOG.warn(e); } } return lines; }