Author: eglynn
Date: Fri Oct 24 08:56:44 2008
New Revision: 707662

URL: http://svn.apache.org/viewvc?rev=707662&view=rev
Log:
[dOSGI] Use asynchronous Discovery service lookup when no reference available 
synchronously

Modified:
    
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/AbstractClientHook.java
    
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/AbstractHook.java
    
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/CxfListenerHook.java
    
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtils.java

Modified: 
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/AbstractClientHook.java
URL: 
http://svn.apache.org/viewvc/cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/AbstractClientHook.java?rev=707662&r1=707661&r2=707662&view=diff
==============================================================================
--- 
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/AbstractClientHook.java
 (original)
+++ 
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/AbstractClientHook.java
 Fri Oct 24 08:56:44 2008
@@ -19,6 +19,7 @@
 package org.apache.cxf.dosgi.dsw.hooks;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Hashtable;
@@ -35,6 +36,7 @@
 import org.osgi.framework.Filter;
 import org.osgi.framework.ServiceReference;
 import org.osgi.service.discovery.ServiceEndpointDescription;
+import org.osgi.service.discovery.ServiceListener;
 
 public class AbstractClientHook extends AbstractHook {
     
@@ -44,15 +46,24 @@
         super(bc, dp);
     }
     
-    protected List<ServiceEndpointDescription> 
processClientDescriptions(BundleContext requestingContext, 
-                                                            String 
interfaceName, 
-                                                            String filter, 
-                                                            boolean matchAll, 
-                                                            boolean 
allServices) {
-        List<ServiceEndpointDescription> sds = 
-            getServiceDescriptions(requestingContext, interfaceName, filter, 
false);
-        List<ServiceEndpointDescription> actualSds = new 
ArrayList<ServiceEndpointDescription>(sds.size()); 
-        
+    protected void processClientDescriptions(BundleContext requestingContext, 
+                                             String interfaceName, 
+                                             String filter, 
+                                             boolean matchAll, 
+                                             boolean allServices) {
+       processServiceDescriptions(getServiceDescriptions(requestingContext, 
interfaceName, filter, false),
+                                          requestingContext, 
+                                   interfaceName, 
+                                   filter, 
+                                   allServices);        
+    }
+    
+    protected void processServiceDescriptions(List<ServiceEndpointDescription> 
sds,
+                                                 BundleContext 
requestingContext, 
+                                              String interfaceName, 
+                                              String filter, 
+                                              boolean allServices) {
+
         for (ServiceEndpointDescription sd  : sds) {
             
             
@@ -95,14 +106,14 @@
                         }
                         
                     }
-                    actualSds.add(sd);
+                    //actualSds.add(sd);
                 }
             } catch (ClassNotFoundException ex) {
                 LOG.warning("No class can be found for " + interfaceName);
                 continue;
             }
         }
-        return actualSds;
+        //return actualSds;
     }
     
     @SuppressWarnings("unchecked")
