directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From szoer...@apache.org
Subject svn commit: r558336 - in /directory/sandbox/szoerner/groovyldap: build.xml ivy.xml src/main/groovy/demoModifyDN.groovy src/main/java/org/apache/directory/groovyldap/LDAP.java src/main/java/org/apache/directory/groovyldap/Search.java
Date Sat, 21 Jul 2007 16:15:28 GMT
Author: szoerner
Date: Sat Jul 21 09:15:27 2007
New Revision: 558336

URL: http://svn.apache.org/viewvc?view=rev&rev=558336
Log:
Several changes to the API. 
Ivy support added

Added:
    directory/sandbox/szoerner/groovyldap/ivy.xml
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModifyDN.groovy
Modified:
    directory/sandbox/szoerner/groovyldap/build.xml
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAP.java
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/Search.java

Modified: directory/sandbox/szoerner/groovyldap/build.xml
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/build.xml?view=diff&rev=558336&r1=558335&r2=558336
==============================================================================
--- directory/sandbox/szoerner/groovyldap/build.xml (original)
+++ directory/sandbox/szoerner/groovyldap/build.xml Sat Jul 21 09:15:27 2007
@@ -16,33 +16,38 @@
   specific language governing permissions and limitations
   under the License.
 -->
-<project name="groovyldap" default="jarfile">
+<project name="groovyldap" xmlns:ivy="antlib:fr.jayasoft.ivy.ant" default="jarfile">
+	
 	<property name="project.name" value="groovyldap" />
 	<property name="project.version" value="0.1" />
 
+	<!-- directories and target file -->
 	<property name="dir.src" value="./src/main" />
-
+	<property name="dir.lib" value="./lib" />
 	<property name="dir.target" value="./dist/" />
 	<property name="dir.classes" value="${dir.target}/classes" />
 	<property name="target.jar" value="${dir.target}/groovy-ldap.jar" />
 
-	<!-- file location of Groovy jar, to be adjusted --> 
-	<property name="groovy.jar" value="C:/java/groovy-1.0/groovy-1.0.jar" />
+	<!-- used for compilation -->
+	<path id="lib.path.id">
+		<fileset dir="${dir.lib}" />
+	</path>
 
-	<target name="prepare">
+	<target name="prepare" description="--> prepare directories ">
 		<mkdir dir="${dir.target}" />
 		<mkdir dir="${dir.classes}" />
+		<mkdir dir="${dir.lib}" />
 	</target>
-
-	<target name="compile" depends="prepare">
-		<javac srcdir="${dir.src}" destdir="${dir.classes}" includes="**/*.java">
-			<classpath>
-				<pathelement location="${groovy.jar}" />
-			</classpath>
-		</javac>
+	
+	<target name="resolve" description="--> retrieve dependencies with ivy">
+		<ivy:retrieve />
+	</target>
+	
+	<target name="compile" depends="prepare, resolve">
+		<javac srcdir="${dir.src}" destdir="${dir.classes}" includes="**/*.java" classpathref="lib.path.id"
/>
 	</target>
 
-	<target name="jarfile" depends="compile">
+	<target name="jarfile" depends="compile" description="--> create jar file from compiled
classes">
 		<jar destfile="${target.jar}">
 			<fileset dir="${dir.classes}" />
 		</jar>
@@ -50,6 +55,6 @@
 
 	<target name="clean">
 		<delete dir="${dir.classes}" />
+		<delete dir="${dir.lib}" />
 	</target>
-
 </project>

Added: directory/sandbox/szoerner/groovyldap/ivy.xml
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/ivy.xml?view=auto&rev=558336
==============================================================================
--- directory/sandbox/szoerner/groovyldap/ivy.xml (added)
+++ directory/sandbox/szoerner/groovyldap/ivy.xml Sat Jul 21 09:15:27 2007
@@ -0,0 +1,6 @@
+<ivy-module version="1.0">
+    <info organisation="Apache Directory Project" module="groovyldap" />
+    <dependencies>
+        <dependency org="groovy" name="groovy" rev="1.0" />
+    </dependencies>
+</ivy-module>
\ No newline at end of file

Added: directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModifyDN.groovy
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModifyDN.groovy?view=auto&rev=558336
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModifyDN.groovy (added)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModifyDN.groovy Sat Jul 21 09:15:27
2007
@@ -0,0 +1,27 @@
+import org.apache.directory.groovyldap.LDAP
+
+ldap = LDAP.newInstance('ldap://zanzibar:10389', 'uid=admin,ou=system' ,'secret')
+
+superior='dc=example,dc=com'
+dn = 'cn=Myra Ellen Amos,dc=example,dc=com'
+newRdn = 'cn=Tori Amos'
+newDn = 'cn=Tori Amos,dc=example,dc=com'
+    
+assert !ldap.exists(dn)
+
+// define an entry for modifyDN operation 
+attrs = [
+  objectclass: ['top', 'person'],              
+  sn: 'Amos',
+  cn: ['Tori Amos', 'Myra Ellen Amos'],
+]
+ldap.add(dn, attrs)
+assert ldap.exists(dn)
+
+ldap.modifyDn(dn, newRdn, true, superior)
+
+assert ldap.exists(newDn)
+tori = ldap.read(newDn)
+assert tori.cn == 'Tori Amos'
+
+ldap.delete(newDn)
\ 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=558336&r1=558335&r2=558336
==============================================================================
--- 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 Jul 21 09:15:27 2007
@@ -41,6 +41,7 @@
 import javax.naming.directory.SearchResult;
 import javax.naming.ldap.InitialLdapContext;
 import javax.naming.ldap.LdapContext;
