Author: fmeschbe
Date: Thu Nov 29 06:28:03 2007
New Revision: 599454

URL: http://svn.apache.org/viewvc?rev=599454&view=rev
Log:
SLING-109 Adapt to new Resource and provider interfaces
   - JcrNodeResource implements Node, Object, URL and StreamProvider
   - Add Descendable interface to offload listing children and
     accessing children from the JcrResourceManager to the
     Resource implementation.

Added:
    
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/Descendable.java
Modified:
    incubator/sling/trunk/jcr/resource/pom.xml
    
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceManager.java
    
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerFactoryImpl.java
    
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java
    
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResourceIterator.java
    
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerTest.java

Modified: incubator/sling/trunk/jcr/resource/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/pom.xml?rev=599454&r1=599453&r2=599454&view=diff
==============================================================================
--- incubator/sling/trunk/jcr/resource/pom.xml (original)
+++ incubator/sling/trunk/jcr/resource/pom.xml Thu Nov 29 06:28:03 2007
@@ -79,6 +79,7 @@
                         </Export-Package>
                         <Private-Package>
                             org.apache.sling.jcr.resource.internal.*,
+                            org.apache.jackrabbit.net,
                             org.apache.jackrabbit.ocm.lock,
                             
org.apache.jackrabbit.ocm.manager.atomictypeconverter.impl,
                             org.apache.jackrabbit.ocm.manager.beanconverter,
@@ -97,7 +98,9 @@
                             org.kxml2.io, org.xmlpull.v1
                         </Private-Package>
                         <Import-Package>
-                            
org.apache.jackrabbit.ocm.mapper.impl.digester;resolution:=optional,*
+                            !org.apache.jackrabbit.classloader,
+                            !org.apache.jackrabbit.ocm.mapper.impl.digester,
+                            *
                         </Import-Package>
 
                         <Sling-Namespaces>
@@ -146,9 +149,16 @@
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-simple</artifactId>
         </dependency>
+
         <dependency>
             <groupId>org.apache.jackrabbit</groupId>
             <artifactId>jackrabbit-ocm</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.jackrabbit</groupId>
+            <artifactId>jackrabbit-classloader</artifactId>
+            <version>1.3.1</version>
+            <scope>compile</scope>
         </dependency>
 
         <dependency>

Modified: 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceManager.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceManager.java?rev=599454&r1=599453&r2=599454&view=diff
==============================================================================
--- 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceManager.java
 (original)
+++ 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceManager.java
 Thu Nov 29 06:28:03 2007
@@ -27,7 +27,6 @@
 import java.util.List;
 import java.util.Map;
 
-import javax.jcr.Item;
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
@@ -41,17 +40,22 @@
 import org.apache.jackrabbit.ocm.manager.ObjectContentManager;
 import org.apache.jackrabbit.ocm.reflection.ReflectionUtils;
 import org.apache.sling.api.SlingException;
+import org.apache.sling.api.resource.NodeProvider;
 import org.apache.sling.api.resource.NonExistingResource;
+import org.apache.sling.api.resource.ObjectProvider;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceManager;
 import org.apache.sling.api.resource.ResourceMetadata;
 import org.apache.sling.jcr.resource.DefaultMappedObject;
 import org.apache.sling.jcr.resource.JcrResourceUtil;
 import org.apache.sling.jcr.resource.PathResolver;
+import org.apache.sling.jcr.resource.internal.helper.BundleResource;
+import org.apache.sling.jcr.resource.internal.helper.Descendable;
 import org.apache.sling.jcr.resource.internal.helper.JcrNodeResource;
 import org.apache.sling.jcr.resource.internal.helper.JcrNodeResourceIterator;
 import org.apache.sling.jcr.resource.internal.helper.Mapping;
 import org.apache.sling.jcr.resource.internal.helper.ResourcePathIterator;
