Hi Claus,

I'm my bad, I should not import the class, and it is useless.
I will commit a quick fix for it shortly.

Thanks for pointing that out.  

--  
Willem Jiang

Red Hat, Inc.
FuseSource is now part of Red Hat
Web: http://www.fusesource.com | http://www.redhat.com
Blog: http://willemjiang.blogspot.com (http://willemjiang.blogspot.com/) 
(English)
          http://jnn.iteye.com (http://jnn.javaeye.com/) (Chinese)
Twitter: willemjiang  
Weibo: 姜宁willem





On Friday, January 18, 2013 at 3:03 PM, Claus Ibsen wrote:

> On Fri, Jan 18, 2013 at 2:57 AM, <ningji...@apache.org 
> (mailto:ningji...@apache.org)> wrote:
> > Author: ningjiang
> > Date: Fri Jan 18 01:57:33 2013
> > New Revision: 1435006
> >  
> > URL: http://svn.apache.org/viewvc?rev=1435006&view=rev
> > Log:
> > CAMEL-5969, CAMEL-5972 added the OsgiiServiceRegistry.lookupByType() and 
> > supports to lookup service with filter on name=blabla
> >  
> > Modified:
> > camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java
> > camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundleContext.java
> > camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/ServiceRegistryTest.java
> >  
> > Modified: 
> > camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java
> > URL: 
> > http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java?rev=1435006&r1=1435005&r2=1435006&view=diff
> > ==============================================================================
> > --- 
> > camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java
> >  (original)
> > +++ 
> > camel/trunk/components/camel-core-osgi/src/main/java/org/apache/camel/core/osgi/OsgiServiceRegistry.java
> >  Fri Jan 18 01:57:33 2013
> > @@ -17,6 +17,7 @@
> > package org.apache.camel.core.osgi;
> >  
> > import java.util.Collections;
> > +import java.util.HashMap;
> > import java.util.Map;
> > import java.util.Queue;
> > import java.util.concurrent.ConcurrentHashMap;
> > @@ -25,6 +26,7 @@ import java.util.concurrent.ConcurrentLi
> > import org.apache.camel.CamelContext;
> > import org.apache.camel.spi.Registry;
> > import org.apache.camel.support.LifecycleStrategySupport;
> > +import org.apache.camel.util.ObjectHelper;
> > import org.osgi.framework.BundleContext;
> > import org.osgi.framework.ServiceReference;
> >  
> > @@ -34,21 +36,46 @@ import org.osgi.framework.ServiceReferen
> > public class OsgiServiceRegistry extends LifecycleStrategySupport 
> > implements Registry {
> > private final BundleContext bundleContext;
> > private final Map<String, Object> serviceCacheMap = new 
> > ConcurrentHashMap<String, Object>();
> > - private final Queue<ServiceReference> serviceReferenceQueue = new 
> > ConcurrentLinkedQueue<ServiceReference>();
> > + private final Map<Class<?>, Map<String, Object>> typeCacheMap = new 
> > ConcurrentHashMap<Class<?>, Map<String, Object>>();
> > + private final Queue<ServiceReference<?>> serviceReferenceQueue = new 
> > ConcurrentLinkedQueue<ServiceReference<?>>();
> >  
> > public OsgiServiceRegistry(BundleContext bc) {
> > bundleContext = bc;
> > }
> >  
> > + /**
> > + * Support to lookup the Object with filter with the (name=NAME) and class 
> > type
> > + *
> > + */
> > public <T> T lookup(String name, Class<T> type) {
> > - Object service = lookup(name);
> > + Object service = serviceCacheMap.get(name);
> > + if (service == null) {
> > + ServiceReference<?> sr = null;
> > + try {
> > + ServiceReference<?>[] refs = 
> > bundleContext.getServiceReferences(type.getName(), "(name=" + name + ")");
> > + if (refs != null && refs.length > 0) {
> > + // just return the first one
> > + sr = refs[0];
> > + }
> > + serviceReferenceQueue.add(sr);
> > + service = bundleContext.getService(sr);
> > + if (service != null) {
> > + serviceCacheMap.put(name, service);
> > + }
> > + } catch (Exception ex) {
> > + throw ObjectHelper.wrapRuntimeCamelException(ex);
> > + }
> > + }
> > return type.cast(service);
> > }
> >  
> > + /**
> > + * It's only support to look up the ServiceReference with Class name
> > + */
> > public Object lookup(String name) {
> > Object service = serviceCacheMap.get(name);
> > if (service == null) {
> > - ServiceReference sr = bundleContext.getServiceReference(name);
> > + ServiceReference<?> sr = bundleContext.getServiceReference(name);
> > if (sr != null) {
> > // Need to keep the track of Service
> > // and call ungetService when the camel context is closed
> > @@ -63,14 +90,31 @@ public class OsgiServiceRegistry extends
> > }
> >  
> > public <T> Map<String, T> lookupByType(Class<T> type) {
> > - // not implemented so we return an empty map
> > - return Collections.<String, T>emptyMap();
> > + Map<String, T> result = new HashMap<String, T>();
> > + try {
> > + ServiceReference<?>[] refs = 
> > bundleContext.getAllServiceReferences(type.getName(), null);
> > + if (refs != null) {
> > + for (ServiceReference<?> sr : refs) {
> > + serviceReferenceQueue.add(sr);
> > + Object service = bundleContext.getService(sr);
> > + if (service != null) {
> > + String name = (String)sr.getProperty("name");
> > + if (name != null) {
> > + result.put(name , type.cast(service));
> > + }
> > + }
> > + }
> > + }
> > + } catch (Exception ex) {
> > + throw ObjectHelper.wrapRuntimeCamelException(ex);
> > + }
> > + return result;
> > }
> >  
> > @Override
> > public void onContextStop(CamelContext context) {
> > // Unget the OSGi service
> > - ServiceReference sr = serviceReferenceQueue.poll();
> > + ServiceReference<?> sr = serviceReferenceQueue.poll();
> > while (sr != null) {
> > bundleContext.ungetService(sr);
> > sr = serviceReferenceQueue.poll();
> >  
> > Modified: 
> > camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundleContext.java
> > URL: 
> > http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundleContext.java?rev=1435006&r1=1435005&r2=1435006&view=diff
> > ==============================================================================
> > --- 
> > camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundleContext.java
> >  (original)
> > +++ 
> > camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/CamelMockBundleContext.java
> >  Fri Jan 18 01:57:33 2013
> > @@ -16,6 +16,10 @@
> > */
> > package org.apache.camel.core.osgi;
> >  
> > +import java.util.Dictionary;
> > +import java.util.Hashtable;
> > +
> > +import com.sun.org.apache.xml.internal.security.encryption.Reference;
>  
>  
>  
> This is a mistake it should not be this internal Reference class from
> SUN that should be imported.
> This causes the CI server to fail building the code.
>  
>  
>  
>  
> > import org.apache.camel.CamelContext;
> > import org.apache.camel.Component;
> > import org.apache.camel.component.file.FileComponent;
> > @@ -26,8 +30,10 @@ import org.apache.camel.spi.Language;
> > import org.apache.camel.spi.LanguageResolver;
> > import org.osgi.framework.Bundle;
> > import org.osgi.framework.Constants;
> > +import org.osgi.framework.InvalidSyntaxException;
> > import org.osgi.framework.ServiceReference;
> > import org.springframework.osgi.mock.MockBundleContext;
> > +import org.springframework.osgi.mock.MockServiceReference;
> >  
> > public class CamelMockBundleContext extends MockBundleContext {
> >  
> > @@ -61,5 +67,14 @@ public class CamelMockBundleContext exte
> > return null;
> > }
> > }
> > -
> > +
> > + @SuppressWarnings({"rawtypes", "unchecked"})
> > + public ServiceReference[] getAllServiceReferences(String clazz, String 
> > filter) throws InvalidSyntaxException {
> > + MockServiceReference reference = new MockServiceReference(getBundle(), 
> > new String[] {clazz});
> > + // setup the name property with the class name
> > + Dictionary properties = new Hashtable();
> > + properties.put("name", clazz);
> > + reference.setProperties(properties);
> > + return new ServiceReference[] {reference};
> > + }
> > }
> >  
> > Modified: 
> > camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/ServiceRegistryTest.java
> > URL: 
> > http://svn.apache.org/viewvc/camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/ServiceRegistryTest.java?rev=1435006&r1=1435005&r2=1435006&view=diff
> > ==============================================================================
> > --- 
> > camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/ServiceRegistryTest.java
> >  (original)
> > +++ 
> > camel/trunk/components/camel-core-osgi/src/test/java/org/apache/camel/core/osgi/ServiceRegistryTest.java
> >  Fri Jan 18 01:57:33 2013
> > @@ -16,6 +16,8 @@
> > */
> > package org.apache.camel.core.osgi;
> >  
> > +import java.util.Map;
> > +
> > import org.apache.camel.core.osgi.test.MyService;
> > import org.apache.camel.impl.DefaultCamelContext;
> > import org.junit.Test;
> > @@ -32,9 +34,11 @@ public class ServiceRegistryTest extends
> >  
> > Object service = context.getRegistry().lookup(MyService.class.getName());
> > assertNotNull("MyService should not be null", service);
> > + assertTrue("It should be the instance of MyService ", service instanceof 
> > MyService);
> >  
> > - service = context.getRegistry().lookupByType(MyService.class);
> > - assertNotNull("MyService should not be null", service);
> > + Map<String, MyService> collection = 
> > context.getRegistry().lookupByType(MyService.class);
> > + assertNotNull("MyService should not be null", collection);
> > + assertNotNull("There should have one MyService.", 
> > collection.get(MyService.class.getName()));
> > context.stop();
> > }
>  
>  
>  
>  
>  
> --  
> Claus Ibsen
> -----------------
> Red Hat, Inc.
> FuseSource is now part of Red Hat
> Email: cib...@redhat.com (mailto:cib...@redhat.com)
> Web: http://fusesource.com
> Twitter: davsclaus
> Blog: http://davsclaus.com
> Author of Camel in Action: http://www.manning.com/ibsen



Reply via email to