Author: angelo.vandersijpt at luminis.eu
Date: Mon Oct 18 15:02:39 2010
New Revision: 182

Log:
AMDATU-103 Refactored the SPARQLEnpointService to be a ManagedService.

Modified:
   
trunk/gadget-bundles/sparqlendpoint-gadget/src/main/java/org/amdatu/gadget/sparqlendpoint/Activator.java
   
trunk/gadget-bundles/sparqlendpoint-gadget/src/main/java/org/amdatu/gadget/sparqlendpoint/service/SPARQLEndpointServiceImpl.java

Modified: 
trunk/gadget-bundles/sparqlendpoint-gadget/src/main/java/org/amdatu/gadget/sparqlendpoint/Activator.java
==============================================================================
--- 
trunk/gadget-bundles/sparqlendpoint-gadget/src/main/java/org/amdatu/gadget/sparqlendpoint/Activator.java
    (original)
+++ 
trunk/gadget-bundles/sparqlendpoint-gadget/src/main/java/org/amdatu/gadget/sparqlendpoint/Activator.java
    Mon Oct 18 15:02:39 2010
@@ -48,10 +48,9 @@
                 SPARQLEndpointServiceImpl.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(SesameService.class).setRequired(true)).add(
-                
createServiceDependency().setService(HttpContextServiceFactory.class).setRequired(true)).add(
-                
createServiceDependency().setService(WebContainer.class).setRequired(true)));
+                
.add(createServiceDependency().setService(SesameService.class).setRequired(true))
+                
.add(createServiceDependency().setService(HttpContextServiceFactory.class).setRequired(true))
+                
.add(createConfigurationDependency().setPid(SPARQLEndpointServiceImpl.PID)));
     }
 
     /**

Modified: 
trunk/gadget-bundles/sparqlendpoint-gadget/src/main/java/org/amdatu/gadget/sparqlendpoint/service/SPARQLEndpointServiceImpl.java
==============================================================================
--- 
trunk/gadget-bundles/sparqlendpoint-gadget/src/main/java/org/amdatu/gadget/sparqlendpoint/service/SPARQLEndpointServiceImpl.java
    (original)
+++ 
trunk/gadget-bundles/sparqlendpoint-gadget/src/main/java/org/amdatu/gadget/sparqlendpoint/service/SPARQLEndpointServiceImpl.java
    Mon Oct 18 15:02:39 2010
@@ -18,6 +18,7 @@
 
 import java.io.IOException;
 import java.net.URL;
+import java.util.Dictionary;
 import java.util.List;
 
 import javax.ws.rs.Consumes;
@@ -47,6 +48,8 @@
 import org.openrdf.query.TupleQueryResult;
 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;
 
 /**
@@ -56,12 +59,12 @@
  * @author ivol
  */
 @Path("sparqlendpoint")
-public class SPARQLEndpointServiceImpl implements ResourceProvider {
+public class SPARQLEndpointServiceImpl implements ResourceProvider, 
ManagedService {
     // Max row count for one triple query
     private static final int MAX_ROW_COUNT = 250;
 
-    // The PID
-    private final static String PID = "org.amdatu.gadget.sparqlendpoint";
+    // The PID and configuration properties
+    public final static String PID = "org.amdatu.gadget.sparqlendpoint";
     private final static String HOSTNAME = "hostname";
     private final static String PORTNR = "portnr";
 
@@ -71,10 +74,11 @@
     private volatile BundleContext m_bundleContext;
     private volatile HttpContextServiceFactory m_httpContextServiceFactory;
     private volatile GadgetManagement m_gadgetManagement;
-    private volatile ConfigurationAdmin m_configurationAdmin;
 
     // The private HTTP context service for this bundle
     private Component m_httpContextComponent;
+    private String m_hostname;
+    private String m_portnr;
 
     /**
      * The init() method is invoked by the Felix dependency manager.
@@ -83,17 +87,11 @@
         // Create our own http context service which registers static 
resources and JSPs automatically
         m_httpContextComponent = 
m_httpContextServiceFactory.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/SPARQLEndpointGadget.jsp", 
-                    GadgetCategory.AMDATU_PLATFORM, true);
-            m_gadgetManagement.addGadget(gadgetDef);
-        } catch (IOException e) {
-            m_logService.log(LogService.LOG_ERROR, "Could not register SPARQL 
endpoint gadget", e);
-        }
+        // 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
+        GadgetDefinition gadgetDef = new GadgetDefinition("http://"; + 
m_hostname + ":" + m_portnr + "/" + Activator.RESOURCE_ID + 
"/jsp/SPARQLEndpointGadget.jsp",
+                GadgetCategory.AMDATU_PLATFORM, true);
+        m_gadgetManagement.addGadget(gadgetDef);
         m_logService.log(LogService.LOG_INFO, getClass().getName() + " service 
initialized");
     }
 
@@ -180,4 +178,21 @@
     public String getResourceId() {
         return Activator.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