directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kayyag...@apache.org
Subject svn commit: r718198 - /directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/LdapPersistenceUtil.java
Date Mon, 17 Nov 2008 08:56:48 GMT
Author: kayyagari
Date: Mon Nov 17 00:56:47 2008
New Revision: 718198

URL: http://svn.apache.org/viewvc?rev=718198&view=rev
Log:
o added some more methods
o fixed few bugs
o added javadoc

Modified:
    directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/LdapPersistenceUtil.java

Modified: directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/LdapPersistenceUtil.java
URL: http://svn.apache.org/viewvc/directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/LdapPersistenceUtil.java?rev=718198&r1=718197&r2=718198&view=diff
==============================================================================
--- directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/LdapPersistenceUtil.java
(original)
+++ directory/sandbox/kayyagari/apacheds-olm/src/main/java/org/apache/directory/olm/util/LdapPersistenceUtil.java
Mon Nov 17 00:56:47 2008
@@ -35,6 +35,8 @@
 
 import org.apache.directory.olm.Attribute;
 import org.apache.directory.olm.Entry;
+import org.apache.directory.olm.MultiValueAttribute;
+import org.apache.directory.olm.SingleValueAttribute;
 import org.apache.directory.server.core.CoreSession;
 import org.apache.directory.server.core.entry.ClonedServerEntry;
 import org.apache.directory.server.core.entry.DefaultServerAttribute;
@@ -51,8 +53,12 @@
 import org.apache.directory.shared.ldap.entry.Modification;
 import org.apache.directory.shared.ldap.entry.ModificationOperation;
 import org.apache.directory.shared.ldap.entry.Value;
+import org.apache.directory.shared.ldap.filter.ExprNode;
+import org.apache.directory.shared.ldap.filter.SearchScope;
+import org.apache.directory.shared.ldap.message.AliasDerefMode;
 import org.apache.directory.shared.ldap.name.LdapDN;
 import org.apache.directory.shared.ldap.schema.AttributeType;
+import org.apache.directory.shared.ldap.schema.AttributeTypeOptions;
 import org.apache.directory.shared.ldap.schema.ObjectClass;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -159,30 +165,41 @@
             }
 
             boolean isHr = syntaxRegistry.lookup( attr.getSyntax() ).isHumanReadable();
-            if ( isHr )
+            
+            if ( attr.isSingleValue() )
             {
-                if ( attr.isSingleValue() )
+                Object value = ( ( SingleValueAttribute ) attr ).getValue();
+                
+                if ( isHr )
                 {
-                    serverEntry.add( attr.getName(), String.valueOf( attr.getValue() ) );
+                    serverEntry.add( attr.getName(), String.valueOf( value ) );
                 }
                 else
                 {
-                    Iterator values = attr.getValues();
-                    if ( values != null )
+                    // assuming all non-HR values as byte[]
+                    serverEntry.add( attr.getName(), ( byte[] ) value );
+                }
+            }
+            else
+            {
+                Iterator values = ( ( MultiValueAttribute ) attr ).getValues();
+                if ( values != null )
+                {
+                    while ( values.hasNext() )
                     {
-                        while ( values.hasNext() )
+                        Object v = values.next();
+                        if( isHr )
                         {
-                            Object v = values.next();
                             serverEntry.add( attr.getName(), String.valueOf( v ) );
                         }
+                        else
+                        {
+                         // assuming all non-HR values as byte[]
+                            serverEntry.add( attr.getName(), ( byte[] ) v );
+                        }
                     }
                 }
             }
-            else
-            {
-                // assuming all non-HR values as byte[]
-                serverEntry.add( attr.getName(), ( byte[] ) attr.getValue() );
-            }
         }
     }
 
@@ -281,7 +298,9 @@
         Set<Entry> additionalEntires = new HashSet<Entry>();
 
         Entry parentEntry = null;
-
+        
+        ObjectClass parentOc = null;
+        
         for ( String s : ocNameList )
         {
             ObjectClass oc = registries.getObjectClassRegistry().lookup( s );
@@ -294,6 +313,11 @@
             {
                 parentEntry = entry;
                 parentEntry.setDN( serverEntry.getDn().getUpName() );
+                parentOc = oc;
+            }
+            else
+            {
+                additionalEntires.add( entry );
             }
 
             // should be set last to avoid any interception during initialization
@@ -302,7 +326,20 @@
 
         for ( Entry e : additionalEntires )
         {
-            parentEntry.addEntry( e );
+            boolean isSuperior = false;
+            for( ObjectClass oc : parentOc.getSuperClasses() )
+            {
+                if( e.getName().equalsIgnoreCase( oc.getName() ) )
+                {
+                    isSuperior = true;
+                    break;
+                }
+            }
+
+            if( !isSuperior )
+            {
+                parentEntry.addEntry( e );
+            }
         }
 
         return parentEntry;
@@ -340,33 +377,34 @@
                 olmAttr = ( Attribute ) Class.forName( f.getType().getName() ).newInstance();
             }
 
