Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id E33D418851 for ; Tue, 4 Aug 2015 14:15:42 +0000 (UTC) Received: (qmail 16698 invoked by uid 500); 4 Aug 2015 14:15:42 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 16648 invoked by uid 500); 4 Aug 2015 14:15:42 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 16639 invoked by uid 99); 4 Aug 2015 14:15:42 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 04 Aug 2015 14:15:42 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 88909DFFB8; Tue, 4 Aug 2015 14:15:42 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: cschneider@apache.org To: commits@camel.apache.org Message-Id: <88eef08f4e70447487ec1cc6b1ea7f89@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: camel git commit: [CAMEL-9048] Do not load classes to probe for interface availability Date: Tue, 4 Aug 2015 14:15:42 +0000 (UTC) Repository: camel Updated Branches: refs/heads/master 1cb9bdeb7 -> 1a73fa61a [CAMEL-9048] Do not load classes to probe for interface availability Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1a73fa61 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1a73fa61 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1a73fa61 Branch: refs/heads/master Commit: 1a73fa61a6e0f1125e516798d0cabc1198328684 Parents: 1cb9bde Author: Christian Schneider Authored: Tue Aug 4 16:15:21 2015 +0200 Committer: Christian Schneider Committed: Tue Aug 4 16:15:21 2015 +0200 ---------------------------------------------------------------------- .../org/apache/camel/impl/osgi/Activator.java | 61 ++++++++++++++------ 1 file changed, 44 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/1a73fa61/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 12135cb..97729b5 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,7 @@ 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; @@ -85,9 +88,13 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { private BundleTracker tracker; private Map> resolvers = new ConcurrentHashMap>(); private long bundleId; + + // Map from package name to the capability we export for this package + private Map packageCapabilities = new HashMap<>(); public void start(BundleContext context) throws Exception { LOG.info("Camel activator starting"); + cachePackageCapabilities(context); bundleId = context.getBundle().getBundleId(); BundleContext systemBundleContext = context.getBundle(0).getBundleContext(); tracker = new BundleTracker(systemBundleContext, Bundle.ACTIVE, this); @@ -100,6 +107,20 @@ 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 + * + * @param classes interfaces we want to track + */ + private void cachePackageCapabilities(BundleContext context) { + BundleWiring ourWiring = context.getBundle().adapt(BundleWiring.class); + List 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()); @@ -152,7 +173,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { } protected void registerComponents(Bundle bundle, List resolvers) { - if (checkCompat(bundle, Component.class)) { + if (canSee(bundle, Component.class)) { Map components = new HashMap(); for (Enumeration e = bundle.getEntryPaths(META_INF_COMPONENT); e != null && e.hasMoreElements();) { String path = (String) e.nextElement(); @@ -167,7 +188,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { } protected void registerLanguages(Bundle bundle, List resolvers) { - if (checkCompat(bundle, Language.class)) { + if (canSee(bundle, Language.class)) { Map languages = new HashMap(); for (Enumeration e = bundle.getEntryPaths(META_INF_LANGUAGE); e != null && e.hasMoreElements();) { String path = (String) e.nextElement(); @@ -188,7 +209,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { } protected void registerDataFormats(Bundle bundle, List resolvers) { - if (checkCompat(bundle, DataFormat.class)) { + if (canSee(bundle, DataFormat.class)) { Map dataformats = new HashMap(); for (Enumeration e = bundle.getEntryPaths(META_INF_DATAFORMAT); e != null && e.hasMoreElements();) { String path = (String) e.nextElement(); @@ -203,7 +224,7 @@ public class Activator implements BundleActivator, BundleTrackerCustomizer { } protected void registerTypeConverterLoader(Bundle bundle, List 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) { @@ -212,6 +233,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 imports = wiring.getRequiredWires(PACKAGE_NAMESPACE); + for (BundleWire importWire : imports) { + if (packageCap.equals(importWire.getCapability())) { + return true; + } + } + return false; + } protected static class BundleComponentResolver extends BaseResolver implements ComponentResolver { @@ -520,19 +559,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 getConverterPackages(URL resource) { Set packages = new LinkedHashSet(); if (resource != null) {