You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2012/01/17 10:07:03 UTC

svn commit: r1232348 - in /karaf/cellar/branches/cellar-2.2.x: dosgi/src/main/java/org/apache/karaf/cellar/dosgi/ImportServiceListener.java itests/src/test/java/org/apache/karaf/cellar/itests/CellarSampleDosgiGreeterTest.java

Author: jbonofre
Date: Tue Jan 17 09:07:03 2012
New Revision: 1232348

URL: http://svn.apache.org/viewvc?rev=1232348&view=rev
Log:
[KARAF-823] Update DOSGi unit tests and merge the import service listener from trunk

Modified:
    karaf/cellar/branches/cellar-2.2.x/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/ImportServiceListener.java
    karaf/cellar/branches/cellar-2.2.x/itests/src/test/java/org/apache/karaf/cellar/itests/CellarSampleDosgiGreeterTest.java

Modified: karaf/cellar/branches/cellar-2.2.x/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/ImportServiceListener.java
URL: http://svn.apache.org/viewvc/karaf/cellar/branches/cellar-2.2.x/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/ImportServiceListener.java?rev=1232348&r1=1232347&r2=1232348&view=diff
==============================================================================
--- karaf/cellar/branches/cellar-2.2.x/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/ImportServiceListener.java (original)
+++ karaf/cellar/branches/cellar-2.2.x/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/ImportServiceListener.java Tue Jan 17 09:07:03 2012
@@ -33,11 +33,14 @@ import java.util.Hashtable;
 import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Listener on the import service.
  */
