public static Collection<String> getClassEntriesForBundleUsingProjectClassPathMappings(ClassLoader classLoader, ComponentSpecification bundleSpec, Set<String> packagesToScan) { return classEntriesFrom(bundleClassPathMapping(bundleSpec, classLoader).classPathElements, packagesToScan); }
private static Collection<String> classEntriesFrom(List<String> classPathEntries, Set<String> packagesToScan) { Set<String> packagePathsToScan = packagesToScan.stream().map(OsgiUtil::packageToPath).collect(Collectors.toSet()); List<String> ret = new ArrayList<>(); for (String entry : classPathEntries) { Path path = Paths.get(entry); if (Files.isDirectory(path)) { ret.addAll(classEntriesInPath(path, packagePathsToScan)); } else if (Files.isRegularFile(path) && path.getFileName().toString().endsWith(".jar")) { ret.addAll(classEntriesInJar(path, packagePathsToScan)); } else { throw new RuntimeException("Unsupported path " + path + " in the class path"); } } return ret; }
private static BundleClasspathMapping bundleClassPathMapping(ComponentSpecification bundleSpec, ClassLoader classLoader) { ProjectBundleClassPaths projectBundleClassPaths = loadProjectBundleClassPaths(classLoader); if (projectBundleClassPaths.mainBundle.bundleSymbolicName.equals(bundleSpec.getName())) { return projectBundleClassPaths.mainBundle; } else { log.log(Level.WARNING, "Dependencies of the bundle " + bundleSpec + " will not be scanned. Please file a feature request if you need this"); return matchingBundleClassPathMapping(bundleSpec, projectBundleClassPaths.providedDependencies); } }
@Override public BundleClasses getBundleClasses(ComponentSpecification bundleSpec, Set<String> packagesToScan) { //Temporary hack: Using class name since ClassLoaderOsgiFramework is not available at compile time in this bundle. if (osgiFramework.getClass().getName().equals("com.yahoo.application.container.impl.ClassLoaderOsgiFramework")) { Bundle syntheticClassPathBundle = first(osgiFramework.bundles()); ClassLoader classLoader = syntheticClassPathBundle.adapt(BundleWiring.class).getClassLoader(); return new BundleClasses( syntheticClassPathBundle, OsgiUtil.getClassEntriesForBundleUsingProjectClassPathMappings(classLoader, bundleSpec, packagesToScan)); } else { Bundle bundle = getBundle(bundleSpec); if (bundle == null) throw new RuntimeException("No bundle matching " + quote(bundleSpec)); return new BundleClasses(bundle, OsgiUtil.getClassEntriesInBundleClassPath(bundle, packagesToScan)); } }
private static Collection<String> classEntriesInJar(Path jarPath, Set<String> packagePathsToScan) { Predicate<String> acceptedPackage; if (packagePathsToScan.isEmpty()) { acceptedPackage = ign -> true; } else { acceptedPackage = name -> packagePathsToScan.contains(packagePath(name)); } try (JarFile jarFile = new JarFile(jarPath.toFile())) { return jarFile.stream().map(JarEntry::getName).filter(name -> name.endsWith(CLASS_FILE_TYPE_SUFFIX)).filter(acceptedPackage) .collect(Collectors.toList()); } catch (IOException e) { throw new RuntimeException(e); } }
public static Collection<String> getClassEntriesInBundleClassPath(Bundle bundle, Set<String> packagesToScan) { BundleWiring bundleWiring = bundle.adapt(BundleWiring.class); if (packagesToScan.isEmpty()) { return bundleWiring.listResources("/", "*" + CLASS_FILE_TYPE_SUFFIX, BundleWiring.LISTRESOURCES_LOCAL | BundleWiring.LISTRESOURCES_RECURSE); } else { List<String> ret = new ArrayList<>(); for (String pkg : packagesToScan) { ret.addAll(bundleWiring.listResources(packageToPath(pkg), "*" + CLASS_FILE_TYPE_SUFFIX, BundleWiring.LISTRESOURCES_LOCAL)); } return ret; } }