incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r586028 - in /incubator/sling/whiteboard/microsling/src: main/java/org/apache/sling/microsling/api/ main/java/org/apache/sling/microsling/request/ main/java/org/apache/sling/microsling/services/ main/java/org/apache/sling/microsling/servlet...
Date Thu, 18 Oct 2007 16:34:06 GMT
Author: bdelacretaz
Date: Thu Oct 18 09:34:01 2007
New Revision: 586028

URL: http://svn.apache.org/viewvc?rev=586028&view=rev
Log:
SLING-55 : ServiceLocator interface added to api, used to access ResourceResolver

Added:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/services/
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/services/MicroslingServiceLocator.java
  (with props)
    incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/services/
    incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/services/MicroslingServiceLocatorTest.java
  (with props)
Modified:
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/SlingRequestContext.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/MicroslingRequestContext.java
    incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/SlingRequestContext.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/SlingRequestContext.java?rev=586028&r1=586027&r2=586028&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/SlingRequestContext.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/api/SlingRequestContext.java
Thu Oct 18 09:34:01 2007
@@ -40,6 +40,9 @@
             return ctx;
         }
     }
+    
+    /** The ServiceLocator provides access to other services */
+    ServiceLocator getServiceLocator();
 
     /** Acquire a JCR Session if not done yet, and return it */
     Session getSession() throws RepositoryException;

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/MicroslingRequestContext.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/MicroslingRequestContext.java?rev=586028&r1=586027&r2=586028&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/MicroslingRequestContext.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/request/MicroslingRequestContext.java
Thu Oct 18 09:34:01 2007
@@ -27,6 +27,8 @@
 
 import org.apache.sling.microsling.api.RequestParameterMap;
 import org.apache.sling.microsling.api.Resource;
+import org.apache.sling.microsling.api.ResourceResolver;
+import org.apache.sling.microsling.api.ServiceLocator;
 import org.apache.sling.microsling.api.SlingRequestContext;
 import org.apache.sling.microsling.api.SlingRequestPathInfo;
 import org.apache.sling.microsling.api.exceptions.SlingException;
@@ -44,11 +46,14 @@
     private Resource resource;
     private final SlingRequestPathInfoParser requestPathInfo;
     private final HttpServletRequest request;
+    private final ServiceLocator serviceLocator;
     private RequestParameterMap parameterMap;
 
     /** Build a MicroslingRequestContext and add it to the request attributes */
