Repository: camel Updated Branches: refs/heads/camel-2.15.x 79c5ed6a7 -> 9166b94d6
[CAMEL-9048] Do not load classes to probe for interface availability Conflicts: camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/9166b94d Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9166b94d Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9166b94d Branch: refs/heads/camel-2.15.x Commit: 9166b94d63d8129ac1cb4366000a9624af4ed1fe Parents: 79c5ed6 Author: Christian Schneider <ch...@die-schneider.net> Authored: Tue Aug 4 16:15:21 2015 +0200 Committer: Christian Schneider <ch...@die-schneider.net> Committed: Tue Aug 4 22:29:24 2015 +0200 ---------------------------------------------------------------------- .../org/apache/camel/impl/osgi/Activator.java | 62 ++++++++++++++------ 1 file changed, 45 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/9166b94d/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java b/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java index d73f6c6..df58189 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java +++ b/camel-core/src/main/java/org/apache/camel/impl/osgi/Activator.java @@ -16,6 +16,8 @@ */ package org.apache.camel.impl.osgi; +import static org.osgi.framework.wiring.BundleRevision.PACKAGE_NAMESPACE; + import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; @@ -64,6 +66,9 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; import org.osgi.framework.Constants; import org.osgi.framework.ServiceRegistration; +import org.osgi.framework.wiring.BundleCapability; +import org.osgi.framework.wiring.BundleWire; +import org.osgi.framework.wiring.BundleWiring; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -80,10 +85,14 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { private BundleTracker tracker; private Map<Long, List<BaseService>> resolvers = new ConcurrentHashMap<Long, List<BaseService>>(); + + // Map from package name to the capability we export for this package + private Map<String, BundleCapability> packageCapabilities = new HashMap<>(); public void start(BundleContext context) throws Exception { LOG.info("Camel activator starting"); tracker = new BundleTracker(context, Bundle.ACTIVE, this); + cachePackageCapabilities(context); tracker.open(); LOG.info("Camel activator started"); } @@ -93,6 +102,19 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { tracker.close(); LOG.info("Camel activator stopped"); } + + /** + * Caches the package capabilities that are needed for a set of interface classes + * + */ + private void cachePackageCapabilities(BundleContext context) { + BundleWiring ourWiring = context.getBundle().adapt(BundleWiring.class); + List<BundleCapability> ourExports = ourWiring.getCapabilities(PACKAGE_NAMESPACE); + for (BundleCapability ourExport : ourExports) { + String ourPkgName = (String) ourExport.getAttributes().get(PACKAGE_NAMESPACE); + packageCapabilities.put(ourPkgName, ourExport); + } + } public Object addingBundle(Bundle bundle, BundleEvent event) { LOG.debug("Bundle started: {}", bundle.getSymbolicName()); @@ -122,7 +144,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { } protected void registerComponents(Bundle bundle, List<BaseService> resolvers) { - if (checkCompat(bundle, Component.class)) { + if (canSee(bundle, Component.class)) { Map<String, String> components = new HashMap<String, String>(); for (Enumeration<?> e = bundle.getEntryPaths(META_INF_COMPONENT); e != null && e.hasMoreElements();) { String path = (String) e.nextElement(); @@ -137,7 +159,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { } protected void registerLanguages(Bundle bundle, List<BaseService> resolvers) { - if (checkCompat(bundle, Language.class)) { + if (canSee(bundle, Language.class)) { Map<String, String> languages = new HashMap<String, String>(); for (Enumeration<?> e = bundle.getEntryPaths(META_INF_LANGUAGE); e != null && e.hasMoreElements();) { String path = (String) e.nextElement(); @@ -158,7 +180,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { } protected void registerDataFormats(Bundle bundle, List<BaseService> resolvers) { - if (checkCompat(bundle, DataFormat.class)) { + if (canSee(bundle, DataFormat.class)) { Map<String, String> dataformats = new HashMap<String, String>(); for (Enumeration<?> e = bundle.getEntryPaths(META_INF_DATAFORMAT); e != null && e.hasMoreElements();) { String path = (String) e.nextElement(); @@ -173,7 +195,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { } protected void registerTypeConverterLoader(Bundle bundle, List<BaseService> resolvers) { - if (checkCompat(bundle, TypeConverter.class)) { + if (canSee(bundle, TypeConverter.class)) { URL url1 = bundle.getEntry(META_INF_TYPE_CONVERTER); URL url2 = bundle.getEntry(META_INF_FALLBACK_TYPE_CONVERTER); if (url1 != null || url2 != null) { @@ -182,6 +204,24 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { } } } + + /** + * Check if bundle can see the given class + * @param bundle + * @param clazz + * @return + */ + protected boolean canSee(Bundle bundle, Class<?> clazz) { + BundleCapability packageCap = packageCapabilities.get(clazz.getPackage().getName()); + BundleWiring wiring = bundle.adapt(BundleWiring.class); + List<BundleWire> imports = wiring.getRequiredWires(PACKAGE_NAMESPACE); + for (BundleWire importWire : imports) { + if (packageCap.equals(importWire.getCapability())) { + return true; + } + } + return false; + } protected static class BundleComponentResolver extends BaseResolver<Component> implements ComponentResolver { @@ -490,19 +530,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { } return properties; } - - protected static boolean checkCompat(Bundle bundle, Class<?> clazz) { - // Check bundle compatibility - try { - if (bundle.loadClass(clazz.getName()) != clazz) { - return false; - } - } catch (Throwable t) { - return false; - } - return true; - } - + protected static Set<String> getConverterPackages(URL resource) { Set<String> packages = new LinkedHashSet<String>(); if (resource != null) {