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 */
017 package org.apache.camel.blueprint;
018
019 import org.apache.camel.TypeConverter;
020 import org.apache.camel.core.osgi.OsgiCamelContextHelper;
021 import org.apache.camel.core.osgi.OsgiFactoryFinderResolver;
022 import org.apache.camel.core.osgi.OsgiTypeConverter;
023 import org.apache.camel.core.osgi.utils.BundleContextUtils;
024 import org.apache.camel.core.osgi.utils.BundleDelegatingClassLoader;
025 import org.apache.camel.impl.DefaultCamelContext;
026 import org.apache.camel.spi.FactoryFinder;
027 import org.apache.camel.spi.Registry;
028 import org.osgi.framework.BundleContext;
029 import org.osgi.service.blueprint.container.BlueprintContainer;
030 import org.slf4j.Logger;
031 import org.slf4j.LoggerFactory;
032
033 public class BlueprintCamelContext extends DefaultCamelContext {
034
035 private static final transient Logger LOG = LoggerFactory.getLogger(BlueprintCamelContext.class);
036
037 private BundleContext bundleContext;
038 private BlueprintContainer blueprintContainer;
039
040 public BlueprintCamelContext() {
041 }
042
043 public BlueprintCamelContext(BundleContext bundleContext, BlueprintContainer blueprintContainer) {
044 this.bundleContext = bundleContext;
045 this.blueprintContainer = blueprintContainer;
046
047 // inject common osgi
048 OsgiCamelContextHelper.osgiUpdate(this, bundleContext);
049
050 // and these are blueprint specific
051 setComponentResolver(new BlueprintComponentResolver(bundleContext));
052 setLanguageResolver(new BlueprintLanguageResolver(bundleContext));
053 setDataFormatResolver(new BlueprintDataFormatResolver(bundleContext));
054 setApplicationContextClassLoader(new BundleDelegatingClassLoader(bundleContext.getBundle()));
055 }
056
057 public BundleContext getBundleContext() {
058 return bundleContext;
059 }
060
061 public void setBundleContext(BundleContext bundleContext) {
062 this.bundleContext = bundleContext;
063 }
064
065 public BlueprintContainer getBlueprintContainer() {
066 return blueprintContainer;
067 }
068
069 public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
070 this.blueprintContainer = blueprintContainer;
071 }
072
073 public void init() throws Exception {
074 final ClassLoader original = Thread.currentThread().getContextClassLoader();
075 try {
076 // let's set a more suitable TCCL while starting the context
077 Thread.currentThread().setContextClassLoader(getApplicationContextClassLoader());
078 maybeStart();
079 } finally {
080 Thread.currentThread().setContextClassLoader(original);
081 }
082 }
083
084 private void maybeStart() throws Exception {
085 if (!isStarted() && !isStarting()) {
086 start();
087 } else {
088 // ignore as Camel is already started
089 LOG.trace("Ignoring maybeStart() as Apache Camel is already started");
090 }
091 }
092
093 public void destroy() throws Exception {
094 stop();
095 }
096
097 @Override
098 protected TypeConverter createTypeConverter() {
099 // CAMEL-3614: make sure we use a bundle context which imports org.apache.camel.impl.converter package
100 BundleContext ctx = BundleContextUtils.getBundleContext(getClass());
101 if (ctx == null) {
102 ctx = bundleContext;
103 }
104 FactoryFinder finder = new OsgiFactoryFinderResolver(bundleContext).resolveDefaultFactoryFinder(getClassResolver());
105 return new OsgiTypeConverter(ctx, getInjector(), finder);
106 }
107
108 @Override
109 protected Registry createRegistry() {
110 Registry reg = new BlueprintContainerRegistry(getBlueprintContainer());
111 return OsgiCamelContextHelper.wrapRegistry(this, reg, bundleContext);
112 }
113
114 }