private InputStream getInputStream(String location) throws IOException { URL url = ResourceUtils.getURL(location); return url.openStream(); }
private KeyStore loadKeyStore(String type, String provider, String resource, String password) throws Exception { type = (type != null) ? type : "JKS"; if (resource == null) { return null; } KeyStore store = (provider != null) ? KeyStore.getInstance(type, provider) : KeyStore.getInstance(type); URL url = ResourceUtils.getURL(resource); store.load(url.openStream(), (password != null) ? password.toCharArray() : null); return store; }
private KeyStore loadKeyStore(String type, String provider, String resource, String password) throws Exception { type = (type != null) ? type : "JKS"; if (resource == null) { return null; } KeyStore store = (provider != null) ? KeyStore.getInstance(type, provider) : KeyStore.getInstance(type); URL url = ResourceUtils.getURL(resource); store.load(url.openStream(), (password != null) ? password.toCharArray() : null); return store; }
@Nullable private static KeyStore loadKeyStore( @Nullable String type, @Nullable String resource, @Nullable String password) throws IOException, GeneralSecurityException { if (resource == null) { return null; } final KeyStore store = KeyStore.getInstance(firstNonNull(type, "JKS")); final URL url = ResourceUtils.getURL(resource); store.load(url.openStream(), password != null ? password.toCharArray() : null); return store; } }
protected void loadConfiguration(String location, LogFile logFile) { Assert.notNull(location, "Location must not be null"); try { String configuration = FileCopyUtils.copyToString( new InputStreamReader(ResourceUtils.getURL(location).openStream())); if (logFile != null) { configuration = configuration.replace("${LOG_FILE}", StringUtils.cleanPath(logFile.toString())); } LogManager.getLogManager().readConfiguration( new ByteArrayInputStream(configuration.getBytes())); } catch (Exception ex) { throw new IllegalStateException( "Could not initialize Java logging from " + location, ex); } }
public X509Certificate[] getTrustedX509CertificatesForTrustManager() { try { CertificateFactory certificateFactory = CertificateFactory .getInstance("X.509"); ArrayList<Certificate> allCerts = new ArrayList<>(); for (String trustedCert : ssl.getTrustedX509Certificates()) { try { URL url = ResourceUtils.getURL(trustedCert); Collection<? extends Certificate> certs = certificateFactory .generateCertificates(url.openStream()); allCerts.addAll(certs); } catch (IOException e) { throw new WebServerException( "Could not load certificate '" + trustedCert + "'", e); } } return allCerts.toArray(new X509Certificate[allCerts.size()]); } catch (CertificateException e1) { throw new WebServerException("Could not load CertificateFactory X.509", e1); } }
private void initializeSystem(ConfigurableEnvironment environment, LoggingSystem system, LogFile logFile) { LoggingInitializationContext initializationContext = new LoggingInitializationContext( environment); String logConfig = environment.getProperty(CONFIG_PROPERTY); if (ignoreLogConfig(logConfig)) { system.initialize(initializationContext, null, logFile); } else { try { ResourceUtils.getURL(logConfig).openStream().close(); system.initialize(initializationContext, logConfig, logFile); } catch (Exception ex) { // NOTE: We can't use the logger here to report the problem System.err.println("Logging system failed to initialize " + "using configuration from '" + logConfig + "'"); ex.printStackTrace(System.err); throw new IllegalStateException(ex); } } }
@Override protected void loadConfiguration(LoggingInitializationContext initializationContext, String location, LogFile logFile) { super.loadConfiguration(initializationContext, location, logFile); LoggerContext loggerContext = getLoggerContext(); stopAndReset(loggerContext); try { configureByResourceUrl(initializationContext, loggerContext, ResourceUtils.getURL(location)); } catch (Exception ex) { throw new IllegalStateException( "Could not initialize Logback logging from " + location, ex); } List<Status> statuses = loggerContext.getStatusManager().getCopyOfStatusList(); StringBuilder errors = new StringBuilder(); for (Status status : statuses) { if (status.getLevel() == Status.ERROR) { errors.append((errors.length() > 0) ? String.format("%n") : ""); errors.append(status.toString()); } } if (errors.length() > 0) { throw new IllegalStateException( String.format("Logback configuration error detected: %n%s", errors)); } }
protected void loadConfiguration(String location, LogFile logFile) { Assert.notNull(location, "Location must not be null"); try { LoggerContext ctx = getLoggerContext(); URL url = ResourceUtils.getURL(location); ConfigurationSource source = getConfigurationSource(url); ctx.start(ConfigurationFactory.getInstance().getConfiguration(ctx, source)); } catch (Exception ex) { throw new IllegalStateException( "Could not initialize Log4J2 logging from " + location, ex); } }
private void configureSslKeyStore(AbstractHttp11JsseProtocol<?> protocol, Ssl ssl) { try { protocol.setKeystoreFile(ResourceUtils.getURL(ssl.getKeyStore()).toString()); } catch (FileNotFoundException ex) { throw new WebServerException("Could not load key store: " + ex.getMessage(), ex); } if (ssl.getKeyStoreType() != null) { protocol.setKeystoreType(ssl.getKeyStoreType()); } if (ssl.getKeyStoreProvider() != null) { protocol.setKeystoreProvider(ssl.getKeyStoreProvider()); } }
private void configureSslTrustStore(AbstractHttp11JsseProtocol<?> protocol, Ssl ssl) { if (ssl.getTrustStore() != null) { try { protocol.setTruststoreFile( ResourceUtils.getURL(ssl.getTrustStore()).toString()); } catch (FileNotFoundException ex) { throw new WebServerException( "Could not load trust store: " + ex.getMessage(), ex); } } protocol.setTruststorePass(ssl.getTrustStorePassword()); if (ssl.getTrustStoreType() != null) { protocol.setTruststoreType(ssl.getTrustStoreType()); } if (ssl.getTrustStoreProvider() != null) { protocol.setTruststoreProvider(ssl.getTrustStoreProvider()); } }
private void configureSslKeyStore(SslContextFactory factory, Ssl ssl) { try { URL url = ResourceUtils.getURL(ssl.getKeyStore()); factory.setKeyStoreResource(Resource.newResource(url)); } catch (IOException ex) { throw new WebServerException( "Could not find key store '" + ssl.getKeyStore() + "'", ex); } if (ssl.getKeyStoreType() != null) { factory.setKeyStoreType(ssl.getKeyStoreType()); } if (ssl.getKeyStoreProvider() != null) { factory.setKeyStoreProvider(ssl.getKeyStoreProvider()); } }
private void configureSslTrustStore(SslContextFactory factory, Ssl ssl) { if (ssl.getTrustStorePassword() != null) { factory.setTrustStorePassword(ssl.getTrustStorePassword()); } if (ssl.getTrustStore() != null) { try { URL url = ResourceUtils.getURL(ssl.getTrustStore()); factory.setTrustStoreResource(Resource.newResource(url)); } catch (IOException ex) { throw new WebServerException( "Could not find trust store '" + ssl.getTrustStore() + "'", ex); } } if (ssl.getTrustStoreType() != null) { factory.setTrustStoreType(ssl.getTrustStoreType()); } if (ssl.getTrustStoreProvider() != null) { factory.setTrustStoreProvider(ssl.getTrustStoreProvider()); } }
/** * Initialize log4j from the given file location, with no config file refreshing. * Assumes an XML file in case of a ".xml" file extension, and a properties file * otherwise. * @param location the location of the config file: either a "classpath:" location * (e.g. "classpath:myLog4j.properties"), an absolute file URL * (e.g. "file:C:/log4j.properties), or a plain absolute path in the file system * (e.g. "C:/log4j.properties") * @throws FileNotFoundException if the location specifies an invalid file path */ public static void initLogging(String location) throws FileNotFoundException { String resolvedLocation = SystemPropertyUtils.resolvePlaceholders(location); URL url = ResourceUtils.getURL(resolvedLocation); if (resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) { DOMConfigurator.configure(url); } else { PropertyConfigurator.configure(url); } }
private void reinitializeLoggingSystem(ConfigurableEnvironment environment, String oldLogConfig, LogFile oldLogFile) { Map<String, Object> props = Binder.get(environment) .bind("logging", Bindable.mapOf(String.class, Object.class)).orElseGet(Collections::emptyMap); if (!props.isEmpty()) { String logConfig = environment.resolvePlaceholders("${logging.config:}"); LogFile logFile = LogFile.get(environment); LoggingSystem system = LoggingSystem .get(LoggingSystem.class.getClassLoader()); try { ResourceUtils.getURL(logConfig).openStream().close(); // Three step initialization that accounts for the clean up of the logging // context before initialization. Spring Boot doesn't initialize a logging // system that hasn't had this sequence applied (since 1.4.1). system.cleanUp(); system.beforeInitialize(); system.initialize(new LoggingInitializationContext(environment), logConfig, logFile); } catch (Exception ex) { PropertySourceBootstrapConfiguration.logger .warn("Error opening logging config file " + logConfig, ex); } } }
public static URL getResourceURL(String path) { try { return ResourceUtils.getURL(path.startsWith("/web/") ? "classpath:" + path : path); } catch (FileNotFoundException e) { throw new RuntimeException(e); } }
public static InputStream getInputStream(URL resourceLocation) throws IOException, URISyntaxException { InputStream result = null; if (FILE_SCHEME.equals(resourceLocation.getProtocol())) { final File f = new File(resourceLocation.toURI()); result = new FileInputStream(f); } else { result = ResourceUtils.getURL(resourceLocation.toString()).openStream(); } return result; }
public static InputStream getInputStream(URI resourceLocation) throws IOException, URISyntaxException { return getInputStream(ResourceUtils.getURL(resourceLocation.toString())); }
protected KeyPair loadKeyPair() throws FileNotFoundException { URL keystoreUrl = ResourceUtils.getURL(keyStore); return new KeyStoreKeyFactory(new UrlResource(keystoreUrl), keyStorePassword.toCharArray()) .getKeyPair(keyAlias, keyPassword.toCharArray()); }
/** * Sets the class loader. * * @throws FileNotFoundException if the location is not found. */ private void setClassLoader() throws FileNotFoundException { URL[] urls = new URL[] { ResourceUtils.getURL(location) }; classLoader = URLClassLoader.newInstance(urls, getDefaultClassLoader()); originalClassLoader = overrideThreadContextClassLoader(classLoader); log.debug("Set the class loader: location=[{}], classLoader=[{}]", location); }