directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r882199 - in /directory: apacheds/branches/apacheds-schema/core-integ/src/main/java/org/apache/directory/server/core/integ/ apacheds/branches/apacheds-schema/core-integ/src/main/java/org/apache/directory/server/core/integ/state/ apacheds/br...
Date Thu, 19 Nov 2009 16:40:40 GMT
Author: elecharny
Date: Thu Nov 19 16:40:39 2009
New Revision: 882199

URL: http://svn.apache.org/viewvc?rev=882199&view=rev
Log:
Closed the LdifReader after usage, to avoid a "Too many open files" error during tests.

Modified:
    directory/apacheds/branches/apacheds-schema/core-integ/src/main/java/org/apache/directory/server/core/integ/IntegrationUtils.java
    directory/apacheds/branches/apacheds-schema/core-integ/src/main/java/org/apache/directory/server/core/integ/state/AbstractState.java
    directory/apacheds/branches/apacheds-schema/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/unit/AbstractServerTest.java
    directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/DitToLdifWriter.java
    directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifPartition.java
    directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifStore.java
    directory/apacheds/branches/apacheds-schema/protocol-shared/src/main/java/org/apache/directory/server/protocol/shared/store/LdifFileLoader.java
    directory/apacheds/branches/apacheds-schema/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java
    directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/loader/ldif/JarLdifSchemaLoader.java
    directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/loader/ldif/LdifSchemaLoader.java
    directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifAttributesReader.java
    directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java

Modified: directory/apacheds/branches/apacheds-schema/core-integ/src/main/java/org/apache/directory/server/core/integ/IntegrationUtils.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/core-integ/src/main/java/org/apache/directory/server/core/integ/IntegrationUtils.java?rev=882199&r1=882198&r2=882199&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/core-integ/src/main/java/org/apache/directory/server/core/integ/IntegrationUtils.java
(original)
+++ directory/apacheds/branches/apacheds-schema/core-integ/src/main/java/org/apache/directory/server/core/integ/IntegrationUtils.java
Thu Nov 19 16:40:39 2009
@@ -123,6 +123,9 @@
                 throw new NamingException( message );
             }
         }
+        
+        // And close the reader
+        reader.close();
     }
 
 

Modified: directory/apacheds/branches/apacheds-schema/core-integ/src/main/java/org/apache/directory/server/core/integ/state/AbstractState.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/core-integ/src/main/java/org/apache/directory/server/core/integ/state/AbstractState.java?rev=882199&r1=882198&r2=882199&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/core-integ/src/main/java/org/apache/directory/server/core/integ/state/AbstractState.java
(original)
+++ directory/apacheds/branches/apacheds-schema/core-integ/src/main/java/org/apache/directory/server/core/integ/state/AbstractState.java
Thu Nov 19 16:40:39 2009
@@ -207,6 +207,8 @@
                             new DefaultServerEntry( service.getSchemaManager(), entry.getEntry()
) ); 
                         LOG.debug( "Successfully injected LDIF enry for test {}: {}", settings.getDescription(),
entry );
                     }
+                    
+                    ldifReader.close();
                 }
                 catch ( Exception e )
                 {
@@ -214,6 +216,7 @@
                 }
             }
         }
+        
         ldifs =  settings.getLdifs( ldifs );
         
         if ( ldifs.size() != 0 )
@@ -238,6 +241,8 @@
                             LOG.error( "Cannot inject the following entry : {}. Error : {}.",
entry, e.getMessage() );
                         }
                     }
+                    
+                    ldifReader.close();
                 }
                 catch ( Exception e )
                 {

Modified: directory/apacheds/branches/apacheds-schema/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/unit/AbstractServerTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/unit/AbstractServerTest.java?rev=882199&r1=882198&r2=882199&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/unit/AbstractServerTest.java
(original)
+++ directory/apacheds/branches/apacheds-schema/ldap-api-test/src/test/java/org/apache/directory/shared/client/api/unit/AbstractServerTest.java
Thu Nov 19 16:40:39 2009
@@ -436,5 +436,7 @@
                 new DefaultServerEntry( 
                     rootDSE.getDirectoryService().getSchemaManager(), entry.getEntry() )
); 
         }
