You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by am...@apache.org on 2014/01/05 23:04:26 UTC

svn commit: r1555637 - /cxf/dosgi/trunk/felix/shell.tui/src/main/java/org/apache/felix/shell/tui/Activator.java

Author: amichai
Date: Sun Jan  5 22:04:26 2014
New Revision: 1555637

URL: http://svn.apache.org/r1555637
Log:
Fix resource cleanup in felix shell.tui activator

Modified:
    cxf/dosgi/trunk/felix/shell.tui/src/main/java/org/apache/felix/shell/tui/Activator.java

Modified: cxf/dosgi/trunk/felix/shell.tui/src/main/java/org/apache/felix/shell/tui/Activator.java
URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/felix/shell.tui/src/main/java/org/apache/felix/shell/tui/Activator.java?rev=1555637&r1=1555636&r2=1555637&view=diff
==============================================================================
--- cxf/dosgi/trunk/felix/shell.tui/src/main/java/org/apache/felix/shell/tui/Activator.java (original)
+++ cxf/dosgi/trunk/felix/shell.tui/src/main/java/org/apache/felix/shell/tui/Activator.java Sun Jan  5 22:04:26 2014
@@ -39,6 +39,7 @@ public class Activator implements Bundle
     private BundleContext context;
     private ShellTuiRunnable shellRunnable;
     private Thread thread;
+    private ServiceListener listener;
     private ServiceReference shellRef;
     private ShellService shell;
 
@@ -46,29 +47,9 @@ public class Activator implements Bundle
         context = bcontext;
 
         // Listen for registering/unregistering impl service.
-        ServiceListener sl = new ServiceListener() {
-            public void serviceChanged(ServiceEvent event) {
-                synchronized (Activator.this) {
-                    // Ignore additional services if we already have one.
-                    if (event.getType() == ServiceEvent.REGISTERED && shellRef != null) {
-                        return;
-                    } else if (event.getType() == ServiceEvent.REGISTERED && shellRef == null) {
-                        // Initialize the service if we don't have one.
-                        initializeService();
-                    } else if (event.getType() == ServiceEvent.UNREGISTERING
-                        && event.getServiceReference().equals(shellRef)) {
-                        // Unget the service if it is unregistering.
-                        context.ungetService(shellRef);
-                        shellRef = null;
-                        shell = null;
-                        // Try to get another service.
-                        initializeService();
-                    }
-                }
-            }
-        };
+        listener = new ShellServiceListener();
         try {
-            context.addServiceListener(sl,
+            context.addServiceListener(listener,
                     "(objectClass=" + org.apache.felix.shell.ShellService.class.getName() + ")");
         } catch (InvalidSyntaxException ex) {
             System.err.println("ShellTui: Cannot add service listener.");
@@ -85,6 +66,17 @@ public class Activator implements Bundle
         thread.start();
     }
 
+    public void stop(BundleContext bcontext) {
+        if (shellRunnable != null) {
+            shellRunnable.stop();
+            thread.interrupt();
+        }
+        if (listener != null) {
+            context.removeServiceListener(listener);
+        }
+        uninitializeService();
+    }
+
     private synchronized void initializeService() {
         if (shell != null) {
             return;
@@ -96,16 +88,37 @@ public class Activator implements Bundle
         shell = (ShellService)context.getService(shellRef);
     }
 
-    public void stop(BundleContext bcontext) {
-        if (shellRunnable != null) {
-            shellRunnable.stop();
-            thread.interrupt();
+    private synchronized void uninitializeService() {
+        if (shellRef != null) {
+            context.ungetService(shellRef);
+        }
+        shellRef = null;
+        shell = null;
+    }
+
+    private class ShellServiceListener implements ServiceListener {
+        public void serviceChanged(ServiceEvent event) {
+            synchronized (Activator.this) {
+                if (event.getType() == ServiceEvent.REGISTERED) {
+                    // Ignore additional services if we already have one.
+                    if (shellRef == null) {
+                        // Initialize the service if we don't have one.
+                        initializeService();
+                    }
+                } else if (event.getType() == ServiceEvent.UNREGISTERING
+                        && event.getServiceReference().equals(shellRef)) {
+                    // Unget the service if it is unregistering.
+                    uninitializeService();
+                    // Try to get another service.
+                    initializeService();
+                }
+            }
         }
     }
 
     private class ShellTuiRunnable implements Runnable {
 
-        private boolean stop;
+        private volatile boolean stop;
 
         public void stop() {
             stop = true;