diff options
Diffstat (limited to 'org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/Bundles.java')
-rw-r--r-- | org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/Bundles.java | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/Bundles.java b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/Bundles.java index efca3d27..cc101bae 100644 --- a/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/Bundles.java +++ b/org.eclipse.m2e.core/src/org/eclipse/m2e/core/internal/Bundles.java @@ -11,15 +11,30 @@ package org.eclipse.m2e.core.internal; +import java.io.File; +import java.io.IOException; +import java.net.URL; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; import java.util.Set; import org.osgi.framework.Bundle; +import org.osgi.framework.BundleException; +import org.osgi.framework.Constants; import org.osgi.framework.namespace.BundleNamespace; import org.osgi.framework.namespace.PackageNamespace; import org.osgi.framework.wiring.BundleWire; import org.osgi.framework.wiring.BundleWiring; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.osgi.util.ManifestElement; + +import org.eclipse.m2e.core.internal.equinox.DevClassPathHelper; /** @@ -27,6 +42,8 @@ import org.osgi.framework.wiring.BundleWiring; */ public class Bundles { + private static final Logger log = LoggerFactory.getLogger(Bundles.class); + private static Bundle findDependencyBundle(Bundle bundle, String dependencyName, Set<Bundle> visited) { BundleWiring bundleWiring = bundle.adapt(BundleWiring.class); if(bundleWiring == null) { @@ -62,4 +79,69 @@ public class Bundles { return bundleWiring.getClassLoader(); } + public static List<String> getClasspathEntries(Bundle bundle) { + log.debug("getClasspathEntries(Bundle={})", bundle.toString()); + Set<String> cp = new LinkedHashSet<String>(); + if(DevClassPathHelper.inDevelopmentMode()) { + cp.addAll(Arrays.asList(DevClassPathHelper.getDevClassPath(bundle.getSymbolicName()))); + } + cp.addAll(Arrays.asList(parseBundleClasspath(bundle))); + List<String> entries = new ArrayList<>(); + for(String cpe : cp) { + String entry; + if(".".equals(cpe)) { + entry = getNestedJarOrDir(bundle, "/"); + } else { + entry = getNestedJarOrDir(bundle, cpe); + } + + if(entry != null) { + log.debug("\tEntry:{}", entry); + entries.add(entry); + } + } + return entries; + } + + private static String[] parseBundleClasspath(Bundle bundle) { + String[] result = new String[] {"."}; + String header = bundle.getHeaders().get(Constants.BUNDLE_CLASSPATH); + ManifestElement[] classpathEntries = null; + try { + classpathEntries = ManifestElement.parseHeader(Constants.BUNDLE_CLASSPATH, header); + } catch(BundleException ex) { + log.warn("Could not parse bundle classpath of {}", bundle.toString(), ex); + } + if(classpathEntries != null) { + result = new String[classpathEntries.length]; + for(int i = 0; i < classpathEntries.length; i++ ) { + result[i] = classpathEntries[i].getValue(); + } + } + return result; + } + + private static String getNestedJarOrDir(Bundle bundle, String cp) { + // try embeded entries first + URL url = bundle.getEntry(cp); + if(url != null) { + try { + return FileLocator.toFileURL(url).getFile(); + } catch(IOException ex) { + log.warn("Could not get entry {} for bundle {}", new Object[] {cp, bundle.toString(), ex}); + } + } + + // in development mode entries can be absolute paths outside of bundle basedir + if(DevClassPathHelper.inDevelopmentMode()) { + File file = new File(cp); + if(file.exists() && file.isAbsolute()) { + return file.getAbsolutePath(); + } + } + + log.debug("Bundle {} does not have entry {}", bundle.toString(), cp); + return null; + } + } |