org.springframework.util.ResourceUtils.getURL()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(796)

本文整理了Java中org.springframework.util.ResourceUtils.getURL方法的一些代码示例,展示了ResourceUtils.getURL的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ResourceUtils.getURL方法的具体详情如下:
包路径:org.springframework.util.ResourceUtils
类名称:ResourceUtils
方法名:getURL

ResourceUtils.getURL介绍

[英]Resolve the given resource location to a java.net.URL.

Does not check whether the URL actually exists; simply returns the URL that the given location would correspond to.
[中]将给定的资源位置解析为java。网网址。
不检查URL是否实际存在;只返回给定位置对应的URL。

代码示例

代码示例来源:origin: spring-io/initializr

private InputStream getInputStream(String location) throws IOException {
  URL url = ResourceUtils.getURL(location);
  return url.openStream();
}

代码示例来源:origin: org.springframework.boot/spring-boot

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;
}

代码示例来源:origin: org.springframework.boot/spring-boot

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;
}

代码示例来源:origin: line/armeria

@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;
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

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);
  }
}

代码示例来源:origin: spring-cloud/spring-cloud-gateway

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);
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

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);
    }
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

@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));
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

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);
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

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());
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

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());
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

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());
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

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());
  }
}

代码示例来源:origin: camunda/camunda-bpm-platform

/**
 * 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);
  }
}

代码示例来源:origin: org.springframework.cloud/spring-cloud-context

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);
    }
  }
}

代码示例来源:origin: org.carewebframework/org.carewebframework.web.core

public static URL getResourceURL(String path) {
  try {
    return ResourceUtils.getURL(path.startsWith("/web/") ? "classpath:" + path : path);
  } catch (FileNotFoundException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: com.ebmwebsourcing.webcommons/web-commons-util

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;
}

代码示例来源:origin: com.ebmwebsourcing.webcommons/web-commons-util

public static InputStream getInputStream(URI resourceLocation) throws IOException,
    URISyntaxException {
  return getInputStream(ResourceUtils.getURL(resourceLocation.toString()));
}

代码示例来源:origin: pl.touk.widerest/widerest-api

protected KeyPair loadKeyPair() throws FileNotFoundException {
  URL keystoreUrl = ResourceUtils.getURL(keyStore);
  return new KeyStoreKeyFactory(new UrlResource(keystoreUrl), keyStorePassword.toCharArray())
      .getKeyPair(keyAlias, keyPassword.toCharArray());
}

代码示例来源:origin: net.rakugakibox.spring.boot/logback-access-spring-boot-starter

/**
 * 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);
}

相关文章