java.lang.ClassLoader.getSystemResources()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(12.1k)|赞(0)|评价(0)|浏览(334)

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

ClassLoader.getSystemResources介绍

[英]Returns an enumeration of URLs for the resource with the specified name. The system class loader's resource lookup algorithm is used to find the resource.
[中]

代码示例

代码示例来源:origin: aws/aws-sdk-java

private Enumeration<URL> getGlobalHandlerResources() throws IOException {
  // Classloader may be null if loaded by bootstrap classloader.
  if (HandlerChainFactory.class.getClassLoader() == null) {
    return ClassLoader.getSystemResources(GLOBAL_HANDLER_PATH);
  }
  return HandlerChainFactory.class.getClassLoader().getResources(GLOBAL_HANDLER_PATH);
}

代码示例来源:origin: jersey/jersey

private static Enumeration<URL> getResources(final String name) throws IOException {
  if (ServiceFinder.class.getClassLoader() != null) {
    return ServiceFinder.class.getClassLoader().getResources(name);
  } else {
    return ClassLoader.getSystemResources(name);
  }
}

代码示例来源:origin: jersey/jersey

private static Enumeration<URL> getResources(final String name) throws IOException {
  if (ServiceFinder.class.getClassLoader() != null) {
    return ServiceFinder.class.getClassLoader().getResources(name);
  } else {
    return ClassLoader.getSystemResources(name);
  }
}

代码示例来源:origin: spring-projects/spring-framework

private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
  MultiValueMap<String, String> result = cache.get(classLoader);
  if (result != null) {
    return result;
  }
  try {
    Enumeration<URL> urls = (classLoader != null ?
        classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
        ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
    result = new LinkedMultiValueMap<>();
    while (urls.hasMoreElements()) {
      URL url = urls.nextElement();
      UrlResource resource = new UrlResource(url);
      Properties properties = PropertiesLoaderUtils.loadProperties(resource);
      for (Map.Entry<?, ?> entry : properties.entrySet()) {
        String factoryClassName = ((String) entry.getKey()).trim();
        for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
          result.add(factoryClassName, factoryName.trim());
        }
      }
    }
    cache.put(classLoader, result);
    return result;
  }
  catch (IOException ex) {
    throw new IllegalArgumentException("Unable to load factories from location [" +
        FACTORIES_RESOURCE_LOCATION + "]", ex);
  }
}

代码示例来源:origin: spring-projects/spring-framework

ClassLoader.getSystemResources(resourceName));
Properties props = new Properties();
while (urls.hasMoreElements()) {

代码示例来源:origin: spring-projects/spring-framework

/**
 * Find all class location resources with the given path via the ClassLoader.
 * Called by {@link #findAllClassPathResources(String)}.
 * @param path the absolute path within the classpath (never a leading slash)
 * @return a mutable Set of matching Resource instances
 * @since 4.1.1
 */
protected Set<Resource> doFindAllClassPathResources(String path) throws IOException {
  Set<Resource> result = new LinkedHashSet<>(16);
  ClassLoader cl = getClassLoader();
  Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path));
  while (resourceUrls.hasMoreElements()) {
    URL url = resourceUrls.nextElement();
    result.add(convertClassLoaderURL(url));
  }
  if ("".equals(path)) {
    // The above result is likely to be incomplete, i.e. only containing file system references.
    // We need to have pointers to each of the jar files on the classpath as well...
    addAllClassLoaderJarRoots(cl, result);
  }
  return result;
}

代码示例来源:origin: redisson/redisson

static Set<URL> findPossibleStaticLoggerBinderPathSet() {
  // use Set instead of list in order to deal with bug #138
  // LinkedHashSet appropriate here because it preserves insertion order
  // during iteration
  Set<URL> staticLoggerBinderPathSet = new LinkedHashSet<URL>();
  try {
    ClassLoader loggerFactoryClassLoader = LoggerFactory.class.getClassLoader();
    Enumeration<URL> paths;
    if (loggerFactoryClassLoader == null) {
      paths = ClassLoader.getSystemResources(STATIC_LOGGER_BINDER_PATH);
    } else {
      paths = loggerFactoryClassLoader.getResources(STATIC_LOGGER_BINDER_PATH);
    }
    while (paths.hasMoreElements()) {
      URL path = paths.nextElement();
      staticLoggerBinderPathSet.add(path);
    }
  } catch (IOException ioe) {
    Util.report("Error getting resources from path", ioe);
  }
  return staticLoggerBinderPathSet;
}

代码示例来源:origin: commons-logging/commons-logging