+        
+        reader.close();
     }
 }

Modified: directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/DitToLdifWriter.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/DitToLdifWriter.java?rev=882199&r1=882198&r2=882199&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/DitToLdifWriter.java
(original)
+++ directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/DitToLdifWriter.java
Thu Nov 19 16:40:39 2009
@@ -56,8 +56,6 @@
 
     private LdapConnection connection;
 
-    private LdifReader ldifParser = new LdifReader();
-    
     private FileFilter dirFilter = new FileFilter()
     {
         public boolean accept( File dir )
@@ -71,6 +69,7 @@
     public DitToLdifWriter()
     {
         connection = new LdapConnection( "localhost", 10389 );
+        
         try
         {
             connection.bind( "uid=admin,ou=system", "secret" );
@@ -91,6 +90,7 @@
         while( cursor.next() )
         {
             SearchResponse searchRes = cursor.get();
+            
             if( searchRes instanceof SearchResultEntry )
             {
                 SearchResultEntry searchResultEntry = ( SearchResultEntry ) searchRes;
@@ -100,6 +100,7 @@
                 int size = entryDn.size();
                 
                 filePath.append( baseDir.getAbsolutePath() ).append( File.separator );
+                
                 for( int i =0; i< size; i++ )
                 {
                     filePath.append( entryDn.getRdn( i ).getUpName().toLowerCase() ).append(
File.separator );
@@ -138,25 +139,35 @@
     {
         LOG.warn( "processing dir {}", entryDir.getName() );
         File ldifFile = new File( entryDir, entryDir.getName() + ".ldif" );
+        LdifReader ldifReader = new LdifReader();
         
-        if( ldifFile.exists() )
+        try
         {
-            LOG.warn( "ldif file {} exists", ldifFile.getName() );
-            List<LdifEntry> entries = ldifParser.parseLdifFile( ldifFile.getAbsolutePath()
);
-            if( entries != null && !entries.isEmpty() )
+            if( ldifFile.exists() )
+            {
+                LOG.warn( "ldif file {} exists", ldifFile.getName() );
+                List<LdifEntry> entries = ldifReader.parseLdifFile( ldifFile.getAbsolutePath()
);
+                
+                if( entries != null && !entries.isEmpty() )
+                {
+                    LOG.warn( "adding entry {}", entries.get( 0 ) );
+                    // this ldif will have only one entry
+                    AddResponse resp = connection.add( entries.get( 0 ).getEntry() );
+                    LOG.warn( "{}", resp.getLdapResult().getResultCode() );
+                }
+            }
+            else
             {
-                LOG.warn( "adding entry {}", entries.get( 0 ) );
-                // this ldif will have only one entry
-                AddResponse resp = connection.add( entries.get( 0 ).getEntry() );
-                LOG.warn( "{}", resp.getLdapResult().getResultCode() );
+                LOG.warn( "ldif file doesn't exist {}", ldifFile.getAbsolutePath() );
             }
         }
-        else
+        finally
         {
-            LOG.warn( "ldif file doesn't exist {}", ldifFile.getAbsolutePath() );
+            ldifReader.close();
         }
         
         File[] dirs = entryDir.listFiles( dirFilter );
+        
         if( dirs != null )
         {
             for( File f : dirs )
@@ -178,24 +189,6 @@
         String partitionSuffix = "ou=config";
         File baseDir = new File( "/tmp" );
         configWriter.dumpDitToFS( partitionSuffix, baseDir );
-//        configWriter.loadConfig( baseDir, partitionSuffix );
         configWriter.close();
-
-        /*
-        LdifReader reader = new LdifReader( new File( "src/main/resources/ads-2.ldif" ) );
-        Iterator<LdifEntry> itr = reader.iterator();
-        while( itr.hasNext() )
-        {
-            try
-            {
-                LdifEntry entry = itr.next();
-                configWriter.connection.add( entry.getEntry() );
-            }
-            catch( Exception e )
-            {
-                System.exit( 1 );
-            }
-        }
-        */
     }
 }

Modified: directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifPartition.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifPartition.java?rev=882199&r1=882198&r2=882199&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifPartition.java
(original)
+++ directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifPartition.java
Thu Nov 19 16:40:39 2009
@@ -23,6 +23,7 @@
 import java.io.File;
 import java.io.FileFilter;
 import java.io.FileWriter;
+import java.io.IOException;
 import java.util.Iterator;
 import java.util.List;
 import java.util.UUID;
@@ -94,7 +95,7 @@
     private static Logger LOG = LoggerFactory.getLogger( LdifPartition.class );
     
     /** The LDIF file parser */
-    private LdifReader ldifParser = new LdifReader();
+    //private LdifReader ldifReader;
 
     /** The directory into which the partition is stored */
     private String workingDirectory;
@@ -221,7 +222,8 @@
                 if ( contextEntryFile.exists() )
                 {
                     LdifReader reader = new LdifReader( contextEntryFile );
-                    contextEntry = new DefaultServerEntry( schemaManager, reader.next().getEntry()
); 
+                    contextEntry = new DefaultServerEntry( schemaManager, reader.next().getEntry()
);
+                    reader.close();
                 }
                 else
                 {
@@ -464,10 +466,12 @@
         
         if ( ( entries != null ) && ( entries.length != 0 ) )
         {
+            LdifReader ldifReader = new LdifReader();
+            
             for ( File entry : entries )
             {
                 LOG.debug( "parsing ldif file {}", entry.getName() );
-                List<LdifEntry> ldifEntries = ldifParser.parseLdifFile( entry.getAbsolutePath()
);
+                List<LdifEntry> ldifEntries = ldifReader.parseLdifFile( entry.getAbsolutePath()
);
                 
                 if ( ( ldifEntries != null ) && !ldifEntries.isEmpty() )
                 {
@@ -491,10 +495,12 @@
                     wrappedPartition.getStore().add( serverEntry );
                 }
             }
+            
+            ldifReader.close();
         }
         else
         {
-            // If we don't have ldif files, we won't have sub directories
+            // If we don't have ldif files, we won't have sub-directories
             return;
         }
         
@@ -1083,7 +1089,17 @@
      */
     public void setContextEntry( String contextEntry ) throws NamingException
     {
-        List<LdifEntry> entries = ldifParser.parseLdif( contextEntry );
+        LdifReader ldifReader = new LdifReader();
+        List<LdifEntry> entries = ldifReader.parseLdif( contextEntry );
+        
+        try
+        {
+            ldifReader.close();
+        }
+        catch ( IOException ioe )
+        {
+            // What can we do here ???
+        }
         
         this.contextEntry = new DefaultServerEntry( schemaManager, entries.get( 0 ).getEntry()
);
     }

Modified: directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifStore.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifStore.java?rev=882199&r1=882198&r2=882199&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifStore.java
(original)
+++ directory/apacheds/branches/apacheds-schema/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/LdifStore.java
Thu Nov 19 16:40:39 2009
@@ -67,7 +67,7 @@
 
     private SchemaManager schemaManager;
     
-    private LdifReader ldifParser = new LdifReader();
+    private LdifReader ldifReader;
     
     private FileFilter dirFilter = new FileFilter()
     {
@@ -118,30 +118,42 @@
         
         File ldifFile = new File( entryDir, entryDir.getName() + CONF_FILE_EXTN );
         
-        if( ldifFile.exists() )
+        try
         {
-            LOG.debug( "parsing ldif file {}", ldifFile.getName() );
-            List<LdifEntry> entries = ldifParser.parseLdifFile( ldifFile.getAbsolutePath()
);
-            if( entries != null && !entries.isEmpty() )
-            {
-                // this ldif will have only one entry
-                LdifEntry ldifEntry = entries.get( 0 );
-                LOG.debug( "adding entry {}", ldifEntry );
+            
+            ldifReader = new LdifReader();
 
-                ServerEntry serverEntry = new DefaultServerEntry( schemaManager, ldifEntry.getEntry()
);
+            if( ldifFile.exists() )
+            {
+                LOG.debug( "parsing ldif file {}", ldifFile.getName() );
+                List<LdifEntry> entries = ldifReader.parseLdifFile( ldifFile.getAbsolutePath()
);
                 
-                // call add on the wrapped store not on the self  
-                wrappedStore.add( serverEntry );
+                if( entries != null && !entries.isEmpty() )
+                {
+                    // this ldif will have only one entry
+                    LdifEntry ldifEntry = entries.get( 0 );
+                    LOG.debug( "adding entry {}", ldifEntry );
+    
+                    ServerEntry serverEntry = new DefaultServerEntry( schemaManager, ldifEntry.getEntry()
);
+                    
+                    // call add on the wrapped store not on the self  
+                    wrappedStore.add( serverEntry );
+                }
+            }
+            else
+            {
+                // TODO do we need to bomb out if the expected LDIF file doesn't exist
+                // I think so
+                LOG.warn( "ldif file doesn't exist {}", ldifFile.getAbsolutePath() );
             }
         }
-        else
+        finally
         {
-            // TODO do we need to bomb out if the expected LDIF file doesn't exist
-            // I think so
-            LOG.warn( "ldif file doesn't exist {}", ldifFile.getAbsolutePath() );
+            ldifReader.close();
         }
         
         File[] dirs = entryDir.listFiles( dirFilter );
+        
         if( dirs != null )
         {
             for( File f : dirs )

Modified: directory/apacheds/branches/apacheds-schema/protocol-shared/src/main/java/org/apache/directory/server/protocol/shared/store/LdifFileLoader.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/protocol-shared/src/main/java/org/apache/directory/server/protocol/shared/store/LdifFileLoader.java?rev=882199&r1=882198&r2=882199&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/protocol-shared/src/main/java/org/apache/directory/server/protocol/shared/store/LdifFileLoader.java
(original)
+++ directory/apacheds/branches/apacheds-schema/protocol-shared/src/main/java/org/apache/directory/server/protocol/shared/store/LdifFileLoader.java
Thu Nov 19 16:40:39 2009
@@ -20,12 +20,10 @@
 package org.apache.directory.server.protocol.shared.store;
 
 
-import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
-import java.io.InputStreamReader;
 import java.util.Collections;
 import java.util.List;
 
@@ -178,7 +176,7 @@
         {
             in = getLdifStream();
 
-            for ( LdifEntry ldifEntry:new LdifReader( new BufferedReader( new InputStreamReader(
in ) ) ) )
+            for ( LdifEntry ldifEntry:new LdifReader( in ) )
             {
                 LdapDN dn = ldifEntry.getDn();
 

Modified: directory/apacheds/branches/apacheds-schema/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/apacheds-schema/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java?rev=882199&r1=882198&r2=882199&view=diff
==============================================================================
--- directory/apacheds/branches/apacheds-schema/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java
(original)
+++ directory/apacheds/branches/apacheds-schema/server-unit/src/main/java/org/apache/directory/server/unit/AbstractServerTest.java
Thu Nov 19 16:40:39 2009
@@ -442,5 +442,7 @@
                 new DefaultServerEntry( 
                     rootDSE.getDirectoryService().getSchemaManager(), entry.getEntry() )
); 
         }
+        
+        reader.close();
     }
 }

Modified: directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/loader/ldif/JarLdifSchemaLoader.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/loader/ldif/JarLdifSchemaLoader.java?rev=882199&r1=882198&r2=882199&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/loader/ldif/JarLdifSchemaLoader.java
(original)
+++ directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/loader/ldif/JarLdifSchemaLoader.java
Thu Nov 19 16:40:39 2009
@@ -21,6 +21,7 @@
 
 
 import java.io.File;
+import java.io.InputStream;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.List;
@@ -111,10 +112,11 @@
             if ( pat.matcher( file ).matches() )
             {
                 URL resource = getResource( file, "schema LDIF file" );
+                InputStream in = resource.openStream();
                 
                 try
                 {
-                    LdifReader reader = new LdifReader( resource.openStream() );
+                    LdifReader reader = new LdifReader( in );
                     LdifEntry entry = reader.next();
                     Schema schema = getSchema( entry.getEntry() );
                     schemaMap.put( schema.getSchemaName(), schema );
@@ -129,6 +131,10 @@
                     LOG.error( "Failed to load schema LDIF file " + file, e );
                     throw e;
                 }
+                finally
+                {
+                    in.close();
+                }
             }
         }
     }

Modified: directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/loader/ldif/LdifSchemaLoader.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/loader/ldif/LdifSchemaLoader.java?rev=882199&r1=882198&r2=882199&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/loader/ldif/LdifSchemaLoader.java
(original)
+++ directory/shared/branches/shared-schema/ldap-schema-loader/src/main/java/org/apache/directory/shared/schema/loader/ldif/LdifSchemaLoader.java
Thu Nov 19 16:40:39 2009
@@ -141,10 +141,13 @@
 
         for ( String ldifFile : ldifFiles )
         {
+            File file = new File( schemaDirectory, ldifFile );
+            
             try
             {
-                LdifReader reader = new LdifReader( new File( schemaDirectory, ldifFile )
);
+                LdifReader reader = new LdifReader( file );
                 LdifEntry entry = reader.next();
+                reader.close();
                 Schema schema = getSchema( entry.getEntry() );
                 schemaMap.put( schema.getSchemaName(), schema );
                 
@@ -295,6 +298,8 @@
         out.write( LdifUtils.convertEntryToLdif( entry ) );
         out.flush();
         out.close();
+        
+        reader.close();
     }
 
 
@@ -332,6 +337,7 @@
         {
             LdifReader reader = new LdifReader( ldifFile );
             LdifEntry entry = reader.next();
+            reader.close();
             
             comparatorList.add( entry.getEntry() );
         }
@@ -361,6 +367,7 @@
         {
             LdifReader reader = new LdifReader( ldifFile );
             LdifEntry entry = reader.next();
+            reader.close();
             
             syntaxCheckerList.add( entry.getEntry() );
         }
@@ -390,6 +397,7 @@
         {
             LdifReader reader = new LdifReader( ldifFile );
             LdifEntry entry = reader.next();
+            reader.close();
             
             normalizerList.add( entry.getEntry() );
         }
@@ -419,6 +427,7 @@
         {
             LdifReader reader = new LdifReader( ldifFile );
             LdifEntry entry = reader.next();
+            reader.close();
 
             matchingRuleList.add( entry.getEntry() );
         }
@@ -448,6 +457,7 @@
         {
             LdifReader reader = new LdifReader( ldifFile );
             LdifEntry entry = reader.next();
+            reader.close();
 
             syntaxList.add( entry.getEntry() );
         }
@@ -478,6 +488,7 @@
         {
             LdifReader reader = new LdifReader( ldifFile );
             LdifEntry entry = reader.next();
+            reader.close();
             
             attributeTypeList.add( entry.getEntry() );
         }
@@ -507,6 +518,7 @@
         {
             LdifReader reader = new LdifReader( ldifFile );
             LdifEntry entry = reader.next();
+            reader.close();
             
             matchingRuleUseList.add( entry.getEntry() );
         }
@@ -536,6 +548,7 @@
         {
             LdifReader reader = new LdifReader( ldifFile );
             LdifEntry entry = reader.next();
+            reader.close();
             
             nameFormList.add( entry.getEntry() );
         }
@@ -565,6 +578,7 @@
         {
             LdifReader reader = new LdifReader( ldifFile );
             LdifEntry entry = reader.next();
+            reader.close();
             
             ditContentRuleList.add( entry.getEntry() );
         }
@@ -594,6 +608,7 @@
         {
             LdifReader reader = new LdifReader( ldifFile );
             LdifEntry entry = reader.next();
+            reader.close();
             
             ditStructureRuleList.add( entry.getEntry() );
         }
@@ -624,6 +639,7 @@
         {
             LdifReader reader = new LdifReader( ldifFile );
             LdifEntry entry = reader.next();
+            reader.close();
             
             objectClassList.add( entry.getEntry() );
         }

Modified: directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifAttributesReader.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifAttributesReader.java?rev=882199&r1=882198&r2=882199&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifAttributesReader.java
(original)
+++ directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifAttributesReader.java
Thu Nov 19 16:40:39 2009
@@ -20,6 +20,7 @@
 package org.apache.directory.shared.ldap.ldif;
 
 import java.io.BufferedReader;
+import java.io.IOException;
 import java.io.StringReader;
 import java.util.ArrayList;
 
@@ -274,7 +275,7 @@
         }
 
         StringReader strIn = new StringReader( ldif );
-        in = new BufferedReader( strIn );
+        reader = new BufferedReader( strIn );
 
         try
         {
@@ -294,5 +295,16 @@
             LOG.error( "Cannot parse the ldif buffer : {}", ne.getMessage() );
             throw new NamingException( "Error while parsing the ldif buffer" );
         }
+        finally
+        {
+            try
+            {
+                reader.close();
+            }
+            catch ( IOException ioe )
+            {
+                // Do nothing
+            }
+        }
     }
 }
\ No newline at end of file

Modified: directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java?rev=882199&r1=882198&r2=882199&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
(original)
+++ directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/ldif/LdifReader.java
Thu Nov 19 16:40:39 2009
@@ -20,6 +20,7 @@
 package org.apache.directory.shared.ldap.ldif;
 
 import java.io.BufferedReader;
+import java.io.Closeable;
 import java.io.DataInputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -165,7 +166,7 @@
  * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
  * @version $Rev$, $Date$
  */
-public class LdifReader implements Iterable<LdifEntry>
+public class LdifReader implements Iterable<LdifEntry>, Closeable
 {
     /** A logger */
     private static final Logger LOG = LoggerFactory.getLogger( LdifReader.class );
@@ -244,7 +245,7 @@
     protected LdifEntry prefetched;
 
     /** The ldif Reader */
-    protected Reader in;
+    protected Reader reader;
 
     /** A flag set if the ldif contains entries */
     protected boolean containsEntries;
@@ -267,10 +268,11 @@
         position = new Position();
         version = DEFAULT_VERSION;
     }
+    
 
-    private void init( BufferedReader inf ) throws NamingException
+    private void init( BufferedReader reader ) throws NamingException
     {
-        this.in = inf;
+        this.reader = reader;
         lines = new ArrayList<String>();
         position = new Position();
         version = DEFAULT_VERSION;
@@ -291,44 +293,32 @@
      */
     public LdifReader( String ldifFileName ) throws NamingException
     {
-        File inf = new File( ldifFileName );
+        File file = new File( ldifFileName );
 
-        if ( !inf.exists() )
+        if ( !file.exists() )
         {
-            LOG.error( "File {} cannot be found", inf.getAbsoluteFile() );
-            throw new NamingException( "Cannot find file " + inf.getAbsoluteFile() );
+            LOG.error( "File {} cannot be found", file.getAbsoluteFile() );
+            throw new NamingException( "Cannot find file " + file.getAbsoluteFile() );
         }
 
-        if ( !inf.canRead() )
+        if ( !file.canRead() )
         {
-            LOG.error( "File {} cannot be read", inf.getName() );
-            throw new NamingException( "Cannot read file " + inf.getName() );
+            LOG.error( "File {} cannot be read", file.getName() );
+            throw new NamingException( "Cannot read file " + file.getName() );
         }
 
         try
         {
-            init( new BufferedReader( new FileReader( inf ) ) );
+            init( new BufferedReader( new FileReader( file ) ) );
         }
         catch (FileNotFoundException fnfe)
         {
-            LOG.error( "File {} cannot be found", inf.getAbsoluteFile() );
-            throw new NamingException( "Cannot find file " + inf.getAbsoluteFile() );
+            LOG.error( "File {} cannot be found", file.getAbsoluteFile() );
+            throw new NamingException( "Cannot find file " + file.getAbsoluteFile() );
         }
     }
 
-    /**
-     * A constructor which takes a BufferedReader
-     * 
-     * @param in
-     *            A BufferedReader containing ldif formated input
-     * @throws NamingException
-     *             If the file cannot be processed or if the format is incorrect
-     */
-    public LdifReader( BufferedReader in ) throws NamingException
-    {
-        init( in );
-    }
-
+    
     /**
      * A constructor which takes a Reader
      * 
@@ -363,28 +353,28 @@
      * @throws NamingException
      *             If the file cannot be processed or if the format is incorrect
      */
-    public LdifReader( File in ) throws NamingException
+    public LdifReader( File file ) throws NamingException
     {
-        if ( !in.exists() )
+        if ( !file.exists() )
         {
-            LOG.error( "File {} cannot be found", in.getAbsoluteFile() );
-            throw new NamingException( "Cannot find file " + in.getAbsoluteFile() );
+            LOG.error( "File {} cannot be found", file.getAbsoluteFile() );
+            throw new NamingException( "Cannot find file " + file.getAbsoluteFile() );
         }
 
-        if ( !in.canRead() )
+        if ( !file.canRead() )
         {
-            LOG.error( "File {} cannot be read", in.getName() );
-            throw new NamingException( "Cannot read file " + in.getName() );
+            LOG.error( "File {} cannot be read", file.getName() );
+            throw new NamingException( "Cannot read file " + file.getName() );
         }
 
         try
         {
-            init( new BufferedReader( new FileReader( in ) ) );
+            init( new BufferedReader( new FileReader( file ) ) );
         }
         catch (FileNotFoundException fnfe)
         {
-            LOG.error( "File {} cannot be found", in.getAbsoluteFile() );
-            throw new NamingException( "Cannot find file " + in.getAbsoluteFile() );
+            LOG.error( "File {} cannot be found", file.getAbsoluteFile() );
+            throw new NamingException( "Cannot find file " + file.getAbsoluteFile() );
         }
     }
 
@@ -1454,7 +1444,7 @@
 
         try
         {
-            while ( ( line = ( (BufferedReader) in ).readLine() ) != null )
+            while ( ( line = ( (BufferedReader) reader ).readLine() ) != null )
             {
                 if ( line.length() == 0 )
                 {
@@ -1567,20 +1557,34 @@
             LOG.error( "Cannot parse the file {}, it does not exist", fileName );
             throw new NamingException( "Filename " + fileName + " not found." );
         }
+        
+        BufferedReader reader = null;
 
         // Open the file and then get a channel from the stream
         try
         {
-            BufferedReader inf = new BufferedReader( new InputStreamReader( new FileInputStream(
file ), 
+            reader = new BufferedReader( new InputStreamReader( new FileInputStream( file
), 
                 Charset.forName( encoding ) ) );
 
-            return parseLdif( inf );
+            return parseLdif( reader );
         }
         catch (FileNotFoundException fnfe)
         {
             LOG.error( "Cannot find file {}", fileName );
             throw new NamingException( "Filename " + fileName + " not found." );
         }
+        finally
+        {
+            // close the reader
+            try
+            {
+                reader.close();
+            }
+            catch ( IOException ioe )
+            {
+                // Nothing to do
+            }
+        }
     }
 
     /**
@@ -1602,11 +1606,11 @@
         }
 
         StringReader strIn = new StringReader( ldif );
-        BufferedReader inf = new BufferedReader( strIn );
+        BufferedReader reader = new BufferedReader( strIn );
 
         try
         {
-            List<LdifEntry> entries = parseLdif( inf );
+            List<LdifEntry> entries = parseLdif( reader );
 
             if ( LOG.isDebugEnabled() )
             {
@@ -1620,6 +1624,19 @@
             LOG.error( "Cannot parse the ldif buffer : {}", ne.getMessage() );
             throw new NamingException( "Error while parsing the ldif buffer" );
         }
+        finally
+        {
+            // Close the reader
+            try
+            {
+                reader.close();
+            }
+            catch ( IOException ioe )
+            {
+                // Nothing to do
+            }
+            
+        }
     }
 
     // ------------------------------------------------------------------------
@@ -1771,12 +1788,12 @@
      * @throws NamingException
      *             If something went wrong
      */
-    public List<LdifEntry> parseLdif( BufferedReader inf ) throws NamingException
+    public List<LdifEntry> parseLdif( BufferedReader reader ) throws NamingException
     {
         // Create a list that will contain the read entries
         List<LdifEntry> entries = new ArrayList<LdifEntry>();
 
-        this.in = inf;
+        this.reader = reader;
 
         // First get the version - if any -
         version = parseVersion();
@@ -1809,4 +1826,16 @@
     {
         return containsEntries;
     }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void close() throws IOException
+    {
+        if ( reader != null )
+        {
+            reader.close();
+        }
+    }
 }
\ No newline at end of file



Mime
View raw message