changeset 546:5ad8ad98f110

Register storage as a service when it's started reviewed-by: vanaltj review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-August/002608.html
author Mario Torre <neugens.limasoftware@gmail.com>
date Fri, 17 Aug 2012 13:06:49 +0200
parents bf3f8d8990bb
children 3d7b096dc162
files client/core/src/main/java/com/redhat/thermostat/client/internal/Main.java common/core/src/main/java/com/redhat/thermostat/common/utils/OSGIUtils.java
diffstat 2 files changed, 18 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/client/core/src/main/java/com/redhat/thermostat/client/internal/Main.java	Fri Aug 17 12:54:25 2012 +0200
+++ b/client/core/src/main/java/com/redhat/thermostat/client/internal/Main.java	Fri Aug 17 13:06:49 2012 +0200
@@ -78,6 +78,7 @@
 import com.redhat.thermostat.common.storage.Connection.ConnectionStatus;
 import com.redhat.thermostat.common.storage.Connection.ConnectionType;
 import com.redhat.thermostat.common.storage.MongoStorageProvider;
+import com.redhat.thermostat.common.storage.Storage;
 import com.redhat.thermostat.common.storage.StorageProvider;
 import com.redhat.thermostat.common.utils.LoggingUtils;
 import com.redhat.thermostat.common.utils.OSGIUtils;
@@ -283,6 +284,11 @@
         @Override
         public void changed(ConnectionStatus newStatus) {
             if (newStatus == ConnectionStatus.CONNECTED) {
+                
+                // register the storage, so other services can request it
+                Storage storage = ApplicationContext.getInstance().getDAOFactory().getStorage();
+                OSGIUtils.getInstance().registerService(Storage.class, storage);
+
                 showMainWindow();
             } else if (newStatus == ConnectionStatus.FAILED_TO_CONNECT) {
                 if (retry) {
--- a/common/core/src/main/java/com/redhat/thermostat/common/utils/OSGIUtils.java	Fri Aug 17 12:54:25 2012 +0200
+++ b/common/core/src/main/java/com/redhat/thermostat/common/utils/OSGIUtils.java	Fri Aug 17 13:06:49 2012 +0200
@@ -36,6 +36,8 @@
 
 package com.redhat.thermostat.common.utils;
 
+import java.util.Dictionary;
+
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.ServiceReference;
@@ -47,9 +49,19 @@
         return instance;
     }
     
+    @SuppressWarnings({ "rawtypes", "unchecked" })
     public <E extends Object> E getService(Class<E> clazz) {
         BundleContext ctx = FrameworkUtil.getBundle(getClass()).getBundleContext();
         ServiceReference ref = ctx.getServiceReference(clazz.getName());
         return (E) ctx.getService(ref);
     }
+    
+    public <E extends Object> void registerService(Class<? extends E> serviceClass, E service) {
+        registerService(serviceClass, service, null);
+    }
+        
+    public <E extends Object> void registerService(Class<? extends E> serviceClass, E service, Dictionary<String, ?> properties) {
+        BundleContext ctx = FrameworkUtil.getBundle(getClass()).getBundleContext();
+        ctx.registerService(serviceClass.getName(), service, properties);
+    }
 }