public Object run() {
    try {
      if (loader != null) {
        return loader.getResources(name);
      } else {
        return ClassLoader.getSystemResources(name);
      }
    } catch (IOException e) {
      if (isDiagnosticsEnabled()) {
        logDiagnostic("Exception while trying to find configuration file " +
               name + ":" + e.getMessage());
      }
      return null;
    } catch (NoSuchMethodError e) {
      // we must be running on a 1.1 JVM which doesn't support
      // ClassLoader.getSystemResources; just return null in
      // this case.
      return null;
    }
  }
};

代码示例来源:origin: apache/incubator-dubbo

private void loadDirectory(Map<String, Class<?>> extensionClasses, String dir, String type) {
  String fileName = dir + type;
  try {
    Enumeration<java.net.URL> urls;
    ClassLoader classLoader = findClassLoader();
    if (classLoader != null) {
      urls = classLoader.getResources(fileName);
    } else {
      urls = ClassLoader.getSystemResources(fileName);
    }
    if (urls != null) {
      while (urls.hasMoreElements()) {
        java.net.URL resourceURL = urls.nextElement();
        loadResource(extensionClasses, classLoader, resourceURL);
      }
    }
  } catch (Throwable t) {
    logger.error("Exception occurred when loading extension class (interface: " +
        type + ", description file: " + fileName + ").", t);
  }
}

代码示例来源:origin: apache/incubator-dubbo

private void loadDirectory(Map<String, Class<?>> extensionClasses, String dir, String type) {
  String fileName = dir + type;
  try {
    Enumeration<java.net.URL> urls;
    ClassLoader classLoader = findClassLoader();
    if (classLoader != null) {
      urls = classLoader.getResources(fileName);
    } else {
      urls = ClassLoader.getSystemResources(fileName);
    }
    if (urls != null) {
      while (urls.hasMoreElements()) {
        java.net.URL resourceURL = urls.nextElement();
        loadResource(extensionClasses, classLoader, resourceURL);
      }
    }
  } catch (Throwable t) {
    logger.error("Exception occurred when loading extension class (interface: " +
        type + ", description file: " + fileName + ").", t);
  }
}

代码示例来源:origin: javax.activation/activation

public Object run() {
  URL[] ret = null;
  try {
    List v = new ArrayList();
    Enumeration e = ClassLoader.getSystemResources(name);
    while (e != null && e.hasMoreElements()) {
    URL url = (URL)e.nextElement();
    if (url != null)
      v.add(url);
    }
    if (v.size() > 0) {
    ret = new URL[v.size()];
    ret = (URL[])v.toArray(ret);
    }
  } catch (IOException ioex) {
  } catch (SecurityException ex) { }
  return ret;
  }
});

代码示例来源:origin: wildfly/wildfly

static Set<URL> findPossibleStaticLoggerBinderPathSet() {
  // use Set instead of list in order to deal with bug #138
  // LinkedHashSet appropriate here because it preserves insertion order
  // during iteration
  Set<URL> staticLoggerBinderPathSet = new LinkedHashSet<URL>();
  try {
    ClassLoader loggerFactoryClassLoader = LoggerFactory.class.getClassLoader();
    Enumeration<URL> paths;
    if (loggerFactoryClassLoader == null) {
      paths = ClassLoader.getSystemResources(STATIC_LOGGER_BINDER_PATH);
    } else {
      paths = loggerFactoryClassLoader.getResources(STATIC_LOGGER_BINDER_PATH);
    }
    while (paths.hasMoreElements()) {
      URL path = paths.nextElement();
      staticLoggerBinderPathSet.add(path);
    }
  } catch (IOException ioe) {
    Util.report("Error getting resources from path", ioe);
  }
  return staticLoggerBinderPathSet;
}

代码示例来源:origin: alibaba/fescar

urls = classLoader.getResources(fileName);
} else {
  urls = ClassLoader.getSystemResources(fileName);

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

private static Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader) {
  MultiValueMap<String, String> result = cache.get(classLoader);
  if (result != null) {
    return result;
  }
  try {
    Enumeration<URL> urls = (classLoader != null ?
        classLoader.getResources(FACTORIES_RESOURCE_LOCATION) :
        ClassLoader.getSystemResources(FACTORIES_RESOURCE_LOCATION));
    result = new LinkedMultiValueMap<>();
    while (urls.hasMoreElements()) {
      URL url = urls.nextElement();
      UrlResource resource = new UrlResource(url);
      Properties properties = PropertiesLoaderUtils.loadProperties(resource);
      for (Map.Entry<?, ?> entry : properties.entrySet()) {
        String factoryClassName = ((String) entry.getKey()).trim();
        for (String factoryName : StringUtils.commaDelimitedListToStringArray((String) entry.getValue())) {
          result.add(factoryClassName, factoryName.trim());
        }
      }
    }
    cache.put(classLoader, result);
    return result;
  }
  catch (IOException ex) {
    throw new IllegalArgumentException("Unable to load factories from location [" +
        FACTORIES_RESOURCE_LOCATION + "]", ex);
  }
}

