You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2011/03/28 08:04:22 UTC

svn commit: r1086132 - in /directory/shared/trunk: i18n/src/main/resources/org/apache/directory/shared/i18n/ ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/

Author: elecharny
Date: Mon Mar 28 06:04:22 2011
New Revision: 1086132

URL: http://svn.apache.org/viewvc?rev=1086132&view=rev
Log:
Cleaned up the Value classes :
o Updated the Javadoc
o Removed some useless methods (setNormalized, normalize)
o Removed useless fields (normalized, valid, same)
o Other minor refactoring

Modified:
    directory/shared/trunk/i18n/src/main/resources/org/apache/directory/shared/i18n/errors.properties
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/AbstractValue.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/BinaryValue.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/StringValue.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/Value.java

Modified: directory/shared/trunk/i18n/src/main/resources/org/apache/directory/shared/i18n/errors.properties
URL: http://svn.apache.org/viewvc/directory/shared/trunk/i18n/src/main/resources/org/apache/directory/shared/i18n/errors.properties?rev=1086132&r1=1086131&r2=1086132&view=diff
==============================================================================
--- directory/shared/trunk/i18n/src/main/resources/org/apache/directory/shared/i18n/errors.properties (original)
+++ directory/shared/trunk/i18n/src/main/resources/org/apache/directory/shared/i18n/errors.properties Mon Mar 28 06:04:22 2011
@@ -595,7 +595,7 @@ ERR_04469=Cannot use standard serializat
 ERR_04470=Cannot read the attribute as it's OID (''{0}'') does not exist
 ERR_04471=Cannot serialize a Modification with no attribute
 ERR_04472=The attribute ''{0}'' is incorrect
-ERR_04473_NOT_VALID_VALUE=Not a valid value
+ERR_04473_NOT_VALID_VALUE=Not a valid value ''{0}'' for the AttributeType ''{1}''
 ERR_04474=Expected string to normalize
 ERR_04475=Expected byte[] to normalize
 ERR_04476=Cannot set an AttributeType {0} when another one ({1}) is already present

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/AbstractValue.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/AbstractValue.java?rev=1086132&r1=1086131&r2=1086132&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/AbstractValue.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/AbstractValue.java Mon Mar 28 06:04:22 2011
@@ -25,6 +25,7 @@ import org.apache.directory.shared.ldap.
 import org.apache.directory.shared.ldap.model.message.ResultCodeEnum;
 import org.apache.directory.shared.ldap.model.schema.AttributeType;
 import org.apache.directory.shared.ldap.model.schema.LdapComparator;
+import org.apache.directory.shared.ldap.model.schema.LdapSyntax;
 import org.apache.directory.shared.ldap.model.schema.MatchingRule;
 import org.apache.directory.shared.ldap.model.schema.Normalizer;
 import org.apache.directory.shared.ldap.model.schema.SyntaxChecker;
@@ -51,15 +52,6 @@ public abstract class AbstractValue<T> i
     /** the canonical representation of the wrapped value */
     protected T normalizedValue;
 
-    /** A flag set when the value has been normalized */
-    //protected boolean normalized;
-
-    /** cached results of the isValid() method call */
-    protected Boolean valid;
-
-    /** A flag set if the normalized data is different from the wrapped data */
-    protected boolean same;
-    
     /** The computed hashcode. We don't want to compute it each time the hashcode() method is called */
     protected volatile int h;
     
