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.framework.ServiceEvent;
030 import org.osgi.framework.ServiceListener;
031 import org.osgi.service.blueprint.container.BlueprintContainer;
032 import org.slf4j.Logger;
033 import org.slf4j.LoggerFactory;
034
035 public class BlueprintCamelContext extends DefaultCamelContext implements ServiceListener {
036
037 private static final transient Logger LOG = LoggerFactory.getLogger(BlueprintCamelContext.class);
038
039 private BundleContext bundleContext;
040 private BlueprintContainer blueprintContainer;
041
042 public BlueprintCamelContext() {
043 }
044
045 public BlueprintCamelContext(BundleContext bundleContext, BlueprintContainer blueprintContainer) {
046 this.bundleContext = bundleContext;
047 this.blueprintContainer = blueprintContainer;
048
049 // inject common osgi
050 OsgiCamelContextHelper.osgiUpdate(this, bundleContext);
051
052 // and these are blueprint specific
053 setComponentResolver(new BlueprintComponentResolver(bundleContext));
054 setLanguageResolver(new BlueprintLanguageResolver(bundleContext));
055 setDataFormatResolver(new BlueprintDataFormatResolver(bundleContext));
056 setApplicationContextClassLoader(new BundleDelegatingClassLoader(bundleContext.getBundle()));
057 }
058
059 public BundleContext getBundleContext() {
060 return bundleContext;
061 }
062
063 public void setBundleContext(BundleContext bundleContext) {
064 this.bundleContext = bundleContext;
065 }
066
067 public BlueprintContainer getBlueprintContainer() {
068 return blueprintContainer;
069 }
070
071 public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
072 this.blueprintContainer = blueprintContainer;
073 }
074
075 public void init() throws Exception {
076 // add service listener so we can be notified when blueprint container is done
077 // and we would be ready to start CamelContext
078 bundleContext.addServiceListener(this);
079 }
080
081 public void destroy() throws Exception {
082 // remove listener and stop this CamelContext
083 bundleContext.removeServiceListener(this);
084 stop();
085 }
086
087 @Override
088 public void serviceChanged(ServiceEvent event) {
089 if (LOG.isDebugEnabled()) {
090 LOG.debug("Service {} changed to {}", event, event.getType());
091 }
092 // look for blueprint container to be registered, and then we can start the CamelContext
093 if (event.getType() == ServiceEvent.REGISTERED && event.getServiceReference().isAssignableTo(bundleContext.getBundle(),
094 "org.osgi.service.blueprint.container.BlueprintContainer")) {
095 try {
096 maybeStart();
097 } catch (Exception e) {
098 LOG.warn("Error occurred during starting " + this, e);
099 }
100 }
101 }
102
103 @Override
104 protected TypeConverter createTypeConverter() {
105 // CAMEL-3614: make sure we use a bundle context which imports org.apache.camel.impl.converter package
106 BundleContext ctx = BundleContextUtils.getBundleContext(getClass());
107 if (ctx == null) {
108 ctx = bundleContext;
109 }
110 FactoryFinder finder = new OsgiFactoryFinderResolver(bundleContext).resolveDefaultFactoryFinder(getClassResolver());
111 return new OsgiTypeConverter(ctx, getInjector(), finder);
112 }
113
114 @Override
115 protected Registry createRegistry() {
116 Registry reg = new BlueprintContainerRegistry(getBlueprintContainer());
117 return OsgiCamelContextHelper.wrapRegistry(this, reg, bundleContext);
118 }
119
120 private void maybeStart() throws Exception {
121 if (!isStarted() && !isStarting()) {
122 final ClassLoader original = Thread.currentThread().getContextClassLoader();
123 try {
124 // let's set a more suitable TCCL while starting the context
125 Thread.currentThread().setContextClassLoader(getApplicationContextClassLoader());
126 LOG.debug("Starting {}", this);
127 start();
128 } finally {
129 Thread.currentThread().setContextClassLoader(original);
130 }
131 } else {
132 // ignore as Camel is already started
133 LOG.trace("Ignoring maybeStart() as {} is already started", this);
134 }
135 }
136 }