protected InputStream loadResource(String resourceName) throws IOException { return IOUtils.getResourceAsStream(resourceName, getClass()); }
private Collection<? extends CRL> loadCRL(String crlPath) throws CertificateException, IOException, CRLException { Collection<? extends CRL> crlList = null; if (crlPath != null) { InputStream in = null; try { in = IOUtils.getResourceAsStream(crlPath, getClass()); crlList = CertificateFactory.getInstance("X.509").generateCRLs(in); } finally { if (in != null) { in.close(); } } } return crlList; }
@Test public void testLoadingResourcesAsStream() throws Exception { InputStream is = IOUtils.getResourceAsStream("log4j2-test.xml", getClass(), false, false); assertNotNull(is); is = IOUtils.getResourceAsStream("does-not-exist.properties", getClass(), false, false); assertNull(is); }
/** * Attempts to load a resource from the file system, from a URL, or from the classpath, in that order. * * @param resourceName The name of the resource to load * @param callingClass The Class object of the calling object * @return an InputStream to the resource or null if resource not found * @throws java.io.IOException IO error */ public static InputStream getResourceAsStream(final String resourceName, final Class callingClass) throws IOException { return getResourceAsStream(resourceName, callingClass, true, true); }
private Collection<? extends CRL> loadCRL(String crlPath) throws CertificateException, IOException, CRLException { Collection<? extends CRL> crlList = null; if (crlPath != null) { InputStream in = null; try { in = IOUtils.getResourceAsStream(crlPath, getClass()); crlList = CertificateFactory.getInstance("X.509").generateCRLs(in); } finally { if (in != null) { in.close(); } } } return crlList; }
private KeyStore loadKeyStore() throws GeneralSecurityException, IOException { KeyStore tempKeyStore = KeyStore.getInstance(keystoreType); InputStream is = IOUtils.getResourceAsStream(keyStoreName, getClass()); if (null == is) { throw new FileNotFoundException(cannotLoadFromClasspath("Keystore: " + keyStoreName).getMessage()); } tempKeyStore.load(is, keyStorePassword.toCharArray()); return tempKeyStore; }
/** * Attempts to load a resource from the file system, from a URL, or from the classpath, in that order. * * @param resourceName The name of the resource to load * @param callingClass The Class object of the calling object * @return the requested resource as a string * @throws java.io.IOException IO error */ public static String getResourceAsString(final String resourceName, final Class callingClass) throws IOException { try (InputStream is = getResourceAsStream(resourceName, callingClass)) { if (is != null) { return toString(is); } else { throw new IOException("Unable to load resource " + resourceName); } } }
private KeyStore createTrustStore() throws CreateException { trustStorePassword = null == trustStorePassword ? "" : trustStorePassword; KeyStore trustStore; try { trustStore = KeyStore.getInstance(trustStoreType); InputStream is = IOUtils.getResourceAsStream(trustStoreName, getClass()); if (null == is) { throw new FileNotFoundException( "Failed to load truststore from classpath or local file: " + trustStoreName); } trustStore.load(is, trustStorePassword.toCharArray()); } catch (Exception e) { throw new CreateException( failedToLoad("TrustStore: " + trustStoreName), e, this); } return trustStore; }
public void load(String fileName) { try { InputStream config = IOUtils.getResourceAsStream(fileName, TlsProperties.class); if (config == null) { logger.warn(String.format("File %s not found, using default configuration.", fileName)); } else { logger.info(String.format("Loading configuration file: %s", fileName)); Properties properties = loadProperties(config); String enabledCipherSuitesProperty = properties.getProperty("enabledCipherSuites"); String enabledProtocolsProperty = properties.getProperty("enabledProtocols"); String defaultProtocolProperty = properties.getProperty("defaultProtocol"); if (enabledCipherSuitesProperty != null) { enabledCipherSuites = StringUtils.splitAndTrim(enabledCipherSuitesProperty, ","); } if (enabledProtocolsProperty != null) { enabledProtocols = StringUtils.splitAndTrim(enabledProtocolsProperty, ","); } if (defaultProtocolProperty != null) { defaultProtocol = defaultProtocolProperty.trim(); } } } catch (IOException e) { logger.warn(String.format("Cannot read file %s, using default configuration", fileName), e); } }
/** * Read in the properties from a properties file. The file may be on the file system or the classpath. * * @param fileName - The name of the properties file * @param callingClass - The Class which is calling this method. This is used to determine the classpath. * @return a java.util.Properties object containing the properties. */ public static synchronized Properties loadProperties(String fileName, final Class<?> callingClass) throws IOException { InputStream is = IOUtils.getResourceAsStream(fileName, callingClass, /* tryAsFile */true, /* tryAsUrl */false); if (is == null) { I18nMessage error = CoreMessages.cannotLoadFromClasspath(fileName); throw new IOException(error.toString()); } return loadProperties(is); }
@Test public void testLoadingResourcesAsStream() throws Exception { InputStream is = IOUtils.getResourceAsStream("log4j2-test.xml", getClass(), false, false); assertNotNull(is); is = IOUtils.getResourceAsStream("does-not-exist.properties", getClass(), false, false); assertNull(is); }