@@ -106,23 +98,27 @@ public abstract class AbstractValue<T> i
     {
         if ( attributeType == null )
         {
+            // No attributeType : the normalized value and the user provided value are the same
             normalizedValue = wrappedValue;
             return;
         }
         
         this.attributeType = attributeType;
         
-        try
+        // We first have to normalize the value before we can check its syntax
+        // Get the Aequality matchingRule, if we have one
+        MatchingRule equality = attributeType.getEquality();
+        
+        if ( equality != null )
         {
-            MatchingRule equality = attributeType.getEquality();
+            // If we have an Equality MR, we *must* have a normalizer
+            Normalizer normalizer = equality.getNormalizer();
             
-            if ( equality != null )
+            if ( normalizer != null )
             {
-                Normalizer normalizer = equality.getNormalizer();
-                
-                if ( normalizer != null )
+                if ( wrappedValue != null )
                 {
-                    if ( wrappedValue != null )
+                    try
                     {
                         if ( isHR() )
                         {     
@@ -133,23 +129,42 @@ public abstract class AbstractValue<T> i
                             normalizedValue = (T)normalizer.normalize( new BinaryValue( (byte[])wrappedValue ) ).getNormReference();
                         }
                     }
+                    catch ( LdapException ne )
+                    {
+                        String message = I18n.err( I18n.ERR_04447_CANNOT_NORMALIZE_VALUE, ne.getLocalizedMessage() );
+                        LOG.info( message );
+                    }
                 }
             }
+            else
+            {
+                String message = "The '" + attributeType.getName() + "' AttributeType does not have" +
+                    " a normalizer";
+                LOG.error( message );
+                throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, message );
+            }
         }
-        catch ( LdapException ne )
+        else
         {
-            String message = I18n.err( I18n.ERR_04447_CANNOT_NORMALIZE_VALUE, ne.getLocalizedMessage() );
-            LOG.info( message );
+            // No MatchingRule, there is nothing we can do but make the normalized value
+            // to be a reference on the user provided value
+            normalizedValue = wrappedValue;
         }
         
-        // and checks that the value is syntaxically correct
+        // and checks that the value syntax is valid
         try
         {
-            if ( ! isValid( attributeType.getSyntax().getSyntaxChecker() ) )
+            LdapSyntax syntax = attributeType.getSyntax();
+            
+            if ( syntax != null )
             {
-                String message = I18n.err( I18n.ERR_04473_NOT_VALID_VALUE, wrappedValue );
-                LOG.info( message );
-                throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, message );
+                // Check the syntax
+                if ( ! isValid( syntax.getSyntaxChecker() ) )
+                {
+                    String message = I18n.err( I18n.ERR_04473_NOT_VALID_VALUE, wrappedValue, attributeType );
+                    LOG.info( message );
+                    throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, message );
+                }
             }
         }
         catch ( LdapException le )
@@ -159,6 +174,7 @@ public abstract class AbstractValue<T> i
             throw new LdapInvalidAttributeValueException( ResultCodeEnum.INVALID_ATTRIBUTE_SYNTAX, message );
         }
         
+        // Rehash the Value now
         h=0;
         hashCode();
     }
@@ -176,99 +192,26 @@ public abstract class AbstractValue<T> i
     {
         if ( attributeType != null )
         {
-            MatchingRule mr = getMatchingRule();
-    
-            if ( mr == null )
-            {
-                return null;
-            }
-    
-            return (LdapComparator<T>)mr.getLdapComparator();
-        }
-        else
-        {
-            return null;
-        }
-    }
-    
-    
-    /**
-     * Find a matchingRule to use for normalization and comparison.  If an equality
-     * matchingRule cannot be found it checks to see if other matchingRules are
-     * available: SUBSTR, and ORDERING.  If a matchingRule cannot be found null is
-     * returned.
-     *
-     * @return a matchingRule or null if one cannot be found for the attributeType
-     * @throws LdapException if resolution of schema entities fail
-     */
-    protected final MatchingRule getMatchingRule() throws LdapException
-    {
-        if ( attributeType != null )
-        {
             MatchingRule mr = attributeType.getEquality();
     
-            if ( mr == null )
+            if ( mr != null )
             {
-                mr = attributeType.getOrdering();
+                return (LdapComparator<T>)mr.getLdapComparator();
             }
-    
-            if ( mr == null )
-            {
-                mr = attributeType.getSubstring();
-            }
-    
-            return mr;
-        }
-        else
-        {
-            return null;
         }
-    }
-
 
