Author: ningjiang Date: Thu Jul 9 04:51:07 2009 New Revision: 792398 URL: http://svn.apache.org/viewvc?rev=792398&view=rev Log: CAMEL-1789 Let the Camel Route support to lookup the service which is exported from the OSGi bundle
Added: camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CompositeRegistry.java (with props) camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiServiceRegistry.java (with props) camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelContextFactoryTest.java (with props) camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelMockBundleContext.java (with props) camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyService.java (with props) Modified: camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactory.java camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelOsgiTestSupport.java Modified: camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactory.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactory.java?rev=792398&r1=792397&r2=792398&view=diff ============================================================================== --- camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactory.java (original) +++ camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactory.java Thu Jul 9 04:51:07 2009 @@ -18,10 +18,13 @@ import java.util.List; +import org.apache.camel.CamelContext; import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.converter.AnnotationTypeConverterLoader; import org.apache.camel.impl.converter.DefaultTypeConverter; import org.apache.camel.impl.converter.TypeConverterLoader; +import org.apache.camel.spring.SpringCamelContext; +import org.apache.camel.util.ObjectHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; @@ -54,6 +57,7 @@ if (LOG.isDebugEnabled()) { LOG.debug("Using OSGI resolvers"); } + updateRegistry(context); LOG.debug("Using OsgiFactoryFinderResolver"); context.setFactoryFinderResolver(new OsgiFactoryFinderResolver()); LOG.debug("Using OsgiPackageScanClassResolver"); @@ -64,13 +68,23 @@ context.setLanguageResolver(new OsgiLanguageResolver()); addOsgiAnnotationTypeConverterLoader(context, bundleContext); } else { - // TODO: should we not thrown an excpetion to not allow it to startup + // TODO: should we not thrown an exception to not allow it to startup LOG.warn("BundleContext not set, cannot run in OSGI container"); } return context; } + protected void updateRegistry(DefaultCamelContext context) { + ObjectHelper.notNull(bundleContext, "BundleContext"); + LOG.debug("Setting the OSGi ServiceRegistry"); + OsgiServiceRegistry osgiServiceRegistry = new OsgiServiceRegistry(bundleContext); + CompositeRegistry compositeRegistry = new CompositeRegistry(); + compositeRegistry.addRegistry(osgiServiceRegistry); + compositeRegistry.addRegistry(context.getRegistry()); + context.setRegistry(compositeRegistry); + } + protected void addOsgiAnnotationTypeConverterLoader(DefaultCamelContext context, BundleContext bundleContext) { LOG.debug("Using OsgiAnnotationTypeConverterLoader"); DefaultTypeConverter typeConverter = (DefaultTypeConverter) context.getTypeConverter(); Modified: camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java?rev=792398&r1=792397&r2=792398&view=diff ============================================================================== --- camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java (original) +++ camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CamelContextFactoryBean.java Thu Jul 9 04:51:07 2009 @@ -22,10 +22,13 @@ import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; +import org.apache.camel.CamelContext; +import org.apache.camel.impl.DefaultCamelContext; import org.apache.camel.impl.converter.AnnotationTypeConverterLoader; import org.apache.camel.impl.converter.DefaultTypeConverter; import org.apache.camel.impl.converter.TypeConverterLoader; import org.apache.camel.spring.SpringCamelContext; +import org.apache.camel.util.ObjectHelper; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.framework.BundleContext; @@ -56,6 +59,7 @@ if (LOG.isDebugEnabled()) { LOG.debug("Using OSGI resolvers"); } + updateRegistry(context); LOG.debug("Using OsgiFactoryFinderResolver"); context.setFactoryFinderResolver(new OsgiFactoryFinderResolver()); LOG.debug("Using OsgiPackageScanClassResolver"); @@ -73,6 +77,16 @@ return context; } + protected void updateRegistry(DefaultCamelContext context) { + ObjectHelper.notNull(bundleContext, "BundleContext"); + LOG.debug("Setting the OSGi ServiceRegistry"); + OsgiServiceRegistry osgiServiceRegistry = new OsgiServiceRegistry(bundleContext); + CompositeRegistry compositeRegistry = new CompositeRegistry(); + compositeRegistry.addRegistry(osgiServiceRegistry); + compositeRegistry.addRegistry(context.getRegistry()); + context.setRegistry(compositeRegistry); + } + protected void addOsgiAnnotationTypeConverterLoader(SpringCamelContext context) { LOG.debug("Using OsgiAnnotationTypeConverterLoader"); Added: camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CompositeRegistry.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CompositeRegistry.java?rev=792398&view=auto ============================================================================== --- camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CompositeRegistry.java (added) +++ camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CompositeRegistry.java Thu Jul 9 04:51:07 2009 @@ -0,0 +1,79 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.camel.osgi; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.apache.camel.spi.Registry; + +/** + * This registry will look up the object with the sequence of the registry list untill it find the Object. + */ +public class CompositeRegistry implements Registry { + private List<Registry> registryList; + + public CompositeRegistry() { + registryList = new ArrayList<Registry>(); + } + + public CompositeRegistry(List<Registry> registries) { + registryList = registries; + } + + public void addRegistry(Registry registry) { + registryList.add(registry); + } + + public <T> T lookup(String name, Class<T> type) { + T answer = null; + for (Registry registry : registryList) { + answer = registry.lookup(name, type); + if (answer != null) { + break; + } + } + return answer; + } + + public Object lookup(String name) { + Object answer = null; + for (Registry registry : registryList) { + answer = registry.lookup(name); + if (answer != null) { + break; + } + } + return answer; + } + + @SuppressWarnings("unchecked") + public <T> Map<String, T> lookupByType(Class<T> type) { + Map<String, T> answer = Collections.EMPTY_MAP; + for (Registry registry : registryList) { + answer = registry.lookupByType(type); + if (answer != Collections.EMPTY_MAP) { + break; + } + } + return answer; + } + +} Propchange: camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CompositeRegistry.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/CompositeRegistry.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiServiceRegistry.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiServiceRegistry.java?rev=792398&view=auto ============================================================================== --- camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiServiceRegistry.java (added) +++ camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiServiceRegistry.java Thu Jul 9 04:51:07 2009 @@ -0,0 +1,66 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.osgi; + +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.camel.spi.Registry; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.springframework.osgi.context.BundleContextAware; + +/** + * The OsgiServiceRegistry support to get the service object from the bundle context + */ +public class OsgiServiceRegistry implements Registry { + private BundleContext bundleContext; + private Map<String, Object> serviceCacheMap = new ConcurrentHashMap<String, Object>(); + + public OsgiServiceRegistry(BundleContext bc) { + bundleContext = bc; + } + + public <T> T lookup(String name, Class<T> type) { + Object service = lookup(name); + return type.cast(service); + } + + public Object lookup(String name) { + Object service = serviceCacheMap.get(name); + if (service == null) { + ServiceReference sr = bundleContext.getServiceReference(name); + if (sr != null) { + // TODO need to keep the track of Service + // and call ungetService when the camel context is closed + service = bundleContext.getService(sr); + if (service != null) { + serviceCacheMap.put(name, service); + } + } + } + return service; + } + + @SuppressWarnings("unchecked") + public <T> Map<String, T> lookupByType(Class<T> type) { + // not implemented so we return an empty map + return Collections.EMPTY_MAP; + } + +} Propchange: camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiServiceRegistry.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiServiceRegistry.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelContextFactoryTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelContextFactoryTest.java?rev=792398&view=auto ============================================================================== --- camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelContextFactoryTest.java (added) +++ camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelContextFactoryTest.java Thu Jul 9 04:51:07 2009 @@ -0,0 +1,34 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.camel.osgi; + +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.osgi.test.MyService; +import org.junit.Test; + +public class CamelContextFactoryTest extends CamelOsgiTestSupport { + @Test + public void osigServiceRegistryTest() { + CamelContextFactory factory = new CamelContextFactory(); + factory.setBundleContext(getBundleContext()); + DefaultCamelContext context = factory.createContext(); + MyService myService = context.getRegistry().lookup(MyService.class.getName(), MyService.class); + assertNotNull("MyService should not be null", myService); + } + +} Propchange: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelContextFactoryTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelContextFactoryTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelMockBundleContext.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelMockBundleContext.java?rev=792398&view=auto ============================================================================== --- camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelMockBundleContext.java (added) +++ camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelMockBundleContext.java Thu Jul 9 04:51:07 2009 @@ -0,0 +1,40 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.camel.osgi; + +import org.apache.camel.osgi.test.MyService; +import org.osgi.framework.Constants; +import org.osgi.framework.ServiceReference; +import org.springframework.osgi.mock.MockBundleContext; + +/** + * + */ +public class CamelMockBundleContext extends MockBundleContext { + + public Object getService(ServiceReference reference) { + String[] classNames = (String[]) reference.getProperty(Constants.OBJECTCLASS); + System.out.println("The class name is " + classNames[0]); + if (classNames[0].equals("org.apache.camel.osgi.test.MyService")) { + return new MyService(); + } else { + return null; + } + } + +} Propchange: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelMockBundleContext.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelMockBundleContext.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelOsgiTestSupport.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelOsgiTestSupport.java?rev=792398&r1=792397&r2=792398&view=diff ============================================================================== --- camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelOsgiTestSupport.java (original) +++ camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelOsgiTestSupport.java Thu Jul 9 04:51:07 2009 @@ -16,7 +16,6 @@ */ package org.apache.camel.osgi; -import junit.framework.TestCase; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -26,7 +25,7 @@ public class CamelOsgiTestSupport extends Assert { private Activator testActivator; - private MockBundleContext bundleContext = new MockBundleContext(); + private MockBundleContext bundleContext = new CamelMockBundleContext(); private OsgiPackageScanClassResolver resolver = new OsgiPackageScanClassResolver(bundleContext); private MockBundle bundle = new CamelMockBundle(); Added: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyService.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyService.java?rev=792398&view=auto ============================================================================== --- camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyService.java (added) +++ camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyService.java Thu Jul 9 04:51:07 2009 @@ -0,0 +1,24 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.osgi.test; + +public class MyService { + public String sayHi() { + return "Hello"; + } + +} Propchange: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyService.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/test/MyService.java ------------------------------------------------------------------------------ svn:keywords = Rev Date