incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r722499 - /incubator/sling/trunk/extensions/jcrinstall/service/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RepositoryObserver.java
Date Tue, 02 Dec 2008 15:31:34 GMT
Author: bdelacretaz
Date: Tue Dec  2 07:31:34 2008
New Revision: 722499

URL: http://svn.apache.org/viewvc?rev=722499&view=rev
Log:
SLING-747 - HTTP interface at /system/sling/jcrinstall to enable/disable jcrinstall, simulating
the repository going away. Needed for integration tests

Modified:
    incubator/sling/trunk/extensions/jcrinstall/service/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RepositoryObserver.java

Modified: incubator/sling/trunk/extensions/jcrinstall/service/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RepositoryObserver.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/extensions/jcrinstall/service/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RepositoryObserver.java?rev=722499&r1=722498&r2=722499&view=diff
==============================================================================
--- incubator/sling/trunk/extensions/jcrinstall/service/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RepositoryObserver.java
(original)
+++ incubator/sling/trunk/extensions/jcrinstall/service/src/main/java/org/apache/sling/jcr/jcrinstall/jcr/impl/RepositoryObserver.java
Tue Dec  2 07:31:34 2008
@@ -19,6 +19,7 @@
 package org.apache.sling.jcr.jcrinstall.jcr.impl;
 
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -34,13 +35,19 @@
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.observation.Event;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletResponse;
 
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.servlets.SlingAllMethodsServlet;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.jcr.jcrinstall.jcr.NodeConverter;
 import org.apache.sling.jcr.jcrinstall.osgi.OsgiController;
 import org.apache.sling.jcr.jcrinstall.osgi.ResourceOverrideRules;
 import org.osgi.framework.FrameworkEvent;
 import org.osgi.framework.FrameworkListener;
+import org.osgi.framework.ServiceReference;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.service.startlevel.StartLevel;
 import org.slf4j.Logger;
@@ -56,14 +63,17 @@
  *  label="%jcrinstall.name" 
  *  description="%jcrinstall.description"
  *  immediate="true"
+ *  @scr.service
  *  @scr.property 
  *      name="service.description" 
  *      value="Sling jcrinstall OsgiController Service"
  *  @scr.property 
  *      name="service.vendor" 
  *      value="The Apache Software Foundation"
+ * @scr.property name="sling.servlet.paths" value="/system/sling/jcrinstall"
  */
-public class RepositoryObserver implements Runnable, FrameworkListener {
+@SuppressWarnings("serial")
+public class RepositoryObserver extends SlingAllMethodsServlet implements Runnable, FrameworkListener
{
 
     private final SortedSet<WatchedFolder> folders = new TreeSet<WatchedFolder>();
     private RegexpFilter folderNameFilter;
@@ -74,6 +84,8 @@
     private boolean observationCycleActive;
     private boolean activated;
     
+    public static final String POST_ENABLE_PARAM = "enabled";
+    
     /** @scr.reference */
     protected OsgiController osgiController;
     
@@ -497,4 +509,57 @@
     protected Set<WatchedFolder> getWatchedFolders() {
         return folders;
     }
+
+    /** A POST can be used to deactivate/reactivate this, simulating a disappearing SlingRepository.
+     *  Used for integration testing.
+     */
+    @Override
+    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response)

+    throws ServletException, IOException {
+        final String enable = request.getParameter(POST_ENABLE_PARAM);
+        if(enable != null) {
+            if(Boolean.parseBoolean(enable)) {
+                log.info("Processing POST with {}=true, attempting to bind SlingRepository",
POST_ENABLE_PARAM);
+                if(repository != null) {
+                    response.sendError(HttpServletResponse.SC_CONFLICT, "Already enabled");
+                    return;
+                }
+                if(componentContext == null) {
+                    response.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, "No ComponentContext,
cannot enable service");
+                    return;
+                }
+                final ServiceReference ref = componentContext.getBundleContext().getServiceReference(SlingRepository.class.getName());
+                if(ref == null) {
+                    response.sendError(HttpServletResponse.SC_EXPECTATION_FAILED, "No SlingRepository
ServiceReference available");
+                    return;
+                }
+                
+                final SlingRepository r = (SlingRepository)componentContext.getBundleContext().getService(ref);
+                bindSlingRepository(r);
+                
+            } else {
+                log.info("Processing POST with {}=false, attempting to unbind SlingRepository",
POST_ENABLE_PARAM);
+                if(repository == null) {
+                    response.sendError(HttpServletResponse.SC_CONFLICT, "Not currently enabled,
cannot disable");
+                    return;
+                }
+                unbindSlingRepository(repository);
+            }
+        } else {
+            response.sendError(HttpServletResponse.SC_BAD_REQUEST,
+                    "Use '" + POST_ENABLE_PARAM + "' parameter to enable/disable the RepositoryObserver");
+            return;
+        }
+        
+        doGet(request, response);
+    }
+
+    @Override
+    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)

+    throws ServletException, IOException 
+    {
+        final String status = repository != null ? "enabled" : "disabled";
+        response.setContentType("text/plain");
+        response.getWriter().write(getClass().getSimpleName() + " is " + status);
+    }
 }
\ No newline at end of file



Mime
View raw message