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.impl.DefaultCamelContext;
028 import org.apache.camel.spi.Registry;
029 import org.apache.commons.logging.Log;
030 import org.apache.commons.logging.LogFactory;
031 import org.osgi.framework.BundleContext;
032 import org.osgi.service.blueprint.container.BlueprintContainer;
033
034 public class BlueprintCamelContext extends DefaultCamelContext {
035
036 private static final transient Log LOG = LogFactory.getLog(BlueprintCamelContext.class);
037
038 private BundleContext bundleContext;
039 private BlueprintContainer blueprintContainer;
040
041 public BlueprintCamelContext() {
042 }
043
044 public BlueprintCamelContext(BundleContext bundleContext, BlueprintContainer blueprintContainer) {
045 this.bundleContext = bundleContext;
046 this.blueprintContainer = blueprintContainer;
047 setNameStrategy(new OsgiCamelContextNameStrategy(bundleContext));
048 setClassResolver(new OsgiClassResolver(bundleContext));
049 setFactoryFinderResolver(new OsgiFactoryFinderResolver(bundleContext));
050 setPackageScanClassResolver(new OsgiPackageScanClassResolver(bundleContext));
051 setComponentResolver(new BlueprintComponentResolver(bundleContext));
052 setLanguageResolver(new BlueprintLanguageResolver(bundleContext));
053 setDataFormatResolver(new BlueprintDataFormatResolver(bundleContext));
054 }
055
056 public BundleContext getBundleContext() {
057 return bundleContext;
058 }
059
060 public void setBundleContext(BundleContext bundleContext) {
061 this.bundleContext = bundleContext;
062 }
063
064 public BlueprintContainer getBlueprintContainer() {
065 return blueprintContainer;
066 }
067
068 public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
069 this.blueprintContainer = blueprintContainer;
070 }
071
072 public void init() throws Exception {
073 maybeStart();
074 }
075
076 private void maybeStart() throws Exception {
077 if (!isStarted() && !isStarting()) {
078 start();
079 } else {
080 // ignore as Camel is already started
081 LOG.trace("Ignoring maybeStart() as Apache Camel is already started");
082 }
083 }
084
085 public void destroy() throws Exception {
086 stop();
087 }
088
089 @Override
090 protected TypeConverter createTypeConverter() {
091 // CAMEL-3614: make sure we use a bundle context which imports org.apache.camel.impl.converter package
092 BundleContext ctx = BundleContextUtils.getBundleContext(getClass());
093 if (ctx == null) {
094 ctx = bundleContext;
095 }
096 return new OsgiTypeConverter(ctx, getInjector());
097 }
098
099 @Override
100 protected Registry createRegistry() {
101 Registry reg = new BlueprintContainerRegistry(getBlueprintContainer());
102 return OsgiCamelContextHelper.wrapRegistry(this, reg, bundleContext);
103 }
104
105 }