-    /**
-     * Gets a normalizer using getMatchingRule() to resolve the matchingRule
-     * that the normalizer is extracted from.
-     *
-     * @return a normalizer associated with the attributeType or null if one cannot be found
-     * @throws LdapException if resolution of schema entities fail
-     */
-    protected final Normalizer getNormalizer() throws LdapException
-    {
-        if ( attributeType != null )
-        {
-            MatchingRule mr = getMatchingRule();
-    
-            if ( mr == null )
-            {
-                return null;
-            }
-    
-            return mr.getNormalizer();
-        }
-        else
-        {
-            return null;
-        }
+        return null;
     }
 
     
     /**
      * {@inheritDoc}
      */
-    public boolean instanceOf( AttributeType attributeType ) throws LdapException
+    public boolean instanceOf( AttributeType attributeType )
     {
-        if ( ( attributeType != null ) && this.attributeType.equals( attributeType ) )
-        {
-            if ( this.attributeType.equals( attributeType ) )
-            {
-                return true;
-            }
-            
-            return this.attributeType.isDescendantOf( attributeType );
-        }
-
-        return false;
+        return ( attributeType != null ) && 
+               ( this.attributeType.equals( attributeType ) || 
+                 this.attributeType.isDescendantOf( attributeType ) ); 
     }
 
 
@@ -312,9 +255,7 @@ public abstract class AbstractValue<T> i
             throw new LdapException( message );
         }
         
-        valid = syntaxChecker.isValidSyntax( normalizedValue );
-        
-        return valid;
+        return syntaxChecker.isValidSyntax( normalizedValue );
     }
 
 

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/BinaryValue.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/BinaryValue.java?rev=1086132&r1=1086131&r2=1086132&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/BinaryValue.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/BinaryValue.java Mon Mar 28 06:04:22 2011
@@ -53,7 +53,6 @@ public class BinaryValue extends Abstrac
     public BinaryValue()
     {
         wrappedValue = null;
-        valid = null;
         normalizedValue = null;
     }
 
@@ -67,6 +66,7 @@ public class BinaryValue extends Abstrac
     {
         if ( attributeType != null )
         {
+            // We must have a Syntax
             if ( attributeType.getSyntax() == null )
             {
                 throw new IllegalArgumentException( I18n.err( I18n.ERR_04445 ) );
@@ -101,8 +101,6 @@ public class BinaryValue extends Abstrac
             this.wrappedValue = null;
             this.normalizedValue = null;
         }
-
-        valid = null;
     }
 
 
@@ -128,7 +126,6 @@ public class BinaryValue extends Abstrac
      * determine how to properly normalize the wrapped value.
      *
      * @return the normalized version of the wrapped value
-     * @throws org.apache.directory.shared.ldap.model.exception.LdapException if schema entity resolution fails or normalization fails
      */
     public byte[] getNormValue()
     {
@@ -144,31 +141,8 @@ public class BinaryValue extends Abstrac
 
 
     /**
-     * Gets the normalized (canonical) representation for the wrapped string.
-     * If the wrapped String is null, null is returned, otherwise the normalized
-     * form is returned.  If no the normalizedValue is null, then this method
-     * will attempt to generate it from the wrapped value: repeated calls to
-     * this method do not unnecessarily normalize the wrapped value.  Only changes
-     * to the wrapped value result in attempts to normalize the wrapped value.
-     *
-     * @return a reference to the normalized version of the wrapped value
-     */
-    public byte[] getNormalizedValueReference()
-    {
-        if ( isNull() )
-        {
-            return null;
-        }
-
-        return normalizedValue;
-    }
-
-
-    /**
      *
      * @see ServerValue#compareTo(Value)
-     * @throws IllegalStateException on failures to extract the comparator, or the
-     * normalizers needed to perform the required comparisons based on the schema
      */
     public int compareTo( Value<byte[]> value )
     {
@@ -202,12 +176,12 @@ public class BinaryValue extends Abstrac
                 if ( comparator != null )
                 {
                     return comparator
-                        .compare( getNormalizedValueReference(), binaryValue.getNormalizedValueReference() );
+                        .compare( getNormReference(), binaryValue.getNormReference() );
                 }
                 else
                 {
-                    return new ByteArrayComparator( null ).compare( getNormalizedValueReference(), binaryValue
-                        .getNormalizedValueReference() );
+                    return new ByteArrayComparator( null ).compare( getNormReference(), binaryValue
+                        .getNormReference() );
                 }
             }
             catch ( LdapException e )
@@ -242,7 +216,7 @@ public class BinaryValue extends Abstrac
                 return 0;
             }
 
-            byte[] normalizedValue = getNormalizedValueReference();
+            byte[] normalizedValue = getNormReference();
             h = Arrays.hashCode( normalizedValue );
         }
 
