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.impl.DefaultCamelContext;
027 import org.apache.camel.spi.Registry;
028 import org.apache.commons.logging.Log;
029 import org.apache.commons.logging.LogFactory;
030 import org.osgi.framework.BundleContext;
031 import org.osgi.service.blueprint.container.BlueprintContainer;
032
033 public class BlueprintCamelContext extends DefaultCamelContext {
034
035 private static final transient Log LOG = LogFactory.getLog(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 setNameStrategy(new OsgiCamelContextNameStrategy(bundleContext));
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 start();
078 } else {
079 // ignore as Camel is already started
080 LOG.trace("Ignoring maybeStart() as Apache Camel is already started");
081 }
082 }
083
084 public void destroy() throws Exception {
085 stop();
086 }
087
088 @Override
089 protected TypeConverter createTypeConverter() {
090 return new OsgiTypeConverter(bundleContext, getInjector());
091 }
092
093 @Override
094 protected Registry createRegistry() {
095 Registry reg = new BlueprintContainerRegistry(getBlueprintContainer());
096 return OsgiCamelContextHelper.wrapRegistry(this, reg, bundleContext);
097 }
098
099 }