+import org.osgi.framework.Bundle;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -126,22 +130,8 @@
             if (path.length() == 0) {
                 // return the base resource
                 return base;
-            } else if (base.getRawData() instanceof Node) {
-                try {
-                    Node baseNode = (Node) base.getRawData();
-                    if (baseNode.hasNode(path)) {
-                        return new JcrNodeResource(this, 
baseNode.getNode(path));
-                    }
-
-                    log.error("getResource: There is no node at {} below {}",
-                        path, base.getURI());
-                    return null;
-                } catch (RepositoryException re) {
-                    log.error(
-                        "getResource: Problem accessing relative resource at "
-                            + path, re);
-                    return null;
-                }
+            } else if (base instanceof Descendable) {
+                return ((Descendable) base).getDescendent(path);
             }
         }
 
@@ -150,17 +140,18 @@
         return getResource(path);
     }
 
-    public Iterator<Resource> listChildren(final Resource parent)
-            throws SlingException {
-        if (parent.getRawData() instanceof Node) {
+    public Iterator<Resource> listChildren(Resource parent) {
+        if (parent instanceof Descendable) {
+            return ((Descendable) parent).listChildren();
+        }
 
-            try {
-                return new JcrNodeResourceIterator(this,
-                    ((Node) parent.getRawData()).getNodes());
-            } catch (RepositoryException re) {
-                throw new SlingException("Cannot get children of Resource "
-                    + parent, re);
+        try {
+            parent = getResource(parent.getURI());
+            if (parent instanceof Descendable) {
+                return ((Descendable) parent).listChildren();
             }
+        } catch (SlingException se) {
+            log.warn("listChildren: Error trying to resolve parent resource " 
+ parent.getURI(), se);
         }
 
         // return an empty iterator if parent has no node
@@ -308,14 +299,15 @@
      */
     public void store(Resource resource) throws SlingException {
         String path = resource.getURI();
-        if (resource.getObject() != null) {
+        if (resource instanceof ObjectProvider) {
+            Object data = ((ObjectProvider) resource).getObject();
             try {
                 if (itemExists(path)) {
                     checkPermission(path, ACTION_SET_PROPERTY);
-                    getObjectContentManager().update(resource.getObject());
+                    getObjectContentManager().update(data);
                 } else {
                     checkPermission(path, ACTION_CREATE);
-                    getObjectContentManager().insert(resource.getObject());
+                    getObjectContentManager().insert(data);
                 }
             } catch (RepositoryException re) {
                 throw new SlingException("Problem storing object for resource "
@@ -370,10 +362,10 @@
                 // recursively copy directly in the repository
                 getSession().getWorkspace().copy(source, destination);
             } else {
-                Object copied = getObjectContentManager().getObject(
-                    resource.getObject().getClass(), source);
-                setPath(copied, destination);
-                getObjectContentManager().insert(copied);
+                // TODO: Create node at destination:
+                //     - same primary node type
+                //     - same mixins
+                //     - same non-protected properties
             }
 
         } catch (AccessControlException ace) {
@@ -408,13 +400,13 @@
             throws SlingException {
 
         String path = resource.getURI();
-        if (!(resource.getRawData() instanceof Item)) {
-            log.info("orderBefore: Resource {} has no attached Item", path);
+        if (!(resource instanceof NodeProvider)) {
+            log.info("orderBefore: Resource {} is not based on a JCR", path);
             return;
         }
 
         try {
-            Node parent = ((Item) resource.getRawData()).getParent();
+            Node parent = ((NodeProvider) resource).getNode().getParent();
 
             // check whether the parent node supports child node ordering
             if (!parent.getPrimaryNodeType().hasOrderableChildNodes()) {
@@ -571,11 +563,22 @@
      */
     protected Resource getResourceInternal(String path, Class<?> type)
             throws RepositoryException {
+
+        // check bundle resources
+        Bundle bundle = factory.getBundleForResource(path);
+        if (bundle != null) {
+            Resource result = BundleResource.getResource(bundle, path);
+            if (result != null) {
+                return result;
+            }
+        }
+
+        // check JCR repository
         if (itemExists(path)) {
             Resource result = new JcrNodeResource(this, getSession(), path, 
type);
             result.getResourceMetadata().put(ResourceMetadata.RESOLUTION_PATH,
                 path);
-            log.info("Found Resource at path '{}'", path);
+            log.info("Found JCR Node Resource at path '{}'", path);
             return result;
         }
 

Modified: 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerFactoryImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerFactoryImpl.java?rev=599454&r1=599453&r2=599454&view=diff
==============================================================================
--- 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerFactoryImpl.java
 (original)
+++ 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerFactoryImpl.java
 Thu Nov 29 06:28:03 2007
@@ -24,6 +24,7 @@
 import java.util.Hashtable;
 import java.util.List;
 import java.util.Map;
+import java.util.StringTokenizer;
 
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
@@ -199,11 +200,13 @@
             case BundleEvent.STARTING: // STARTED:
                 // register mappings before the bundle gets activated
                 
objectContentManagerFactory.registerMapperClient(event.getBundle());
+                addBundleResources(event.getBundle());
                 break;
 
             case BundleEvent.STOPPED:
                 // remove mappings after the bundle has stopped
                 
objectContentManagerFactory.unregisterMapperClient(event.getBundle());
+                removeBundleResources(event.getBundle());
                 break;
 
             case BundleEvent.UNINSTALLED:
@@ -308,6 +311,49 @@
         return mappings;
     }
 
+    // ---------- Bundle provided resources -----------------------------------
+
+    private Map<String, Bundle> bundleResourcesByPrefix = new HashMap<String, 
Bundle>();
+    private Map<Long, String[]> bundleResourcesByBundleId = new HashMap<Long, 
String[]>();
+
+    private void addBundleResources(Bundle bundle) {
+        String prefixes = (String) 
bundle.getHeaders().get("Sling-Resource-Prefixes");
+        if (prefixes != null) {
+            StringTokenizer pt = new StringTokenizer(prefixes, ", \t\n\r\f");
+            List<String> prefixList = new ArrayList<String>();
+            while (pt.hasMoreTokens()) {
+                String prefix = pt.nextToken().trim();
+                if (prefix.length() > 0) {
+                    bundleResourcesByPrefix.put(prefix, bundle);
+                    prefixList.add(prefix);
+                }
+            }
+            if (prefixList.size() > 0) {
+                bundleResourcesByBundleId.put(bundle.getBundleId(),
+                    prefixList.toArray(new String[prefixList.size()]));
+            }
+        }
+    }
+
+    private void removeBundleResources(Bundle bundle) {
+        String[] prefixes = 
bundleResourcesByBundleId.get(bundle.getBundleId());
+        if (prefixes != null) {
+            for (String prefix : prefixes) {
+                bundleResourcesByPrefix.remove(prefix);
+            }
+        }
+    }
+
+    Bundle getBundleForResource(String path) {
+        for (Map.Entry<String, Bundle> entry : 
bundleResourcesByPrefix.entrySet()) {
+            if (path.startsWith(entry.getKey())) {
+                return entry.getValue();
+            }
+        }
+
+        return null;
+    }
+
     // ---------- SCR Integration ---------------------------------------------
 
     /** Activates this component, called by SCR before registering as a 
service */
@@ -323,16 +369,17 @@
             Session session = getAdminSession(null);
 
             Bundle[] bundles = 
componentContext.getBundleContext().getBundles();
-            for (int i = 0; i < bundles.length; i++) {
-                if ((bundles[i].getState() & (Bundle.INSTALLED | 
Bundle.UNINSTALLED)) == 0) {
+            for (Bundle bundle : bundles) {
+                if ((bundle.getState() & (Bundle.INSTALLED | 
Bundle.UNINSTALLED)) == 0) {
                     // load content for bundles which are neither INSTALLED nor
                     // UNINSTALLED
-                    initialContentLoader.registerBundle(session, bundles[i]);
+                    initialContentLoader.registerBundle(session, bundle);
                 }
 
-                if (bundles[i].getState() == Bundle.ACTIVE) {
+                if (bundle.getState() == Bundle.ACTIVE) {
                     // register active bundles with the mapper client
-                    
objectContentManagerFactory.registerMapperClient(bundles[i]);
+                    objectContentManagerFactory.registerMapperClient(bundle);
+                    addBundleResources(bundle);
                 }
             }
         } catch (Throwable t) {

Added: 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/Descendable.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/Descendable.java?rev=599454&view=auto
==============================================================================
--- 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/Descendable.java
 (added)
+++ 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/Descendable.java
 Thu Nov 29 06:28:03 2007
@@ -0,0 +1,44 @@
+/*
+ * 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;
+
+import java.util.Iterator;
+
+import org.apache.sling.api.resource.Resource;
+
+/**
+ * The <code>Descendable</code> defines the API for resources (or other
+ * objects) which may have descendants, which may be enumerated or directly
+ * access.
+ */
+public interface Descendable {
+
+    /**
+     * Returns an iterator on all direct descendents aka children. If this
+     * resource has no children, an empty iterator is returned.
+     */
+    Iterator<Resource> listChildren();
+
+    /**
+     * Returns the descedent at the given relative path or <code>null</code>
+     * if this resource does not have such a descendent.
+     */
+    Resource getDescendent(String relPath);
+
+}

Modified: 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java?rev=599454&r1=599453&r2=599454&view=diff
==============================================================================
--- 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java
 (original)
+++ 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResource.java
 Thu Nov 29 06:28:03 2007
@@ -27,28 +27,43 @@
 import static org.apache.sling.api.resource.ResourceMetadata.CONTENT_TYPE;
 import static org.apache.sling.api.resource.ResourceMetadata.CREATION_TIME;
 import static org.apache.sling.api.resource.ResourceMetadata.MODIFICATION_TIME;
+import static org.apache.sling.api.resource.ResourceMetadata.RESOLUTION_PATH;
 import static 
org.apache.sling.jcr.resource.JcrResourceConstants.SLING_RESOURCE_TYPE_PROPERTY;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Iterator;
 
 import javax.jcr.Node;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
+import org.apache.jackrabbit.net.URLFactory;
+import org.apache.sling.api.resource.NodeProvider;
+import org.apache.sling.api.resource.ObjectProvider;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.resource.StreamProvider;
+import org.apache.sling.api.resource.URLProvider;
 import org.apache.sling.jcr.resource.internal.JcrResourceManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /** A Resource that wraps a JCR Node */
-public class JcrNodeResource implements Resource {
+public class JcrNodeResource implements Resource, NodeProvider, StreamProvider,
+        ObjectProvider, URLProvider, Descendable {
+
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(getClass());
 
     private static final Object UNDEFINED = "undefined";
 
     /** The relative path name of the data property of an nt:file node */
     private static final String FILE_DATA_PROP = JCR_CONTENT + "/" + JCR_DATA;
 
-    private final JcrResourceManager cMgr;
+    private final JcrResourceManager resourceManager;
 
     private final Node node;
 
@@ -62,21 +77,9 @@
 
     private final ResourceMetadata metadata;
 
-    public JcrNodeResource(JcrResourceManager cMgr, Session s, String path)
-            throws RepositoryException {
-        this.cMgr = cMgr;
-        node = (Node) s.getItem(path);
-        this.path = node.getPath();
-        metadata = new ResourceMetadata();
-        resourceType = getResourceTypeForNode(node);
-
-        // check for nt:file metadata
-        setMetaData(node, metadata);
-    }
-
     public JcrNodeResource(JcrResourceManager cMgr, Session s, String path,
             Class<?> type) throws RepositoryException {
-        this.cMgr = cMgr;
+        this.resourceManager = cMgr;
         node = (Node) s.getItem(path);
         this.path = node.getPath();
         metadata = new ResourceMetadata();
@@ -87,27 +90,19 @@
         setMetaData(node, metadata);
     }
 
-    public JcrNodeResource(JcrResourceManager cMgr, Node node)
+    public JcrNodeResource(JcrResourceManager resourceManager, Node node)
             throws RepositoryException {
-        this.cMgr = cMgr;
+        this.resourceManager = resourceManager;
         this.node = node;
         this.path = node.getPath();
         metadata = new ResourceMetadata();
-        metadata.put(ResourceMetadata.RESOLUTION_PATH, path);
+        metadata.put(RESOLUTION_PATH, path);
         resourceType = getResourceTypeForNode(node);
 
         // check for nt:file metadata
         setMetaData(node, metadata);
     }
 
-    public String toString() {
-        return "JcrNodeResource, type=" + resourceType + ", path=" + path;
-    }
-
-    public Object getRawData() {
-        return node;
-    }
-
     public String getURI() {
         return path;
     }
@@ -116,19 +111,26 @@
         return resourceType;
     }
 
-    public Object getObject() {
-        if (object == UNDEFINED) {
-            // lazy loaded object
-            object = cMgr.getObject(getURI(), objectType);
-        }
+    public ResourceMetadata getResourceMetadata() {
+        return metadata;
+    }
 
-        return object;
+    JcrResourceManager getResourceManager() {
+        return resourceManager;
     }
 
-    public ResourceMetadata getResourceMetadata() {
-        return metadata;
+    public String toString() {
+        return "JcrNodeResource, type=" + resourceType + ", path=" + path;
+    }
+
+    //---------- NodeProvider interface ---------------------------------------
+
+    public Node getNode() {
+        return node;
     }
 
+    //---------- StreamProvider interface -------------------------------------
+
     /**
      * Returns a stream to the <em>jcr:content/jcr:data</em> property if the
      * [EMAIL PROTECTED] #getRawData() raw data} is an <em>nt:file</em> node. 
Otherwise
@@ -136,12 +138,11 @@
      */
     public InputStream getInputStream() throws IOException {
         // implement this for nt:file only
-        if (!(getRawData() instanceof Node)) {
+        if (getNode() == null) {
             return null;
         }
 
         try {
-            Node node = (Node) getRawData();
             if (node.isNodeType(NT_FILE) && node.hasProperty(FILE_DATA_PROP)) {
                 return node.getProperty(FILE_DATA_PROP).getStream();
             }
@@ -152,6 +153,51 @@
 
         // fallback to non-streamable resource
         return null;
+    }
+
+    //---------- ObjectProvider interface 
---------------------------------------
+
+    public Object getObject() {
+        if (object == UNDEFINED) {
+            // lazy loaded object
+            object = resourceManager.getObject(getURI(), objectType);
+        }
+
+        return object;
+    }
+
+    //---------- URLProvider interface ----------------------------------------
+
+    public URL getURL() throws MalformedURLException {
+        try {
+            return URLFactory.createURL(node.getSession(), node.getPath());
+        } catch (RepositoryException re) {
+            throw (MalformedURLException) new MalformedURLException(
+                "Cannot create URL for " + this).initCause(re);
+        }
+    }
+
+    //---------- Descendable interface ----------------------------------------
+
+    public Iterator<Resource> listChildren() {
+        return new JcrNodeResourceIterator(this);
+    }
+
+    public Resource getDescendent(String relPath) {
+        try {
+            if (node.hasNode(relPath)) {
+                return new JcrNodeResource(resourceManager, 
node.getNode(relPath));
+            }
+
+            log.error("getResource: There is no node at {} below {}",
+                path, getURI());
+            return null;
+        } catch (RepositoryException re) {
+            log.error(
+                "getResource: Problem accessing relative resource at "
+                    + path, re);
+            return null;
+        }
     }
 
     /**

Modified: 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResourceIterator.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResourceIterator.java?rev=599454&r1=599453&r2=599454&view=diff
==============================================================================
--- 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResourceIterator.java
 (original)
+++ 
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/helper/JcrNodeResourceIterator.java
 Thu Nov 29 06:28:03 2007
@@ -22,6 +22,8 @@
 import java.util.NoSuchElementException;
 
 import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
+
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.jcr.resource.internal.JcrResourceManager;
 import org.slf4j.Logger;
@@ -51,6 +53,21 @@
      * Creates an instance using the given resource manager and the nodes
      * provided as a node iterator.
      */
+    public JcrNodeResourceIterator(JcrNodeResource parent) {
+        try {
+            NodeIterator nodes = parent.getNode().getNodes();
+
+            this.resourceManager = parent.getResourceManager();
+            this.nodes = nodes;
+            this.nextResult = seek();
+        } catch (RepositoryException re) {
+            log.error("<init>: Cannot get children of resource " + parent, re);
+            this.resourceManager = null;
+            this.nodes = null;
+            this.nextResult = null;
+        }
+    }
+
     public JcrNodeResourceIterator(JcrResourceManager resourceManager,
             NodeIterator nodes) {
         this.resourceManager = resourceManager;
@@ -63,7 +80,7 @@
     }
 
     public Resource next() {
-        if (nextResult == null) {
+        if (!hasNext()) {
             throw new NoSuchElementException();
         }
 

Modified: 
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerTest.java
URL: 
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerTest.java?rev=599454&r1=599453&r2=599454&view=diff
==============================================================================
--- 
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerTest.java
 (original)
+++ 
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceManagerTest.java
 Thu Nov 29 06:28:03 2007
@@ -25,7 +25,6 @@
 import java.util.Locale;
 import java.util.Map;
 
-import javax.jcr.Item;
 import javax.jcr.NamespaceRegistry;
 import javax.jcr.Node;
 import javax.jcr.Session;
@@ -38,6 +37,7 @@
 import junit.framework.TestCase;
 
 import org.apache.sling.api.SlingConstants;
+import org.apache.sling.api.resource.NodeProvider;
 import org.apache.sling.api.resource.NonExistingResource;
 import org.apache.sling.api.resource.Resource;
 import org.apache.sling.api.resource.ResourceManager;
@@ -109,8 +109,10 @@
         assertEquals(root, res.getURI());
         assertEquals(rootNode.getPrimaryNodeType().getName(),
             res.getResourceType());
-        assertNotNull(res.getRawData());
-        assertTrue(rootNode.isSame((Item) res.getRawData()));
+
+        assertTrue(res instanceof NodeProvider);
+        assertNotNull(((NodeProvider) res).getNode());
+        assertTrue(rootNode.isSame(((NodeProvider) res).getNode()));
 
         // missing resource
         String path = root + "/missing";
@@ -125,8 +127,10 @@
         assertEquals(root, res.getURI());
         assertEquals(rootNode.getPrimaryNodeType().getName(),
             res.getResourceType());
-        assertNotNull(res.getRawData());
-        assertTrue(rootNode.isSame((Item) res.getRawData()));
+
+        assertTrue(res instanceof NodeProvider);
+        assertNotNull(((NodeProvider) res).getNode());
+        assertTrue(rootNode.isSame(((NodeProvider) res).getNode()));
 
         // missing resource below root should resolve root
         String path = root + "/missing";
@@ -135,8 +139,10 @@
         assertEquals(root, res.getURI());
         assertEquals(rootNode.getPrimaryNodeType().getName(),
             res.getResourceType());
-        assertNotNull(res.getRawData());
-        assertTrue(rootNode.isSame((Item) res.getRawData()));
+
+        assertTrue(res instanceof NodeProvider);
+        assertNotNull(((NodeProvider) res).getNode());
+        assertTrue(rootNode.isSame(((NodeProvider) res).getNode()));
 
         // root with selectors/ext should resolve root
         path = root + ".print.a4.html";
@@ -145,8 +151,10 @@
         assertEquals(root, res.getURI());
         assertEquals(rootNode.getPrimaryNodeType().getName(),
             res.getResourceType());
-        assertNotNull(res.getRawData());
-        assertTrue(rootNode.isSame((Item) res.getRawData()));
+
+        assertTrue(res instanceof NodeProvider);
+        assertNotNull(((NodeProvider) res).getNode());
+        assertTrue(rootNode.isSame(((NodeProvider) res).getNode()));
 
         // missing resource should return NON_EXISTING Resource
         path = root + System.currentTimeMillis();
@@ -155,7 +163,9 @@
         assertTrue(res instanceof NonExistingResource);
         assertEquals(path, res.getURI());
         assertEquals(Resource.RESOURCE_TYPE_NON_EXISTING, 
res.getResourceType());
-        assertNull(res.getRawData());
+
+        assertTrue(res instanceof NodeProvider);
+        assertNull(((NodeProvider) res).getNode());
     }
 
     private static class ResourceManagerTestRequest implements 
HttpServletRequest {


Reply via email to