directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r656578 - in /directory: apacheds/trunk/core-unit/src/main/java/org/apache/directory/server/core/unit/ apacheds/trunk/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/gui/ apacheds/trunk/protocol-shared/src/main/java...
Date Thu, 15 May 2008 09:50:13 GMT
Author: elecharny
Date: Thu May 15 02:50:13 2008
New Revision: 656578

URL: http://svn.apache.org/viewvc?rev=656578&view=rev
Log:
o Transformed the LdifReader class to implement Iterable
o Modified the code to exploit this modification
o Added some javadoc
o Fixed some warnings

Modified:
    directory/apacheds/trunk/core-unit/src/main/java/org/apache/directory/server/core/unit/AbstractTestCase.java
    directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/gui/PartitionFrame.java
    directory/apacheds/trunk/protocol-shared/src/main/java/org/apache/directory/server/protocol/shared/store/LdifFileLoader.java
    directory/apacheds/trunk/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java
    directory/apacheds/trunk/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java
    directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
    directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifReaderTest.java

Modified: directory/apacheds/trunk/core-unit/src/main/java/org/apache/directory/server/core/unit/AbstractTestCase.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-unit/src/main/java/org/apache/directory/server/core/unit/AbstractTestCase.java?rev=656578&r1=656577&r2=656578&view=diff
==============================================================================
--- directory/apacheds/trunk/core-unit/src/main/java/org/apache/directory/server/core/unit/AbstractTestCase.java
(original)
+++ directory/apacheds/trunk/core-unit/src/main/java/org/apache/directory/server/core/unit/AbstractTestCase.java
Thu May 15 02:50:13 2008
@@ -214,12 +214,9 @@
 
         if ( in != null )
         {
-            Iterator<LdifEntry> list = new LdifReader( in );
-            
-            while ( list.hasNext() )
+            for ( LdifEntry ldifEntry:new LdifReader( in ) )
             {
-                entry = list.next();
-                testEntries.add( entry );
+                testEntries.add( ldifEntry );
             }
         }
 