-public class ImportServiceListener implements ListenerHook {
+public class ImportServiceListener implements ListenerHook, Runnable {
 
     private static final transient Logger LOGGER = LoggerFactory.getLogger(ImportServiceListener.class);
 
@@ -45,18 +48,24 @@ public class ImportServiceListener imple
     private ClusterManager clusterManager;
     private CommandStore commandStore;
     private EventTransportFactory eventTransportFactory;
-    private Map<String,EndpointDescription> remoteEndpoints;
+    private Map<String, EndpointDescription> remoteEndpoints;
+
+    private Set<ListenerInfo> pendingListeners = new LinkedHashSet<ListenerInfo>();
 
     private final Map<EndpointDescription, ServiceRegistration> registrations = new HashMap<EndpointDescription, ServiceRegistration>();
 
     private final Map<String, EventProducer> producers = new HashMap<String, EventProducer>();
     private final Map<String, EventConsumer> consumers = new HashMap<String, EventConsumer>();
 
+    private final ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
+
     public void init() {
         remoteEndpoints = clusterManager.getMap(Constants.REMOTE_ENDPOINTS);
+        service.scheduleAtFixedRate(this, 0, 5, TimeUnit.SECONDS);
     }
 
     public void destroy() {
+        service.shutdown();
         for (Map.Entry<EndpointDescription, ServiceRegistration> entry : registrations.entrySet()) {
             ServiceRegistration registration = entry.getValue();
             registration.unregister();
@@ -67,7 +76,12 @@ public class ImportServiceListener imple
         }
         consumers.clear();
         producers.clear();
+    }
 
+    public void run() {
+        for (ListenerInfo listener : pendingListeners) {
+            checkListener(listener);
+        }
     }
 
     @Override
@@ -81,20 +95,9 @@ public class ImportServiceListener imple
                     continue;
                 }
 
+                pendingListeners.add(listenerInfo);
                 // Make sure we only import remote services
-
-                // Iterate through known services and import them if needed
-                Set<EndpointDescription> matches = new LinkedHashSet<EndpointDescription>();
-                for (Map.Entry<String,EndpointDescription> entry : remoteEndpoints.entrySet()) {
-                    EndpointDescription endpointDescription = entry.getValue();
-                    if (endpointDescription.matches(listenerInfo.getFilter()) && !endpointDescription.getNodes().contains(clusterManager.getNode().getId())) {
-                        matches.add(endpointDescription);
-                    }
-                }
-
-                for (EndpointDescription endpoint : matches) {
-                    importService(endpoint, listenerInfo);
-                }
+                checkListener(listenerInfo);
             }
         } finally {
             Thread.currentThread().setContextClassLoader(originalClassLoader);
@@ -115,7 +118,7 @@ public class ImportServiceListener imple
                 String filter = "(&" + listenerInfo.getFilter() + "(!(" + Constants.ENDPOINT_FRAMEWORK_UUID + "=" + clusterManager.getNode().getId() + ")))";
                 // Iterate through known services and import them if needed
                 Set<EndpointDescription> matches = new LinkedHashSet<EndpointDescription>();
-                for (Map.Entry<String,EndpointDescription> entry : remoteEndpoints.entrySet()) {
+                for (Map.Entry<String, EndpointDescription> entry : remoteEndpoints.entrySet()) {
                     EndpointDescription endpointDescription = entry.getValue();
                     if (endpointDescription.matches(filter)) {
                         matches.add(endpointDescription);
@@ -125,6 +128,33 @@ public class ImportServiceListener imple
                 for (EndpointDescription endpoint : matches) {
                     unimportService(endpoint);
                 }
+
+                pendingListeners.remove(listenerInfo);
+            }
+        } finally {
+            Thread.currentThread().setContextClassLoader(originalClassLoader);
+        }
+    }
+
+    /**
+     * Checks if there is a match for the current {@link ListenerInfo}.
+     *
+     * @param listenerInfo
+     */
+    private void checkListener(ListenerInfo listenerInfo) {
+        ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
+        try {
+            Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+            // Iterate through known services and import them if needed
+            Set<EndpointDescription> matches = new LinkedHashSet<EndpointDescription>();
+            for (Map.Entry<String, EndpointDescription> entry : remoteEndpoints.entrySet()) {
+                EndpointDescription endpointDescription = entry.getValue();
+                if (endpointDescription.matches(listenerInfo.getFilter()) && !endpointDescription.getNodes().contains(clusterManager.getNode().getId())) {
+                    matches.add(endpointDescription);
+                }
+            }
+            for (EndpointDescription endpoint : matches) {
+                importService(endpoint, listenerInfo);
             }
         } finally {
             Thread.currentThread().setContextClassLoader(originalClassLoader);
@@ -141,21 +171,21 @@ public class ImportServiceListener imple
         LOGGER.debug("CELLAR DOSGI: importing remote service");
 
         EventProducer requestProducer = producers.get(endpoint.getId());
-        if(requestProducer == null) {
-            requestProducer = eventTransportFactory.getEventProducer(Constants.INTERFACE_PREFIX + Constants.SEPARATOR + endpoint.getId(),Boolean.FALSE);
-            producers.put(endpoint.getId(),requestProducer);
+        if (requestProducer == null) {
+            requestProducer = eventTransportFactory.getEventProducer(Constants.INTERFACE_PREFIX + Constants.SEPARATOR + endpoint.getId(), Boolean.FALSE);
+            producers.put(endpoint.getId(), requestProducer);
         }
 
         EventConsumer resultConsumer = consumers.get(endpoint.getId());
-        if(resultConsumer == null) {
+        if (resultConsumer == null) {
             resultConsumer = eventTransportFactory.getEventConsumer(Constants.RESULT_PREFIX + Constants.SEPARATOR + clusterManager.getNode().getId() + endpoint.getId(), Boolean.FALSE);
-            consumers.put(endpoint.getId(),resultConsumer);
-        } else if(!resultConsumer.isConsuming()) {
+            consumers.put(endpoint.getId(), resultConsumer);
+        } else if (!resultConsumer.isConsuming()) {
             resultConsumer.start();
         }
 
-        producers.put(endpoint.getId(),requestProducer);
-        consumers.put(endpoint.getId(),resultConsumer);
+        producers.put(endpoint.getId(), requestProducer);
+        consumers.put(endpoint.getId(), resultConsumer);
 
         ExecutionContext executionContext = new ClusteredExecutionContext(requestProducer,commandStore);
 
@@ -164,6 +194,8 @@ public class ImportServiceListener imple
                 remoteServiceFactory,
                 new Hashtable<String, Object>(endpoint.getProperties()));
         registrations.put(endpoint, registration);
+
+        pendingListeners.remove(listenerInfo);
     }
 
     /**
@@ -177,7 +209,7 @@ public class ImportServiceListener imple
 
         producers.remove(endpoint.getId());
         EventConsumer consumer = consumers.remove(endpoint.getId());
-        if(consumer != null) {
+        if (consumer != null) {
             consumer.stop();
         }
     }

Modified: karaf/cellar/branches/cellar-2.2.x/itests/src/test/java/org/apache/karaf/cellar/itests/CellarSampleDosgiGreeterTest.java
URL: http://svn.apache.org/viewvc/karaf/cellar/branches/cellar-2.2.x/itests/src/test/java/org/apache/karaf/cellar/itests/CellarSampleDosgiGreeterTest.java?rev=1232348&r1=1232347&r2=1232348&view=diff
==============================================================================
--- karaf/cellar/branches/cellar-2.2.x/itests/src/test/java/org/apache/karaf/cellar/itests/CellarSampleDosgiGreeterTest.java (original)
+++ karaf/cellar/branches/cellar-2.2.x/itests/src/test/java/org/apache/karaf/cellar/itests/CellarSampleDosgiGreeterTest.java Tue Jan 17 09:07:03 2012
@@ -50,7 +50,7 @@ public class CellarSampleDosgiGreeterTes
         ClusterManager clusterManager = getOsgiService(ClusterManager.class);
         assertNotNull(clusterManager);
 
-        System.err.println(executeCommand("features:addurl mvn:org.apache.karaf.cellar.samples/dosgi-greeter/3.0.0-SNAPSHOT/xml/features"));
+        System.err.println(executeCommand("features:addurl mvn:org.apache.karaf.cellar.samples/dosgi-greeter/2.2.3-SNAPSHOT/xml/features"));
 
         System.err.println(executeCommand("admin:list"));
 
@@ -71,14 +71,12 @@ public class CellarSampleDosgiGreeterTes
         System.err.println(executeCommand("cluster:group-set client-grp "+localNode.getId()));
         System.err.println(executeCommand("cluster:group-set service-grp "+node1));
         System.err.println(executeCommand("cluster:group-list"));
-
+        
+        System.err.println(executeCommand("cluster:features-install client-grp greeter-client"));
+        Thread.sleep(10000);
         System.err.println(executeCommand("cluster:features-install service-grp greeter-service"));
         Thread.sleep(10000);
         System.err.println(executeCommand("cluster:list-services"));
-        System.err.println(executeCommand("cluster:features-install client-grp greeter-client"));
-        Thread.sleep(10000);
-        System.err.println(executeCommand("features:list"));
-        System.err.println(executeCommand("osgi:list"));
         String greetOutput = executeCommand("dosgi-greeter:greet Hi 10");
         System.err.println(greetOutput);
         assertEquals("Expected 10 greets", 10, countGreetsFromNode(greetOutput, node1));
@@ -120,6 +118,6 @@ public class CellarSampleDosgiGreeterTes
     @Configuration
     public Option[] config() {
         return new Option[]{
-                cellarDistributionConfiguration(), keepRuntimeFolder(),logLevel(LogLevelOption.LogLevel.ERROR)};
+                cellarDistributionConfiguration(), keepRuntimeFolder(), logLevel(LogLevelOption.LogLevel.ERROR), debugConfiguration("5005", true)};
     }
 }