changeset 974:605f87eac75f

Minor cleanup in LauncherImpl.shutdown() Reviewed-by: jerboaa, neugens Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2013-February/005674.html
author Omair Majid <omajid@redhat.com>
date Thu, 14 Feb 2013 15:35:01 -0500
parents 2bab65a9bc4c
children 023499bc265f
files launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java
diffstat 2 files changed, 16 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Thu Feb 14 15:07:59 2013 -0500
+++ b/launcher/src/main/java/com/redhat/thermostat/launcher/internal/LauncherImpl.java	Thu Feb 14 15:35:01 2013 -0500
@@ -44,10 +44,8 @@
 import java.util.logging.Level;
 
 import org.apache.commons.cli.Options;
-import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
-import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.ServiceReference;
 
 import com.redhat.thermostat.common.ActionListener;
@@ -68,7 +66,6 @@
 import com.redhat.thermostat.common.locale.Translate;
 import com.redhat.thermostat.common.tools.ApplicationState;
 import com.redhat.thermostat.common.utils.LoggingUtils;
-import com.redhat.thermostat.common.utils.OSGIUtils;
 import com.redhat.thermostat.launcher.BundleManager;
 import com.redhat.thermostat.storage.core.ConnectionException;
 import com.redhat.thermostat.storage.core.DbService;
@@ -159,20 +156,21 @@
 
     private void shutdown() throws InternalError {
         try {
-            ApplicationService appSvc = OSGIUtils.getInstance().getService(ApplicationService.class);
-            appSvc.getApplicationExecutor().shutdown();
-            appSvc.getTimerFactory().shutdown();
+            ServiceReference appServiceRef = context.getServiceReference(ApplicationService.class);
+            if (appServiceRef != null) {
+                ApplicationService appSvc = (ApplicationService) context.getService(appServiceRef);
+                appSvc.getApplicationExecutor().shutdown();
+                appSvc.getTimerFactory().shutdown();
+                appSvc = null;
+                context.ungetService(appServiceRef);
+            }
 
-            Bundle bundle = FrameworkUtil.getBundle(LauncherImpl.class);
-            if (bundle != null) {
-                BundleContext ctx = bundle.getBundleContext();
-                if (ctx != null) {
-                    ServiceReference storageRef = ctx.getServiceReference(Storage.class);
-                    if (storageRef != null) {
-                        Storage storage = (Storage) ctx.getService(storageRef);
-                        if (storage != null) {
-                            storage.shutdown();
-                        }
+            if (context != null) {
+                ServiceReference storageRef = context.getServiceReference(Storage.class);
+                if (storageRef != null) {
+                    Storage storage = (Storage) context.getService(storageRef);
+                    if (storage != null) {
+                        storage.shutdown();
                     }
                 }
             }
--- a/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java	Thu Feb 14 15:07:59 2013 -0500
+++ b/launcher/src/test/java/com/redhat/thermostat/launcher/internal/LauncherImplTest.java	Thu Feb 14 15:35:01 2013 -0500
@@ -105,7 +105,7 @@
 import com.redhat.thermostat.utils.keyring.KeyringProvider;
 
 @RunWith(PowerMockRunner.class)
-@PrepareForTest({FrameworkUtil.class, HelpCommand.class, OSGIUtils.class})
+@PrepareForTest({FrameworkUtil.class, HelpCommand.class})
 public class LauncherImplTest {
     
     private static String defaultKeyringProvider;
@@ -256,10 +256,7 @@
         ApplicationService appSvc = mock(ApplicationService.class);
         when(appSvc.getTimerFactory()).thenReturn(timerFactory);
         when(appSvc.getApplicationExecutor()).thenReturn(exec);
-        OSGIUtils osgi = mock(OSGIUtils.class);
-        when(osgi.getService(ApplicationService.class)).thenReturn(appSvc);
-        PowerMockito.mockStatic(OSGIUtils.class);
-        when(OSGIUtils.getInstance()).thenReturn(osgi);
+        bundleContext.registerService(ApplicationService.class, appSvc, null);
 
         loggingInitializer = mock(LoggingInitializer.class);
         dbServiceFactory = mock(DbServiceFactory.class);