use of org.eclipse.osgi.storage.bundlefile.BundleFile in project rt.equinox.framework by eclipse.
the class ClasspathManager method getExternalClassPath.
/**
* Uses the requested classpath as an absolute path to locate a source for a new ClasspathEntry.
* @param cp the requested classpath
* @param cpGeneration the source generation to search for the classpath
* @return a classpath entry which uses an absolut path as a source
*/
public ClasspathEntry getExternalClassPath(String cp, Generation cpGeneration) {
File file = new File(cp);
if (!file.isAbsolute())
return null;
BundleFile bundlefile = createBundleFile(file, cpGeneration);
if (bundlefile != null)
return createClassPathEntry(bundlefile, cpGeneration);
return null;
}
use of org.eclipse.osgi.storage.bundlefile.BundleFile in project rt.equinox.framework by eclipse.
the class ClasspathManager method getClasspath.
/**
* Creates a new ClasspathEntry object for the requested classpath if the source exists.
* @param cp the requested classpath.
* @param cpGeneration the source generation to search for the classpath
* @return a new ClasspathEntry for the requested classpath or null if the source does not exist.
*/
public ClasspathEntry getClasspath(String cp, Generation cpGeneration) {
BundleFile bundlefile = null;
File file;
BundleEntry cpEntry = cpGeneration.getBundleFile().getEntry(cp);
// check for internal library directories in a bundle jar file
if (// $NON-NLS-1$
cpEntry != null && cpEntry.getName().endsWith("/"))
bundlefile = createBundleFile(cp, cpGeneration);
else // check for internal library jars
if ((file = cpGeneration.getBundleFile().getFile(cp, false)) != null)
bundlefile = createBundleFile(file, cpGeneration);
if (bundlefile != null)
return createClassPathEntry(bundlefile, cpGeneration);
return null;
}
use of org.eclipse.osgi.storage.bundlefile.BundleFile in project rt.equinox.framework by eclipse.
the class ClasspathEntry method getMRBundleFiles.
private static List<BundleFile> getMRBundleFiles(BundleFile bundlefile, Generation generation) {
Storage storage = generation.getBundleInfo().getStorage();
if (storage.getRuntimeVersion().getMajor() < 9) {
return Collections.emptyList();
}
List<BundleFile> mrBundleFiles = new ArrayList<>();
for (int i = storage.getRuntimeVersion().getMajor(); i > 8; i--) {
String versionPath = BundleInfo.MULTI_RELEASE_VERSIONS + i + '/';
BundleEntry versionEntry = bundlefile.getEntry(versionPath);
if (versionEntry != null) {
mrBundleFiles.add(storage.createNestedBundleFile(versionPath, bundlefile, generation, BundleInfo.MULTI_RELEASE_FILTER_PREFIXES));
}
}
return Collections.unmodifiableList(mrBundleFiles);
}
use of org.eclipse.osgi.storage.bundlefile.BundleFile in project knime-core by knime.
the class EclipseUtil method findClasses.
/**
* Searches the given class loader for classes that match the given class filter. This method is capable of
* searching through Eclipse plug-ins but it will not recursivly search dependencies.
*
* @param filter a filter for classes
* @param classLoader the class loader that should be used for searching
* @return a collection with matching classes
* @throws IOException if an I/O error occurs while scanning the class path
* @since 2.12
*/
public static Collection<Class<?>> findClasses(final ClassFilter filter, final ClassLoader classLoader) throws IOException {
List<URL> classPathUrls = new ArrayList<>();
if (classLoader instanceof ModuleClassLoader) {
ModuleClassLoader cl = (ModuleClassLoader) classLoader;
ClasspathManager cpm = cl.getClasspathManager();
for (ClasspathEntry e : cpm.getHostClasspathEntries()) {
BundleFile bf = e.getBundleFile();
classPathUrls.add(bf.getEntry("").getLocalURL());
}
} else if (classLoader instanceof URLClassLoader) {
URLClassLoader cl = (URLClassLoader) classLoader;
for (URL u : cl.getURLs()) {
classPathUrls.add(u);
}
} else {
FileLocator.toFileURL(classLoader.getResource(""));
}
// filter classpath for nested entries
for (Iterator<URL> it = classPathUrls.iterator(); it.hasNext(); ) {
URL url1 = it.next();
for (URL url2 : classPathUrls) {
if ((url1 != url2) && url2.getPath().startsWith(url1.getPath())) {
it.remove();
break;
}
}
}
List<Class<?>> classes = new ArrayList<>();
for (URL url : classPathUrls) {
if ("file".equals(url.getProtocol())) {
String path = url.getPath();
// path = path.replaceFirst(Pattern.quote(classPath) + "$", "");
collectInDirectory(new File(path), "", filter, classes, classLoader);
} else if ("jar".equals(url.getProtocol())) {
String path = url.getPath().replaceFirst("^file:", "").replaceFirst("\\!.+$", "");
collectInJar(new JarFile(path), filter, classes, classLoader);
} else {
throw new IllegalStateException("Cannot read from protocol '" + url.getProtocol() + "'");
}
}
return classes;
}
use of org.eclipse.osgi.storage.bundlefile.BundleFile in project knime-core by knime.
the class JavaSnippet method resolveBuildPathForJavaType.
/**
* Get file and jar urls required for compiling with given java type
*/
private static synchronized Set<File> resolveBuildPathForJavaType(final Class<?> javaType) {
if (javaType.isPrimitive()) {
return Collections.emptySet();
}
final Set<File> javaTypeCache = CLASSPATH_FOR_CLASS_CACHE.get(javaType);
if (javaTypeCache != null) {
return javaTypeCache;
}
final Set<File> result = new LinkedHashSet<>();
final Set<URL> urls = new LinkedHashSet<>();
ClassUtil.streamForClassHierarchy(javaType).filter(c -> c.getClassLoader() instanceof ModuleClassLoader).flatMap(c -> {
final ModuleClassLoader moduleClassLoader = (ModuleClassLoader) c.getClassLoader();
return Arrays.stream(moduleClassLoader.getClasspathManager().getHostClasspathEntries());
}).forEach(entry -> {
final BundleFile file = entry.getBundleFile();
try {
final URL url = file.getBaseFile().toURI().toURL();
urls.add(url);
} catch (MalformedURLException e) {
LOGGER.error("Could not resolve URL for bundle file \"" + file.toString() + "\" while assembling build path for custom java type \"" + javaType.getName() + "\"");
}
result.add(file.getBaseFile());
});
/* Check whether the java snippet compiler can later find the class with this classpath */
try (final URLClassLoader classpathClassLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]))) {
classpathClassLoader.loadClass(javaType.getName());
} catch (NoClassDefFoundError | ClassNotFoundException e) {
LOGGER.error("Classpath for \"" + javaType.getName() + "\" could not be assembled.", e);
// indicate that this type should not be provided in java snippet
return null;
} catch (IOException e) {
// thrown by URLClassLoader.close()
LOGGER.error("Unable to close classloader used for testing of custom type classpath.", e);
}
if (result.contains(null)) {
throw new IllegalStateException("Couldn't assemble classpath for custom type \"" + javaType + "\", illegal <null> value in list:\n " + result.stream().map(f -> f == null ? "<NULL>" : f.getAbsolutePath()).collect(Collectors.joining("\n ")));
}
CLASSPATH_FOR_CLASS_CACHE.put(javaType, result);
return result;
}
Aggregations