private static String packagePath(Class< ? > referenceClass) { return packagePath(referenceClass.getPackage()); }
private static String packagePath(Package basePackage) { return packagePath(basePackage.getName()); }
/** * Finds the {@link URL} of the resource * * @param packageName * the base package name to find the resource * @param resourceFileName * the resource file name relative to the package folder * @return the URL of the specified resource */ public static URL find(String packageName, String resourceFileName) { String packagePath = packagePath(packageName); String resourcePath = packagePath + resourceFileName; if (!resourcePath.startsWith("/")) resourcePath = "/" + resourcePath; if (_logger.isTraceEnabled()) _logger.trace("search resource: " + resourcePath); for (ClassLoader cl = Thread.currentThread().getContextClassLoader(); cl != null; cl = cl .getParent()) { URL r = cl.getResource(resourcePath); if (r != null) return r; } return FileResource.class.getResource(resourcePath); }
/** * list up resources recursively under the given base package name * * @param classLoader * @param basePackageName * package name to search. e.g. org.xerial.util * @return the list of resources represented as {@link VirtualFile} */ public static List<VirtualFile> listResources(ClassLoader classLoader, String basePackageName, ResourceFilter resourceFilter) { String packagePath = packagePath(basePackageName); List<URL> resourceURLList = getURLListFromAllAncestorOrSelfClassLoaders(classLoader, packagePath); ArrayList<VirtualFile> fileList = new ArrayList<VirtualFile>(); for (URL resourceURL : resourceURLList) { fileList.addAll(listResources(resourceURL, packagePath, resourceFilter)); } return fileList; }