directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From szoer...@apache.org
Subject svn commit: r550049 - in /directory/sandbox/szoerner/groovyldap/src/main: groovy/ java/org/apache/directory/groovyldap/
Date Sat, 23 Jun 2007 15:01:27 GMT
Author: szoerner
Date: Sat Jun 23 08:01:26 2007
New Revision: 550049

URL: http://svn.apache.org/viewvc?view=rev&rev=550049
Log:
Second variant. More groovy

Added:
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearchClosure.groovy
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java
Removed:
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAPConnection.java
Modified:
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModify.groovy
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoRead.groovy
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearch.groovy

Modified: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy?view=diff&rev=550049&r1=550048&r2=550049
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy (original)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoAdd.groovy Sat Jun 23 08:01:26
2007
@@ -1,14 +1,11 @@
-import org.apache.directory.groovyldap.LDAPConnection
+import org.apache.directory.groovyldap.LDAP
 
-con = new LDAPConnection(hostname: 'zanzibar', port: 10389)
-con.connect('uid=admin,ou=system', 'secret');
+ldap = LDAP.newInstance('ldap://zanzibar:10389', 'uid=admin,ou=system' ,'secret')
 
 heather = [
   objectclass: ['top', 'person'],              
   sn: 'Nova',
   cn: 'Heather Nova'
 ]
-         
-con.add('cn=Heather Nova,dc=example,dc=com', heather)
+ldap.add('cn=Heather Nova,dc=example,dc=com', heather)
 
-con.disconnect()
\ No newline at end of file

Modified: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModify.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModify.groovy?view=diff&rev=550049&r1=550048&r2=550049
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModify.groovy (original)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModify.groovy Sat Jun 23 08:01:26
2007
@@ -1,14 +1,13 @@
-import org.apache.directory.groovyldap.*
+import org.apache.directory.groovyldap.LDAP
 
-con = new LDAPConnection(hostname: 'zanzibar', port: 10389)
-con.connect('uid=admin,ou=system', 'secret');
+ldap = LDAP.newInstance('ldap://zanzibar:10389', 'uid=admin,ou=system' ,'secret')
 
 dn = 'cn=Heather Nova,dc=example,dc=com'
 
 // Adding a single attribute
 //
 descr = [ description: 'a singer-songwriter' ]
-con.modify(dn, 'ADD', descr)
+ldap.modify(dn, 'ADD', descr)
 
 // performing two operations atomically
 //
@@ -16,6 +15,4 @@
     [ 'REPLACE', [description: 'a singer-songwriter, born in Bermuda'] ],
     [ 'ADD', [userPassword: 'secret'] ]
 ]
-con.modify(dn, mods)
-
-con.disconnect()
\ No newline at end of file
+ldap.modify(dn, mods)

Modified: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoRead.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoRead.groovy?view=diff&rev=550049&r1=550048&r2=550049
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoRead.groovy (original)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoRead.groovy Sat Jun 23 08:01:26
2007
@@ -1,15 +1,12 @@
-import org.apache.directory.groovyldap.LDAPConnection
+import org.apache.directory.groovyldap.LDAP
 
-con = new LDAPConnection(hostname: 'zanzibar', port: 10389)
-con.connect('uid=admin,ou=system', 'secret');
+ldap = LDAP.newInstance("ldap://zanzibar:10389")
 
 // Simple entry lookup via dn
-heather = con.read('cn=Heather Nova,dc=example,dc=com')
+heather = ldap.read('cn=Heather Nova,dc=example,dc=com')
 
 print """
 DN: ${heather.dn}
 Common name: ${heather.cn}
 Object classes: ${heather.objectclass}
 """
-
-con.disconnect()

Modified: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearch.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearch.groovy?view=diff&rev=550049&r1=550048&r2=550049
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearch.groovy (original)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearch.groovy Sat Jun 23 08:01:26
2007
@@ -1,13 +1,10 @@
 import org.apache.directory.groovyldap.*
 
-con = new LDAPConnection(hostname: 'zanzibar', port: 10389)
-con.connect('uid=admin,ou=system', 'secret');
+ldap = LDAP.newInstance('ldap://zanzibar:10389/')
 
 // search op
