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

Log:
AMDATU-103 Refactored CourseGadgetServiceImpl to use ConfigAdmin.

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

Modified: 
trunk/example-bundles/course-gadget/src/main/java/org/amdatu/example/course/gadget/osgi/Activator.java
==============================================================================
--- 
trunk/example-bundles/course-gadget/src/main/java/org/amdatu/example/course/gadget/osgi/Activator.java
      (original)
+++ 
trunk/example-bundles/course-gadget/src/main/java/org/amdatu/example/course/gadget/osgi/Activator.java
      Thu Oct 14 16:59:45 2010
@@ -49,9 +49,8 @@
                 .setImplementation(CourseGadgetServiceImpl.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(WebContainer.class).setRequired(true));
+                
.add(createConfigurationDependency().setPid(CourseGadgetServiceImpl.PID));
         manager.add(friendsgadgetService);
     }
 

Modified: 
trunk/example-bundles/course-gadget/src/main/java/org/amdatu/example/course/gadget/osgi/service/CourseGadgetServiceImpl.java
==============================================================================
--- 
trunk/example-bundles/course-gadget/src/main/java/org/amdatu/example/course/gadget/osgi/service/CourseGadgetServiceImpl.java
        (original)
+++ 
trunk/example-bundles/course-gadget/src/main/java/org/amdatu/example/course/gadget/osgi/service/CourseGadgetServiceImpl.java
        Thu Oct 14 16:59:45 2010
@@ -18,6 +18,7 @@
 
 import java.io.IOException;
 import java.net.URL;
+import java.util.Dictionary;
 
 import org.amdatu.application.gadgetmanagement.GadgetManagement;
 import org.amdatu.example.course.gadget.osgi.Activator;
@@ -28,6 +29,8 @@
 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.cm.ManagedService;
 import org.osgi.service.log.LogService;
 
 /**
@@ -35,9 +38,9 @@
  * gadget XML).
  * @author ivol
  */
-public class CourseGadgetServiceImpl implements ResourceProvider {
-    // The PID
-    private final static String PID = "org.amdatu.example.course.gadget";
+public class CourseGadgetServiceImpl implements ResourceProvider, 
ManagedService {
+    // The PID and configuration properties
+    public final static String PID = "org.amdatu.example.course.gadget";
     private final static String HOSTNAME = "hostname";
     private final static String PORTNR = "portnr";
 
@@ -45,13 +48,14 @@
     private volatile LogService m_logService;
     private volatile HttpContextServiceFactory m_httpContextFactoryService;
     private volatile GadgetManagement m_gadgetManagement;
-    private volatile ConfigurationAdmin m_configurationAdmin;
 
     // Instance variables, injected by the OSGi framework
     private BundleContext m_bundleContext;
 
     // 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.
@@ -60,19 +64,11 @@
         // Create our own http context and register resources
         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
-                            + "/jsp/CourseGadget.jsp", 
GadgetCategory.AMDATU_EXAMPLES, false);
-            gadgetDef.setServiceName("course");
-            m_gadgetManagement.addGadget(gadgetDef);
-        } catch (IOException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not register course 
gadget", e);
-        }
+        GadgetDefinition gadgetDef =
+                new GadgetDefinition("http://"; + m_hostname + ":" + m_portnr + 
"/" + Activator.RESOURCE_ID
+                        + "/jsp/CourseGadget.jsp", 
GadgetCategory.AMDATU_EXAMPLES, false);
+        gadgetDef.setServiceName("course");
+        m_gadgetManagement.addGadget(gadgetDef);
         m_logService.log(LogService.LOG_INFO, getClass().getName() + " service 
initialized");
     }
 
@@ -104,4 +100,20 @@
     public long getBundleId() {
         return m_bundleContext.getBundle().getBundleId();
     }
+
+    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