directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From szoer...@apache.org
Subject svn commit: r548097 - in /directory/sandbox/szoerner/groovyldap/src/main: groovy/demoModify.groovy java/org/apache/directory/groovyldap/LDAPConnection.java java/org/apache/directory/groovyldap/ModificationType.java
Date Sun, 17 Jun 2007 19:34:02 GMT
Author: szoerner
Date: Sun Jun 17 12:34:00 2007
New Revision: 548097

URL: http://svn.apache.org/viewvc?view=rev&rev=548097
Log:
Support for modify ops added

Added:
    directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModify.groovy
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/ModificationType.java
Modified:
    directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAPConnection.java

Added: 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=auto&rev=548097
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModify.groovy (added)
+++ directory/sandbox/szoerner/groovyldap/src/main/groovy/demoModify.groovy Sun Jun 17 12:34:00
2007
@@ -0,0 +1,21 @@
+import org.apache.directory.groovyldap.*
+
+con = new LDAPConnection(hostname: 'zanzibar', port: 10389)
+con.connect('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)
+
+// performing two operations atomically
+//
+mods = [ 
+    [ 'REPLACE', [description: 'a singer-songwriter, born in Bermuda'] ],
+    [ 'ADD', [userPassword: 'secret'] ]
+]
+con.modify(dn, mods)
+
+con.disconnect()
\ No newline at end of file

Modified: directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAPConnection.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAPConnection.java?view=diff&rev=548097&r1=548096&r2=548097
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAPConnection.java
(original)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/LDAPConnection.java
Sun Jun 17 12:34:00 2007
@@ -20,7 +20,9 @@
 package org.apache.directory.groovyldap;
 
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
@@ -30,6 +32,7 @@
 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;
@@ -124,26 +127,96 @@
     {
         ctx.close();
     }
-    
-    public void add (String dn, Map<String, Object> attributes) throws NamingException
{
-        
+
+
+    public void add( String dn, Map<String, Object> attributes ) throws NamingException
+    {
+
         BasicAttributes attrs = new BasicAttributes();
-        
-        for (String key: attributes.keySet()) {
-            Attribute attr = new BasicAttribute(key);
-            Object value = attributes.get( key );
-            if (value instanceof Collection) {
-                Collection values = ( Collection ) value;
-                for (Object val: values) {
-                    attr.add( val );
-                }
-            } else {
-                attr.add( value );
-            }
+        for ( String key : attributes.keySet() )
+        {
+            Attribute attr = createAttribute( key, attributes.get( key ) );
             attrs.put( attr );
         }
-        
         ctx.createSubcontext( dn, attrs );
+    }
+
+
+    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()] );
+
+        ctx.modifyAttributes( dn, modItems );
+    }
+
+
+    public void modify( String dn, String modType, Map<String, Object> attributes )
throws NamingException
+    {
+        ModificationType type = ModificationType.valueOf( modType );
+        modify( dn, type, attributes );
+    }
+
+
+    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;
+    }
+
+
+    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()] );
+
+        ctx.modifyAttributes( dn, modItems );
     }
 
 

Added: directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/ModificationType.java
URL: http://svn.apache.org/viewvc/directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/ModificationType.java?view=auto&rev=548097
==============================================================================
--- directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/ModificationType.java
(added)
+++ directory/sandbox/szoerner/groovyldap/src/main/java/org/apache/directory/groovyldap/ModificationType.java
Sun Jun 17 12:34:00 2007
@@ -0,0 +1,39 @@
+/*
+ *  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 javax.naming.directory.DirContext;
+
+public enum ModificationType
+{
+    ADD(DirContext.ADD_ATTRIBUTE), DELETE(DirContext.REMOVE_ATTRIBUTE), REPLACE(DirContext.REPLACE_ATTRIBUTE);
+    
+    private int jndiValue;
+
+    private ModificationType( int jndiValue )
+    {
+        this.jndiValue = jndiValue;
+    }
+
+    public int getJndiValue()
+    {
+        return jndiValue;
+    }
+}



Mime
View raw message