001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.impl.osgi; 018 019import java.io.BufferedInputStream; 020import java.io.BufferedReader; 021import java.io.IOException; 022import java.io.InputStreamReader; 023import java.lang.reflect.Method; 024import java.net.URL; 025import java.util.ArrayList; 026import java.util.Collection; 027import java.util.Dictionary; 028import java.util.Enumeration; 029import java.util.HashMap; 030import java.util.Hashtable; 031import java.util.LinkedHashSet; 032import java.util.List; 033import java.util.Map; 034import java.util.Properties; 035import java.util.Set; 036import java.util.StringTokenizer; 037import java.util.concurrent.ConcurrentHashMap; 038 039import org.apache.camel.CamelContext; 040import org.apache.camel.Component; 041import org.apache.camel.Converter; 042import org.apache.camel.TypeConverter; 043import org.apache.camel.TypeConverterLoaderException; 044import org.apache.camel.impl.converter.AnnotationTypeConverterLoader; 045import org.apache.camel.impl.osgi.tracker.BundleTracker; 046import org.apache.camel.impl.osgi.tracker.BundleTrackerCustomizer; 047import org.apache.camel.impl.scan.AnnotatedWithPackageScanFilter; 048import org.apache.camel.model.DataFormatDefinition; 049import org.apache.camel.spi.ComponentResolver; 050import org.apache.camel.spi.DataFormat; 051import org.apache.camel.spi.DataFormatResolver; 052import org.apache.camel.spi.Injector; 053import org.apache.camel.spi.Language; 054import org.apache.camel.spi.LanguageResolver; 055import org.apache.camel.spi.PackageScanFilter; 056import org.apache.camel.spi.TypeConverterLoader; 057import org.apache.camel.spi.TypeConverterRegistry; 058import org.apache.camel.util.IOHelper; 059import org.apache.camel.util.ObjectHelper; 060import org.apache.camel.util.StringHelper; 061import org.osgi.framework.Bundle; 062import org.osgi.framework.BundleActivator; 063import org.osgi.framework.BundleContext; 064import org.osgi.framework.BundleEvent; 065import org.osgi.framework.Constants; 066import org.osgi.framework.ServiceRegistration; 067import org.osgi.framework.wiring.BundleCapability; 068import org.osgi.framework.wiring.BundleWire; 069import org.osgi.framework.wiring.BundleWiring; 070import org.slf4j.Logger; 071import org.slf4j.LoggerFactory; 072 073import static org.osgi.framework.wiring.BundleRevision.PACKAGE_NAMESPACE; 074 075public class Activator implements BundleActivator, BundleTrackerCustomizer { 076 077 public static final String META_INF_COMPONENT = "META-INF/services/org/apache/camel/component/"; 078 public static final String META_INF_LANGUAGE = "META-INF/services/org/apache/camel/language/"; 079 public static final String META_INF_LANGUAGE_RESOLVER = "META-INF/services/org/apache/camel/language/resolver/"; 080 public static final String META_INF_DATAFORMAT = "META-INF/services/org/apache/camel/dataformat/"; 081 public static final String META_INF_TYPE_CONVERTER = "META-INF/services/org/apache/camel/TypeConverter"; 082 public static final String META_INF_FALLBACK_TYPE_CONVERTER = "META-INF/services/org/apache/camel/FallbackTypeConverter"; 083 public static final String EXTENDER_NAMESPACE = "osgi.extender"; 084 public static final String CAMEL_EXTENDER = "org.apache.camel"; 085 086 private static final Logger LOG = LoggerFactory.getLogger(Activator.class); 087 088 private BundleTracker tracker; 089 private final Map<Long, List<BaseService>> resolvers = new ConcurrentHashMap<Long, List<BaseService>>(); 090 private long bundleId; 091 092 // Map from package name to the capability we export for this package 093 private final Map<String, BundleCapability> packageCapabilities = new HashMap<String, BundleCapability>(); 094 095 public void start(BundleContext context) throws Exception { 096 LOG.info("Camel activator starting"); 097 cachePackageCapabilities(context); 098 bundleId = context.getBundle().getBundleId(); 099 BundleContext systemBundleContext = context.getBundle(0).getBundleContext(); 100 tracker = new BundleTracker(systemBundleContext, Bundle.ACTIVE, this); 101 tracker.open(); 102 LOG.info("Camel activator started"); 103 } 104 105 public void stop(BundleContext context) throws Exception { 106 LOG.info("Camel activator stopping"); 107 tracker.close(); 108 packageCapabilities.clear(); 109 LOG.info("Camel activator stopped"); 110 } 111 112 /** 113 * Caches the package capabilities that are needed for a set of interface classes 114 */ 115 private void cachePackageCapabilities(BundleContext context) { 116 BundleWiring ourWiring = context.getBundle().adapt(BundleWiring.class); 117 List<BundleCapability> ourExports = ourWiring.getCapabilities(PACKAGE_NAMESPACE); 118 for (BundleCapability ourExport : ourExports) { 119 String ourPkgName = (String) ourExport.getAttributes().get(PACKAGE_NAMESPACE); 120 packageCapabilities.put(ourPkgName, ourExport); 121 } 122 } 123 124 public Object addingBundle(Bundle bundle, BundleEvent event) { 125 LOG.debug("Bundle started: {}", bundle.getSymbolicName()); 126 if (extenderCapabilityWired(bundle)) { 127 List<BaseService> r = new ArrayList<BaseService>(); 128 registerComponents(bundle, r); 129 registerLanguages(bundle, r); 130 registerDataFormats(bundle, r); 131 registerTypeConverterLoader(bundle, r); 132 for (BaseService service : r) { 133 service.register(); 134 } 135 resolvers.put(bundle.getBundleId(), r); 136 } 137 138 return bundle; 139 } 140 141 private boolean extenderCapabilityWired(Bundle bundle) { 142 BundleWiring wiring = bundle.adapt(BundleWiring.class); 143 if (wiring == null) { 144 return true; 145 } 146 List<BundleWire> requiredWires = wiring.getRequiredWires(EXTENDER_NAMESPACE); 147 for (BundleWire requiredWire : requiredWires) { 148 if (CAMEL_EXTENDER.equals(requiredWire.getCapability().getAttributes().get(EXTENDER_NAMESPACE))) { 149 if (this.bundleId == requiredWire.getProviderWiring().getBundle().getBundleId()) { 150 LOG.debug("Camel extender requirement of bundle {} correctly wired to this implementation", bundle.getBundleId()); 151 return true; 152 } else { 153 LOG.info("Not processing bundle {} as it requires a camel extender but is not wired to the this implementation", bundle.getBundleId()); 154 return false; 155 } 156 } 157 } 158 return true; 159 } 160 161 public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) { 162 } 163 164 public void removedBundle(Bundle bundle, BundleEvent event, Object object) { 165 LOG.debug("Bundle stopped: {}", bundle.getSymbolicName()); 166 List<BaseService> r = resolvers.remove(bundle.getBundleId()); 167 if (r != null) { 168 for (BaseService service : r) { 169 service.unregister(); 170 } 171 } 172 } 173 174 protected void registerComponents(Bundle bundle, List<BaseService> resolvers) { 175 if (canSee(bundle, Component.class)) { 176 Map<String, String> components = new HashMap<String, String>(); 177 for (Enumeration<?> e = bundle.getEntryPaths(META_INF_COMPONENT); e != null && e.hasMoreElements();) { 178 String path = (String) e.nextElement(); 179 LOG.debug("Found entry: {} in bundle {}", path, bundle.getSymbolicName()); 180 String name = path.substring(path.lastIndexOf("/") + 1); 181 components.put(name, path); 182 } 183 if (!components.isEmpty()) { 184 resolvers.add(new BundleComponentResolver(bundle, components)); 185 } 186 } 187 } 188 189 protected void registerLanguages(Bundle bundle, List<BaseService> resolvers) { 190 if (canSee(bundle, Language.class)) { 191 Map<String, String> languages = new HashMap<String, String>(); 192 for (Enumeration<?> e = bundle.getEntryPaths(META_INF_LANGUAGE); e != null && e.hasMoreElements();) { 193 String path = (String) e.nextElement(); 194 LOG.debug("Found entry: {} in bundle {}", path, bundle.getSymbolicName()); 195 String name = path.substring(path.lastIndexOf("/") + 1); 196 languages.put(name, path); 197 } 198 if (!languages.isEmpty()) { 199 resolvers.add(new BundleLanguageResolver(bundle, languages)); 200 } 201 for (Enumeration<?> e = bundle.getEntryPaths(META_INF_LANGUAGE_RESOLVER); e != null && e.hasMoreElements();) { 202 String path = (String) e.nextElement(); 203 LOG.debug("Found entry: {} in bundle {}", path, bundle.getSymbolicName()); 204 String name = path.substring(path.lastIndexOf("/") + 1); 205 resolvers.add(new BundleMetaLanguageResolver(bundle, name, path)); 206 } 207 } 208 } 209 210 protected void registerDataFormats(Bundle bundle, List<BaseService> resolvers) { 211 if (canSee(bundle, DataFormat.class)) { 212 Map<String, String> dataformats = new HashMap<String, String>(); 213 for (Enumeration<?> e = bundle.getEntryPaths(META_INF_DATAFORMAT); e != null && e.hasMoreElements();) { 214 String path = (String) e.nextElement(); 215 LOG.debug("Found entry: {} in bundle {}", path, bundle.getSymbolicName()); 216 String name = path.substring(path.lastIndexOf("/") + 1); 217 dataformats.put(name, path); 218 } 219 if (!dataformats.isEmpty()) { 220 resolvers.add(new BundleDataFormatResolver(bundle, dataformats)); 221 } 222 } 223 } 224 225 protected void registerTypeConverterLoader(Bundle bundle, List<BaseService> resolvers) { 226 if (canSee(bundle, TypeConverter.class)) { 227 URL url1 = bundle.getEntry(META_INF_TYPE_CONVERTER); 228 URL url2 = bundle.getEntry(META_INF_FALLBACK_TYPE_CONVERTER); 229 if (url1 != null || url2 != null) { 230 LOG.debug("Found TypeConverter in bundle {}", bundle.getSymbolicName()); 231 resolvers.add(new BundleTypeConverterLoader(bundle, url2 != null)); 232 } 233 } 234 } 235 236 /** 237 * Check if bundle can see the given class 238 */ 239 protected boolean canSee(Bundle bundle, Class<?> clazz) { 240 if (bundle.getBundleId() == bundleId) { 241 // Need extra handling of camel core as it does not import the api 242 return true; 243 } 244 BundleCapability packageCap = packageCapabilities.get(clazz.getPackage().getName()); 245 if (packageCap != null) { 246 BundleWiring wiring = bundle.adapt(BundleWiring.class); 247 List<BundleWire> imports = wiring.getRequiredWires(PACKAGE_NAMESPACE); 248 for (BundleWire importWire : imports) { 249 if (packageCap.equals(importWire.getCapability())) { 250 return true; 251 } 252 } 253 } 254 255 // it may be running outside real OSGi container such as when unit testing with camel-test-blueprint 256 // then we need to use a different canSee algorithm that works outside real OSGi 257 if (bundle.getBundleId() > 0) { 258 Bundle root = bundle.getBundleContext().getBundle(0); 259 if (root != null && "org.apache.felix.connect".equals(root.getSymbolicName())) { 260 return checkCompat(bundle, clazz); 261 } 262 } 263 264 return false; 265 } 266 267 /** 268 * Check if bundle can see the given class used by camel-test-blueprint 269 */ 270 protected static boolean checkCompat(Bundle bundle, Class<?> clazz) { 271 // Check bundle compatibility 272 try { 273 if (bundle.loadClass(clazz.getName()) != clazz) { 274 return false; 275 } 276 } catch (Throwable t) { 277 return false; 278 } 279 return true; 280 } 281 282 protected static class BundleComponentResolver extends BaseResolver<Component> implements ComponentResolver { 283 284 private final Map<String, String> components; 285 286 public BundleComponentResolver(Bundle bundle, Map<String, String> components) { 287 super(bundle, Component.class); 288 this.components = components; 289 } 290 291 public Component resolveComponent(String name, CamelContext context) throws Exception { 292 return createInstance(name, components.get(name), context); 293 } 294 295 public void register() { 296 doRegister(ComponentResolver.class, "component", components.keySet()); 297 } 298 } 299 300 protected static class BundleLanguageResolver extends BaseResolver<Language> implements LanguageResolver { 301 302 private final Map<String, String> languages; 303 304 public BundleLanguageResolver(Bundle bundle, Map<String, String> languages) { 305 super(bundle, Language.class); 306 this.languages = languages; 307 } 308 309 public Language resolveLanguage(String name, CamelContext context) { 310 return createInstance(name, languages.get(name), context); 311 } 312 313 public void register() { 314 doRegister(LanguageResolver.class, "language", languages.keySet()); 315 } 316 } 317 318 protected static class BundleMetaLanguageResolver extends BaseResolver<LanguageResolver> implements LanguageResolver { 319 320 private final String name; 321 private final String path; 322 323 public BundleMetaLanguageResolver(Bundle bundle, String name, String path) { 324 super(bundle, LanguageResolver.class); 325 this.name = name; 326 this.path = path; 327 } 328 329 public Language resolveLanguage(String name, CamelContext context) { 330 LanguageResolver resolver = createInstance(this.name, path, context); 331 return resolver.resolveLanguage(name, context); 332 } 333 334 public void register() { 335 doRegister(LanguageResolver.class, "resolver", name); 336 } 337 } 338 339 protected static class BundleDataFormatResolver extends BaseResolver<DataFormat> implements DataFormatResolver { 340 341 private final Map<String, String> dataformats; 342 343 public BundleDataFormatResolver(Bundle bundle, Map<String, String> dataformats) { 344 super(bundle, DataFormat.class); 345 this.dataformats = dataformats; 346 } 347 348 public DataFormat resolveDataFormat(String name, CamelContext context) { 349 return createInstance(name, dataformats.get(name), context); 350 } 351 352 public DataFormatDefinition resolveDataFormatDefinition(String name, CamelContext context) { 353 return null; 354 } 355 356 public void register() { 357 doRegister(DataFormatResolver.class, "dataformat", dataformats.keySet()); 358 } 359 } 360 361 protected static class BundleTypeConverterLoader extends BaseResolver<TypeConverter> implements TypeConverterLoader { 362 363 private final AnnotationTypeConverterLoader loader = new Loader(); 364 private final Bundle bundle; 365 private final boolean hasFallbackTypeConverter; 366 367 public BundleTypeConverterLoader(Bundle bundle, boolean hasFallbackTypeConverter) { 368 super(bundle, TypeConverter.class); 369 ObjectHelper.notNull(bundle, "bundle"); 370 this.bundle = bundle; 371 this.hasFallbackTypeConverter = hasFallbackTypeConverter; 372 } 373 374 public synchronized void load(TypeConverterRegistry registry) throws TypeConverterLoaderException { 375 // must be synchronized to ensure we don't load type converters concurrently 376 // which cause Camel apps to fails in OSGi thereafter 377 try { 378 loader.load(registry); 379 } catch (Exception e) { 380 throw new TypeConverterLoaderException("Cannot load type converters using OSGi bundle: " + bundle.getBundleId(), e); 381 } 382 } 383 384 public void register() { 385 if (hasFallbackTypeConverter) { 386 // The FallbackTypeConverter should have a higher ranking 387 doRegister(TypeConverterLoader.class, Constants.SERVICE_RANKING, 100); 388 } else { 389 // The default service ranking is Integer(0); 390 doRegister(TypeConverterLoader.class); 391 } 392 } 393 394 class Loader extends AnnotationTypeConverterLoader { 395 396 Loader() { 397 super(null); 398 } 399 400 public void load(TypeConverterRegistry registry) throws TypeConverterLoaderException { 401 PackageScanFilter test = new AnnotatedWithPackageScanFilter(Converter.class, true); 402 Set<Class<?>> classes = new LinkedHashSet<Class<?>>(); 403 Set<String> packages = getConverterPackages(bundle.getEntry(META_INF_TYPE_CONVERTER)); 404 405 if (LOG.isTraceEnabled()) { 406 LOG.trace("Found {} {} packages: {}", new Object[]{packages.size(), META_INF_TYPE_CONVERTER, packages}); 407 } 408 // if we only have camel-core on the classpath then we have already pre-loaded all its type converters 409 // but we exposed the "org.apache.camel.core" package in camel-core. This ensures there is at least one 410 // packageName to scan, which triggers the scanning process. That allows us to ensure that we look for 411 // META-INF/services in all the JARs. 412 if (packages.size() == 1 && "org.apache.camel.core".equals(packages.iterator().next())) { 413 LOG.debug("No additional package names found in classpath for annotated type converters."); 414 // no additional package names found to load type converters so break out 415 return; 416 } 417 418 // now filter out org.apache.camel.core as its not needed anymore (it was just a dummy) 419 packages.remove("org.apache.camel.core"); 420 421 for (String pkg : packages) { 422 423 if (StringHelper.isClassName(pkg)) { 424 // its a FQN class name so load it directly 425 LOG.trace("Loading {} class", pkg); 426 try { 427 Class<?> clazz = bundle.loadClass(pkg); 428 if (test.matches(clazz)) { 429 classes.add(clazz); 430 } 431 // the class could be found and loaded so continue to next 432 continue; 433 } catch (Throwable t) { 434 // Ignore 435 LOG.trace("Failed to load " + pkg + " class due " + t.getMessage() + ". This exception will be ignored.", t); 436 } 437 } 438 439 // its not a FQN but a package name so scan for classes in the bundle 440 Enumeration<URL> e = bundle.findEntries("/" + pkg.replace('.', '/'), "*.class", true); 441 while (e != null && e.hasMoreElements()) { 442 String path = e.nextElement().getPath(); 443 String externalName = path.substring(path.charAt(0) == '/' ? 1 : 0, path.indexOf('.')).replace('/', '.'); 444 LOG.trace("Loading {} class", externalName); 445 try { 446 Class<?> clazz = bundle.loadClass(externalName); 447 if (test.matches(clazz)) { 448 classes.add(clazz); 449 } 450 } catch (Throwable t) { 451 // Ignore 452 LOG.trace("Failed to load " + externalName + " class due " + t.getMessage() + ". This exception will be ignored.", t); 453 } 454 } 455 } 456 457 // load the classes into type converter registry 458 LOG.debug("Found {} @Converter classes to load", classes.size()); 459 for (Class<?> type : classes) { 460 if (LOG.isTraceEnabled()) { 461 LOG.trace("Loading converter class: {}", ObjectHelper.name(type)); 462 } 463 loadConverterMethods(registry, type); 464 } 465 466 // register fallback converters 467 URL fallbackUrl = bundle.getEntry(META_INF_FALLBACK_TYPE_CONVERTER); 468 if (fallbackUrl != null) { 469 LOG.debug("Found {} to load the FallbackTypeConverter", META_INF_FALLBACK_TYPE_CONVERTER); 470 TypeConverter tc = createInstance("FallbackTypeConverter", fallbackUrl, registry.getInjector()); 471 registry.addFallbackTypeConverter(tc, false); 472 } 473 474 // now clear the maps so we do not hold references 475 visitedClasses.clear(); 476 visitedURIs.clear(); 477 } 478 } 479 480 } 481 482 protected abstract static class BaseResolver<T> extends BaseService { 483 484 private final Class<T> type; 485 486 public BaseResolver(Bundle bundle, Class<T> type) { 487 super(bundle); 488 this.type = type; 489 } 490 491 protected T createInstance(String name, String path, CamelContext context) { 492 if (path == null) { 493 return null; 494 } 495 URL url = bundle.getEntry(path); 496 LOG.trace("The entry {}'s url is {}", name, url); 497 //Setup the TCCL with Camel context application class loader 498 ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader(); 499 try { 500 ClassLoader newClassLoader = context.getApplicationContextClassLoader(); 501 if (newClassLoader != null) { 502 Thread.currentThread().setContextClassLoader(newClassLoader); 503 } 504 T answer = createInstance(name, url, context.getInjector()); 505 if (answer != null) { 506 initBundleContext(answer); 507 } 508 return answer; 509 } finally { 510 Thread.currentThread().setContextClassLoader(oldClassLoader); 511 } 512 } 513 514 private void initBundleContext(T answer) { 515 try { 516 Method method = answer.getClass().getMethod("setBundleContext", BundleContext.class); 517 if (method != null) { 518 method.invoke(answer, bundle.getBundleContext()); 519 } 520 } catch (Exception e) { 521 // ignore 522 } 523 } 524 525 @SuppressWarnings("unchecked") 526 protected T createInstance(String name, URL url, Injector injector) { 527 try { 528 Properties properties = loadProperties(url); 529 String classname = (String) properties.get("class"); 530 Class<?> type = bundle.loadClass(classname); 531 if (!this.type.isAssignableFrom(type)) { 532 throw new IllegalArgumentException("Type is not a " + this.type.getName() + " implementation. Found: " + type.getName()); 533 } 534 return injector.newInstance((Class<T>) type); 535 } catch (ClassNotFoundException e) { 536 throw new IllegalArgumentException("Invalid URI, no " + this.type.getName() + " registered for scheme : " + name, e); 537 } 538 } 539 540 } 541 542 protected abstract static class BaseService { 543 544 protected final Bundle bundle; 545 private ServiceRegistration<?> reg; 546 547 protected BaseService(Bundle bundle) { 548 this.bundle = bundle; 549 } 550 551 public abstract void register(); 552 553 protected void doRegister(Class<?> type, String key, Collection<String> value) { 554 doRegister(type, key, value.toArray(new String[value.size()])); 555 } 556 557 protected void doRegister(Class<?> type, String key, Object value) { 558 Dictionary<String, Object> props = new Hashtable<String, Object>(); 559 props.put(key, value); 560 doRegister(type, props); 561 } 562 563 protected void doRegister(Class<?> type) { 564 doRegister(type, null); 565 } 566 567 protected void doRegister(Class<?> type, Dictionary<String, ?> props) { 568 reg = bundle.getBundleContext().registerService(type.getName(), this, props); 569 } 570 571 public void unregister() { 572 reg.unregister(); 573 } 574 } 575 576 protected static Properties loadProperties(URL url) { 577 Properties properties = new Properties(); 578 BufferedInputStream reader = null; 579 try { 580 reader = IOHelper.buffered(url.openStream()); 581 properties.load(reader); 582 } catch (IOException e) { 583 throw new RuntimeException(e); 584 } finally { 585 IOHelper.close(reader, "properties", LOG); 586 } 587 return properties; 588 } 589 590 protected static Set<String> getConverterPackages(URL resource) { 591 Set<String> packages = new LinkedHashSet<String>(); 592 if (resource != null) { 593 BufferedReader reader = null; 594 try { 595 reader = IOHelper.buffered(new InputStreamReader(resource.openStream())); 596 while (true) { 597 String line = reader.readLine(); 598 if (line == null) { 599 break; 600 } 601 line = line.trim(); 602 if (line.startsWith("#") || line.length() == 0) { 603 continue; 604 } 605 StringTokenizer iter = new StringTokenizer(line, ","); 606 while (iter.hasMoreTokens()) { 607 String name = iter.nextToken().trim(); 608 if (name.length() > 0) { 609 packages.add(name); 610 } 611 } 612 } 613 } catch (Exception ignore) { 614 // Do nothing here 615 } finally { 616 IOHelper.close(reader, null, LOG); 617 } 618 } 619 return packages; 620 } 621 622} 623 624