jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r572347 - /jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/manager/impl/ObjectIterator.java
Date Mon, 03 Sep 2007 14:23:04 GMT
Author: fmeschbe
Date: Mon Sep  3 07:23:03 2007
New Revision: 572347

URL: http://svn.apache.org/viewvc?rev=572347&view=rev
Log:
FELIX-1107 ObjectIterator may return null, which is not readily expected from an Iterator

Modified:
    jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/manager/impl/ObjectIterator.java

Modified: jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/manager/impl/ObjectIterator.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/manager/impl/ObjectIterator.java?rev=572347&r1=572346&r2=572347&view=diff
==============================================================================
--- jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/manager/impl/ObjectIterator.java
(original)
+++ jackrabbit/trunk/contrib/jackrabbit-jcr-mapping/jcr-mapping/src/main/java/org/apache/jackrabbit/ocm/manager/impl/ObjectIterator.java
Mon Sep  3 07:23:03 2007
@@ -18,16 +18,28 @@
 package org.apache.jackrabbit.ocm.manager.impl;
 
 import java.util.Iterator;
+import java.util.NoSuchElementException;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.jackrabbit.ocm.exception.ObjectContentManagerException;
 import org.apache.jackrabbit.ocm.manager.objectconverter.ObjectConverter;
 
 
 /**
- * ObjectIterator is a wrapper class for JCR NodeIterator
+ * ObjectIterator is a wrapper class for JCR NodeIterator, which returns
+ * mapped objects. Note, though, that this iterator may not return the same
+ * number of objects as the underlying node iterator as not all nodes may
+ * successfully be mapped to objects. Any problems mapping nodes to objects are
+ * logged at INFO level.
+ * <p>
+ * This Iterator implementation does not support removing elements, therefore
+ * the {@link #remove()} method throws a <code>UnsupportOperationException</code>.
  * 
  * @author <a href="mailto:christophe.lombart@gmail.com">Christophe Lombart</a>
  *
@@ -35,66 +47,74 @@
 public class ObjectIterator implements Iterator
 {
 
+    private static final Log log = LogFactory.getLog(ObjectIterator.class);
+    
 	private NodeIterator nodeIterator;
 
-	private Class objectClass;
-
 	private Session session;
 
 	private ObjectConverter objectConverter;
 
+    private Object nextResult;
 	
 	/**
 	 * Constructor 
 	 * 
 	 * @param iterator JCR node iterator 
-	 * @param objectClass the object class used to instantiate the objects
 	 * @param converter The object converter
 	 * @param session the JCR session 
 	 */
-	public ObjectIterator(NodeIterator iterator, Class objectClass, ObjectConverter converter,
Session session)
+	public ObjectIterator(NodeIterator iterator, ObjectConverter converter, Session session)
 	{
 		nodeIterator = iterator;
-		this.objectClass = objectClass;
 		objectConverter = converter;
 		this.session = session;
+		
+		// get first result
+        seek();
 	}
 
-	/**
-	 * 
-	 * @see java.util.Iterator#hasNext()
-	 */
-	public boolean hasNext()
-	{
-		return nodeIterator.hasNext();
-	}
-
-	/**
-	 * 
-	 * @see java.util.Iterator#next()
-	 */
-	public Object next() 
-	{
-
-		try
-		{
-			Node node = nodeIterator.nextNode();
-			return objectConverter.getObject(session, node.getPath());
-		}
-		catch (Exception e)
-		{
-           return null;			
-		}
-
-	}
-
-	/**
-	 * 
-	 * @see java.util.Iterator#remove()
-	 */
-	public void remove()
-	{
-		nodeIterator.remove();
-	}
 
+	public boolean hasNext() {
+        return nextResult != null;
+    }
+    
+	
+    public Object next() {
+        if (nextResult == null) {
+            throw new NoSuchElementException();
+        }
+        
+        Object result = nextResult;
+        seek();
+        return result;
+    }
+
+    
+    public void remove() {
+        throw new UnsupportedOperationException();
+    }
+    
+    
+    private void seek() {
+        while (nodeIterator.hasNext()) {
+            try {
+                Node node = nodeIterator.nextNode();
+                Object value = objectConverter.getObject(session, node.getPath());
+                if (value != null) {
+                    nextResult = value;
+                    return;
+                }
+            } catch (RepositoryException re) {
+                log.info("Repository access issue trying to map node to an object", re);
+            } catch (ObjectContentManagerException ocme) {
+                log.info("Mapping Failure", ocme);
+            } catch (Throwable t) {
+                log.info("Unexpected Problem while trying to map a node to an object", t);
+            }
+        }
+        
+        // no more results
+        nextResult = null;
+    }
 }



Mime
View raw message