You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2016/08/16 09:18:38 UTC

svn commit: r1756504 - /sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/GetConfigServlet.java

Author: bdelacretaz
Date: Tue Aug 16 09:18:37 2016
New Revision: 1756504

URL: http://svn.apache.org/viewvc?rev=1756504&view=rev
Log:
Avoid creating configs in GetConfigServlet, to avoid restarting services without a real need

Modified:
    sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/GetConfigServlet.java

Modified: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/GetConfigServlet.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/GetConfigServlet.java?rev=1756504&r1=1756503&r2=1756504&view=diff
==============================================================================
--- sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/GetConfigServlet.java (original)
+++ sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/servlets/GetConfigServlet.java Tue Aug 16 09:18:37 2016
@@ -38,6 +38,7 @@ import org.osgi.service.cm.Configuration
 import org.osgi.service.cm.ConfigurationAdmin;
 
 /** GET returns the contents of an OSGi config by PID */
+@SuppressWarnings("serial")
 @Component
 @Service
 @Properties({
@@ -64,11 +65,10 @@ public class GetConfigServlet extends Sl
             pid = pid.substring(1);
         }
 
-        // Get config and properties
-        final Configuration cfg = configAdmin.getConfiguration(pid);
-        if(cfg == null) {
-            response.sendError(HttpServletResponse.SC_NOT_FOUND, "Config with pid=" + pid + " not found");
-        }
+        // Get config and properties. Avoid using configAdmin.getConfiguration(...) 
+        // to avoid creating a config that does not exist yet, which might cause
+        // services to restart.
+        final Configuration cfg = getUniqueConfig(pid);
         final Dictionary<?, ?> props = cfg.getProperties();
         if(props == null) {
             response.sendError(HttpServletResponse.SC_NOT_FOUND, "Properties of config with pid=" + pid + " not found");
@@ -99,4 +99,21 @@ public class GetConfigServlet extends Sl
             throw (IOException)new IOException("JSONException in doGet").initCause(je);
         }
     }
-}
+    
+    private Configuration getUniqueConfig(String pid) throws ServletException {
+        final String filter = "(service.pid=" + pid + ")";
+        Configuration [] cfg = null;
+        try {
+            cfg = configAdmin.listConfigurations(filter);
+        } catch(Exception e) {
+            throw new ServletException("Error listing configs with filter " + filter, e);
+        }
+        if(cfg == null) {
+            throw new ServletException("No config found with filter " + filter);
+        }
+        if(cfg.length > 1) {
+            throw new ServletException("Expected 1 config, found " + cfg.length + " with filter " + filter);
+        }
+        return cfg[0];
+    }
+}
\ No newline at end of file