@@ -255,8 +229,6 @@ public class BinaryValue extends Abstrac
      *
      * This equals implementation overrides the BinaryValue implementation which
      * is not schema aware.
-     * @throws IllegalStateException on failures to extract the comparator, or the
-     * normalizers needed to perform the required comparisons based on the schema
      */
     public boolean equals( Object obj )
     {
@@ -315,11 +287,11 @@ public class BinaryValue extends Abstrac
                 // Compare normalized values
                 if ( comparator == null )
                 {
-                    return Arrays.equals( getNormalizedValueReference(), other.getNormalizedValueReference() );
+                    return Arrays.equals( getNormReference(), other.getNormReference() );
                 }
                 else
                 {
-                    return comparator.compare( getNormalizedValueReference(), other.getNormalizedValueReference() ) == 0;
+                    return comparator.compare( getNormReference(), other.getNormReference() ) == 0;
                 }
             }
             catch ( LdapException ne )
@@ -331,21 +303,22 @@ public class BinaryValue extends Abstrac
         else
         {
             // now unlike regular values we have to compare the normalized values
-            return Arrays.equals( getNormalizedValueReference(), other.getNormalizedValueReference() );
+            return Arrays.equals( getNormReference(), other.getNormReference() );
         }
     }
 
 
     // -----------------------------------------------------------------------
