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

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

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

ClassLoader.getResources介绍

[英]Returns an enumeration of URLs for the resource with the specified name. This implementation first uses this class loader's parent to find the resource, then it calls #findResources(String) to get additional URLs. The returned enumeration contains the URL objects of both find operations.
[中]返回具有指定名称的资源的URL枚举。这个实现首先使用这个类加载器的父类来查找资源,然后调用#findResources(String)来获取其他URL。返回的枚举包含两个查找操作的URL对象。

代码示例

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

@Override
public Enumeration<URL> getResources(String name) throws IOException {
  return this.enclosingClassLoader.getResources(name);
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * Returns the URL of the index page jelly script.
 */
public URL getIndexPage() {
  // In the current impl dependencies are checked first, so the plugin itself
  // will add the last entry in the getResources result.
  URL idx = null;
  try {
    Enumeration<URL> en = classLoader.getResources("index.jelly");
    while (en.hasMoreElements())
      idx = en.nextElement();
  } catch (IOException ignore) { }
  // In case plugin has dependencies but is missing its own index.jelly,
  // check that result has this plugin's artifactId in it:
  return idx != null && idx.toString().contains(shortName) ? idx : null;
}

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

/**
 * {@inheritDoc}
 */
public Enumeration<URL> getResources(String name) throws IOException {
  List<Enumeration<URL>> enumerations = new ArrayList<Enumeration<URL>>(parents.size() + 1);
  for (ClassLoader parent : parents) {
    enumerations.add(parent.getResources(name));
  }
  enumerations.add(super.getResources(name));
  return new CompoundEnumeration(enumerations);
}

代码示例来源:origin: skylot/jadx

public static String getVersion() {
    try {
      ClassLoader classLoader = Jadx.class.getClassLoader();
      if (classLoader != null) {
        Enumeration<URL> resources = classLoader.getResources("META-INF/MANIFEST.MF");
        while (resources.hasMoreElements()) {
          Manifest manifest = new Manifest(resources.nextElement().openStream());
          String ver = manifest.getMainAttributes().getValue("jadx-version");
          if (ver != null) {
            return ver;
          }
        }
      }
    } catch (Exception e) {
      LOG.error("Can't get manifest file", e);
    }
    return "dev";
  }
}

代码示例来源:origin: apache/storm

