Browse Source

Added importLDIF to assist in importing LDIF data into the directory for testing.

Robert Sanders 20 years ago
parent
commit
6646eb4cd7

+ 79 - 3
sandbox/src/test/java/org/acegisecurity/providers/dao/ldap/LdapTestHelper.java

@@ -1,13 +1,23 @@
 package net.sf.acegisecurity.providers.dao.ldap;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.util.Hashtable;
 
 import javax.naming.Context;
+import javax.naming.Name;
 import javax.naming.NamingException;
+import javax.naming.directory.Attributes;
 import javax.naming.directory.DirContext;
 import javax.naming.directory.InitialDirContext;
 
+import org.apache.ldap.common.ldif.LdifIterator;
+import org.apache.ldap.common.ldif.LdifParser;
+import org.apache.ldap.common.ldif.LdifParserImpl;
+import org.apache.ldap.common.message.LockableAttributesImpl;
+import org.apache.ldap.common.name.LdapName;
 import org.apache.ldap.server.jndi.EnvKeys;
 
 /** 
@@ -26,11 +36,19 @@ public class LdapTestHelper {
 	 * 
 	 */
 	public LdapTestHelper() {
+        // create temporary directory for directory-server to store files in
 		tempDirectory = initTempFiles();
+        // start the apache directory server
 		startServer();
 	}
     
-	
+	/** 
+     * Creates if needed a temporary directory to store the apache directory 
+     * server files.  Since I can't get the class to shutdown cleanly, 
+     * it also ensures a clean start by removing any files in the temp. directory.
+     * 
+     * @return The directory that should be used to store temporary files in.
+	 */
 	protected File initTempFiles() {
 		String tmpDir = System.getProperty("java.io.tmpdir");
 		File dir = new File(tmpDir);
@@ -43,7 +61,45 @@ public class LdapTestHelper {
         System.out.println("Directory temp files at: " + tmp.getAbsolutePath());
 		return tmp;
 	}
+    
+    /** Attempts to open the file and import the contents as LDIF entries 
+     *  into the test directory.
+     *  
+     * @param file The LDIF file to import
+     * @throws IOException
+     * @throws NamingException
+     */
+    public void importLDIF(File file) throws IOException, NamingException {
+        FileInputStream fis = new FileInputStream(file);
+        importLDIF(fis);
+    }
+    
+    /** Attempts to read the provided InputStream for LDIF entries 
+     *   and adds those entries to the test directory server.
+     *   
+     * @param in InputStream of LDIF data.
+     * @throws NamingException
+     * @throws IOException
+     */
+    public void importLDIF(InputStream in) throws NamingException, IOException {
+        DirContext ctx = new InitialDirContext( getServerEnvironment() );
+        try {
+            LdifParser parser = new LdifParserImpl();
+            LdifIterator iterator = new LdifIterator( in );
+            while ( iterator.hasNext() ) {
+                Attributes attributes = new LockableAttributesImpl();
+                String ldif = ( String ) iterator.next();
+                parser.parse( attributes, ldif );
+                Name dn = new LdapName( ( String ) attributes.remove( "dn" ).get() );
+                dn.remove( 0 );
+                ctx.createSubcontext( dn, attributes );
+            }
+        } finally {
+            ctx.close();
+        }
+    }
 	
+    /** starts the apache directory server. */
 	protected void startServer() {
 		try {
 			serverContext = new InitialDirContext( getServerEnvironment() );
@@ -52,7 +108,11 @@ public class LdapTestHelper {
 		}
 	}
 	
+    /** stops the apache directory server, and attempts to remove 
+     *  the data files that the server creates.
+     */
 	protected void shutdownServer() {
+        // close our internal instance of the server-context
 		try {
 			serverContext.close();
 		} catch (NamingException e) {
@@ -60,6 +120,7 @@ public class LdapTestHelper {
 		}
 		serverContext = null;
 		
+        // signal the server that its time to say goodbye
 		Hashtable env = getServerEnvironment();
 		env.put(EnvKeys.SHUTDOWN, "true");
 		try {
@@ -69,6 +130,11 @@ public class LdapTestHelper {
 		}
 	}
 	
+    /** Utility method to remove any files in the temporary directory 
+     *  that we use to store the directory server's data files.
+     *  
+     * @param tempDir The temporary directory.
+     */
 	protected void cleanupTempFiles(File tempDir) {
 		if ((null != tempDir) && (tempDir.exists())) {
 			File[] files = tempDir.listFiles();
@@ -80,7 +146,11 @@ public class LdapTestHelper {
 		}
 	}
 	
-	/** since file..deleteOnExit() isn't working for me, explicitly force cleanup. */
+	/**
+     * This isn't working, probably because I am referencing the class 
+     *  as a static field, but maybe someone can figure out a way to 
+     *  implement this correctly.  
+     */
 	public void finalize() throws Throwable {
         System.out.println("Entering LdapTestHelper.finalize()");
 		shutdownServer();
@@ -90,11 +160,16 @@ public class LdapTestHelper {
         System.out.println("Leaving LdapTestHelper.finalize()");
 	}
 	
-
+    /**
+     * @return The directory that the directory server will use to store its data files.
+     */
 	public File getTempDirectory() {
 		return tempDirectory;
 	}
 	
+    /**
+     * @return The directory that the directory server will use to store its data files.
+     */
 	public String getTempDirectoryPath() {
 		return tempDirectory.getAbsolutePath();
 	}
@@ -110,6 +185,7 @@ public class LdapTestHelper {
 		return env;
 	}
 	
+    /** Get our reference to the server-mode context. */
 	public DirContext getServerContext() {
 		return serverContext;
 	}