You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2005/02/04 23:59:48 UTC

svn commit: r151444 - incubator/directory/kerberos/trunk/core/src/java/org/apache/kerberos/kdc/store/EmbeddedEveStore.java

Author: akarasulu
Date: Fri Feb  4 14:59:47 2005
New Revision: 151444

URL: http://svn.apache.org/viewcvs?view=rev&rev=151444
Log:
fixed a few bugs and cleaned up a little

Modified:
    incubator/directory/kerberos/trunk/core/src/java/org/apache/kerberos/kdc/store/EmbeddedEveStore.java

Modified: incubator/directory/kerberos/trunk/core/src/java/org/apache/kerberos/kdc/store/EmbeddedEveStore.java
URL: http://svn.apache.org/viewcvs/incubator/directory/kerberos/trunk/core/src/java/org/apache/kerberos/kdc/store/EmbeddedEveStore.java?view=diff&r1=151443&r2=151444
==============================================================================
--- incubator/directory/kerberos/trunk/core/src/java/org/apache/kerberos/kdc/store/EmbeddedEveStore.java (original)
+++ incubator/directory/kerberos/trunk/core/src/java/org/apache/kerberos/kdc/store/EmbeddedEveStore.java Fri Feb  4 14:59:47 2005
@@ -19,6 +19,8 @@
 
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.FileNotFoundException;
 import java.util.Hashtable;
 
 import javax.naming.Context;
@@ -59,6 +61,7 @@
      * efficient search is conducted on the more specific DN.
      */
     public static final String KDC_ENTRY_BASEDN_KEY = "kdc.entry.basedn";
+
     public static final String KDC_ENTRY_LDIF_FILE  = "kdc.entry.ldif.file";
 
     /** the krb5kdc schema key for a krb5KDCEntry */
@@ -88,6 +91,45 @@
         this.env = ( Hashtable ) env.clone();
     }
 
