public static Collection<String> getClassEntriesForBundleUsingProjectClassPathMappings(ClassLoader classLoader, ComponentSpecification bundleSpec, Set<String> packagesToScan) { return classEntriesFrom(bundleClassPathMapping(bundleSpec, classLoader).classPathElements, packagesToScan); }
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); } }
default BundleClasses getBundleClasses(ComponentSpecification bundle, Set<String> packagesToScan) { return new BundleClasses(new MockBundle(), Collections.emptySet()); }
@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> 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; }
@Override protected RestApiContext newInstance() { Object instance = super.newInstance(); RestApiContext restApiContext = (RestApiContext) instance; List<JerseyBundlesConfig.Bundles> bundles = restApiContext.bundlesConfig.bundles(); for (JerseyBundlesConfig.Bundles bundleConfig : bundles) { BundleClasses bundleClasses = osgi.getBundleClasses(ComponentSpecification.fromString(bundleConfig.spec()), new HashSet<>(bundleConfig.packages())); restApiContext.addBundle(createBundleInfo(bundleClasses.bundle(), bundleClasses.classEntries())); } componentsToInject.forEach(component -> restApiContext.addInjectableComponent(component.instanceKey(), component.componentId(), component.newOrCachedInstance())); return restApiContext; }
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; } }
private static Collection<String> classEntriesInPath(Path rootPath, Collection<String> packagePathsToScan) { Iterable<File> fileIterator; if (packagePathsToScan.isEmpty()) { fileIterator = fileTreeTraverser().preOrderTraversal(rootPath.toFile()); } else { List<File> files = new ArrayList<>(); for (String packagePath : packagePathsToScan) { for (File file : fileTreeTraverser().children(rootPath.resolve(packagePath).toFile())) { files.add(file); } } fileIterator = files; } List<String> ret = new ArrayList<>(); for (File file : fileIterator) { if (file.isFile() && file.getName().endsWith(CLASS_FILE_TYPE_SUFFIX)) { ret.add(relativePathToClass(rootPath, file.toPath())); } } return ret; }