You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2008/01/29 14:48:54 UTC

svn commit: r616315 - in /incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource: PathResolver.java internal/JcrResourceResolver.java internal/JcrResourceResolverFactoryImpl.java

Author: fmeschbe
Date: Tue Jan 29 05:48:53 2008
New Revision: 616315

URL: http://svn.apache.org/viewvc?rev=616315&view=rev
Log:
SLING-198 Implement new ResourceResolver methods and remove PathResolver interface

Removed:
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/PathResolver.java
Modified:
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java?rev=616315&r1=616314&r2=616315&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java (original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java Tue Jan 29 05:48:53 2008
@@ -42,7 +42,6 @@
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.api.resource.ResourceResolver;
 import org.apache.sling.jcr.resource.JcrResourceUtil;
-import org.apache.sling.jcr.resource.PathResolver;
 import org.apache.sling.jcr.resource.internal.helper.Descendable;
 import org.apache.sling.jcr.resource.internal.helper.Mapping;
 import org.apache.sling.jcr.resource.internal.helper.ResourcePathIterator;
@@ -55,12 +54,12 @@
 /**
  * The <code>JcrResourceResolver</code> class implements the Sling
  * <code>ResourceResolver</code> and <code>ResourceResolver</code>
- * interfaces and in addition is a {@link PathResolver}. Instances of this
+ * interfaces and in addition is a {@link PathMapper}. Instances of this
  * class are retrieved through the
  * {@link org.apache.sling.jcr.resource.JcrResourceResolverFactory#getResourceResolver(Session)}
  * method.
  */
-public class JcrResourceResolver extends SlingAdaptable implements ResourceResolver, PathResolver {
+public class JcrResourceResolver extends SlingAdaptable implements ResourceResolver {
 
     /** default log */
     private final Logger log = LoggerFactory.getLogger(getClass());
@@ -86,42 +85,70 @@
         return result;
     }
 
-    public Resource getResource(String path) throws SlingException {
-        path = JcrResourceUtil.normalize(path);
-        if (path != null) {
-            try {
-                Resource resource = getResourceInternal(path);
-                return resource;
-            } catch (Exception ex) {
-                throw new SlingException("Problem accessing resource" + path,
-                    ex);
-            }
+    /**
+     * @throws AccessControlException If an item would exist but is not readable
+     *             to this manager's session.
+     */
+    public Resource resolve(String uri) throws SlingException {
+
+        // TODO for now use null as a method to make sure this goes up the path
+        // (see SLING-179)
+        return resolve(uri, null);
+        
+    }
+
+    public String map(String resourcePath) {
+
+        // get first map
+        String href = null;
+        Mapping[] mappings = factory.getMappings();
+        for (int i = 0; i < mappings.length && href == null; i++) {
+            href = mappings[i].mapHandle(resourcePath);
         }
 
-        // relative path segments cannot be resolved
-        return null;
-     }
+        // if no mapping's to prefix matches the handle, use the handle itself
+        if (href == null) {
+            href = resourcePath;
+        }
 
-    public Resource getResource(Resource base, String path)
-            throws SlingException {
-        // special case of absolute paths
+        // check virtual mappings
+        String virtual = factory.realToVirtualUri(href);
+        if (virtual != null) {
+            log.debug("map: Using virtual URI {} for path {}", virtual,
+                href);
+            href = virtual;
+        }
+
+        log.debug("map: {} -> {}", resourcePath, href);
+        return href;
+    }
+
+    public Resource getResource(String path) {
+        
+        // if the path is absolute, normalize . and .. segements and get res
         if (path.startsWith("/")) {
-            return getResource(path);
+            path = JcrResourceUtil.normalize(path);
+            return (path != null) ? getResourceInternal(path) : null;
         }
 
-        // resolve relative path segments now
-        path = JcrResourceUtil.normalize(path);
-        if (path != null) {
-            if (path.length() == 0) {
-                // return the base resource
-                return base;
-            } else if (base instanceof Descendable) {
-                return ((Descendable) base).getDescendent(path);
+        // otherwise we have to apply the search path
+        for (String prefix : factory.getPath()) {
+            Resource res = getResource(prefix + path);
+            if (res != null) {
+                return res;
             }
         }
+        
+        // no resource found, if we get here
+        return null;
+     }
+
+    public Resource getResource(Resource base, String path) {
+        
+        if (!path.startsWith("/") && base != null) {
+            path = base.getPath() + "/" + path;
+        }
 
-        // try (again) with absolute resource path
-        path = base.getPath() + "/" + path;
         return getResource(path);
     }
 
@@ -203,60 +230,19 @@
     public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
         if (type == Session.class) {
             return (AdapterType) getSession();
-        } else if (type == PathResolver.class) {
-            return (AdapterType) this;
         }
 
         // fall back to default behaviour
         return super.adaptTo(type);
     }
 
-    // ---------- PathResolver interface --------------------------------------
-
-    /**
-     * @throws AccessControlException If an item would exist but is not readable
-     *             to this manager's session.
-     */
-    public Resource resolve(String uri) throws SlingException {
-        // TODO for now use null as a method to make sure this goes up the path
-        // (see SLING-179)
-        return resolve(uri, null);
-    }
-
-    public String pathToURL(Resource resource) {
-        String path = resource.getPath();
-
-        // get first map
-        String href = null;
-        Mapping[] mappings = factory.getMappings();
-        for (int i = 0; i < mappings.length && href == null; i++) {
-            href = mappings[i].mapHandle(path);
-        }
-
-        // if no mapping's to prefix matches the handle, use the handle itself
-        if (href == null) {
-            href = path;
-        }
-
-        // check virtual mappings
-        String virtual = factory.realToVirtualUri(href);
-        if (virtual != null) {
-            log.debug("pathToURL: Using virtual URI {} for path {}", virtual,
-                href);
-            href = virtual;
-        }
-
-        log.debug("MapHandle: {} -> {}", path, href);
-        return href;
-    }
-
     // ---------- implementation helper ----------------------------------------
 
     /**
      * @throws AccessControlException If an item would exist but is not readable
      *             to this manager's session.
      */
-    public Resource resolve(String uri, String httpMethod) throws SlingException {
+    private Resource resolve(String uri, String httpMethod) throws SlingException {
 
         // decode the request URI (required as the servlet container does not
         try {
@@ -354,7 +340,7 @@
      * @throws AccessControlException If an item exists but this manager has no
      *             read access
      */
-    protected Resource getResourceInternal(String path) throws Exception {
+    protected Resource getResourceInternal(String path) {
 
         Resource resource= null;
 

Modified: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java?rev=616315&r1=616314&r2=616315&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java (original)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java Tue Jan 29 05:48:53 2008
@@ -102,6 +102,13 @@
      */
     private static final String PROP_MAPPING = "resource.resolver.mapping";
 
+    /**
+     * @scr.property values.1="/apps" values.2="/libs"
+     *               label="%resolver.path.name"
+     *               description="%resolver.path.description"
+     */
+    public static final String PROP_PATH = "resource.resolver.path";
+
     /** default log */
     private final Logger log = LoggerFactory.getLogger(getClass());
 
@@ -130,7 +137,7 @@
     // list of ResourceProvider services bound before activation of the
     // component
     private List<ServiceReference> delayedResourceProviders = new LinkedList<ServiceReference>();
-
+    
     private ComponentContext componentContext;
 
     /**
@@ -148,6 +155,9 @@
     /** <code>true</code>, if direct mappings from URI to handle are allowed */
     private boolean allowDirect = false;
 
+    // the search path for ResourceResolver.getResource(String)
+    private String[] path;
+
     /**
      * Map of administrative sessions used to check item existence. Indexed by
      * workspace name. The map is filled on-demand. The sessions are closed when
@@ -339,6 +349,10 @@
         return mappings;
     }
 
+    String[] getPath() {
+        return path;
+    }
+
     // ---------- Bundle provided resources -----------------------------------
 
     private void addBundleResourceProvider(Bundle bundle) {
@@ -435,6 +449,24 @@
             mappings = tmp;
         }
 
+        // from configuration if available
+        path = OsgiUtil.toStringArray(properties.get(PROP_PATH));
+        if (path != null && path.length > 0) {
+            for (int i = 0; i < path.length; i++) {
+                // ensure leading slash
+                if (!path[i].startsWith("/")) {
+                    path[i] = "/" + path[i];
+                }
+                // ensure trailing slash
+                if (!path[i].endsWith("/")) {
+                    path[i] += "/";
+                }
+            }
+        }
+        if (path == null) {
+            path = new String[] { "/" };
+        }
+
         // bind resource providers not bound yet
         for (ServiceReference reference : delayedResourceProviders) {
             bindResourceProvider(reference);
@@ -460,10 +492,10 @@
 
     protected void bindResourceProvider(ServiceReference reference) {
         if (componentContext == null) {
-
+            
             // delay binding resource providers if called before activation
             delayedResourceProviders.add(reference);
-
+            
         } else {
             String[] roots = OsgiUtil.toStringArray(reference.getProperty(ResourceProvider.ROOTS));
             if (roots != null && roots.length > 0) {