public boolean exists() { return getUrl() != null; }
public Set<String> findResourceNames(String location, URL locationUrl) throws IOException { JarFile jarFile = getJarFromUrl(locationUrl); try { // For Tomcat and non-expanded WARs. String prefix = jarFile.getName().toLowerCase().endsWith(".war") ? "WEB-INF/classes/" : ""; return findResourceNamesFromJarFile(jarFile, prefix, location); } finally { jarFile.close(); } }
public Scanner(ClassLoader classLoader) { if (EnvironmentDetection.isAndroid()) { resourceAndClassScanner = new AndroidScanner(classLoader); } else { resourceAndClassScanner = new ClassPathScanner(classLoader); } }
/** * Finds the resources names present at this location and below on the classpath starting with this prefix and * ending with this suffix. */ private Set<String> findResourceNames(Location location, ResourceFilter predicate) throws IOException { Set<String> resourceNames = new TreeSet<String>(); List<URL> locationsUrls = getLocationUrlsForPath(location); for (URL locationUrl : locationsUrls) { LOG.debug("scanning URL: {}", locationUrl.toExternalForm()); UrlResolver urlResolver = createUrlResolver(locationUrl.getProtocol()); URL resolvedUrl = urlResolver.toStandardJavaUrl(locationUrl); String protocol = resolvedUrl.getProtocol(); ClassPathLocationScanner classPathLocationScanner = createLocationScanner(protocol); if (classPathLocationScanner == null) { String scanRoot = UrlUtils.toFilePath(resolvedUrl); LOG.warn("Unable to scan location: {} (unsupported protocol: {})", scanRoot, protocol); } else { Set<String> names = resourceNameCache.get(classPathLocationScanner).get(resolvedUrl); if (names == null) { names = classPathLocationScanner.findResourceNames(location.getPath(), resolvedUrl); resourceNameCache.get(classPathLocationScanner).put(resolvedUrl, names); } resourceNames.addAll(names); } } return filterResourceNames(resourceNames, predicate); }
FileSystemClassPathLocationScanner locationScanner = new FileSystemClassPathLocationScanner(); locationScannerCache.put(protocol, locationScanner); resourceNameCache.put(locationScanner, new HashMap<URL, Set<String>>()); || "wsjar".equals(protocol) //WebSphere ) { JarFileClassPathLocationScanner locationScanner = new JarFileClassPathLocationScanner(); locationScannerCache.put(protocol, locationScanner); resourceNameCache.put(locationScanner, new HashMap<URL, Set<String>>()); JBossVFSv3ClassPathLocationScanner locationScanner = new JBossVFSv3ClassPathLocationScanner(); locationScannerCache.put(protocol, locationScanner); resourceNameCache.put(locationScanner, new HashMap<URL, Set<String>>()); || "bundleresource".equals(protocol)) //Equinox ) { OsgiClassPathLocationScanner locationScanner = new OsgiClassPathLocationScanner(); locationScannerCache.put(protocol, locationScanner); resourceNameCache.put(locationScanner, new HashMap<URL, Set<String>>());
for (File file : files) { if (file.canRead()) { String resourcePath = toResourceNameOnClasspath(classPathRootOnDisk, file); if (file.isDirectory()) { if (!ignorePath(resourcePath)) { resourceNames.addAll(findResourceNamesFromFileSystem(classPathRootOnDisk, scanRootLocation, file));
@Override public List<Resource> scanForResources(Location path, ResourceFilter predicate) { try { List<Resource> resources = new ArrayList<Resource>(); Set<String> resourceNames = findResourceNames(path, predicate); for (String resourceName : resourceNames) { resources.add(new ClassPathResource(resourceName, classLoader)); LOG.trace("... found resource: {}", resourceName); } return resources; } catch (IOException e) { throw new ClassPathScanException(e); } }
/** * Creates an appropriate URL resolver scanner for this url protocol. * * @param protocol The protocol of the location url to scan. * @return The url resolver for this protocol. */ private UrlResolver createUrlResolver(String protocol) { if (new EnvironmentDetection(classLoader).isJBossVFSv2() && protocol.startsWith("vfs")) { return new JBossVFSv2UrlResolver(); } return new DefaultUrlResolver(); }
public Set<String> findResourceNames(String location, URL locationUrl) throws IOException { Set<String> resourceNames = new TreeSet<String>(); Bundle bundle = getTargetBundleOrCurrent(FrameworkUtil.getBundle(getClass()), locationUrl); @SuppressWarnings({"unchecked"}) Enumeration<URL> entries = bundle.findEntries(locationUrl.getPath(), "*", true); if (entries != null) { while (entries.hasMoreElements()) { URL entry = entries.nextElement(); String resourceName = getPathWithoutLeadingSlash(entry); resourceNames.add(resourceName); } } return resourceNames; }
@Override public List<Class<?>> scanForClasses(Location location, ClassFilter predicate) { try { List<Class<?>> classes = new ArrayList<Class<?>>(); Set<String> resourceNames = findResourceNames(location, FilterResource.bySuffix(".class")); LOG.debug("scanning for classes at {} found {} resources to check", location, resourceNames.size()); for (String resourceName : resourceNames) { String className = toClassName(resourceName); try { Class<?> clazz = classLoader.loadClass(className); if (predicate.isMatch(clazz)) { classes.add(clazz); LOG.trace("... matched class: {} ", className); } } catch (NoClassDefFoundError err) { // This happens on class that inherits from an other class which are no longer in the classpath // e.g. "public class MyTestRunner extends BlockJUnit4ClassRunner" and junit was in scope "provided" LOG.debug("... class " + className + " could not be loaded and will be ignored.", err); } catch (ClassNotFoundException err) { // This happens on class that inherits from an other class which are no longer in the classpath // e.g. "public class MyTestRunner extends BlockJUnit4ClassRunner" and junit was in scope "provided" LOG.debug("... class " + className + " could not be loaded and will be ignored.", err); } } return classes; } catch (IOException e) { throw new ClassPathScanException(e); } }
private Bundle getTargetBundleOrCurrent(Bundle currentBundle, URL locationUrl) { try { Bundle targetBundle = currentBundle.getBundleContext().getBundle(getBundleId(locationUrl.getHost())); return targetBundle != null ? targetBundle : currentBundle; } catch (Exception e) { return currentBundle; } }
public Set<String> findResourceNames(String location, URL locationUrl) throws IOException { String filePath = UrlUtils.toFilePath(locationUrl); File folder = new File(filePath); if (!folder.isDirectory()) { LOG.debug("Skipping path as it is not a directory: " + filePath); return new TreeSet<String>(); } String classPathRootOnDisk = filePath.substring(0, filePath.length() - location.length()); if (!classPathRootOnDisk.endsWith(File.separator)) { classPathRootOnDisk = classPathRootOnDisk + File.separator; } LOG.debug("Scanning starting at classpath root in filesystem: " + classPathRootOnDisk); return findResourceNamesFromFileSystem(classPathRootOnDisk, location, folder); }
public String getLocationOnDisk() { URL url = getUrl(); if (url == null) { throw new ClassPathScanException("Unable to location resource on disk: " + location); } try { return new File(URLDecoder.decode(url.getPath(), "UTF-8")).getAbsolutePath(); } catch (UnsupportedEncodingException e) { throw new ClassPathScanException("Unknown encoding: UTF-8", e); } }