use of org.eclipse.osgi.internal.loader.ModuleClassLoader in project rt.equinox.framework by eclipse.
the class EquinoxBundle method getResource.
@Override
public URL getResource(String name) {
try {
equinoxContainer.checkAdminPermission(this, AdminPermission.RESOURCE);
} catch (SecurityException e) {
return null;
}
checkValid();
if (isFragment()) {
return null;
}
ModuleClassLoader classLoader = getModuleClassLoader(false);
if (classLoader != null) {
return classLoader.getResource(name);
}
return new ClasspathManager((Generation) module.getCurrentRevision().getRevisionInfo(), null).findLocalResource(name);
}
use of org.eclipse.osgi.internal.loader.ModuleClassLoader 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.internal.loader.ModuleClassLoader in project rt.equinox.framework by eclipse.
the class ClassLoadingBundleTests method testLoaderUninstalledBundle.
public void testLoaderUninstalledBundle() throws BundleException, IOException {
String testResourcePath = "testResource";
// $NON-NLS-1$
File config = OSGiTestsActivator.getContext().getDataFile(getName());
Map<String, String> testHeaders = new HashMap<String, String>();
testHeaders.put(Constants.BUNDLE_MANIFESTVERSION, "2");
testHeaders.put(Constants.BUNDLE_SYMBOLICNAME, getName());
config.mkdirs();
File testBundleFile = SystemBundleTests.createBundle(config, getName(), testHeaders, Collections.singletonMap(testResourcePath, "testValue"));
Bundle test = getContext().installBundle(getName(), new FileInputStream(testBundleFile));
test.start();
BundleWiring wiring = test.adapt(BundleWiring.class);
assertNotNull("No wiring found.", wiring);
ModuleClassLoader bundleClassLoader = (ModuleClassLoader) wiring.getClassLoader();
URL testResource = bundleClassLoader.findLocalResource(testResourcePath);
assertNotNull("No test resource found.", testResource);
test.update(new FileInputStream(testBundleFile));
testResource = bundleClassLoader.findLocalResource(testResourcePath);
assertNull("Found resource.", testResource);
Object[] expectedFrameworkEvents = new Object[] { new FrameworkEvent(FrameworkEvent.INFO, test, null) };
Object[] actualFrameworkEvents = frameworkListenerResults.getResults(1);
compareResults(expectedFrameworkEvents, actualFrameworkEvents);
wiring = test.adapt(BundleWiring.class);
assertNotNull("No wiring found.", wiring);
bundleClassLoader = (ModuleClassLoader) wiring.getClassLoader();
testResource = bundleClassLoader.findLocalResource(testResourcePath);
assertNotNull("No test resource found.", testResource);
test.uninstall();
testResource = bundleClassLoader.findLocalResource(testResourcePath);
assertNull("Found resource.", testResource);
actualFrameworkEvents = frameworkListenerResults.getResults(1);
compareResults(expectedFrameworkEvents, actualFrameworkEvents);
}
use of org.eclipse.osgi.internal.loader.ModuleClassLoader 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;
}
use of org.eclipse.osgi.internal.loader.ModuleClassLoader in project rt.equinox.framework by eclipse.
the class Handler method findBundleEntry.
protected BundleEntry findBundleEntry(URL url, Module module) throws IOException {
ModuleRevision current = module.getCurrentRevision();
ModuleWiring wiring = current == null ? null : current.getWiring();
ModuleClassLoader classloader = (ModuleClassLoader) (current == null ? null : wiring.getClassLoader());
if (classloader == null)
throw new FileNotFoundException(url.getPath());
BundleEntry entry = classloader.getClasspathManager().findLocalEntry(url.getPath(), url.getPort());
if (entry == null) {
// this isn't strictly needed but is kept to maintain compatibility
entry = classloader.getClasspathManager().findLocalEntry(url.getPath());
}
if (entry == null) {
throw new FileNotFoundException(url.getPath());
}
return entry;
}
Aggregations