Modified: directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/gui/PartitionFrame.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/gui/PartitionFrame.java?rev=656578&r1=656577&r2=656578&view=diff
==============================================================================
--- directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/gui/PartitionFrame.java
(original)
+++ directory/apacheds/trunk/core/src/main/java/org/apache/directory/server/core/partition/impl/btree/gui/PartitionFrame.java
Thu May 15 02:50:13 2008
@@ -448,11 +448,9 @@
         try
         {
             in = new FileReader( selected );
-            Iterator list = new LdifReader( in );
 
-            while ( list.hasNext() )
+            for ( LdifEntry entry:new LdifReader( in ) )
             {
-                LdifEntry entry = ( LdifEntry ) list.next();
                 String updn = entry.getDn();
                 
                 LdapDN ndn = new LdapDN( StringTools.deepTrimToLower( updn ) );

Modified: directory/apacheds/trunk/protocol-shared/src/main/java/org/apache/directory/server/protocol/shared/store/LdifFileLoader.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-shared/src/main/java/org/apache/directory/server/protocol/shared/store/LdifFileLoader.java?rev=656578&r1=656577&r2=656578&view=diff
==============================================================================
--- directory/apacheds/trunk/protocol-shared/src/main/java/org/apache/directory/server/protocol/shared/store/LdifFileLoader.java
(original)
+++ directory/apacheds/trunk/protocol-shared/src/main/java/org/apache/directory/server/protocol/shared/store/LdifFileLoader.java
Thu May 15 02:50:13 2008
@@ -173,23 +173,20 @@
      */
     public int execute()
     {
-        Name rdn;
+        Name rdn = null;
         InputStream in = null;
 
         try
         {
             in = getLdifStream();
-            LdifReader ldifIterator = new LdifReader( new BufferedReader( new InputStreamReader(
in ) ) );
 
-            while ( ldifIterator.hasNext() )
+            for ( LdifEntry ldifEntry:new LdifReader( new BufferedReader( new InputStreamReader(
in ) ) ) )
             {
-                LdifEntry entry = ldifIterator.next();
+                String dn = ldifEntry.getDn();
 
-                String dn = entry.getDn();
-
-                if ( entry.isEntry() )
+                if ( ldifEntry.isEntry() )
                 {
-                    Attributes attributes = entry.getAttributes();
+                    Attributes attributes = ldifEntry.getAttributes();
                     boolean filterAccepted = applyFilters( dn, attributes );
 
                     if ( !filterAccepted )
@@ -219,7 +216,7 @@
                 } else
                 {
                     //modify
-                    List<ModificationItemImpl> items = entry.getModificationItems();
+                    List<ModificationItemImpl> items = ldifEntry.getModificationItems();
                     try
                     {
                         ctx.modifyAttributes( dn, items.toArray( new ModificationItem[items.size()]
) );

Modified: directory/apacheds/trunk/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java?rev=656578&r1=656577&r2=656578&view=diff
==============================================================================
--- directory/apacheds/trunk/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java
(original)
+++ directory/apacheds/trunk/server-tools/src/main/java/org/apache/directory/server/tools/ImportCommand.java
Thu May 15 02:50:13 2008
@@ -657,14 +657,11 @@
         if ( ldifReader.containsEntries() )
         {
             // Parse the file and inject every entry
-            Iterator<LdifEntry> entries = ldifReader.iterator();
             long t0 = System.currentTimeMillis();
             int nbAdd = 0;
 
-            while ( entries.hasNext() )
+            for ( LdifEntry entry:ldifReader )
             {
-                LdifEntry entry = entries.next();
-
                 // Check if we have had some error, has next() does not throw any exception
                 if ( ldifReader.hasError() )
                 {
@@ -709,14 +706,11 @@
         else
         {
             // Parse the file and inject every modification
-            Iterator<LdifEntry> entries = ldifReader.iterator();
             long t0 = System.currentTimeMillis();
             int nbMod = 0;
 
-            while ( entries.hasNext() )
+            for ( LdifEntry entry:ldifReader )
             {
-                LdifEntry entry = entries.next();
-
                 // Check if we have had some error, has next() does not throw any exception
                 if ( ldifReader.hasError() )
                 {

Modified: directory/apacheds/trunk/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java?rev=656578&r1=656577&r2=656578&view=diff
==============================================================================
--- directory/apacheds/trunk/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java
(original)
+++ directory/apacheds/trunk/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java
Thu May 15 02:50:13 2008
@@ -27,7 +27,6 @@
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Hashtable;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 
@@ -141,9 +140,9 @@
         
         LdifReader ldifReader = new LdifReader( in );
         List<LdifEntry> entries = new ArrayList<LdifEntry>();
-        while ( ldifReader.hasNext() )
+
+        for ( LdifEntry entry:ldifReader )
         {
-            LdifEntry entry = ldifReader.next();
             rootDSE.createSubcontext( entry.getDn(), entry.getAttributes() );
             
             if ( verifyEntries )
@@ -430,13 +429,10 @@
     {
         try
         {
-            Iterator<LdifEntry> iterator = new LdifReader( in );
-
-            while ( iterator.hasNext() )
+            for ( LdifEntry ldifEntry:new LdifReader( in ) )
             {
-                LdifEntry entry = iterator.next();
-                LdapDN dn = new LdapDN( entry.getDn() );
-                rootDSE.createSubcontext( dn, entry.getAttributes() );
+                LdapDN dn = new LdapDN( ldifEntry.getDn() );
+                rootDSE.createSubcontext( dn, ldifEntry.getAttributes() );
             }
         }
         catch ( Exception e )

Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java?rev=656578&r1=656577&r2=656578&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
(original)
+++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
Thu May 15 02:50:13 2008
@@ -37,6 +37,7 @@
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.NoSuchElementException;
 
 import javax.naming.InvalidNameException;
 import javax.naming.NamingException;
@@ -161,7 +162,7 @@
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class LdifReader implements Iterator<LdifEntry>
+public class LdifReader implements Iterable<LdifEntry>
 {
     /** A logger */
     private static final Logger LOG = LoggerFactory.getLogger( LdifReader.class );
@@ -171,10 +172,10 @@
      * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
      * @version $Rev$, $Date$
      */
-    protected class Position
+    public class Position
     {
         /** The current position */
-        protected int pos;
+        private int pos;
 
         /**
          * Creates a new instance of Position.
@@ -875,14 +876,10 @@
     /**
      * Parse an AttributeType/AttributeValue
      * 
-     * @param entry
-     *            The entry where to store the value
-     * @param line
-     *            The line to parse
-     * @param lowerLine
-     *            The same line, lowercased
-     * @throws NamingException
-     *             If anything goes wrong
+     * @param entry The entry where to store the value
+     * @param line The line to parse
+     * @param lowerLine The same line, lowercased
+     * @throws NamingException If anything goes wrong
      */
     public void parseAttributeValue( LdifEntry entry, String line, String lowerLine ) throws
NamingException
     {
@@ -1621,8 +1618,9 @@
      * Gets the next LDIF on the channel.
      * 
      * @return the next LDIF as a String.
+     * @exception NoSuchElementException If we can't read the next entry
      */
-    public LdifEntry next()
+    private LdifEntry nextInternal()
     {
         try
         {
@@ -1638,6 +1636,7 @@
             catch (NamingException ne)
             {
                 error = ne;
+                throw new NoSuchElementException( ne.getMessage() );
             }
 
             LOG.debug( "next(): -- returning ldif {}\n", entry );
@@ -1654,6 +1653,29 @@
 
     
     /**
+     * Gets the next LDIF on the channel.
+     * 
+     * @return the next LDIF as a String.
+     * @exception NoSuchElementException If we can't read the next entry
+     */
+    public LdifEntry next()
+    {
+        return nextInternal();
+    }
+
+
+    /**
+     * Tests to see if another LDIF is on the input channel.
+     * 
+     * @return true if another LDIF is available false otherwise.
+     */
+    private boolean hasNextInternal()
+    {
+        return null != prefetched;
+    }
+
+    
+    /**
      * Tests to see if another LDIF is on the input channel.
      * 
      * @return true if another LDIF is available false otherwise.
@@ -1662,7 +1684,18 @@
     {
         LOG.debug( "hasNext(): -- returning {}", ( prefetched != null ) ? Boolean.TRUE :
Boolean.FALSE );
 
-        return null != prefetched;
+        return hasNextInternal();
+    }
+
+
+    /**
+     * Always throws UnsupportedOperationException!
+     * 
+     * @see java.util.Iterator#remove()
+     */
+    private void removeInternal()
+    {
+        throw new UnsupportedOperationException();
     }
 
     
@@ -1673,7 +1706,7 @@
      */
     public void remove()
     {
-        throw new UnsupportedOperationException();
+        removeInternal();
     }
 
     /**
@@ -1681,7 +1714,23 @@
      */
     public Iterator<LdifEntry> iterator()
     {
-        return this;
+        return new Iterator<LdifEntry>() 
+        {
+            public boolean hasNext() 
+            {
+                return hasNextInternal();
+            }
+          
+            public LdifEntry next() 
+            {
+                return nextInternal();
+            }
+          
+            public void remove() 
+            {
+                throw new UnsupportedOperationException();
+            }
+        };
     }
 
     /**
@@ -1722,19 +1771,20 @@
         prefetched = parseEntry();
 
         // When done, get the entries one by one.
-        while ( hasNext() )
+        try
         {
-            LdifEntry entry = next();
-
-            if ( error != null )
+            for ( LdifEntry entry:this )
             {
-                throw new NamingException( "Error while parsing ldif : " + error.getMessage()
);
-            }
-            else if ( entry != null )
-            {
-                entries.add( entry );
+                if ( entry != null )
+                {
+                    entries.add( entry );
+                }
             }
         }
+        catch ( NoSuchElementException nsee )
+        {
+            throw new NamingException( "Error while parsing ldif : " + error.getMessage()
);
+        }
 
         return entries;
     }

Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifReaderTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifReaderTest.java?rev=656578&r1=656577&r2=656578&view=diff
==============================================================================
--- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifReaderTest.java
(original)
+++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/ldif/LdifReaderTest.java
Thu May 15 02:50:13 2008
@@ -259,7 +259,7 @@
         testReaderAttrIdCaseInsensitive( ldif );
     }
 
-    private void testReaderAttrIdCaseInsensitive( String ldif )
+    public void testReaderAttrIdCaseInsensitive( String ldif )
             throws NamingException
     {
         LdifReader reader = new LdifReader();



Mime
View raw message