public static List<URL> findResources(String name) {
  try {
    Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(name);
    List<URL> ret = new ArrayList<URL>();
    while (resources.hasMoreElements()) {
      ret.add(resources.nextElement());
    }
    return ret;
  } catch (IOException e) {
    throw new RuntimeException(e);
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
protected Enumeration<URL> findResources(String name) throws IOException {
  List<URL> resources = new ArrayList<URL>();
  if (FAST_LOOKUP) {
      for (PluginWrapper p : activePlugins) {
        resources.addAll(Collections.list(ClassLoaderReflectionToolkit._findResources(p.classLoader, name)));
      }
  } else {
    for (PluginWrapper p : activePlugins) {
      resources.addAll(Collections.list(p.classLoader.getResources(name)));
    }
  }
  return Collections.enumeration(resources);
}

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

private static void loadFilterConfig(Properties filterProperties, ClassLoader classLoader) throws IOException {
  if (classLoader == null) {
    return;
  }
  
  for (Enumeration<URL> e = classLoader.getResources("META-INF/druid-filter.properties"); e.hasMoreElements();) {
    URL url = e.nextElement();
    Properties property = new Properties();
    InputStream is = null;
    try {
      is = url.openStream();
      property.load(is);
    } finally {
      JdbcUtils.close(is);
    }
    filterProperties.putAll(property);
  }
}

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

@Override
public Enumeration<URL> getResources(String name) throws IOException {
  if (CandidateComponentsIndexLoader.COMPONENTS_RESOURCE_LOCATION.equals(name)) {
    if (this.resourceUrls != null) {
      return this.resourceUrls;
    }
    throw this.cause;
  }
  return super.getResources(name);
}

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

@Nullable
private static CandidateComponentsIndex doLoadIndex(ClassLoader classLoader) {
  if (shouldIgnoreIndex) {
    return null;
  }
  try {
    Enumeration<URL> urls = classLoader.getResources(COMPONENTS_RESOURCE_LOCATION);
    if (!urls.hasMoreElements()) {
      return null;
    }
    List<Properties> result = new ArrayList<>();
    while (urls.hasMoreElements()) {
      URL url = urls.nextElement();
      Properties properties = PropertiesLoaderUtils.loadProperties(new UrlResource(url));
      result.add(properties);
    }
    if (logger.isDebugEnabled()) {
      logger.debug("Loaded " + result.size() + "] index(es)");
    }
    int totalCount = result.stream().mapToInt(Properties::size).sum();
    return (totalCount > 0 ? new CandidateComponentsIndex(result) : null);
  }
  catch (IOException ex) {
    throw new IllegalStateException("Unable to load indexes from location [" +
        COMPONENTS_RESOURCE_LOCATION + "]", ex);
  }
}

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

@Override
public Enumeration<URL> getResources( String name ) throws IOException
{
  return name.startsWith( "META-INF/services" ) ? super.getResources( "test/" + name )
                         : super.getResources( name );
}

代码示例来源: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: apache/incubator-dubbo

public static void checkDuplicate(String path, boolean failOnError) {
  try {
    // search in caller's classloader
    Enumeration<URL> urls = ClassHelper.getCallerClassLoader(Version.class).getResources(path);
    Set<String> files = new HashSet<String>();
    while (urls.hasMoreElements()) {
      URL url = urls.nextElement();
      if (url != null) {
        String file = url.getFile();
        if (file != null && file.length() > 0) {
          files.add(file);
        }
      }
    }
    // duplicated jar is found
    if (files.size() > 1) {
      String error = "Duplicate class " + path + " in " + files.size() + " jar " + files;
      if (failOnError) {
        throw new IllegalStateException(error);
      } else {
        logger.error(error);
      }
    }
  } catch (Throwable e) {
    logger.error(e.getMessage(), e);
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
public Enumeration<URL> getResources(String name) throws IOException {
  if (isMasked(name)) return Collections.emptyEnumeration();
  return super.getResources(name);
}

代码示例来源: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: prestodb/presto

@Override
public Enumeration<URL> getResources(String name)
    throws IOException
{
  // If this is an SPI resource, use SPI resources
  if (isSpiClass(name)) {
    return spiClassLoader.getResources(name);
  }
  // Use local resources
  return super.getResources(name);
}

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

@Override
public Enumeration<URL> getResources( String name ) throws IOException
{
  return name.startsWith( "META-INF/services" ) ? Collections.enumeration( Collections.<URL>emptySet() )
                         : super.getResources( name );
}

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

@Override
 public Enumeration<URL> getResources(final String name) throws IOException
 {
  final List<URL> urls = new ArrayList<>();
  Iterators.addAll(urls, Iterators.forEnumeration(super.getResources(name)));
  Iterators.addAll(urls, Iterators.forEnumeration(druidLoader.getResources(name)));
  return Iterators.asEnumeration(urls.iterator());
 }
}

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

@Test
public void testDoesNotFindExistingResourceWithGetResourcesAndNullOverride() throws IOException {
  assertNotNull(thisClassLoader.getResources(EXISTING_RESOURCE));
  overridingLoader.override(EXISTING_RESOURCE, null);
  assertEquals(0, countElements(overridingLoader.getResources(EXISTING_RESOURCE)));
}

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

@Test
public void testFindsExistingResourceWithGetResourcesAndNoOverrides() throws IOException {
  assertNotNull(thisClassLoader.getResources(EXISTING_RESOURCE));
  assertNotNull(overridingLoader.getResources(EXISTING_RESOURCE));
  assertEquals(1, countElements(overridingLoader.getResources(EXISTING_RESOURCE)));
}

相关文章