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.CompositeRegistry;
021 import org.apache.camel.core.osgi.OsgiCamelContextHelper;
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.OsgiServiceRegistry;
026 import org.apache.camel.core.osgi.OsgiTypeConverter;
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 setClassResolver(new OsgiClassResolver(bundleContext));
048 setFactoryFinderResolver(new OsgiFactoryFinderResolver(bundleContext));
049 setPackageScanClassResolver(new OsgiPackageScanClassResolver(bundleContext));
050 setComponentResolver(new BlueprintComponentResolver(bundleContext));
051 setLanguageResolver(new BlueprintLanguageResolver(bundleContext));
052 setDataFormatResolver(new BlueprintDataFormatResolver(bundleContext));
053 }
054
055 public BundleContext getBundleContext() {
056 return bundleContext;
057 }
058
059 public void setBundleContext(BundleContext bundleContext) {
060 this.bundleContext = bundleContext;
061 }
062
063 public BlueprintContainer getBlueprintContainer() {
064 return blueprintContainer;
065 }
066
067 public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
068 this.blueprintContainer = blueprintContainer;
069 }
070
071 public void init() throws Exception {
072 maybeStart();
073 }
074
075 private void maybeStart() throws Exception {
076 if (!isStarted() && !isStarting()) {
077 // Make sure we will not get into the endless loop of calling star
078 LOG.info("Starting Apache Camel as property ShouldStartContext is true");
079 start();
080 } else {
081 // ignore as Camel is already started
082 LOG.trace("Ignoring maybeStart() as Apache Camel is already started");
083 }
084 }
085
086 public void destroy() throws Exception {
087 stop();
088 }
089
090 @Override
091 protected TypeConverter createTypeConverter() {
092 return new OsgiTypeConverter(bundleContext, getInjector());
093 }
094
095 @Override
096 protected Registry createRegistry() {
097 Registry reg = new BlueprintContainerRegistry(getBlueprintContainer());
098 return OsgiCamelContextHelper.wrapRegistry(this, reg, bundleContext);
099 }
100
101 }