Author: angelo.vandersijpt at luminis.eu
Date: Thu Oct 14 16:59:48 2010
New Revision: 171

Log:
AMDATU-103 Refactored FriendsGadgetServiceImpl to use ConfigAdmin.

Modified:
   
trunk/example-bundles/friends-gadget/src/main/java/org/amdatu/example/friends/gadget/osgi/Activator.java
   
trunk/example-bundles/friends-gadget/src/main/java/org/amdatu/example/friends/gadget/osgi/service/FriendsGadgetServiceImpl.java

Modified: 
trunk/example-bundles/friends-gadget/src/main/java/org/amdatu/example/friends/gadget/osgi/Activator.java
==============================================================================
--- 
trunk/example-bundles/friends-gadget/src/main/java/org/amdatu/example/friends/gadget/osgi/Activator.java
    (original)
+++ 
trunk/example-bundles/friends-gadget/src/main/java/org/amdatu/example/friends/gadget/osgi/Activator.java
    Thu Oct 14 16:59:48 2010
@@ -50,10 +50,9 @@
                 .setImplementation(FriendsGadgetServiceImpl.class)
                 
.add(createServiceDependency().setService(LogService.class).setRequired(true))
                 
.add(createServiceDependency().setService(GadgetManagement.class).setRequired(true))
-                
.add(createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true))
                 
.add(createServiceDependency().setService(HttpContextServiceFactory.class).setRequired(true))
                 
.add(createServiceDependency().setService(HttpService.class).setRequired(true))
-                
.add(createServiceDependency().setService(WebContainer.class).setRequired(true));
+                
.add(createConfigurationDependency().setPid(FriendsGadgetServiceImpl.PID));
         manager.add(friendsgadgetService);
     }
 

Modified: 
trunk/example-bundles/friends-gadget/src/main/java/org/amdatu/example/friends/gadget/osgi/service/FriendsGadgetServiceImpl.java
==============================================================================
--- 
trunk/example-bundles/friends-gadget/src/main/java/org/amdatu/example/friends/gadget/osgi/service/FriendsGadgetServiceImpl.java
     (original)
+++ 
trunk/example-bundles/friends-gadget/src/main/java/org/amdatu/example/friends/gadget/osgi/service/FriendsGadgetServiceImpl.java
     Thu Oct 14 16:59:48 2010
@@ -20,6 +20,7 @@
 
 import java.io.IOException;
 import java.net.URL;
+import java.util.Dictionary;
 
 import org.amdatu.application.gadgetmanagement.GadgetManagement;
 import org.amdatu.example.friends.gadget.osgi.Activator;
@@ -30,6 +31,7 @@
 import org.apache.felix.dm.Component;
 import org.osgi.framework.BundleContext;
 import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.cm.ConfigurationException;
 import org.osgi.service.log.LogService;
 
 /**
@@ -38,8 +40,8 @@
  * @author ivol
  */
 public class FriendsGadgetServiceImpl implements ResourceProvider {
-    // The PID
-    private final static String PID = "org.amdatu.example.friends.gadget";
+    // The PID and configuration properties
+    public final static String PID = "org.amdatu.example.friends.gadget";
     private final static String HOSTNAME = "hostname";
     private final static String PORTNR = "portnr";
 
@@ -54,7 +56,8 @@
 
     // Other instance variables
     private Component m_httpContextComponent;
-
+    private String m_hostname;
+    private String m_portnr;
     /**
      * The init() method is invoked by the Felix dependency manager.
      */
@@ -62,21 +65,12 @@
         // Create our own http context service
         m_httpContextComponent = 
m_httpContextFactoryService.create(m_bundleContext, this);
 
-        try {
-            // Register the gadget with the Gadget management service. Note 
that we can do this as
-            // many times as we want, since the gadget URL is the unique 
identifier
-            String hostName = 
m_configurationAdmin.getConfiguration(PID).getProperties().get(HOSTNAME).toString();
-            String portNr = 
m_configurationAdmin.getConfiguration(PID).getProperties().get(PORTNR).toString();
-            GadgetDefinition gadgetDef = new GadgetDefinition("http://"; + 
hostName + ":" + portNr + "/" + Activator.RESOURCE_ID + 
"/static/xml/FriendsGadget.xml", 
-                    GadgetCategory.AMDATU_EXAMPLES, false);
-            
-            gadgetDef.setServiceName("friends");
-            
-            m_gadgetManagement.addGadget(gadgetDef);
-        } catch (IOException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not register friends 
gadget", e);
-        }
+        GadgetDefinition gadgetDef = new GadgetDefinition("http://"; + 
m_hostname + ":" + m_portnr + "/" + Activator.RESOURCE_ID + 
"/static/xml/FriendsGadget.xml",
+                GadgetCategory.AMDATU_EXAMPLES, false);
+
+        gadgetDef.setServiceName("friends");
 
+        m_gadgetManagement.addGadget(gadgetDef);
         m_logService.log(LogService.LOG_INFO, getClass().getName() + " service 
initialized");
     }
 
@@ -103,4 +97,20 @@
     public String getResourceId() {
         return RESOURCE_ID;
     }
+
+    public void updated(Dictionary dictionary) throws ConfigurationException {
+        if (dictionary != null) {
+            checkAvailability(dictionary, new String[] {HOSTNAME, PORTNR});
+            m_hostname = (String) dictionary.get(HOSTNAME);
+            m_portnr = (String) dictionary.get(PORTNR);
+        }
+    }
+
+    private void checkAvailability(Dictionary dictionary, String[] 
mandatoryKeys) throws ConfigurationException {
+        for (String mandatoryKey : mandatoryKeys) {
+            if (dictionary.get(mandatoryKey) == null) {
+                throw new ConfigurationException("Missing configuration key", 
mandatoryKey);
+            }
+        }
+    }
 }

Reply via email to