directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From szoer...@apache.org
Subject svn commit: r552122 - in /directory/sandbox/szoerner/groovyldap/src/main: groovy/demoCompare.groovy java/org/apache/directory/groovyldap/LDAP.java java/org/apache/directory/groovyldap/WithContext.java java/org/apache/directory/groovyldap/util/Util.java
Date Sat, 30 Jun 2007 08:52:25 GMT
Author: szoerner
Date: Sat Jun 30 01:52:24 2007
New Revision: 552122

URL: http://svn.apache.org/viewvc?view=rev&rev=552122
Log:
Several changes after feedback from Groovy mailing group 

Added:
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoCompare.groovy
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/WithContext.java
Modified:
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/util/Util.java

Added: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoCompare.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoCompare.groovy?view=auto&rev=552122
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoCompare.groovy (added)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoCompare.groovy Sat Jun 30 01:52:24
2007
@@ -0,0 +1,8 @@
+import org.apache.directory.groovyldap.LDAP
+
+ldap = LDAP.newInstance('ldap://zanzibar:10389', 'uid=admin,ou=system' ,'secret')
+
+assert ldap.compare('cn=Heather Nova,dc=example,dc=com', [cn: 'HEATHER NOVA'] )
+
+
+println ldap.compare('cn=Heather Nova,dc=example,dc=com', [sn: '***'] )
\ No newline at end of file

Modified: directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java?view=diff&rev=552122&r1=552121&r2=552122
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java
(original)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java
Sat Jun 30 01:52:24 2007
@@ -34,6 +34,7 @@
 import javax.naming.NamingException;
 import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
+import javax.naming.directory.BasicAttribute;
 import javax.naming.directory.BasicAttributes;
 import javax.naming.directory.ModificationItem;
 import javax.naming.directory.SearchControls;
@@ -131,42 +132,20 @@
 
 
     /**
-     * LDAP add operation for a map. Adds a new entry to the directory.
-     * 
-     * @param dn DN of the entry
-     * @param attributes attributes of the entry
+     * Open an LDAP context and perform a given task within this context.
+     *  
+     * @param <T>
+     * @param action
+     * @return
      * @throws NamingException
      */
-    public void add( String dn, Map<String, Object> attributes ) throws NamingException
-    {
-
-        BasicAttributes attrs = new BasicAttributes();
-        for ( String key : attributes.keySet() )
-        {
-            Attribute attr = Util.createAttribute( key, attributes.get( key ) );
-            attrs.put( attr );
-        }
-        addInternal( dn, attrs );
-    }
-
-
-    /**
-     * LDAP add operation for an LDIF entry. Adds a new entry to the directory, which is
given in LDIF. The dn is given via the first LDIF line.
-     */
-    public void add( String ldifEntry ) throws NamingException
-    {
-        String dn = Util.getDNFromLDIFEntry( ldifEntry );
-        Attributes attrs = Util.getAttributesFromLDIF( ldifEntry );
-        addInternal( dn, attrs );
-    }
-
-    protected void addInternal( String dn, Attributes attrs ) throws NamingException
+    protected <T> T performWithContext( WithContext<T> action ) throws NamingException
     {
         LdapContext ctx = null;
         try
         {
             ctx = new InitialLdapContext( createEnvironment(), null );
-            ctx.createSubcontext( dn, attrs );
+            return action.perform( ctx );
         }
         catch ( NamingException ne )
         {
@@ -178,6 +157,35 @@
         }
     }
 
+
+    /**
+     * LDAP add operation. Adds a new entry to the directory. The attributes have to be provided
as a map.
+     * 
+     * @param dn DN of the entry
+     * @param attributes attributes of the entry
+     * @throws NamingException
+     */
+    public void add( final String dn, final Map<String, Object> attributes ) throws
NamingException
+    {
+        WithContext<Object> action = new WithContext<Object>()
+        {
+            public Object perform( LdapContext ctx ) throws NamingException
+            {
+                BasicAttributes attrs = new BasicAttributes();
+                for ( String key : attributes.keySet() )
+                {
+                    Attribute attr = Util.createAttribute( key, attributes.get( key ) );
+                    attrs.put( attr );
+                }
+
+                ctx.createSubcontext( dn, attrs );
+                return null;
+            }
+        };
+        performWithContext( action );
+    }
+
+
     /**
      * LDAP delete operation. Deletes an entry from the directory.
      * 
@@ -185,67 +193,118 @@
      * @param attributes attributes of the entry
      * @throws NamingException
      */
