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/18 14:43:23 UTC

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

Author: fmeschbe
Date: Fri Jan 18 05:43:22 2008
New Revision: 613159

URL: http://svn.apache.org/viewvc?rev=613159&view=rev
Log:
SLING-161 Createa a Resource instance factory in the JcrResourceResolver to create
a node or property based Resource as appropriate and add JcrPropertyResource
implementation.

Added:
    incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.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=613159&r1=613158&r2=613159&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 Fri Jan 18 05:43:22 2008
@@ -27,7 +27,9 @@
 import java.util.List;
 import java.util.Map;
 
+import javax.jcr.Item;
 import javax.jcr.Node;
+import javax.jcr.Property;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Value;
@@ -52,14 +54,15 @@
 import org.apache.sling.jcr.resource.internal.helper.ResourceProvider;
 import org.apache.sling.jcr.resource.internal.helper.jcr.JcrNodeResource;
 import org.apache.sling.jcr.resource.internal.helper.jcr.JcrNodeResourceIterator;
+import org.apache.sling.jcr.resource.internal.helper.jcr.JcrPropertyResource;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * 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 class are
- * retrieved through the
+ * <code>ResourceResolver</code> and <code>ResourceResolver</code>
+ * interfaces and in addition is a {@link PathResolver}. Instances of this
+ * class are retrieved through the
  * {@link org.apache.sling.jcr.resource.JcrResourceResolverFactory#getResourceResolver(Session)}
  * method.
  */
@@ -304,7 +307,8 @@
         return href;
     }
 
-    // ---------- former ResourceManager interface -----------------------------------
+    // ---------- former ResourceManager interface
+    // -----------------------------------
 
     /**
      * @throws AccessControlException If this manager has does not have enough
@@ -480,7 +484,7 @@
      * <code>path</code> is mapping. If no mapping exists for an existing
      * node, the node's content is loaded into a new instance of the
      * {@link #DEFAULT_CONTENT_CLASS default content class}.
-     *
+     * 
      * @param type Load the node's content into an object of the given type if
      *            not <code>null</code>.
      * @return the <code>Content</code> object loaded from the node or
@@ -553,7 +557,6 @@
                         ResourceMetadata.RESOLUTION_PATH, uriPath);
                 }
 
-
                 return resource;
             }
 
@@ -584,7 +587,7 @@
 
     /**
      * Creates a JcrNodeResource with the given path if existing
-     *
+     * 
      * @throws AccessControlException If an item exists but this manager has no
      *             read access
      */
@@ -610,7 +613,7 @@
      * Checks whether the item exists and this content manager's session has
      * read access to the item. If the item does not exist, access control is
      * ignored by this method and <code>false</code> is returned.
-     *
+     * 
      * @param path The path to the item to check
      * @return <code>true</code> if the item exists and this content manager's
      *         session has read access. If the item does not exist,
@@ -619,13 +622,46 @@
      * @throws AccessControlException If the item really exists but this content
      *             manager's session has no read access to it.
      */
-    public boolean itemExists(String path) throws RepositoryException {
+    boolean itemExists(String path) throws RepositoryException {
         if (factory.itemReallyExists(getSession(), path)) {
             checkPermission(path, ACTION_READ);
             return true;
         }
 
         return false;
+    }
+
+    /**
+     * Creates a <code>Resource</code> instance for the item found at the
+     * given path. If no item exists at that path or the item does not have
+     * read-access for the session of this resolver, <code>null</code> is
+     * returned.
+     * 
+     * @param path The absolute path
+     * @return The <code>Resource</code> for the item at the given path.
+     * @throws RepositoryException If an error occurrs accessingor checking the
+     *             item in the repository.
+     * @throws AccessControlException If the item really exists but this content
+     *             manager's session has no read access to it.
+     */
+    Resource createResource(String path) throws RepositoryException {
+        if (itemExists(path)) {
+            Item item = getSession().getItem(path);
+            if (item.isNode()) {
+                log.debug(
+                    "createResource: Found JCR Node Resource at path '{}'",
+                    path);
+                return new JcrNodeResource(this, (Node) item);
+            }
+
+            log.debug(
+                "createResource: Found JCR Property Resource at path '{}'",
+                path);
+            return new JcrPropertyResource(path, (Property) item);
+        }
+
+        log.debug("createResource: No JCR Item exists at path '{}'", path);
+        return 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=613159&r1=613158&r2=613159&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 Fri Jan 18 05:43:22 2008
@@ -240,13 +240,7 @@
 
     public Resource getResource(JcrResourceResolver jcrResourceResolver,
             String path) throws RepositoryException {
-
-        if (jcrResourceResolver.itemExists(path)) {
-            log.info("getResource: Found JCR Node Resource at path '{}'", path);
-            return new JcrNodeResource(jcrResourceResolver, path);
-        }
-
-        return null;
+        return jcrResourceResolver.createResource(path);
     }
 
     // ---------- EventAdmin Event Dispatching ---------------------------------

Added: incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java?rev=613159&view=auto
==============================================================================
--- incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java (added)
+++ incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/JcrPropertyResource.java Fri Jan 18 05:43:22 2008
@@ -0,0 +1,100 @@
+/*
+ * 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.jcr.resource.internal.helper.jcr;
+
+import static org.apache.sling.api.resource.ResourceMetadata.RESOLUTION_PATH;
+
+import java.util.Calendar;
+
+import javax.jcr.Item;
+import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
+import javax.jcr.ValueFormatException;
+
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+
+public class JcrPropertyResource implements Resource {
+
+    private final String path;
+
+    private final Property property;
+
+    private final String resourceType;
+    
+    private final ResourceMetadata metadata;
+
+    public JcrPropertyResource(String path, Property property)
+            throws RepositoryException {
+        this.path = path;
+        this.property = property;
+        this.resourceType = JcrNodeResource.getResourceTypeForNode(property.getParent())
+            + "/" + property.getName();
+        this.metadata = new ResourceMetadata();
+        this.metadata.put(RESOLUTION_PATH, path);
+    }
+
+    public String getPath() {
+        return path;
+    }
+
+    public ResourceMetadata getResourceMetadata() {
+        return metadata;
+    }
+
+    public String getResourceType() {
+        return resourceType;
+    }
+
+    @SuppressWarnings("unchecked")
+    public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) {
+
+        // the property itself
+        if (type == Property.class || type == Item.class) {
+            return (AdapterType) property;
+        }
+
+        // the property value
+        try {
+            if (type == Boolean.class) {
+                return (AdapterType) new Boolean(getProperty().getBoolean());
+            } else if (type == Long.class) {
+                return (AdapterType) new Long(getProperty().getLong());
+            } else if (type == Double.class) {
+                return (AdapterType) new Double(getProperty().getDouble());
+            } else if (type == Calendar.class) {
+                return (AdapterType) getProperty().getDate();
+            } else if (type == Value.class) {
+                return (AdapterType) getProperty().getValue();
+            }
+        } catch (ValueFormatException vfe) {
+            // TODO: log
+        } catch (RepositoryException re) {
+            // TODO: log
+        }
+        
+        // no adapter here
+        return null;
+    }
+
+    public Property getProperty() {
+        return property;
+    }
+}