@@ -116,8 +127,10 @@
     
     @SuppressWarnings("unchecked")
     protected List<ServiceEndpointDescription> getServiceDescriptions(
-        BundleContext context, String interfaceName, String filterValue, 
-        boolean matchAll) {
+        final BundleContext  context, 
+        final String interfaceName, 
+        final String filterValue, 
+        final boolean matchAll) {
         
         // if bundle has remote-services.xml attached then those metadata 
takes precedence
         List<ServiceEndpointDescription> sds = new 
ArrayList<ServiceEndpointDescription>();
@@ -131,7 +144,58 @@
         // found some SDs as it may find more suitable SDs ?
         if (sds.isEmpty()) {
             // try discovery service
-            sds.addAll(getFromDiscoveryService(interfaceName, filterValue));
+               
+               // REVISIT Discovery RI will change soon to use standard 
"objectClass" 
+               // property key, instead of "interface-name". At that point we 
can
+               // just reuse the given filter instead of constructing a 
modified one. Also
+               // we currently throw away any filter predicates unrelated to 
the interface
+               // name, which is clearly inappropriate.
+               
+                       String modifiedFilter = 
+                               "(" + 
ServiceEndpointDescription.PROP_KEY_INTERFACE_NAME 
+                               + "=" + interfaceName + ")";
+
+               Collection<ServiceEndpointDescription> discovered = 
+                       getFromDiscoveryService(interfaceName, modifiedFilter);
+               if (discovered.size() != 0) {
+                               LOG.info("synchronous lookup discovered " + 
discovered.size()
+                                                + " references for interface: 
" + interfaceName);
+                sds.addAll(discovered);
+               } else {
+                       LOG.info("nothing discovered initially for interface: " 
+                                                + interfaceName +  ", fallback 
to async lookup");
+                               if (filterValue != null && filterValue.length() 
> 0) {
+                                       LOG.fine("installing service listener 
for: " + modifiedFilter);
+                           listenToDiscoveryService(
+                               new ServiceListener() {
+                                                       public void 
serviceAvailable(ServiceEndpointDescription sd) {
+                                                               
LOG.info("received serviceAvailable callback: " 
+                                                                               
 + sd.getProperties());
+                                                           
List<ServiceEndpointDescription> notified = 
+                                                               new 
ArrayList<ServiceEndpointDescription>();
+                                                           notified.add(sd);
+                                                           
processServiceDescriptions(notified,
+                                                                               
           context,
+                                                                               
           interfaceName,
+                                                                               
           filterValue,
+                                                                               
           matchAll);
+                                                               
LOG.fine("removing service listener : " + this);
+                                                           
unlistenToDiscoveryService(this);
+                                                       }
+                                                       public void 
serviceModified(ServiceEndpointDescription oldSD,
+                                                                               
            ServiceEndpointDescription newSD) {
+                                                               // we don't 
currently use this notification, but we could do
+                                                               // so to allow 
to support transparent service re-location 
+                                                       }
+       
+                                                       public void 
serviceUnavailable(ServiceEndpointDescription sd) {
+                                                       // we don't currently 
use this notification, but we could do
+                                                               // so to allow 
to drive transparent fail-over
+                                                       }                       
+                               },
+                               modifiedFilter);
+                               }
+               }
         }
         
         // do it just in case too for the moment

Modified: 
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/AbstractHook.java
URL: 
http://svn.apache.org/viewvc/cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/AbstractHook.java?rev=707662&r1=707661&r2=707662&view=diff
==============================================================================
--- 
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/AbstractHook.java
 (original)
+++ 
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/AbstractHook.java
 Fri Oct 24 08:56:44 2008
@@ -33,6 +33,7 @@
 import org.osgi.framework.BundleContext;
 import org.osgi.service.discovery.Discovery;
 import org.osgi.service.discovery.ServiceEndpointDescription;
+import org.osgi.service.discovery.ServiceListener;
 
 public abstract class AbstractHook {
     private final CxfDistributionProvider distributionProvider;
@@ -87,7 +88,6 @@
         return props;
     }
     
-    @SuppressWarnings("unchecked")
     protected Collection<ServiceEndpointDescription> getFromDiscoveryService(
         String interfaceName, String filterValue) {
        
@@ -113,6 +113,37 @@
         return Collections.emptyList();
     }
     
+    protected Collection<ServiceEndpointDescription> listenToDiscoveryService(
+               ServiceListener listener, String filterValue) {
+              
+        OsgiService<Discovery> pair = OsgiUtils.getOsgiService(getContext(), 
Discovery.class);
+        if (pair != null) {
+            try {
+                pair.getService().addServiceListener(listener, filterValue);
+            } finally {
+                pair.ungetService(getContext());
+            }
+        }
+       
+        return Collections.emptyList();
+    }
+
+    protected Collection<ServiceEndpointDescription> 
unlistenToDiscoveryService(
+               ServiceListener listener) {
+              
+        OsgiService<Discovery> pair = OsgiUtils.getOsgiService(getContext(), 
Discovery.class);
+        if (pair != null) {
+            try {
+                pair.getService().removeServiceListener(listener);
+            } finally {
+                pair.ungetService(getContext());
+            }
+        }
+       
+        return Collections.emptyList();
+    }
+
+    
     protected String getIdentificationProperty() {
         Bundle b = bc.getBundle();
         Object name = 

Modified: 
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/CxfListenerHook.java
URL: 
http://svn.apache.org/viewvc/cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/CxfListenerHook.java?rev=707662&r1=707661&r2=707662&view=diff
==============================================================================
--- 
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/CxfListenerHook.java
 (original)
+++ 
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/CxfListenerHook.java
 Fri Oct 24 08:56:44 2008
@@ -43,6 +43,10 @@
     static {
         SYSTEM_PACKAGES = new HashSet<String>();
         SYSTEM_PACKAGES.add("org.osgi.service");
+        SYSTEM_PACKAGES.add("org.apache.felix");
+        SYSTEM_PACKAGES.add("org.ops4j.pax.logging");
+        SYSTEM_PACKAGES.add("ch.ethz.iks.slp");
+        SYSTEM_PACKAGES.add("org.ungoverned.osgi.service");
     }
     
     private ThreadLocal<Boolean> findCallsInProgress

Modified: 
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtils.java
URL: 
http://svn.apache.org/viewvc/cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtils.java?rev=707662&r1=707661&r2=707662&view=diff
==============================================================================
--- 
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtils.java
 (original)
+++ 
cxf/sandbox/dosgi/dsw/cxf-dsw/src/main/java/org/apache/cxf/dosgi/dsw/hooks/ServiceHookUtils.java
 Fri Oct 24 08:56:44 2008
@@ -54,6 +54,10 @@
                 Map<String, String> javaInterfaces = 
getJavaInterfaceVersionMap(sd);
                 Map<String, String> endpointInterfaces = 
getJavaInterfaceEndpointInterfaceMap(sd);
                 Map<String, Object> properties = getPropertiesMap(sd);
+                LOG.info("publish java interfaces: " + javaInterfaces
+                                + ",\nendpoint interfaces: " + 
endpointInterfaces
+                                + ",\nproperties: " + properties);
+                
                 sdPublished = pair.getService().publishService(javaInterfaces,
                                                                
endpointInterfaces,
                                                                properties);


Reply via email to