-    public void delete( String dn ) throws NamingException
+    public void delete( final String dn ) throws NamingException
     {
+        // Check, whether entry exists. If not, throw an exception
         if ( !exists( dn ) )
         {
             throw new NameNotFoundException( "Entry " + dn + " does not exist!" );
         }
 
-        LdapContext ctx = null;
-        try
-        {
-            ctx = new InitialLdapContext( createEnvironment(), null );
-            ctx.destroySubcontext( dn );
-        }
-        catch ( NamingException ne )
-        {
-            throw ne;
-        }
-        finally
+        WithContext<Object> action = new WithContext<Object>()
         {
-            ctx.close();
-        }
+            public Object perform( LdapContext ctx ) throws NamingException
+            {
+                ctx.destroySubcontext( dn );
+                return null;
+            }
+        };
+        performWithContext( action );
     }
 
 
     /**
-     * Check whether an entry with the given DN exists. The method performs a search to check
this.
+     * Reads an entry by its DN.
      */
-    public boolean exists( String dn ) throws NamingException
+    public Object read( final String dn ) throws NamingException
     {
-        LdapContext ctx = null;
-        boolean result = false;
-        try
+        WithContext<Object> action = new WithContext<Object>()
         {
-            ctx = new InitialLdapContext( createEnvironment(), null );
-
-            SearchControls ctls = new SearchControls();
-            ctls.setSearchScope( SearchControls.OBJECT_SCOPE );
-            ctls.setReturningAttributes( new String[0] );
-            ctls.setReturningObjFlag( false );
-
-            try
+            public Object perform( LdapContext ctx ) throws NamingException
             {
-                ctx.search( dn, "(objectClass=*)", ctls );
-                result = true;
+                return ctx.lookup( dn );
             }
-            catch ( NameNotFoundException nne )
+        };
+        return performWithContext( action );
+    }
+
+
+    /**
+     * Check whether an entry with the given DN exists. The method performs a search to check
this, which is more efficient than reading the entry.
+     */
+    public boolean exists( final String dn ) throws NamingException
+    {
+        WithContext<Boolean> action = new WithContext<Boolean>()
+        {
+            public Boolean perform( LdapContext ctx ) throws NamingException
             {
+                SearchControls ctls = new SearchControls();
+                ctls.setSearchScope( SearchControls.OBJECT_SCOPE );
+                ctls.setReturningAttributes( new String[0] );
+                ctls.setReturningObjFlag( false );
+
+                try
+                {
+                    ctx.search( dn, "(objectClass=*)", ctls );
+                    return Boolean.TRUE;
+                }
+                catch ( NameNotFoundException nne )
+                {
+                }
+                return Boolean.FALSE;
             }
-        }
-        catch ( NamingException ne )
+
+        };
+        return performWithContext( action );
+    }
+
+
+    /**
+     * LDAP compare operation.
+     * 
+     * @param dn Distinguished name of the entry.
+     * @param assertion attribute assertion.
+     * @return
+     * @throws NamingException
+     */
+    public boolean compare( final String dn, final Map<String, Object> assertion )
throws NamingException
+    {
+        if ( assertion.size() != 1 )
         {
-            throw ne;
+            throw new IllegalArgumentException( "Assertion may only include one attribute"
);
         }
-        finally
+
+        WithContext<Boolean> action = new WithContext<Boolean>()
         {
-            ctx.close();
-        }
-        return result;
+            public Boolean perform( LdapContext ctx ) throws NamingException
+            {
+                SearchControls ctls = new SearchControls();
+                ctls.setReturningAttributes( new String[0] );
+                ctls.setSearchScope( SearchControls.OBJECT_SCOPE );
+                ctls.setReturningObjFlag( false );
+
+                String attrName = assertion.keySet().iterator().next();
+                String filter = "(" + attrName + "={0})";
+                Object value = assertion.get( attrName );
+
+                NamingEnumeration<SearchResult> enumeration = ctx.search( dn, filter,
new Object[]
+                    { value }, ctls );
+
+                return enumeration.hasMore();
+            }
+        };
+        return performWithContext( action );
     }
 
 
