incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r693474 - in /incubator/sling/trunk/servlets/resolver/src: main/java/org/apache/sling/servlets/resolver/ main/java/org/apache/sling/servlets/resolver/resource/ test/java/org/apache/sling/servlets/resolver/resource/
Date Tue, 09 Sep 2008 14:40:05 GMT
Author: fmeschbe
Date: Tue Sep  9 07:40:03 2008
New Revision: 693474

URL: http://svn.apache.org/viewvc?rev=693474&view=rev
Log:
SLING-652 Be more lenient to failed servlet service acquiry:
   * First check the service properties
   * Catch service acquisition problems to fail gracefully

Modified:
    incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/SlingServletResolver.java
    incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/resource/ServletResourceProvider.java
    incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/resource/ServletResourceProviderFactory.java
    incubator/sling/trunk/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/resource/ServletResourceProviderCreateTest.java

Modified: incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/SlingServletResolver.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/SlingServletResolver.java?rev=693474&r1=693473&r2=693474&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/SlingServletResolver.java
(original)
+++ incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/SlingServletResolver.java
Tue Sep  9 07:40:03 2008
@@ -564,6 +564,7 @@
     private boolean createServlet(ServletContext servletContext,
             ServiceReference reference) {
 
+        // check for a name, this is required
         String name = AbstractServiceReferenceConfig.getName(reference);
         if (name == null) {
             log.error(
@@ -572,24 +573,32 @@
             return false;
         }
 
-        Servlet servlet = (Servlet) context.locateService(REF_SERVLET,
-            reference);
-        if (servlet == null) {
-            log.error(
-                "bindServlet: Servlet service not available from reference {}",
-                reference);
+        // check for Sling properties in the service registration
+        ServletResourceProvider provider = servletResourceProviderFactory.create(reference);
+        if (provider == null) {
+            // this is expected if the servlet is not destined for Sling
             return false;
         }
 
-        ServletResourceProvider provider = servletResourceProviderFactory.create(
-            reference, servlet);
-        if (provider == null) {
+        // only now try to access the servlet service, this may still fail
+        Servlet servlet = null;
+        try {
+            servlet = (Servlet) context.locateService(REF_SERVLET,reference);
+        } catch (Throwable t) {
+            log.warn("bindServlet: Failed getting the service for reference "
+                + reference, t);
+        }
+        if (servlet == null) {
             log.error(
-                "createServlet: Cannot register servlet {} without path or resource type
configuration",
-                name);
+                "bindServlet: Servlet service not available from reference {}",
+                reference);
             return false;
         }
 
+        // assign the servlet to the provider
+        provider.setServlet(servlet);
+        
+        // initialize now
         try {
             servlet.init(new SlingServletConfig(servletContext, reference, name));
             log.debug("bindServlet: Servlet {} added", name);

Modified: incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/resource/ServletResourceProvider.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/resource/ServletResourceProvider.java?rev=693474&r1=693473&r2=693474&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/resource/ServletResourceProvider.java
(original)
+++ incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/resource/ServletResourceProvider.java
Tue Sep  9 07:40:03 2008
@@ -30,22 +30,26 @@
 
 public class ServletResourceProvider implements ResourceProvider {
 
-    private final Servlet servlet;
+    private Servlet servlet;
 
     private Set<String> resourcePaths;
 
-    ServletResourceProvider(Set<String> resourcePaths, Servlet servlet) {
-        this.servlet = servlet;
+    ServletResourceProvider(Set<String> resourcePaths) {
         this.resourcePaths = resourcePaths;
     }
 
+    public void setServlet(Servlet servlet) {
+        this.servlet = servlet;
+    }
+    
     public Resource getResource(ResourceResolver resourceResolver,
             HttpServletRequest request, String path) {
         return getResource(resourceResolver, path);
     }
 
     public Resource getResource(ResourceResolver resourceResolver, String path) {
-        if (resourcePaths.contains(path)) {
+        // only return a resource if the servlet has been assigned
+        if (servlet != null && resourcePaths.contains(path)) {
             return new ServletResource(resourceResolver, servlet, path);
         }
 

Modified: incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/resource/ServletResourceProviderFactory.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/resource/ServletResourceProviderFactory.java?rev=693474&r1=693473&r2=693474&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/resource/ServletResourceProviderFactory.java
(original)
+++ incubator/sling/trunk/servlets/resolver/src/main/java/org/apache/sling/servlets/resolver/resource/ServletResourceProviderFactory.java
Tue Sep  9 07:40:03 2008
@@ -27,8 +27,6 @@
 import java.util.HashSet;
 import java.util.Set;
 
-import javax.servlet.Servlet;
-
 import org.apache.sling.api.servlets.HttpConstants;
 import org.apache.sling.commons.osgi.OsgiUtil;
 import org.apache.sling.jcr.resource.JcrResourceUtil;
@@ -80,7 +78,7 @@
         this.servletRoot = servletRoot;
     }
 
-    public ServletResourceProvider create(ServiceReference ref, Servlet servlet) {
+    public ServletResourceProvider create(ServiceReference ref) {
 
         Set<String> pathSet = new HashSet<String>();
 
@@ -104,7 +102,7 @@
                 getServiceIdentifier(ref), pathSet);
         }
         
-        return new ServletResourceProvider(pathSet, servlet);
+        return new ServletResourceProvider(pathSet);
     }
 
     private void addByPath(Set<String> pathSet, ServiceReference ref) {

Modified: incubator/sling/trunk/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/resource/ServletResourceProviderCreateTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/resource/ServletResourceProviderCreateTest.java?rev=693474&r1=693473&r2=693474&view=diff
==============================================================================
--- incubator/sling/trunk/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/resource/ServletResourceProviderCreateTest.java
(original)
+++ incubator/sling/trunk/servlets/resolver/src/test/java/org/apache/sling/servlets/resolver/resource/ServletResourceProviderCreateTest.java
Tue Sep  9 07:40:03 2008
@@ -56,9 +56,9 @@
         msr.setProperty(ServletResolverConstants.SLING_SERVLET_RESOURCE_TYPES,
             RES_TYPE);
         // msr.setProperty(ServletResolverConstants.SLING_SERVLET_METHODS, "*");
-
-        ServletResourceProvider srp = factory.create(msr, TEST_SERVLET);
+        ServletResourceProvider srp = factory.create(msr);
         assertNotNull(srp);
+        srp.setServlet(TEST_SERVLET);
 
         String[] paths = srp.getSerlvetPaths();
         assertNotNull(paths);
@@ -84,8 +84,9 @@
             RES_TYPE);
         msr.setProperty(ServletResolverConstants.SLING_SERVLET_METHODS, "GET");
 
-        ServletResourceProvider srp = factory.create(msr, TEST_SERVLET);
+        ServletResourceProvider srp = factory.create(msr);
         assertNotNull(srp);
+        srp.setServlet(TEST_SERVLET);
 
         String[] paths = srp.getSerlvetPaths();
         assertNotNull(paths);
@@ -110,8 +111,9 @@
         msr.setProperty(ServletResolverConstants.SLING_SERVLET_METHODS,
             new String[] { "GET", "POST", "PUT" });
 
-        ServletResourceProvider srp = factory.create(msr, TEST_SERVLET);
+        ServletResourceProvider srp = factory.create(msr);
         assertNotNull(srp);
+        srp.setServlet(TEST_SERVLET);
 
         String[] paths = srp.getSerlvetPaths();
         assertNotNull(paths);
@@ -139,8 +141,9 @@
             RES_TYPE);
         msr.setProperty(ServletResolverConstants.SLING_SERVLET_METHODS, "*");
 
-        ServletResourceProvider srp = factory.create(msr, TEST_SERVLET);
+        ServletResourceProvider srp = factory.create(msr);
         assertNotNull(srp);
+        srp.setServlet(TEST_SERVLET);
 
         String[] paths = srp.getSerlvetPaths();
         assertNotNull(paths);



Mime
View raw message