-            if ( attr.isHR() )
-            {
-                if ( !registries.getAttributeTypeRegistry().lookup( attr.getId() ).isSingleValue()
)
-                {
-                    Iterator<Value<?>> values = attr.getAll();
-
-                    Set set = new HashSet();
-                    while ( values.hasNext() )
-                    {
-                        set.add( values.next().get() );
-                    }
+            boolean isSingleValue = registries.getAttributeTypeRegistry().lookup( attr.getId()
).isSingleValue();
 
-                    // java.util.Set is the common container for all the multi valued attribute
values
-                    olmAttr.getClass().getDeclaredMethod( "setValues", Set.class ).invoke(
olmAttr, set );
-                }
-                else
+            if ( !isSingleValue )
+            {
+                Iterator<Value<?>> values = attr.getAll();
+                
+                Set set = new HashSet();
+                while ( values.hasNext() )
                 {
-                    olmAttr.getClass().getDeclaredMethod( "setValue", olmAttr.getJavaType()
).invoke( olmAttr,
-                        convertToCorrectType( attr.get().get(), olmAttr.getJavaType() ) );
+                    set.add( values.next().get() );
                 }
-
+                
+                // java.util.Set is the common container for all the multi valued attribute
values
+                olmAttr.getClass().getDeclaredMethod( "setValues", Set.class ).invoke( olmAttr,
set );
             }
             else
             {
                 olmAttr.getClass().getDeclaredMethod( "setValue", olmAttr.getJavaType() ).invoke(
olmAttr,
-                    attr.get().get() );
+                    convertToCorrectType( attr.get().get(), olmAttr.getJavaType() ) );
             }
