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.OsgiCamelContextNameStrategy;
022    import org.apache.camel.core.osgi.OsgiClassResolver;
023    import org.apache.camel.core.osgi.OsgiFactoryFinderResolver;
024    import org.apache.camel.core.osgi.OsgiPackageScanClassResolver;
025    import org.apache.camel.core.osgi.OsgiTypeConverter;
026    import org.apache.camel.core.osgi.utils.BundleContextUtils;
027    import org.apache.camel.core.osgi.utils.BundleDelegatingClassLoader;
028    import org.apache.camel.impl.DefaultCamelContext;
029    import org.apache.camel.spi.FactoryFinder;
030    import org.apache.camel.spi.Registry;
031    import org.osgi.framework.BundleContext;
032    import org.osgi.service.blueprint.container.BlueprintContainer;
033    import org.slf4j.Logger;
034    import org.slf4j.LoggerFactory;
035    
036    public class BlueprintCamelContext extends DefaultCamelContext {
037    
038        private static final transient Logger LOG = LoggerFactory.getLogger(BlueprintCamelContext.class);
039    
040        private BundleContext bundleContext;
041        private BlueprintContainer blueprintContainer;
042    
043        public BlueprintCamelContext() {
044        }
045    
046        public BlueprintCamelContext(BundleContext bundleContext, BlueprintContainer blueprintContainer) {
047            this.bundleContext = bundleContext;
048            this.blueprintContainer = blueprintContainer;
049            setNameStrategy(new OsgiCamelContextNameStrategy(bundleContext));
050            setClassResolver(new OsgiClassResolver(bundleContext));
051            setFactoryFinderResolver(new OsgiFactoryFinderResolver(bundleContext));
052            setPackageScanClassResolver(new OsgiPackageScanClassResolver(bundleContext));
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            final ClassLoader original = Thread.currentThread().getContextClassLoader();
077            try {
078                // let's set a more suitable TCCL while starting the context
079                Thread.currentThread().setContextClassLoader(getApplicationContextClassLoader());
080                maybeStart();
081            } finally {
082                Thread.currentThread().setContextClassLoader(original);
083            }
084        }
085    
086        private void maybeStart() throws Exception {
087            if (!isStarted() && !isStarting()) {
088                start();
089            } else {
090                // ignore as Camel is already started
091                LOG.trace("Ignoring maybeStart() as Apache Camel is already started");
092            }
093        }
094    
095        public void destroy() throws Exception {
096            stop();
097        }
098    
099        @Override
100        protected TypeConverter createTypeConverter() {
101            // CAMEL-3614: make sure we use a bundle context which imports org.apache.camel.impl.converter package
102            BundleContext ctx = BundleContextUtils.getBundleContext(getClass());
103            if (ctx == null) {
104                ctx = bundleContext;
105            }
106            FactoryFinder finder = new OsgiFactoryFinderResolver(bundleContext).resolveDefaultFactoryFinder(getClassResolver());
107            return new OsgiTypeConverter(ctx, getInjector(), finder);
108        }
109    
110        @Override
111        protected Registry createRegistry() {
112            Registry reg = new BlueprintContainerRegistry(getBlueprintContainer());
113            return OsgiCamelContextHelper.wrapRegistry(this, reg, bundleContext);
114        }
115    
116    }