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 */ 017package org.apache.camel.osgi; 018 019import org.apache.camel.TypeConverter; 020import org.apache.camel.core.osgi.OsgiCamelContextHelper; 021import org.apache.camel.core.osgi.OsgiFactoryFinderResolver; 022import org.apache.camel.core.osgi.OsgiTypeConverter; 023import org.apache.camel.core.osgi.utils.BundleContextUtils; 024import org.apache.camel.spi.FactoryFinder; 025import org.apache.camel.spi.Registry; 026import org.apache.camel.spring.SpringCamelContext; 027import org.osgi.framework.BundleContext; 028import org.springframework.context.ApplicationContext; 029 030public class OsgiSpringCamelContext extends SpringCamelContext { 031 032 private final BundleContext bundleContext; 033 034 public OsgiSpringCamelContext(ApplicationContext applicationContext, BundleContext bundleContext) { 035 super(applicationContext); 036 this.bundleContext = bundleContext; 037 OsgiCamelContextHelper.osgiUpdate(this, bundleContext); 038 } 039 040 @Override 041 protected TypeConverter createTypeConverter() { 042 // CAMEL-3614: make sure we use a bundle context which imports org.apache.camel.impl.converter package 043 BundleContext ctx = BundleContextUtils.getBundleContext(getClass()); 044 if (ctx == null) { 045 ctx = bundleContext; 046 } 047 FactoryFinder finder = new OsgiFactoryFinderResolver(bundleContext).resolveDefaultFactoryFinder(getClassResolver()); 048 return new OsgiTypeConverter(ctx, getInjector(), finder); 049 } 050 051 @Override 052 protected Registry createRegistry() { 053 return OsgiCamelContextHelper.wrapRegistry(this, super.createRegistry(), bundleContext); 054 } 055 056 @Override 057 public void setName(String name) { 058 super.setName(name); 059 // in OSGi prefix the bundle id to the management name so it will be unique in the JVM 060 // and also nicely sorted based on bundle id 061 super.setManagementName(bundleContext.getBundle().getBundleId() + "-" + name); 062 } 063 064}