You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ni...@apache.org on 2009/09/05 15:25:26 UTC
svn commit: r811642 - in /camel/trunk/components/camel-osgi/src:
main/java/org/apache/camel/osgi/ test/java/org/apache/camel/osgi/
Author: ningjiang
Date: Sat Sep 5 13:25:26 2009
New Revision: 811642
URL: http://svn.apache.org/viewvc?rev=811642&view=rev
Log:
CAMEL-1990 clean up the OSGi service reference when the camel context is closed
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/main/java/org/apache/camel/osgi/OsgiServiceRegistry.java
camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelContextFactoryTest.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=811642&r1=811641&r2=811642&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 Sat Sep 5 13:25:26 2009
@@ -79,6 +79,8 @@
ObjectHelper.notNull(bundleContext, "BundleContext");
LOG.debug("Setting the OSGi ServiceRegistry");
OsgiServiceRegistry osgiServiceRegistry = new OsgiServiceRegistry(bundleContext);
+ // Need to clean up the OSGi service when camel context is closed.
+ context.addLifecycleStrategy(osgiServiceRegistry);
CompositeRegistry compositeRegistry = new CompositeRegistry();
compositeRegistry.addRegistry(osgiServiceRegistry);
compositeRegistry.addRegistry(context.getRegistry());
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=811642&r1=811641&r2=811642&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 Sat Sep 5 13:25:26 2009
@@ -81,6 +81,8 @@
ObjectHelper.notNull(bundleContext, "BundleContext");
LOG.debug("Setting the OSGi ServiceRegistry");
OsgiServiceRegistry osgiServiceRegistry = new OsgiServiceRegistry(bundleContext);
+ // Need to clean up the OSGi service when camel context is closed.
+ context.addLifecycleStrategy(osgiServiceRegistry);
CompositeRegistry compositeRegistry = new CompositeRegistry();
compositeRegistry.addRegistry(osgiServiceRegistry);
compositeRegistry.addRegistry(context.getRegistry());
Modified: 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=811642&r1=811641&r2=811642&view=diff
==============================================================================
--- camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiServiceRegistry.java (original)
+++ camel/trunk/components/camel-osgi/src/main/java/org/apache/camel/osgi/OsgiServiceRegistry.java Sat Sep 5 13:25:26 2009
@@ -16,11 +16,20 @@
*/
package org.apache.camel.osgi;
+import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Component;
+import org.apache.camel.Endpoint;
+import org.apache.camel.Route;
+import org.apache.camel.Service;
+import org.apache.camel.spi.LifecycleStrategy;
import org.apache.camel.spi.Registry;
+import org.apache.camel.spi.RouteContext;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.springframework.osgi.context.BundleContextAware;
@@ -28,9 +37,10 @@
/**
* The OsgiServiceRegistry support to get the service object from the bundle context
*/
-public class OsgiServiceRegistry implements Registry {
+public class OsgiServiceRegistry implements Registry, LifecycleStrategy {
private BundleContext bundleContext;
- private Map<String, Object> serviceCacheMap = new ConcurrentHashMap<String, Object>();
+ private Map<String, Object> serviceCacheMap = new ConcurrentHashMap<String, Object>();
+ private ConcurrentLinkedQueue<ServiceReference> serviceReferenceQueue = new ConcurrentLinkedQueue<ServiceReference>();
public OsgiServiceRegistry(BundleContext bc) {
bundleContext = bc;
@@ -46,8 +56,9 @@
if (service == null) {
ServiceReference sr = bundleContext.getServiceReference(name);
if (sr != null) {
- // TODO need to keep the track of Service
+ // Need to keep the track of Service
// and call ungetService when the camel context is closed
+ serviceReferenceQueue.add(sr);
service = bundleContext.getService(sr);
if (service != null) {
serviceCacheMap.put(name, service);
@@ -63,4 +74,65 @@
return Collections.EMPTY_MAP;
}
+ public void onComponentAdd(String name, Component component) {
+ // Do nothing here
+
+ }
+
+ public void onComponentRemove(String name, Component component) {
+ // Do nothing here
+
+ }
+
+ public void onContextStart(CamelContext context) {
+ // Do nothing here
+
+ }
+
+ public void onContextStop(CamelContext context) {
+ // Unget the OSGi service
+ ServiceReference sr = serviceReferenceQueue.poll();
+ while (sr != null) {
+ bundleContext.ungetService(sr);
+ sr = serviceReferenceQueue.poll();
+ }
+ // Clean up the OSGi Service Cache
+ serviceCacheMap.clear();
+ }
+
+ public void onEndpointAdd(Endpoint endpoint) {
+ // Do nothing here
+
+ }
+
+ public void onEndpointRemove(Endpoint endpoint) {
+ // Do nothing here
+
+ }
+
+ public void onRouteContextCreate(RouteContext routeContext) {
+ // Do nothing here
+
+ }
+
+ public void onRoutesAdd(Collection<Route> routes) {
+ // Do nothing here
+
+ }
+
+ public void onRoutesRemove(Collection<Route> routes) {
+ // Do nothing here
+
+ }
+
+ public void onServiceAdd(CamelContext context, Service service, Route route) {
+ // Do nothing here
+
+ }
+
+ public void onServiceRemove(CamelContext context, Service service, Route route) {
+ // Do nothing here
+
+ }
+
}
Modified: 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=811642&r1=811641&r2=811642&view=diff
==============================================================================
--- camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelContextFactoryTest.java (original)
+++ camel/trunk/components/camel-osgi/src/test/java/org/apache/camel/osgi/CamelContextFactoryTest.java Sat Sep 5 13:25:26 2009
@@ -23,12 +23,14 @@
public class CamelContextFactoryTest extends CamelOsgiTestSupport {
@Test
- public void osigServiceRegistryTest() {
+ public void osigServiceRegistryTest() throws Exception {
CamelContextFactory factory = new CamelContextFactory();
factory.setBundleContext(getBundleContext());
DefaultCamelContext context = factory.createContext();
+ context.start();
MyService myService = context.getRegistry().lookup(MyService.class.getName(), MyService.class);
assertNotNull("MyService should not be null", myService);
+ context.stop();
}
}