+
+    /**
+     * Tries to find an LDIF either on file system or packaged within a jar.
+     *
+     * @return the input stream to the ldif file
+     * @throws FileNotFoundException if the file cannot be found
+     */
+    private InputStream getLdifStream() throws FileNotFoundException
+    {
+        String ldifPath = ( ( String ) env.get( KDC_ENTRY_LDIF_FILE ) ).trim();
+
+        File file = new File( ldifPath );
+
+        InputStream in = null;
+
+        if ( file.exists() )
+        {
+            in = new FileInputStream( file );
+        }
+        else
+        {
+            // if file not on system see if something is bundled with the jar ...
+
+            in = getClass().getResourceAsStream( ldifPath );
+
+            if ( in == null )
+            {
+                System.err.println( "LDIF file '" + file.getAbsolutePath() + "' does not exit!" );
+
+                System.err.println( "Nor does LDIF resource '" + ldifPath + "' exit!" );
+
+                System.exit( 4 );
+            }
+        }
+
+        return in;
+    }
+
+
     /**
      * Fires up the ApacheDS backing store using the environment properties supplied to the
      * constructor.  The JNDI default context factor and some other parameters
@@ -108,11 +150,12 @@
         }
         catch ( NamingException e )
         {
-            // @todo for now until we can find a better means of error handling
-            e.printStackTrace();
             String msg = "Failed to create initial context for ApacheDS provider";
+
             NestableRuntimeException fault;
+
             fault = new NestableRuntimeException( msg, e );
+
             throw fault;
         }
 
@@ -122,6 +165,7 @@
             try
             {
                 ctxRoot = new LdapName( ctx.getNameInNamespace() );
+
                 searchBaseDn = new LdapName( ( String ) env.get( KDC_ENTRY_BASEDN_KEY ) );
                 
                 if ( searchBaseDn.startsWith( ctxRoot ) )
@@ -134,18 +178,22 @@
                 else
                 {
                     String msg = "Failed to create initial context for ApacheDS provider";
+
                     IllegalArgumentException fault;
+
                     fault = new IllegalArgumentException( msg );
+
                     throw fault;
                 }
             }
             catch ( NamingException e )
             {
-                // @todo for now until we can find a better means of error handling
-                e.printStackTrace();
                 String msg = "Failed to find search base for ApacheDS store";
+
                 NestableRuntimeException fault;
+
                 fault = new NestableRuntimeException( msg, e );
+
                 throw fault;
             }
         }
@@ -157,97 +205,123 @@
             return;
         }
 
+        Name rdn = null;
+
         try
         {
-            File file = new File( ( ( String ) env.get( KDC_ENTRY_LDIF_FILE ) ).trim() );
-
-            if ( ! file.exists() )
-            {
-                System.err.println( "LDIF file '" + file.getAbsolutePath() + "' does not exit!" );
+            InputStream in = getLdifStream();
 
-                System.exit( 4 );
-            }
-
-            FileInputStream in = new FileInputStream( file );
             LdifIterator iterator = new LdifIterator( in );
+
             LdifParser ldifParser = new LdifParserImpl();
 
             while ( iterator.hasNext() )
             {
                 String ldif = ( String ) iterator.next();
+
                 Attributes attributes = new LockableAttributesImpl();
+
                 ldifParser.parse( attributes, ldif );
+
                 String dn = ( String ) attributes.remove( "dn" ).get();
 
                 if ( attributes.get( "objectClass" ).contains( "krb5KDCEntry" ) )
                 {
                     String pw = ( String ) attributes.get( "userpassword" ).get();
+
                     String krbPrincipal = ( String ) attributes.get( PRINCIPAL_ATTR ).get();
+
                     KerberosPrincipal principal = new KerberosPrincipal( krbPrincipal );
+
                     KerberosKey key = new KerberosKey( principal, pw.toCharArray(), "DES" ) ;
+
                     byte[] encodedKey = key.getEncoded();
+
                     attributes.put( KEY_ATTR, encodedKey );
+
                     attributes.put( VERSION_ATTR, Integer.toString( key.getVersionNumber() ) );
+
                     attributes.put( TYPE_ATTR, Integer.toString( key.getKeyType() ) );
                 }
 
+                rdn = getRelativeName( ctx, dn );
+
                 try
                 {
-                    if ( ctx.lookup( dn ) == null )
-                    {
-                        System.out.println( "Entry " + dn + " from LDIF exists." );
-                        continue;
-                    }
+                    System.err.println( "attempting look up before creation of " + rdn );
+
+                    ctx.lookup( rdn );
+
+                    System.err.println( "succeeded on looked up of " + rdn + " will not create" );
                 }
                 catch( Exception e )
                 {
-                    System.out.println( "Entry " + dn
-                            + " from LDIF does not exist.  Creating it ..." );
 
-                }
+                    System.err.println( "creating " + rdn );
 
-                ctx.createSubcontext( getRelativeName( ctx, dn ), attributes );
+                    ctx.createSubcontext( rdn, attributes );
+
+                    System.err.println( "successfully created " + rdn );
+                }
             }
         }
         catch( Exception e )
         {
-            // @todo for now until we can find a better means of error handling
-            e.printStackTrace();
             String msg = "Failed to import initial LDIF into ApacheDS store";
+
             NestableRuntimeException fault;
+
             fault = new NestableRuntimeException( msg, e );
+
             throw fault;
         }
 
         try
         {
-            String ldifFile = ( String ) env.get( KDC_ENTRY_LDIF_FILE );
-            FileInputStream in = new FileInputStream( ldifFile );
+            InputStream in = getLdifStream();
+
             LdifIterator iterator = new LdifIterator( in );
+
             LdifParser ldifParser = new LdifParserImpl();
 
             while ( iterator.hasNext() )
             {
                 String ldif = ( String ) iterator.next();
+
                 Attributes attributes = new LockableAttributesImpl();
+
                 ldifParser.parse( attributes, ldif );
+
                 String dn = ( String ) attributes.remove( "dn" ).get();
 
-                Context stored = ( Context ) ctx.lookup( getRelativeName( ctx, dn ) );
+                rdn = getRelativeName( ctx, dn );
+
+                System.err.println( "looking up " + rdn );
+
+                Object stored = ( Object ) ctx.lookup( rdn );
+
+                System.err.println( "looked up " + rdn + " and found " + stored  );
 
                 if ( stored == null )
                 {
+                    System.err.println( rdn + " was null" );
+
                     throw new IllegalStateException( "LDIF entries not being pushed to disk" );
                 }
             }
         }
         catch( Exception e )
         {
-            // @todo for now until we can find a better means of error handling
             e.printStackTrace();
+
+            System.err.println( "failed to find " + rdn );
+
             String msg = "Failed to import initial LDIF into ApacheDS store";
+
             NestableRuntimeException fault;
+
             fault = new NestableRuntimeException( msg, e );
+
             throw fault;
         }
     }
@@ -255,6 +329,7 @@
     public Name getRelativeName( Context base, String dn ) throws NamingException
     {
         LdapName rdn = new LdapName( dn );
+
         LdapName baseDn = new LdapName( base.getNameInNamespace() );
 
         if ( rdn.startsWith( baseDn ) )
@@ -266,13 +341,13 @@
         }
         else
         {
-            throw new NamingException( dn + " is not a subordinate of context:"
-                + baseDn.toString() );
+            throw new NamingException( dn + " is not a subordinate of context:" + baseDn.toString() );
         }
 
         return rdn;
     }
 
+
     public PrincipalStoreEntry getEntry( KerberosPrincipal principal ) throws KerberosException
     {
         if ( principal == null )
@@ -281,16 +356,22 @@
         }
 
         Attributes attributes = new LockableAttributesImpl();
+
         attributes.put( PRINCIPAL_ATTR, principal.getName() );
+
         try
         {
             Attributes attrs = null;
+
             NamingEnumeration list = ctx.search( searchBaseDn, attributes );
+
             if ( list.hasMore() )
             {
                 SearchResult result = ( SearchResult ) list.next();
+
                 attrs = result.getAttributes();
             }
+
             list.close();
 
             if ( attrs == null )
@@ -303,10 +384,11 @@
         catch ( NamingException e )
         {
             e.printStackTrace();
+
             return null;
         }
     }
-    
+
 
     /**
      * Marshals an a PrincipalStoreEntry from an Attributes object.