-    // Private Helper Methods (might be put into abstract base class)
+    // Cloneable methods
     // -----------------------------------------------------------------------
     /**
-     * @return a copy of the current value
+     * {@inheritDoc}
      */
     public BinaryValue clone()
     {
         BinaryValue clone = ( BinaryValue ) super.clone();
 
+        // We have to copy the byte[], they are just referenced by suoer.clone()
         if ( normalizedValue != null )
         {
             clone.normalizedValue = new byte[normalizedValue.length];

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/StringValue.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/StringValue.java?rev=1086132&r1=1086131&r2=1086132&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/StringValue.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/StringValue.java Mon Mar 28 06:04:22 2011
@@ -54,19 +54,19 @@ public class StringValue extends Abstrac
      */
     public StringValue()
     {
-        valid = null;
     }
 
 
     /**
      * Creates a StringValue without an initial wrapped value.
      *
-     * @param attributeType the schema type associated with this StringValue
+     * @param attributeType the schema attribute type associated with this StringValue
      */
     public StringValue( AttributeType attributeType )
     {
         if ( attributeType != null )
         {
+            // We must have a Syntax
             if ( attributeType.getSyntax() == null )
             {
                 throw new IllegalArgumentException( I18n.err( I18n.ERR_04445 ) );
@@ -92,15 +92,14 @@ public class StringValue extends Abstrac
     {
         this.wrappedValue = value;
         this.normalizedValue = value;
-        valid = null;
     }
 
 
     /**
-     * Creates a StringValue with an initial wrapped String value.
+     * Creates a schema aware StringValue with an initial wrapped String value.
      *
      * @param attributeType the schema type associated with this StringValue
-     * @param value the value to wrap which can be null
+     * @param value the value to wrap
      * @throws LdapInvalidAttributeValueException If the added value is invalid accordingly 
      * to the schema
      */
@@ -115,9 +114,7 @@ public class StringValue extends Abstrac
     // Value<String> Methods
     // -----------------------------------------------------------------------
     /**
-     * Get a copy of the stored value.
-     *
-     * @return A copy of the stored value.
+     * {@inheritDoc}
      */
     public String get()
     {
@@ -128,22 +125,10 @@ public class StringValue extends Abstrac
     
     
     /**
-     * Gets the normalized (canonical) representation for the wrapped string.
-     * If the wrapped String is null, null is returned, otherwise the normalized
-     * form is returned.  If the normalizedValue is null, then this method
-     * will attempt to generate it from the wrapped value: repeated calls to
-     * this method do not unnecessarily normalize the wrapped value.  Only changes
-     * to the wrapped value result in attempts to normalize the wrapped value.
-     *
-     * @return gets the normalized value
+     * {@inheritDoc}
      */
     public String getNormValue()
     {
-        if ( isNull() )
-        {
-            return null;
-        }
-
         return normalizedValue;
     }
     
@@ -221,9 +206,7 @@ public class StringValue extends Abstrac
     // Cloneable methods
     // -----------------------------------------------------------------------
     /**
-     * Get a clone of the Client Value
-     * 
-     * @return a copy of the current value
+     * {@inheritDoc}
      */
     public StringValue clone()
     {
@@ -396,7 +379,7 @@ public class StringValue extends Abstrac
      */
     public byte[] getBytes()
     {
-        return Strings.getBytesUtf8(wrappedValue);
+        return Strings.getBytesUtf8( wrappedValue );
     }
     
     

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/Value.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/Value.java?rev=1086132&r1=1086131&r2=1086132&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/Value.java (original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/shared/ldap/model/entry/Value.java Mon Mar 28 06:04:22 2011
@@ -85,7 +85,7 @@ public interface Value<T> extends Clonea
      * @return <code>true</code> if the value is associated with the given
      * attributeType or one of its ascendant
      */
-    boolean instanceOf( AttributeType attributeType ) throws LdapException;
+    boolean instanceOf( AttributeType attributeType );
 
     
     /**
@@ -126,19 +126,16 @@ public interface Value<T> extends Clonea
     
     
     /**
-     * Tells if the value has already be normalized or not.
+     * Tells if the value is schema aware or not.
      *
-     * @return <code>true</code> if the value has already been normalized.
+     * @return <code>true</code> if the value is sxhema aware
      */
     boolean isSchemaAware();
     
     
     /**
      * Uses the syntaxChecker associated with the attributeType to check if the
-     * value is valid.  Repeated calls to this method do not attempt to re-check
-     * the syntax of the wrapped value every time if the wrapped value does not
-     * change. Syntax checks only result on the first check, and when the wrapped
-     * value changes.
+     * value is valid.
      * 
      * @param checker the SyntaxChecker to use to validate the value
      * @return <code>true</code> if the value is valid
@@ -148,20 +145,10 @@ public interface Value<T> extends Clonea
 
     
     /**
-     * Set the normalized flag.
-     * 
-     * @param normalized the value : true or false
-     *
-    void setNormalized( boolean normalized );
-
-    
-    /**
      * Gets the normalized (canonical) representation for the wrapped string.
      * If the wrapped String is null, null is returned, otherwise the normalized
      * form is returned.  If the normalizedValue is null, then this method
-     * will attempt to generate it from the wrapped value: repeated calls to
-     * this method do not unnecessarily normalize the wrapped value.  Only changes
-     * to the wrapped value result in attempts to normalize the wrapped value.
+     * will attempt to generate it from the wrapped value.
      *
      * @return gets the normalized value
      */
@@ -178,30 +165,9 @@ public interface Value<T> extends Clonea
 
     
     /**
-     * Normalize the value. In order to use this method, the Value
-     * must be schema aware.
-     * 
-     * @exception LdapException if the value cannot be normalized
-     *
-    void normalize() throws LdapException;
-
-    
-    /**
-     * Normalize the value. For a client String value, applies the given normalizer.
-     * 
-     * It supposes that the client has access to the schema in order to select the
-     * appropriate normalizer.
-     * 
-     * @param normalizer the normalizer to apply to the value
-     * @exception LdapException if the value cannot be normalized
-     *
-    void normalize( Normalizer normalizer ) throws LdapException;
-    
-    
-    /**
      * Tells if the current value is Human Readable
      * 
-     * @return <code>true</code> if the value is Binary, <code>false</code> otherwise
+     * @return <code>true</code> if the value is a String, <code>false</code> otherwise
      */
     boolean isHR();