/** * Lists up all resources satisfying the given resourceFilter from the given * current class loader * * @param resourceFilter * @param classLoader * @return the list of resources matching the condition specified in the * resourceFilter */ public static List<VirtualFile> listResources(ClassLoader classLoader, ResourceFilter resourceFilter) { List<URLClassLoader> classLoaderList = getAllAncestorOrSelfClassLoaders(classLoader); ArrayList<VirtualFile> fileList = new ArrayList<VirtualFile>(); for (URLClassLoader cl : classLoaderList) { for (URL url : cl.getURLs()) { fileList.addAll(listResources(url, "", resourceFilter)); } } return fileList; }
/** * 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; }
private Map<String, Class< ? extends SilkFunctionPlugin>> getPluginTable() { if (pluginTable == null) { pluginTable = new TreeMap<String, Class< ? extends SilkFunctionPlugin>>(); // load plugins ClassLoader cl = SilkParser.class.getClassLoader(); for (Class<SilkFunctionPlugin> each : FileResource.findClasses(SilkFunctionPlugin.class.getPackage(), SilkFunctionPlugin.class, cl)) { String functionName = each.getSimpleName().toLowerCase(); _logger.trace("loaded " + functionName); pluginTable.put(functionName, each); } } return pluginTable; }
private static void collectFileResources(String resourceURLString, ArrayList<VirtualFile> fileList, String packagePath, ResourceFilter resourceFilter) { if (resourceURLString.endsWith(".svn")) { return; // omit the .svn folder } String logicalName = extractLogicalName(packagePath, resourceURLString); if (logicalName == null) throw new IllegalArgumentException("packagePath=" + packagePath + ", resourceURL=" + resourceURLString); try { File file = new File(new URL(resourceURLString).toURI()); if (resourceFilter.accept(file.getPath())) fileList.add(new SystemFile(file, logicalName)); if (file.isDirectory()) { for (File childFile : file.listFiles()) { String childResourceURL = resourceURLString + (resourceURLString.endsWith("/") ? "" : "/") + childFile.getName(); collectFileResources(childResourceURL, fileList, packagePath, resourceFilter); } } } catch (MalformedURLException e) { _logger.error(e); } catch (URISyntaxException e) { _logger.error(e); } }
/** * list up resources recursively under the given base package name * * @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(String basePackageName) { return listResources(basePackageName, new ResourceFilter() { public boolean accept(String resourcePath) { return true; } }); }
private static String packagePath(Class< ? > referenceClass) { return packagePath(referenceClass.getPackage()); }
public static <T> String loadIntoString(Package referencePackage, String path) throws IOException { BufferedInputStream in = openByteStream(referencePackage, path); if (in == null) throw new FileNotFoundException(String.format("reference package:%s, path:%s", referencePackage.getName(), path)); ByteArrayOutputStream buf = new ByteArrayOutputStream(); try { byte[] tmp = new byte[4028]; for (int readBytes = 0; (readBytes = in.read(tmp)) != -1;) { buf.write(tmp, 0, readBytes); } buf.flush(); return buf.toString(); } finally { in.close(); } }
private static ArrayList<URL> getURLListFromAllAncestorOrSelfClassLoaders( ClassLoader classLoader, String packageAsPath) { String path = !packageAsPath.endsWith("/") ? packageAsPath + "/" : packageAsPath; ArrayList<URLClassLoader> classLoaderSet = getAllAncestorOrSelfClassLoaders(classLoader); ArrayList<URL> resultURLList = new ArrayList<URL>(); try { for (URLClassLoader loader : classLoaderSet) { for (Enumeration<URL> resourceEnum = loader.findResources(path); resourceEnum .hasMoreElements();) { URL url = resourceEnum.nextElement(); resultURLList.add(url); } } } catch (IOException e) { _logger.error(e); } return resultURLList; }
private Map<String, Class< ? extends SilkFunctionPlugin>> getPluginTable() { if (pluginTable == null) { pluginTable = new TreeMap<String, Class< ? extends SilkFunctionPlugin>>(); // load plugins ClassLoader cl = SilkParser.class.getClassLoader(); for (Class<SilkFunctionPlugin> each : FileResource.findClasses(SilkFunctionPlugin.class.getPackage(), SilkFunctionPlugin.class, cl)) { String functionName = each.getSimpleName().toLowerCase(); _logger.trace("loaded " + functionName); pluginTable.put(functionName, each); } } return pluginTable; }
if (protocol.equals("file")) { String resourceURLString = resourceURL.toString(); collectFileResources(resourceURLString, fileList, packagePath, resourceFilter); URL jarFileURL = new URL(physicalURL); String logicalName = extractLogicalName(packagePath, jarEntry.getName()); if (logicalName != null && resourceFilter.accept(logicalName)) fileList.add(new FileInJarArchive(jarFileURL, logicalName, jarEntry
/** * list up resources recursively under the given base package name * * @param basePackageName * package name to search. e.g. org.xerial.util * @param classLoader * class loader * @return the list of resources represented as {@link VirtualFile} */ public static List<VirtualFile> listResources(String basePackageName, ClassLoader classLoader) { return listResources(classLoader, basePackageName, new ResourceFilter() { public boolean accept(String resourcePath) { return true; } }); }
private static String packagePath(Package basePackage) { return packagePath(basePackage.getName()); }
/** * Load the given resource file into String * * @param <T> * @param referenceClass * @param path * @return * @throws IOException */ public static <T> String loadIntoString(Class<T> referenceClass, String path) throws IOException { BufferedInputStream in = openByteStream(referenceClass, path); if (in == null) throw new FileNotFoundException(String.format("reference class:%s, path:%s", referenceClass.getName(), path)); ByteArrayOutputStream buf = new ByteArrayOutputStream(); try { byte[] tmp = new byte[4028]; for (int readBytes = 0; (readBytes = in.read(tmp)) != -1;) { buf.write(tmp, 0, readBytes); } buf.flush(); return buf.toString(); } finally { in.close(); } }
/** * Finds the {@link URL} of the resource * * @param resourceFileName * the resource file name, relative to the root package * @return the URL of the specified resource */ public static URL find(String resourceFileName) { return find("", resourceFileName); }
/** * list up resources recursively under the given base package name * * @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(String basePackageName, ResourceFilter resourceFilter) { return listResources(Thread.currentThread().getContextClassLoader(), basePackageName, resourceFilter); }
/** * 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); }
/** * Create a temporary file from the specified resource, then return the path * to the temporary file * * @param <T> * @param searchBase * @param path * @param tempDir * @return * @throws IOException */ public static <T> File copyToTemp(Class<T> searchBase, String path, File tempDir) throws IOException { BufferedInputStream resource = openByteStream(searchBase, path); try { if (resource == null) throw new FileNotFoundException(path); File tmp = FileUtil.createTempFile(tempDir, "tmp-", new File(path).getName()); FileUtil.copy(resource, tmp); return tmp; } finally { if (resource != null) resource.close(); } }
/** * Finds the {@link URL} of the resource * * @param basePackage * the base package 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(Package basePackage, String resourceFileName) { return find(basePackage.getName(), resourceFileName); }
/** * Lists up all resources satisfying the given resourceFilter from the * current class loader * * @param resourceFilter * @return the list of resources matching the condition specified in the * resourceFilter */ public static List<VirtualFile> listResources(ResourceFilter resourceFilter) { return listResources(Thread.currentThread().getContextClassLoader(), resourceFilter); }