代码示例来源:origin: robovm/robovm

public Object run() {
    try {
      if (loader != null) {
        return loader.getResources(name);
      } else {
        return ClassLoader.getSystemResources(name);
      }
    } catch(IOException e) {
      if (isDiagnosticsEnabled()) {
        logDiagnostic(
          "Exception while trying to find configuration file "
          + name + ":" + e.getMessage());
      }
      return null;
    } catch(NoSuchMethodError e) {
      // we must be running on a 1.1 JVM which doesn't support
      // ClassLoader.getSystemResources; just return null in
      // this case.
      return null;
    }
  }
};

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

ClassLoader.getSystemResources(resourceName));
Properties props = new Properties();
while (urls.hasMoreElements()) {

代码示例来源:origin: weibocom/motan

private ConcurrentMap<String, Class<T>> loadExtensionClasses(String prefix) {
  String fullName = prefix + type.getName();
  List<String> classNames = new ArrayList<String>();
  try {
    Enumeration<URL> urls;
    if (classLoader == null) {
      urls = ClassLoader.getSystemResources(fullName);
    } else {
      urls = classLoader.getResources(fullName);
    }
    if (urls == null || !urls.hasMoreElements()) {
      return new ConcurrentHashMap<String, Class<T>>();
    }
    while (urls.hasMoreElements()) {
      URL url = urls.nextElement();
      parseUrl(type, url, classNames);
    }
  } catch (Exception e) {
    throw new MotanFrameworkException(
        "ExtensionLoader loadExtensionClasses error, prefix: " + prefix + " type: " + type.getClass(), e);
  }
  return loadClass(classNames);
}

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

/**
 * Find all class location resources with the given path via the ClassLoader.
 * Called by {@link #findAllClassPathResources(String)}.
 * @param path the absolute path within the classpath (never a leading slash)
 * @return a mutable Set of matching Resource instances
 * @since 4.1.1
 */
protected Set<Resource> doFindAllClassPathResources(String path) throws IOException {
  Set<Resource> result = new LinkedHashSet<>(16);
  ClassLoader cl = getClassLoader();
  Enumeration<URL> resourceUrls = (cl != null ? cl.getResources(path) : ClassLoader.getSystemResources(path));
  while (resourceUrls.hasMoreElements()) {
    URL url = resourceUrls.nextElement();
    result.add(convertClassLoaderURL(url));
  }
  if ("".equals(path)) {
    // The above result is likely to be incomplete, i.e. only containing file system references.
    // We need to have pointers to each of the jar files on the classpath as well...
    addAllClassLoaderJarRoots(cl, result);
  }
  return result;
}

代码示例来源:origin: ltsopensource/light-task-scheduler

private static Set<URLDefinition> collectExtensionUrls(String resourceName, ClassLoader classLoader) {
  try {
    final Enumeration<URL> configs;
    if (classLoader != null) {
      configs = classLoader.getResources(resourceName);
    } else {
      configs = ClassLoader.getSystemResources(resourceName);
    }
    Set<URLDefinition> urlDefinitions = new HashSet<URLDefinition>();
    while (configs.hasMoreElements()) {
      URL url = configs.nextElement();
      final URI uri = url.toURI();
      ClassLoader highestClassLoader = findHighestReachableClassLoader(url, classLoader, resourceName);
      urlDefinitions.add(new URLDefinition(uri, highestClassLoader));
    }
    return urlDefinitions;
  } catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
  }
  return Collections.emptySet();
}

代码示例来源:origin: ltsopensource/light-task-scheduler

private static Set<URLDefinition> collectExtensionUrls(String resourceName, ClassLoader classLoader) {
  try {
    final Enumeration<URL> configs;
    if (classLoader != null) {
      configs = classLoader.getResources(resourceName);
    } else {
      configs = ClassLoader.getSystemResources(resourceName);
    }
    Set<URLDefinition> urlDefinitions = new HashSet<URLDefinition>();
    while (configs.hasMoreElements()) {
      URL url = configs.nextElement();
      final URI uri = url.toURI();
      ClassLoader highestClassLoader = findHighestReachableClassLoader(url, classLoader, resourceName);
      urlDefinitions.add(new URLDefinition(uri, highestClassLoader));
    }
    return urlDefinitions;
  } catch (Exception e) {
    LOGGER.error(e.getMessage(), e);
  }
  return Collections.emptySet();
}

相关文章