+import javax.naming.ldap.LdapName;
 
 import org.apache.directory.groovyldap.util.Util;
 
@@ -153,9 +154,15 @@
         }
         finally
         {
-            try {
-                ctx.close();
-            } catch (Exception e) {
+            try
+            {
+                if ( ctx != null )
+                {
+                    ctx.close();
+                }
+            }
+            catch ( Exception e )
+            {
                 // Ignored
             }
         }
@@ -301,6 +308,36 @@
     }
 
 
+    /**
+     * LDAP modify DN operation.
+     * 
+     * @param dn
+     * @param newRdn
+     * @param deleteOldRdn
+     * @param newSuperior
+     * @throws NamingException
+     */
+    public void modifyDn( final String dn, final String newRdn, final boolean deleteOldRdn,
final String newSuperior )
+        throws NamingException
+    {
+        WithContext<Object> action = new WithContext<Object>()
+        {
+            public Object perform( LdapContext ctx ) throws NamingException
+            {
+                LdapName source = new LdapName( dn );
+                LdapName target = new LdapName( newSuperior );
+
+                target.add( newRdn );
+                ctx.addToEnvironment( "java.naming.ldap.deleteRDN", Boolean.valueOf( deleteOldRdn
).toString() );
+                ctx.rename( source, target );
+
+                return null;
+            }
+        };
+        performWithContext( action );
+    }
+
+
     // search
 
     // modify
@@ -455,6 +492,71 @@
             ctx.close();
         }
     }
+
+
+    public List<Object> search( Map<String, Object> searchParams ) throws NamingException
+    {
+        Search search = new Search( searchParams );
+        return this.search( search );
+    }
+
+
+    public List<Object> search( Search search ) throws NamingException
+    {
+
+        LdapContext ctx = null;
+        List<Object> result = new ArrayList<Object>();
+        try
+        {
+            Properties env = createEnvironment();
+            ctx = new InitialLdapContext( env, null );
+
+            SearchControls ctls = new SearchControls();
+            ctls.setSearchScope( search.getScope().getJndiValue() );
+            ctls.setReturningAttributes( search.getAttrs() );
+            ctls.setReturningObjFlag( true );
+
+            NamingEnumeration<SearchResult> enm = ctx.search( search.getBase(), search.getFilter(),
search
+                .getFilterArgs(), ctls );
+
+            while ( enm.hasMoreElements() )
+            {
+                SearchResult sr = enm.nextElement();
+                result.add( sr.getObject() );
+            }
+        }
+        catch ( NamingException ne )
+        {
+            throw ne;
+        }
+        finally
+        {
+            if (ctx != null) {
+                ctx.close();
+            }
+        }
+        return result;
+    }
+    
+    public Object searchUnique( Search search ) throws NamingException
+    {
+        List<Object> results = this.search( search );
+        switch (results.size()) {
+            case 0:
+                return null;
+            case 1:
+                return results.get( 0 );
+            default:
+                throw new NamingException("Result of search is not unique");
+        }
+    }
+    
+    public Object searchUnique ( Map<String, Object> searchParams ) throws NamingException
+    {
+        Search search = new Search( searchParams );
+        return this.searchUnique( search );
+    }
+
 
 
     public List<Object> search( String filter, String base, SearchScope scope ) throws
NamingException

Modified: directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/Search.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/Search.java?view=diff&rev=558336&r1=558335&r2=558336
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/Search.java
(original)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/Search.java
Sat Jul 21 09:15:27 2007
@@ -21,6 +21,8 @@
 package org.apache.directory.groovyldap;
 
 
+import java.util.Collection;
+import java.util.Collections;
 import java.util.Map;
 
 
@@ -56,7 +58,7 @@
     public Search( Map<String, Object> map )
     {
         this();
-        
+
         for ( String key : map.keySet() )
         {
             if ( key.equalsIgnoreCase( "base" ) )
@@ -70,8 +72,24 @@
             else if ( key.equalsIgnoreCase( "scope" ) )
             {
                 this.setScope( SearchScope.valueOf( map.get( key ).toString() ) );
-            } else {
-                throw new IllegalArgumentException("Unknown parameter for search: "+key);
+            }
+            else if ( key.equalsIgnoreCase( "filterArgs" ) )
+            {
+                Object value = map.get( key );
+                Object[] values = null;
+                if (value instanceof Object[]) {
+                    values = ( Object[] ) value;
+                } else if (value instanceof Collection) {
+                    Collection c = ( Collection ) value;
+                    values = c.toArray();
+                } else {
+                    values = new Object[] { value };
+                }
+                this.setFilterArgs( values );
+            }
+            else
+            {
+                throw new IllegalArgumentException( "Unknown parameter for search: " + key
);
             }
         }
     }



Mime
View raw message