+    // search
+
+    // modify
+
+    // modify DN
+
+    // compare
+
     public void eachEntry( String filter, String base, SearchScope scope, Closure closure
) throws NamingException
     {
 
@@ -391,31 +450,6 @@
         {
             ctx.close();
         }
-    }
-
-
-    /**
-     * Read an entry by its DN.
-     */
-    public Object read( String dn ) throws NamingException
-    {
-        LdapContext ctx = null;
-        Object result = null;
-        try
-        {
-            ctx = new InitialLdapContext( createEnvironment(), null );
-            result = ctx.lookup( dn );
-        }
-        catch ( NamingException ne )
-        {
-            throw ne;
-        }
-        finally
-        {
-            ctx.close();
-        }
-
-        return result;
     }
 
 

Added: directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/WithContext.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/WithContext.java?view=auto&rev=552122
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/WithContext.java
(added)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/WithContext.java
Sat Jun 30 01:52:24 2007
@@ -0,0 +1,9 @@
+package org.apache.directory.groovyldap;
+
+import javax.naming.NamingException;
+import javax.naming.ldap.LdapContext;
+
+public interface WithContext <T>
+{
+    T perform(LdapContext ctx) throws NamingException;
+}

Modified: directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/util/Util.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/util/Util.java?view=diff&rev=552122&r1=552121&r2=552122
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/util/Util.java
(original)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/util/Util.java
Sat Jun 30 01:52:24 2007
@@ -20,15 +20,11 @@
 package org.apache.directory.groovyldap.util;
 
 
-import java.io.IOException;
-import java.io.LineNumberReader;
-import java.io.StringReader;
 import java.util.Collection;
 
 import javax.naming.directory.Attribute;
-import javax.naming.directory.Attributes;
 import javax.naming.directory.BasicAttribute;
-import javax.naming.directory.BasicAttributes;
+
 
 /**
  * Utility methods.
@@ -43,6 +39,7 @@
 
     }
 
+
     /** 
      * Creates an attribute from the given parameters. If value is a collection, a multi-valued
attribute will be created.
      * 
@@ -67,85 +64,4 @@
         }
         return attr;
     }
-    
-    /** 
-     */
-    public static Attributes getAttributesFromLDIF( String ldifEntry )
-    {
-        Attributes attrs = new BasicAttributes();
-        
-        StringReader sr = new StringReader(ldifEntry);
-        LineNumberReader lnr = new LineNumberReader(sr);
-        
-        try {
-            String line = null;
-            boolean dnFound = false;
-            while ((line = lnr.readLine()) != null) {    
-                System.out.println("Line: ["+line+"]");
-                
-                if (!dnFound && line.trim().equals( "" )) {
-                    continue;
-                }
-                int pos = line.indexOf( ':' ); 
-                if (pos == -1) {
-                    throw new IllegalArgumentException("error in LDIF, line"+lnr.getLineNumber()+",
no ':' found.");
-                }
-                String attrName = line.substring( 0, pos ).trim();
-                
-                if (attrName.equals( "dn" ) && dnFound) {
-                    throw new IllegalArgumentException("error in LDIF, line"+lnr.getLineNumber()+",
dn found twice");
-                } else if (attrName.equals( "dn" )) {
-                    dnFound = true;
-                } else {
-                    String value = line.substring( pos+1 ).trim();
-                    if (attrs.get( attrName ) == null) {
-                        Attribute attr = new BasicAttribute(attrName, value);
-                        attrs.put( attr );
-                    } else {
-                        Attribute attr = attrs.get( attrName );
-                        attr.add( value );
-                    }
-                }
-            }
-        } catch (IOException io) {
-            // ignored
-        }
-        
-        return attrs;
-    }
-    
-    /** 
-     */
-    public static String getDNFromLDIFEntry( String ldifEntry )
-    {
-        String dn = null;
-        
-        StringReader sr = new StringReader(ldifEntry);
-        LineNumberReader lnr = new LineNumberReader(sr);
-        
-        try {
-            String line = null;
-            boolean dnFound = false;
-            while ((line = lnr.readLine()) != null) {    
-                if (!dnFound && line.trim().equals( "" )) {
-                    continue;
-                }
-                int pos = line.indexOf( ':' ); 
-                if (pos == -1) {
-                    throw new IllegalArgumentException("error in LDIF, line"+lnr.getLineNumber()+",
no ':' found.");
-                }
-                
-                String attrName = line.substring( 0, pos ).trim();
-                if (attrName.equals( "dn" )) {
-                    dn = line.substring( pos+1 ).trim();
-                } 
-            }
-        } catch (IOException io) {
-            // ignored
-        }
-        
-        return dn;
-    }
-
-
 }



Mime
View raw message