+//            if ( attr.isHR() )
+//            {
+//            }
+//            else
+//            {
+//                olmAttr.getClass().getDeclaredMethod( "setValue", olmAttr.getJavaType()
).invoke( olmAttr,
+//                    attr.get().get() );
+//            }
 
             ( ( InterceptFieldEnabled ) olmAttr ).setInterceptFieldCallback( new AttributeFieldInterceptor(
entry ) );
             f.set( entry, olmAttr );
@@ -434,14 +472,14 @@
 
     /**
      * 
-     * adds an Attribute to an exisitng entry.
+     * adds an Attribute with the given name and value(s) to an existing entry associated
with the given DN.
      *
      * @param dn the DN of existing entry
      * @param attributeName name of the attribute to be added
      * @param values one or more values of the attribute to be added
      * @throws Exception if the attribute cannot be added
      */
-    public void addAttributeToEntry( LdapDN dn, String attributeName, Object... values )
throws Exception
+    public void addAttributeValue( LdapDN dn, String attributeName, Object... values ) throws
Exception
     {
         List<Modification> modList = new ArrayList<Modification>( 1 );
 
@@ -475,46 +513,76 @@
 
     /**
      * 
-     * replaces an attributes value Or removes an attribute from the entry associated with
the given dn.
+     * replaces an Attribute's value present in the entry associated with the given dn.
      *
      * @param dn the DN of the entry to be modified
      * @param attributeName the attribute's name
-     * @param obj value to be replaced, if it is null the attribute will be completely removed
from entry 
+     * @param obj value to be replaced
      * @throws Exception if there are problems in modifying the entry
      */
-    public void replaceOrRemove( LdapDN dn, String attributeName, Object obj ) throws Exception
+    public void replaceAttributeValue( LdapDN dn, String attributeName, Object obj ) throws
Exception
     {
         List<Modification> modList = new ArrayList<Modification>();
 
         Modification mod = new ServerModification();
         EntryAttribute attr;
 
-        if ( obj == null )
+        mod.setOperation( ModificationOperation.REPLACE_ATTRIBUTE );
+        
+        if ( obj instanceof byte[] )
         {
-            mod.setOperation( ModificationOperation.REMOVE_ATTRIBUTE );
-            attr = new DefaultServerAttribute( attrRegistry.lookup( attributeName ) );
+            attr = new DefaultServerAttribute( attrRegistry.lookup( attributeName ), ( byte[]
) obj );
         }
         else
         {
-            mod.setOperation( ModificationOperation.REPLACE_ATTRIBUTE );
+            attr = new DefaultServerAttribute( attrRegistry.lookup( attributeName ), String.valueOf(
obj ) );
+        }
 
-            if ( obj instanceof byte[] )
+        mod.setAttribute( attr );
+        modList.add( mod );
+
+        session.modify( dn, modList );
+    }
+
+
+    /**
+     * 
+     * removes an Attribute's value from an entry associated with the given DN. If the value
is null
+     * the attribute will be completely removed from the entry associated with the given
DN.  
+     *
+     * @param dn the DN of the entry to be modified
+     * @param attributeName name of the attribute
+     * @param obj value to be removed, if it is null the attribute will be completely removed
from entry 
+     * @throws Exception
+     */
+    public void removeAttributeValue( LdapDN dn, String attributeName, Object obj ) throws
Exception
+    {
+        List<Modification> modList = new ArrayList<Modification>();
+
+
+        EntryAttribute attr = new DefaultServerAttribute( attrRegistry.lookup( attributeName
) );
+
+        if ( obj != null )
+        {
+            if( obj instanceof byte[] )
             {
-                attr = new DefaultServerAttribute( attrRegistry.lookup( attributeName ),
( byte[] ) obj );
+                attr.add( ( byte[] ) obj );
             }
             else
             {
-                attr = new DefaultServerAttribute( attrRegistry.lookup( attributeName ),
String.valueOf( obj ) );
+                attr.add( String.valueOf( obj ) );
             }
         }
 
+        Modification mod = new ServerModification();
+        mod.setOperation( ModificationOperation.REMOVE_ATTRIBUTE );
         mod.setAttribute( attr );
         modList.add( mod );
-
+        
         session.modify( dn, modList );
     }
-
-
+    
+    
     /**
      * 
      * searches the server with the given filter.
@@ -540,6 +608,35 @@
     }
 
 
+    /**
+     * 
+     * @see CoreSession#search(LdapDN, SearchScope, ExprNode, AliasDerefMode, Set)
+     *
+     * @param dn
+     * @param scope
+     * @param filter
+     * @param aliasDerefMode
+     * @param returningAttributes
+     * @return
+     * @throws Exception
+     */
+    public List<Entry> search( LdapDN dn, SearchScope scope, ExprNode filter, AliasDerefMode
aliasDerefMode,
+        Set<AttributeTypeOptions> returningAttributes ) throws Exception
+    {
+        List<Entry> entries = new ArrayList<Entry>();
+
+        EntryFilteringCursor cursor = session.search( dn, scope, filter, aliasDerefMode,
returningAttributes );
+
+        while ( cursor.next() )
+        {
+            ServerEntry serverEntry = cursor.get().getOriginalEntry();
+            entries.add( convertToOlmEntry( serverEntry ) );
+        }
+
+        return entries;
+    }
+    
+    
     public void modifyDN( LdapDN dn, LdapDN newParent ) throws Exception
     {
         session.move( dn, newParent );
@@ -642,11 +739,11 @@
         {
             if ( attr.isSingleValue() )
             {
-                entryAttr.add( String.valueOf( attr.getValue() ) );
+                entryAttr.add( String.valueOf( ( ( SingleValueAttribute ) attr ).getValue()
) );
             }
             else
             {
-                Iterator itr = attr.getValues();
+                Iterator itr = ( ( MultiValueAttribute ) attr ).getValues();
                 while ( itr.hasNext() )
                 {
                     entryAttr.add( String.valueOf( itr.next() ) );
@@ -655,7 +752,7 @@
         }
         else
         {
-            entryAttr.add( ( byte[] ) attr.getValue() );
+            entryAttr.add( ( byte[] ) ( ( SingleValueAttribute ) attr ).getValue() );
         }
 
         return entryAttr;
@@ -708,7 +805,11 @@
         {
             return Short.valueOf( value.toString() );
         }
-
+        else if ( claz == byte[].class )
+        {
+            return ( byte[] ) value;
+        }
+        
         // this will help during early development to figure out the missing pieces 
         throw new IllegalArgumentException( "Invalid class type " + claz + " for the attribute
value " + value );
     }



Mime
View raw message