-    public MicroslingRequestContext(ServletContext sctx,ServletRequest servletRequest) throws
ServletException {
+    public MicroslingRequestContext(ServletContext sctx,ServletRequest servletRequest,ServiceLocator
serviceLocator) 
+    throws ServletException {
         this.servletContext = sctx;
+        this.serviceLocator = serviceLocator;
 
         if( !(servletRequest instanceof HttpServletRequest) ) {
             throw new ServletException("SlingRequestContext requires an HttpServletRequest");
@@ -83,8 +88,8 @@
 
     public Resource getResource() throws Exception {
         if(resource==null) {
-            // TODO use ServiceLocator here?
-            resource = new MicroslingResourceResolver().getResource(request);
+            final ResourceResolver rr = (ResourceResolver)serviceLocator.getRequiredService(ResourceResolver.class.getName());
+            resource = rr.getResource(request);
             // TODO initialize SlingRequestPathInfo from resource here
         }
         return resource;
@@ -111,4 +116,9 @@
     public RequestParameterMap getRequestParameterMap() {
         return parameterMap;
     }
+    
+    public ServiceLocator getServiceLocator() {
+        return serviceLocator;
+    }
+
 }

Added: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/services/MicroslingServiceLocator.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/services/MicroslingServiceLocator.java?rev=586028&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/services/MicroslingServiceLocator.java
(added)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/services/MicroslingServiceLocator.java
Thu Oct 18 09:34:01 2007
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.microsling.services;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.sling.microsling.api.ResourceResolver;
+import org.apache.sling.microsling.api.ServiceLocator;
+import org.apache.sling.microsling.resource.MicroslingResourceResolver;
+
+/** Poor man's ServiceLocator (no, poorer than that) which uses a 
+ *  static list of services. This is mostly meant to introduce
+ *  the ServiceLocator interface in microsling.
+ *  See Sling OSGi for the real McCoy.
+ */
+
+public class MicroslingServiceLocator implements ServiceLocator {
+
+    protected final Map <String,Object> services = new HashMap <String, Object>
();
+    
+    public MicroslingServiceLocator() {
+        // initialize our services (pure rocket science, isn't it? ;-)
+        services.put(ResourceResolver.class.getName(),new MicroslingResourceResolver());
+    }
+    
+    public Object getService(String serviceName) {
+        return services.get(serviceName);
+    }
+    
+    
+    public Object getRequiredService(String serviceName) throws ServiceNotAvailableException
{
+        final Object result = services.get(serviceName);
+        if(result== null) {
+            throw new ServiceNotAvailableException("Service '" + serviceName + "' is not
available");
+        }
+        return result;
+    }
+
+    public Object[] getServices(String serviceName, String filter) 
+    throws InvalidServiceFilterSyntaxException {
+        throw new InvalidServiceFilterSyntaxException(
+                "Sorry, MicroslingServiceLocator.getServices is not implemented");
+    }
+
+}

Propchange: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/services/MicroslingServiceLocator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/services/MicroslingServiceLocator.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java?rev=586028&r1=586027&r2=586028&view=diff
==============================================================================
--- incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java
(original)
+++ incubator/sling/whiteboard/microsling/src/main/java/org/apache/sling/microsling/servlet/MicroSlingServlet.java
Thu Oct 18 09:34:01 2007
@@ -30,11 +30,13 @@
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.sling.microsling.api.ServiceLocator;
 import org.apache.sling.microsling.api.SlingServlet;
 import org.apache.sling.microsling.api.exceptions.HttpStatusCodeException;
 import org.apache.sling.microsling.contenttype.ResponseContentTypeResolverFilter;
 import org.apache.sling.microsling.helpers.constants.HttpConstants;
 import org.apache.sling.microsling.request.MicroslingRequestContext;
+import org.apache.sling.microsling.services.MicroslingServiceLocator;
 import org.apache.sling.microsling.slingservlets.DefaultSlingServlet;
 import org.apache.sling.microsling.slingservlets.RhinoJavascriptServlet;
 import org.apache.sling.microsling.slingservlets.SlingPostServlet;
@@ -50,6 +52,7 @@
     private static final long serialVersionUID = 1L;
 
     private MicroSlingFilterHelper filterChain;
+    private ServiceLocator serviceLocator;
 
     private List<SlingServlet> servlets = new LinkedList<SlingServlet>();
 
@@ -57,6 +60,8 @@
     public void init() throws ServletException {
         super.init();
 
+        serviceLocator = new MicroslingServiceLocator();
+        
         // TODO use OSGi to setup this list
         filterChain = new MicroSlingFilterHelper(this);
         addFilter(new ResponseContentTypeResolverFilter());
@@ -81,7 +86,7 @@
             throws ServletException, IOException {
 
         // our filters might need the SlingRequestContext to store info in it
-        new MicroslingRequestContext(getServletContext(),req);
+        new MicroslingRequestContext(getServletContext(),req,serviceLocator);
         filterChain.service(req, resp);
     }
 

Added: incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/services/MicroslingServiceLocatorTest.java
URL: http://svn.apache.org/viewvc/incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/services/MicroslingServiceLocatorTest.java?rev=586028&view=auto
==============================================================================
--- incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/services/MicroslingServiceLocatorTest.java
(added)
+++ incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/services/MicroslingServiceLocatorTest.java
Thu Oct 18 09:34:01 2007
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.microsling.services;
+
+import junit.framework.TestCase;
+
+import org.apache.sling.microsling.api.ResourceResolver;
+import org.apache.sling.microsling.api.ServiceLocator;
+
+public class MicroslingServiceLocatorTest extends TestCase {
+    private ServiceLocator serviceLocator;
+    
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        serviceLocator = new MicroslingServiceLocator(); 
+    }
+
+    public void testServiceFound() {
+        try {
+            final Object svc = serviceLocator.getService(ResourceResolver.class.getName());
+            assertNotNull(svc);
+            assertTrue(svc instanceof ResourceResolver);
+        } catch(Exception e) {
+            fail("Unexpected Exception in getService call " + e);
+        }
+    }
+    
+    public void testServiceNotFound() {
+        try {
+            final Object svc = serviceLocator.getService("some stuff");
+            assertNull(svc);
+        } catch(Exception e) {
+            fail("Unexpected Exception in getService call " + e);
+        }
+    }
+    
+    public void testRequiredServiceFound() {
+        try {
+            final Object svc = serviceLocator.getRequiredService(ResourceResolver.class.getName());
+            assertNotNull(svc);
+            assertTrue(svc instanceof ResourceResolver);
+        } catch(Exception e) {
+            fail("Unexpected Exception in getService call " + e);
+        }
+    }
+    
+    public void testRequiredServiceNotFound() {
+        try {
+            serviceLocator.getRequiredService("some stuff");
+            fail("Expected Exception when service is not found");
+            
+        } catch(ServiceLocator.ServiceNotAvailableException sna) {
+            //fine - as expected
+            
+        } catch(Exception e) {
+            fail("Unexpected Exception in getRequiredService call " + e);
+        }
+    }
+}

Propchange: incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/services/MicroslingServiceLocatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/whiteboard/microsling/src/test/java/org/apache/sling/microsling/services/MicroslingServiceLocatorTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL



Mime
View raw message