changeset 780:b8cb23fefea9

Fix NPE on Eclipse shutdown Currently, every time the Eclipse plugins are stopped (ie. when Eclipse is shutdown), an NPE is thrown while trying to disconnect from DbService. This commit changes the catch clause for an unsuccessful disconnect to use the Logger instead of printing to the console. Reviewed-by: omajid Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-October/004030.html
author Elliott Baron <ebaron@redhat.com>
date Thu, 15 Nov 2012 12:42:21 -0500
parents 39fcc5389e32
children 837fe8d3f1fd
files eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/internal/Activator.java eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/internal/jobs/ConnectDbJob.java
diffstat 2 files changed, 16 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/internal/Activator.java	Thu Nov 15 12:27:27 2012 -0500
+++ b/eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/internal/Activator.java	Thu Nov 15 12:42:21 2012 -0500
@@ -36,6 +36,7 @@
 
 package com.redhat.thermostat.eclipse.internal;
 
+import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Platform;
 import org.eclipse.jface.resource.ImageDescriptor;
 import org.eclipse.ui.IWorkbenchPage;
@@ -48,6 +49,7 @@
 import com.redhat.thermostat.common.TimerFactory;
 import com.redhat.thermostat.common.appctx.ApplicationContext;
 import com.redhat.thermostat.common.utils.OSGIUtils;
+import com.redhat.thermostat.eclipse.LoggerFacility;
 import com.redhat.thermostat.storage.core.ConnectionException;
 
 /**
@@ -91,11 +93,15 @@
      * )
      */
     public void stop(BundleContext context) throws Exception {
-        DbService dbService = OSGIUtils.getInstance().getService(DbService.class);
-        try {
-            dbService.disconnect();
-        } catch (ConnectionException e) {
-            e.printStackTrace();
+        DbService dbService = OSGIUtils.getInstance().getServiceAllowNull(
+                DbService.class);
+        if (dbService != null) {
+            try {
+                dbService.disconnect();
+            } catch (ConnectionException e) {
+                LoggerFacility.getInstance().log(IStatus.ERROR,
+                        "Error disconnecting from database", e);
+            }
         }
         plugin = null;
         super.stop(context);
@@ -149,9 +155,10 @@
     public static ImageDescriptor getImageDescriptor(String path) {
         return imageDescriptorFromPlugin(PLUGIN_ID, path);
     }
-    
+
     public boolean isDbConnected() {
-        DbService dbService = OSGIUtils.getInstance().getServiceAllowNull(DbService.class);
+        DbService dbService = OSGIUtils.getInstance().getServiceAllowNull(
+                DbService.class);
         return dbService != null;
     }
 
--- a/eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/internal/jobs/ConnectDbJob.java	Thu Nov 15 12:27:27 2012 -0500
+++ b/eclipse/com.redhat.thermostat.eclipse/src/com/redhat/thermostat/eclipse/internal/jobs/ConnectDbJob.java	Thu Nov 15 12:42:21 2012 -0500
@@ -40,10 +40,10 @@
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.jobs.Job;
-import org.osgi.framework.BundleContext;
 
 import com.redhat.thermostat.common.DbService;
 import com.redhat.thermostat.common.DbServiceFactory;
+import com.redhat.thermostat.common.utils.OSGIUtils;
 import com.redhat.thermostat.eclipse.LoggerFacility;
 import com.redhat.thermostat.eclipse.internal.Activator;
 import com.redhat.thermostat.eclipse.internal.ConnectionConfiguration;
@@ -82,8 +82,7 @@
                 configuration.getPassword(), configuration.getDBConnectionString());
         dbService.connect();
         // register service in order to indicate that we are connected
-        BundleContext ctxt = Activator.getDefault().getBundle().getBundleContext();
-        ctxt.registerService(DbService.class, dbService, null);
+        OSGIUtils.getInstance().registerService(DbService.class, dbService);
     }
 
 }