-results = con.search('dc=example,dc=com', SearchScope.ONE, '(objectClass=*)')
-for (result in results) {
-  entry = result.object
-  println entry.cn
+results = ldap.search('(objectClass=*)', 'dc=example,dc=com', SearchScope.ONE, )
+println "${results.size} entries found:"
+for (entry in results) {
+  println entry.dn
 }
-
-con.disconnect()
\ No newline at end of file

Added: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearchClosure.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearchClosure.groovy?view=auto&rev=550049
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearchClosure.groovy (added)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoSearchClosure.groovy Sat Jun
23 08:01:26 2007
@@ -0,0 +1,8 @@
+import org.apache.directory.groovyldap.LDAP
+
+ldap = LDAP.newInstance('ldap://zanzibar:10389/dc=example,dc=com')
+
+ldap.eachEntry ('(objectClass=person)') { person ->  
+    println person.cn
+}
+

Added: 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=auto&rev=550049
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java
(added)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java
Sat Jun 23 08:01:26 2007
@@ -0,0 +1,350 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+
+package org.apache.directory.groovyldap;
+
+
+import groovy.lang.Closure;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.ModificationItem;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+import javax.naming.ldap.InitialLdapContext;
+import javax.naming.ldap.LdapContext;
+
+
+public class LDAP
+{
+    private static final String DEFAULT_URL = "ldap://localhost:389/";
+
+    private String url;
+
+    private boolean anonymousBind;
+
+    private String bindUser;
+
+    private String bindPassword;
+
+
+    protected Properties createEnvironment()
+    {
+        Properties env = new Properties();
+        env.setProperty( Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"
);
+        env.setProperty( Context.PROVIDER_URL, url );
+        env.setProperty( Context.OBJECT_FACTORIES, "org.apache.directory.groovyldap.DirContextToMapObjectFactory"
);
+        if ( !anonymousBind )
+        {
+            env.setProperty( Context.SECURITY_PRINCIPAL, bindUser );
+            env.setProperty( Context.SECURITY_CREDENTIALS, bindPassword );
+        }
+        return env;
+    }
+
+
+    protected LDAP()
+    {
+        this( DEFAULT_URL );
+    }
+
+
+    protected LDAP( String url )
+    {
+        this.url = url;
+        this.anonymousBind = true;
+    }
+
+
+    protected LDAP( String url, String bindUser, String bindPassword )
+    {
+        this.url = url;
+        this.anonymousBind = false;
+        this.bindUser = bindUser;
+        this.bindPassword = bindPassword;
+    }
+
+
+    public static LDAP newInstance()
+    {
+        return new LDAP();
+    }
+
+
+    public static LDAP newInstance( String url )
+    {
+        return new LDAP( url );
+    }
+
+    public static final SearchScope ONE = SearchScope.ONE;
+
+
+    /** 
+     * Creates an attribute from the given parameters. If value is a collection, a multi-valued
attribute will be created.
+     * 
+     * @param name Name of the attribute
+     * @param value 
+     * @return a JNDI attribute object
+     */
+    protected Attribute createAttribute( String name, Object value )
+    {
+        Attribute attr = new BasicAttribute( name );
+        if ( value instanceof Collection )
+        {
+            Collection values = ( Collection ) value;
+            for ( Object val : values )
+            {
+                attr.add( val );
+            }
+        }
+        else
+        {
+            attr.add( value );
+        }
+        return attr;
+    }
+
+
+    /**
+     * LDAP add operation. Adds a new entry to the directory.
+     * 
+     * @param dn DN of the entry
+     * @param attributes attributes of the entry
+     * @throws NamingException
+     */
+    public void add( String dn, Map<String, Object> attributes ) throws NamingException
+    {
+
+        BasicAttributes attrs = new BasicAttributes();
+        for ( String key : attributes.keySet() )
+        {
+            Attribute attr = createAttribute( key, attributes.get( key ) );
+            attrs.put( attr );
+        }
+
+        LdapContext ctx = null;
+        try
+        {
+            ctx = new InitialLdapContext( createEnvironment(), null );
+            ctx.createSubcontext( dn, attrs );
+        }
+        catch ( NamingException ne )
+        {
+            throw ne;
+        }
+        finally
+        {
+            ctx.close();
+        }
+    }
+
+
+    public void eachEntry( String filter, String base, SearchScope scope, Closure closure
) throws NamingException
+    {
+
+        LdapContext ctx = null;
+        try
+        {
+            ctx = new InitialLdapContext( createEnvironment(), null );
+
+            SearchControls ctls = new SearchControls();
+            ctls.setSearchScope( scope.getJndiValue() );
+            ctls.setReturningObjFlag( true );
+
+            NamingEnumeration<SearchResult> enm = ctx.search( base, filter, ctls );
+            while ( enm.hasMore() )
+            {
+                SearchResult sr = enm.next();
+                Object obj = sr.getObject();
+                closure.call( obj );
+            }
+        }
+        catch ( NamingException ne )
+        {
+            throw ne;
+        }
+        finally
+        {
+            ctx.close();
+        }
+    }
+
+
+    public void eachEntry( String filter, Closure closure ) throws NamingException
+    {
+        eachEntry( filter, "", SearchScope.SUB, closure );
+    }
+
+
+    public void modify( String dn, ModificationType modType, Map<String, Object> attributes
) throws NamingException
+    {
+
+        // Create a list of modification item for a JNDI op 
+        //
+        List<ModificationItem> mods = new ArrayList<ModificationItem>();
+        for ( String key : attributes.keySet() )
+        {
+            Attribute attr = createAttribute( key, attributes.get( key ) );
+            ModificationItem item = new ModificationItem( modType.getJndiValue(), attr );
+            mods.add( item );
+        }
+        ModificationItem[] modItems = mods.toArray( new ModificationItem[mods.size()] );
+
+        LdapContext ctx = null;
+        try
+        {
+            ctx = new InitialLdapContext( createEnvironment(), null );
+            ctx.modifyAttributes( dn, modItems );
+        }
+        catch ( NamingException ne )
+        {
+            throw ne;
+        }
+        finally
+        {
+            ctx.close();
+        }
+    }
+
+
+    public void modify( String dn, String modType, Map<String, Object> attributes )
throws NamingException
+    {
+        ModificationType type = ModificationType.valueOf( modType );
+        modify( dn, type, attributes );
+    }
+
+
+    public void modify( String dn, List<List> modificationItem ) throws NamingException
+    {
+        List<ModificationItem> mods = new ArrayList<ModificationItem>();
+        for ( List pair : modificationItem )
+        {
+            if ( pair.size() != 2 )
+            {
+                throw new IllegalArgumentException( "parameter 2 is not a list of pairs"
);
+            }
+
+            Object oModType = pair.get( 0 );
+            ModificationType modType = null;
+            if ( oModType instanceof ModificationType )
+            {
+                modType = ( ModificationType ) oModType;
+            }
+            else if ( oModType instanceof String )
+            {
+                modType = ModificationType.valueOf( ( String ) oModType );
+            }
+            else
+            {
+                throw new IllegalArgumentException( "parameter 1 of pair is not o valid ModificationType"
);
+            }
+
+            Map<String, Object> attributes = ( Map<String, Object> ) pair.get(
1 );
+            for ( String key : attributes.keySet() )
+            {
+                Attribute attr = createAttribute( key, attributes.get( key ) );
+                ModificationItem item = new ModificationItem( modType.getJndiValue(), attr
);
+                mods.add( item );
+            }
+        }
+        ModificationItem[] modItems = mods.toArray( new ModificationItem[mods.size()] );
+
+        LdapContext ctx = null;
+        try
+        {
+            ctx = new InitialLdapContext( createEnvironment(), null );
+            ctx.modifyAttributes( dn, modItems );
+        }
+        catch ( NamingException ne )
+        {
+            throw ne;
+        }
+        finally
+        {
+            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;
+    }
+
+
+    public List<Object> search( String filter, String base, SearchScope scope ) throws
NamingException
+    {
+        LdapContext ctx = null;
+        List<Object> result = new ArrayList<Object>();
+        try
+        {
+            ctx = new InitialLdapContext( createEnvironment(), null );
+
+            SearchControls ctls = new SearchControls();
+            ctls.setSearchScope( scope.getJndiValue() );
+            ctls.setReturningObjFlag( true );
+
+            NamingEnumeration<SearchResult> enm = ctx.search( base, filter, ctls );
+            while ( enm.hasMoreElements() )
+            {
+                SearchResult sr = enm.nextElement();
+                result.add( sr.getObject() );
+            }
+        }
+        catch ( NamingException ne )
+        {
+            throw ne;
+        }
+        finally
+        {
+            ctx.close();
+        }
+        return result;
+    }
+}
\ No newline at end of file



Mime
View raw message