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 2008/03/15 01:56:28 UTC
svn commit: r637318 [1/2] - in
/directory/apacheds/branches/bigbang/core-entry/src:
main/java/org/apache/directory/server/core/entry/
test/java/org/apache/directory/server/core/entry/
Author: elecharny
Date: Fri Mar 14 17:56:26 2008
New Revision: 637318
URL: http://svn.apache.org/viewvc?rev=637318&view=rev
Log:
o Added the DefaultServerAttribute class missing methods and tests
o Lot of tests added
o Removal of the useless AbstractServerAttribute class
Removed:
directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/AbstractServerAttribute.java
Modified:
directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/DefaultServerAttribute.java
directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/DefaultServerEntry.java
directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerAttribute.java
directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerEntrySerializer.java
directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerEntryUtils.java
directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerModification.java
directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerStringValue.java
directory/apacheds/branches/bigbang/core-entry/src/test/java/org/apache/directory/server/core/entry/DefaultServerAttributeTest.java
directory/apacheds/branches/bigbang/core-entry/src/test/java/org/apache/directory/server/core/entry/DefaultServerEntryTest.java
Modified: directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/DefaultServerAttribute.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/DefaultServerAttribute.java?rev=637318&r1=637317&r2=637318&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/DefaultServerAttribute.java (original)
+++ directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/DefaultServerAttribute.java Fri Mar 14 17:56:26 2008
@@ -19,10 +19,12 @@
package org.apache.directory.server.core.entry;
-import org.apache.directory.shared.ldap.entry.AbstractValue;
+import org.apache.directory.shared.asn1.primitives.OID;
import org.apache.directory.shared.ldap.entry.EntryAttribute;
import org.apache.directory.shared.ldap.entry.Value;
import org.apache.directory.shared.ldap.entry.client.ClientBinaryValue;
+import org.apache.directory.shared.ldap.entry.client.ClientStringValue;
+import org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute;
import org.apache.directory.shared.ldap.schema.AttributeType;
import org.apache.directory.shared.ldap.util.StringTools;
import org.slf4j.Logger;
@@ -31,8 +33,6 @@
import javax.naming.NamingException;
import javax.naming.directory.InvalidAttributeValueException;
-import java.util.Iterator;
-
/**
* A server side entry attribute aware of schema.
@@ -40,7 +40,7 @@
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
* @version $Rev$, $Date$
*/
-public final class DefaultServerAttribute extends AbstractServerAttribute
+public final class DefaultServerAttribute extends DefaultClientAttribute implements ServerAttribute
{
/** Used for serialization */
public static final long serialVersionUID = 2L;
@@ -48,6 +48,361 @@
/** logger for reporting errors that might not be handled properly upstream */
private static final Logger LOG = LoggerFactory.getLogger( DefaultServerAttribute.class );
+ /** The associated AttributeType */
+ protected transient AttributeType attributeType;
+
+
+ // -----------------------------------------------------------------------
+ // utility methods
+ // -----------------------------------------------------------------------
+ /**
+ * Check the attributeType member. It should not be null,
+ * and it should contains a syntax.
+ */
+ protected String getErrorMessage( AttributeType attributeType )
+ {
+ try
+ {
+ if ( attributeType == null )
+ {
+ return "The AttributeType parameter should not be null";
+ }
+
+ if ( attributeType.getSyntax() == null )
+ {
+ return "There is no Syntax associated with this attributeType";
+ }
+
+ return null;
+ }
+ catch ( NamingException ne )
+ {
+ return "This AttributeType is incorrect";
+ }
+ }
+
+
+ /**
+ * Private helper method used to set an UpId from an attributeType
+ */
+ private String getUpId( AttributeType attributeType )
+ {
+ String upId = attributeType.getName();
+
+ if ( upId == null )
+ {
+ upId = attributeType.getOid();
+ }
+
+ return upId;
+ }
+
+
+ /**
+ * <p>
+ * Check if the current attribute type is of the expected attributeType
+ * </p>
+ * <p>
+ * This method won't tell if the current attribute is a descendant of
+ * the attributeType. For instance, the "CN" serverAttribute will return
+ * false if we ask if it's an instance of "Name".
+ * </p>
+ *
+ * @param attributeId The AttributeType ID to check
+ * @return True if the current attribute is of the expected attributeType
+ * @throws InvalidAttributeValueException If there is no AttributeType
+ */
+ public boolean instanceOf( String attributeId ) throws InvalidAttributeValueException
+ {
+ String trimmedId = StringTools.trim( attributeId );
+
+ if ( StringTools.isEmpty( trimmedId ) )
+ {
+ return false;
+ }
+
+ String normId = StringTools.lowerCaseAscii( trimmedId );
+
+ for ( String name:attributeType.getNames() )
+ {
+ if ( normId.equalsIgnoreCase( name ) )
+ {
+ return true;
+ }
+ }
+
+ return normId.equalsIgnoreCase( attributeType.getOid() );
+ }
+
+
+ /**
+ * <p>
+ * Overload the {@link DefaultClientAttribute#setId(String)} method.
+ * </p>
+ * <p>
+ * As the attributeType has already been set, we have to be sure that the
+ * argument is compatible with the attributeType's name.
+ * </p>
+ * <p>
+ * If the given ID is not compatible with the attributeType's possible
+ * names, the previously loaded ID will be kept.
+ * </p>
+ *
+ * @param upId The attribute ID
+ * @param attributeType The associated attributeType
+ */
+ public void setId( String id )
+ {
+ if ( !StringTools.isEmpty( StringTools.trim( id ) ) )
+ {
+ if ( attributeType.getName() == null )
+ {
+ // If the name is null, then we may have to store an OID
+ if ( OID.isOID( id ) && attributeType.getOid().equals( id ) )
+ {
+ // Everything is fine, store the upId.
+ // This should not happen...
+ super.setId( id );
+ }
+ }
+ else
+ {
+ // We have at least one name. Check that the normalized upId
+ // is one of those names. Otherwise, the upId may be an OID too.
+ // In this case, it must be equals to the attributeType OID.
+ String normId = StringTools.lowerCaseAscii( StringTools.trim( id ) );
+
+ for ( String atName:attributeType.getNames() )
+ {
+ if ( atName.equalsIgnoreCase( normId ) )
+ {
+ // Found ! We can store the upId and get out
+ super.setId( normId );
+ return;
+ }
+ }
+
+ // Last case, the UpId is an OID
+ if ( OID.isOID( normId ) && attributeType.getOid().equals( normId ) )
+ {
+ // We have an OID : stores it
+ super.setUpId( normId );
+ }
+ else
+ {
+ // The id is incorrect : this is not allowed
+ throw new IllegalArgumentException( "The ID '" + id + "'is incompatible with the AttributeType's id '" + attributeType.getName() + "'" );
+ }
+ }
+ }
+ else
+ {
+ throw new IllegalArgumentException( "An ID cannnot be null, empty, or resolved to an emtpy" +
+ " value when trimmed" );
+ }
+ }
+
+
+ /**
+ * <p>
+ * Overload the {@link DefaultClientAttribute#setUpId(String)} method.
+ * </p>
+ * <p>
+ * As the attributeType has already been set, we have to be sure that the
+ * argument is compatible with the attributeType's name.
+ * </p>
+ * <p>
+ * If the given ID is not compatible with the attributeType's possible
+ * names, the previously loaded ID will be kept.
+ * </p>
+ *
+ * @param upId The attribute ID
+ * @param attributeType The associated attributeType
+ */
+ public void setUpId( String upId )
+ {
+ if ( !StringTools.isEmpty( StringTools.trim( upId ) ) )
+ {
+ if ( attributeType.getName() == null )
+ {
+ // If the name is null, then we may have to store an OID
+ if ( OID.isOID( upId ) && attributeType.getOid().equals( upId ) )
+ {
+ // Everything is fine, store the upId.
+ // This should not happen...
+ super.setUpId( upId );
+
+ }
+ }
+ else
+ {
+ // We have at least one name. Check that the normalized upId
+ // is one of those names. Otherwise, the upId may be an OID too.
+ // In this case, it must be equals to the attributeType OID.
+ String normUpId = StringTools.lowerCaseAscii( StringTools.trim( upId ) );
+
+ for ( String id:attributeType.getNames() )
+ {
+ if ( id.equalsIgnoreCase( normUpId ) )
+ {
+ // Found ! We can store the upId and get out
+ super.setUpId( upId );
+ return;
+ }
+ }
+
+ // Last case, the UpId is an OID
+ if ( OID.isOID( normUpId ) && attributeType.getOid().equals( normUpId ) )
+ {
+ // We have an OID : stores it
+ super.setUpId( upId );
+ }
+ }
+ }
+ }
+
+
+ /**
+ * <p>
+ * Set the user provided ID. If we have none, the upId is assigned
+ * the attributetype's name. If it does not have any name, we will
+ * use the OID.
+ * </p>
+ * <p>
+ * If we have an upId and an AttributeType, they must be compatible. :
+ * - if the upId is an OID, it must be the AttributeType's OID
+ * - otherwise, its normalized form must be equals to ones of
+ * the attributeType's names.
+ * </p>
+ * <p>
+ * In any case, the ATtributeType will be changed. The caller is responsible for
+ * the present values to be compatoble with the new AttributeType.
+ * </p>
+ *
+ * @param upId The attribute ID
+ * @param attributeType The associated attributeType
+ */
+ public void setUpId( String upId, AttributeType attributeType )
+ {
+ if ( StringTools.isEmpty( StringTools.trim( upId ) ) )
+ {
+ super.setUpId( getUpId( attributeType ) );
+ this.attributeType = attributeType;
+ }
+ else
+ {
+ String name = attributeType.getName();
+
+ if ( name == null )
+ {
+ // If the name is null, then we may have to store an OID
+ if ( OID.isOID( upId ) && attributeType.getOid().equals( upId ) )
+ {
+ // Everything is fine, store the upId.
+ super.setUpId( upId );
+ this.attributeType = attributeType;
+ }
+ else
+ {
+ // We have a difference or the upId is not a valid OID :
+ // we will use the attributeTypeOID in this case.
+ LOG.warn( "The upID ({}) is not an OID or is different from the AttributeType OID({})",
+ upId, attributeType.getOid() );
+ super.setUpId( attributeType.getOid() );
+ this.attributeType = attributeType;
+ }
+ }
+ else
+ {
+ // We have at least one name. Check that the normalized upId
+ // is one of those names. Otherwise, the upId may be an OID too.
+ // In this case, it must be equals to the attributeType OID.
+ String normUpId = StringTools.lowerCaseAscii( StringTools.trim( upId ) );
+
+ for ( String id:attributeType.getNames() )
+ {
+ if ( id.equalsIgnoreCase( normUpId ) )
+ {
+ // Found ! We can store the upId and get out
+ super.setUpId( upId );
+ this.attributeType = attributeType;
+ return;
+ }
+ }
+
+ // UpId was not found in names. It should be an OID, or if not, we
+ // will use the AttributeType name.
+ if ( OID.isOID( normUpId ) && attributeType.getOid().equals( normUpId ) )
+ {
+ // We have an OID : stores it
+ super.setUpId( upId );
+ this.attributeType = attributeType;
+ }
+ else
+ {
+ String message = "The upID (" + upId + ") is not an OID or is different from the AttributeType OID (" +
+ attributeType.getOid() + ")";
+ // Not a valid OID : use the AttributeTypes OID name instead
+ LOG.error( message );
+ throw new IllegalArgumentException( message );
+ }
+ }
+ }
+ }
+
+
+ /**
+ * <p>
+ * Set the attribute type associated with this ServerAttribute.
+ * </p>
+ * <p>
+ * The current attributeType will be replaced. It is the responsibility of
+ * the caller to insure that the existing values are compatible with the new
+ * AttributeType
+ * </p>
+ *
+ * @param the attributeType associated with this entry attribute
+ */
+ public void setAttributeType( AttributeType attributeType )
+ {
+ if ( attributeType == null )
+ {
+ throw new IllegalArgumentException( getErrorMessage( attributeType ) );
+ }
+
+ this.attributeType = attributeType;
+ setUpId( null, attributeType );
+
+ try
+ {
+ if ( attributeType.getSyntax().isHumanReadable() )
+ {
+ isHR = true;
+ }
+ else
+ {
+ isHR = false;
+ }
+ }
+ catch ( NamingException ne )
+ {
+ // If we have an exception while trying to get the Syntax for this attribute
+ // just set it as Binary
+ isHR = false;
+ }
+ }
+
+
+ /**
+ * Get the attribute type associated with this ServerAttribute.
+ *
+ * @return the attributeType associated with this entry attribute
+ */
+ public AttributeType getAttributeType()
+ {
+ return attributeType;
+ }
+
// maybe have some additional convenience constructors which take
// an initial value as a string or a byte[]
@@ -61,8 +416,7 @@
throw new IllegalArgumentException( getErrorMessage( attributeType ) );
}
- this.attributeType = attributeType;
- setUpId( null, attributeType );
+ setAttributeType( attributeType );
}
@@ -78,8 +432,8 @@
throw new IllegalArgumentException( message );
}
- this.attributeType = attributeType;
- setUpId( upId, attributeType );
+ setAttributeType( attributeType );
+ setUpId( upId );
}
@@ -94,7 +448,7 @@
* @param vals an initial set of values for this attribute
* @throws NamingException if there are problems creating the new attribute
*/
- public DefaultServerAttribute( AttributeType attributeType, Value<?>... vals ) throws NamingException
+ public DefaultServerAttribute( AttributeType attributeType, Value<?>... vals )
{
this( null, attributeType, vals );
}
@@ -114,74 +468,23 @@
* @param vals an initial set of values for this attribute
* @throws NamingException if there are problems creating the new attribute
*/
- public DefaultServerAttribute( String upId, AttributeType attributeType, Value<?>... vals ) throws NamingException
+ public DefaultServerAttribute( String upId, AttributeType attributeType, Value<?>... vals )
{
if ( attributeType == null )
{
throw new IllegalArgumentException( getErrorMessage( attributeType ) );
}
- this.attributeType = attributeType;
-
- // The value can be null, this is a valid value.
- if ( vals[0] == null )
- {
- if ( attributeType.getSyntax().isHumanReadable() )
- {
- add( new ServerStringValue( attributeType ) );
- }
- else
- {
- add( new ServerBinaryValue( attributeType ) );
- }
- }
- else
- {
- for ( Value<?> val:vals )
- {
- if ( val instanceof ServerStringValue )
- {
- ServerStringValue serverString = ( ServerStringValue ) val;
-
- if ( attributeType.equals( serverString.getAttributeType() ) )
- {
- add( val );
- }
- else
- {
- add( new ServerStringValue( attributeType, serverString.get() ) );
- }
- }
- else if ( val instanceof ServerBinaryValue )
- {
- ServerBinaryValue serverBinary = ( ServerBinaryValue ) val;
-
- if ( attributeType.equals( serverBinary.getAttributeType() ) )
- {
- add( val );
- }
- else
- {
- add( new ServerBinaryValue( attributeType, serverBinary.getCopy() ) );
- }
- }
- else
- {
- String message = "Unknown value type: " + val.getClass().getName();
- LOG.error( message );
- throw new IllegalStateException( message );
- }
- }
- }
-
+ setAttributeType( attributeType );
setUpId( upId, attributeType );
+ add( vals );
}
/**
* Create a new instance of a EntryAttribute, without ID but with some values.
*/
- public DefaultServerAttribute( AttributeType attributeType, String... vals ) throws NamingException
+ public DefaultServerAttribute( AttributeType attributeType, String... vals )
{
this( null, attributeType, vals );
}
@@ -190,14 +493,14 @@
/**
* Create a new instance of a EntryAttribute.
*/
- public DefaultServerAttribute( String upId, AttributeType attributeType, String... vals ) throws NamingException
+ public DefaultServerAttribute( String upId, AttributeType attributeType, String... vals )
{
if ( attributeType == null )
{
throw new IllegalArgumentException( getErrorMessage( attributeType ) );
}
- this.attributeType = attributeType;
+ setAttributeType( attributeType );
add( vals );
setUpId( upId, attributeType );
}
@@ -206,7 +509,7 @@
/**
* Create a new instance of a EntryAttribute, with some byte[] values.
*/
- public DefaultServerAttribute( AttributeType attributeType, byte[]... vals ) throws NamingException
+ public DefaultServerAttribute( AttributeType attributeType, byte[]... vals )
{
this( null, attributeType, vals );
}
@@ -215,14 +518,14 @@
/**
* Create a new instance of a EntryAttribute, with some byte[] values.
*/
- public DefaultServerAttribute( String upId, AttributeType attributeType, byte[]... vals ) throws NamingException
+ public DefaultServerAttribute( String upId, AttributeType attributeType, byte[]... vals )
{
if ( attributeType == null )
{
throw new IllegalArgumentException( getErrorMessage( attributeType ) );
}
- this.attributeType = attributeType;
+ setAttributeType( attributeType );
add( vals );
setUpId( upId, attributeType );
}
@@ -244,42 +547,38 @@
/**
- * Gets the attribute type associated with this ServerAttribute.
- *
- * @return the attributeType associated with this entry attribute
- */
- public AttributeType getType()
- {
- return attributeType;
- }
-
-
- /**
- * Get's the user provided identifier for this entry. This is the value
- * that will be used as the identifier for the attribute within the
- * entry. If this is a commonName attribute for example and the user
- * provides "COMMONname" instead when adding the entry then this is
- * the format the user will have that entry returned by the directory
- * server. To do so we store this value as it was given and track it
- * in the attribute using this property.
- *
- * @return the user provided identifier for this attribute
+ * <p>
+ * Overload the ClientAttribte isHR method : we can't change this flag
+ * for a ServerAttribute, as the HR is already set using the AttributeType.
+ * Set the attribute to Human Readable or to Binary.
+ * </p>
+ *
+ * @param isHR <code>true</code> for a Human Readable attribute,
+ * <code>false</code> for a Binary attribute.
*/
- public String getUpId()
+ public void setHR( boolean isHR )
{
- return upId;
+ // Do nothing...
}
-
+
/**
+ * <p>
* Checks to see if this attribute is valid along with the values it contains.
- *
+ * </p>
+ * <p>
+ * An attribute is valid if :
+ * <li>All of its values are valid with respect to the attributeType's syntax checker</li>
+ * <li>If the attributeType is SINGLE-VALUE, then no more than a value should be present</li>
+ *</p>
* @return true if the attribute and it's values are valid, false otherwise
* @throws NamingException if there is a failure to check syntaxes of values
*/
public boolean isValid() throws NamingException
{
- if ( attributeType.isSingleValue() && values.size() > 1 )
+ // First check if the attribute has more than one value
+ // if the attribute is supposed to be SINGLE_VALUE
+ if ( attributeType.isSingleValue() && ( values.size() > 1 ) )
{
return false;
}
@@ -297,158 +596,201 @@
/**
- * @see EntryAttribute#add(org.apache.directory.shared.ldap.entry.Value)
+ * @see EntryAttribute#add(org.apache.directory.shared.ldap.entry.Value...)
*/
- public boolean add( Value<?> val ) throws InvalidAttributeValueException, NamingException
+ public int add( Value<?>... vals )
{
- if ( attributeType.getSyntax().isHumanReadable() )
+ int nbAdded = 0;
+
+ for ( Value<?> val:vals )
{
- if ( val == null )
+ try
{
- Value<String> nullSV = new ServerStringValue( attributeType, (String)null );
-
- if ( values.contains( nullSV ) )
+ if ( attributeType.getSyntax().isHumanReadable() )
{
- return false;
+ if ( val == null )
+ {
+ Value<String> nullSV = new ServerStringValue( attributeType, (String)null );
+
+ if ( !values.contains( nullSV ) )
+ {
+ values.add( nullSV );
+ nbAdded++;
+ }
+ }
+ else if ( val instanceof ServerStringValue )
+ {
+ if ( !values.contains( val ) )
+ {
+ if ( values.add( val ) )
+ {
+ nbAdded++;
+ }
+ }
+ }
+ else if ( val instanceof ClientStringValue )
+ {
+ // If we get a Client value, convert it to a Server value first
+ Value<String> serverStringValue = new ServerStringValue( attributeType, (String)val.get() );
+
+ if ( !values.contains( serverStringValue ) )
+ {
+ if ( values.add( serverStringValue ) )
+ {
+ nbAdded++;
+ }
+ }
+ }
+ else
+ {
+ String message = "The value must be a String, as its AttributeType is H/R";
+ LOG.error( message );
+ }
}
else
{
- values.add( nullSV );
- return true;
- }
- }
-
- if ( !( val instanceof ServerStringValue ) )
- {
- String message = "The value must be a String, as its AttributeType is H/R";
- LOG.error( message );
- throw new InvalidAttributeValueException( message );
- }
- }
- else
- {
- if ( val == null )
- {
- Value<byte[]> nullSV = new ServerBinaryValue( attributeType, (byte[])null );
-
- if ( values.contains( nullSV ) )
- {
- return false;
- }
- else
- {
- values.add( nullSV );
- return true;
+ if ( val == null )
+ {
+ Value<byte[]> nullSV = new ServerBinaryValue( attributeType, (byte[])null );
+
+ if ( !values.contains( nullSV ) )
+ {
+ values.add( nullSV );
+ nbAdded++;
+ }
+ }
+ else if ( ( val instanceof ClientBinaryValue ) )
+ {
+ Value<byte[]> serverBinaryValue = new ServerBinaryValue( attributeType, (byte[])val.get() );
+
+ if ( !values.contains( serverBinaryValue ) )
+ {
+ if ( values.add( serverBinaryValue ) )
+ {
+ nbAdded++;
+ }
+ }
+ }
+ else if ( val instanceof ServerBinaryValue )
+ {
+ if ( !values.contains( val ) )
+ {
+ if ( values.add( val ) )
+ {
+ nbAdded++;
+ }
+ }
+ }
+ else
+ {
+ String message = "The value must be a byte[], as its AttributeType is not H/R";
+ LOG.error( message );
+ }
}
}
-
- if ( !( val instanceof AbstractValue ) )
+ catch ( NamingException ne )
{
- String message = "The value must be a byte[], as its AttributeType is not H/R";
+ String message = "Error while adding value '" + val.toString() +"' : " + ne.getMessage();
LOG.error( message );
- throw new InvalidAttributeValueException( message );
}
}
- if ( values.contains( val ) )
- {
- return false;
- }
-
- return values.add( val );
+ return nbAdded;
}
/**
- * @see EntryAttribute#add(org.apache.directory.shared.ldap.entry.Value...)
+ * <p>
+ * Adds some values to this attribute. If the new values are already present in
+ * the attribute values, the method has no effect.
+ * </p>
+ * <p>
+ * The new values are added at the end of list of values.
+ * </p>
+ * <p>
+ * This method returns the number of values that were added.
+ * </p>
+ * If the value's type is different from the attribute's type,
+ * the value is not added.
+ *
+ * @param val some new values to be added which may be null
+ * @return the number of added values, or 0 if none has been added
*/
- public int add( Value<?>... vals ) throws InvalidAttributeValueException, NamingException
+ public int add( String... vals )
{
- int nbAdded = 0;
-
- for ( Value<?> val:vals )
+ if ( isHR )
{
- if ( add( val ) )
+ int nbAdded = 0;
+
+ for ( String val:vals )
{
- nbAdded ++;
+ if ( add( new ServerStringValue( attributeType, val ) ) != 0 )
+ {
+ nbAdded++;
+ }
+ else
+ {
+ LOG.error( "The value '" + val + "' is incorrect, it hasn't been added" );
+ }
}
- }
-
- return nbAdded;
- }
-
-
- /**
- * @see EntryAttribute#add(String)
- */
- public boolean add( String val ) throws InvalidAttributeValueException, NamingException
- {
- if ( attributeType.getSyntax().isHumanReadable() )
- {
- return add( new ServerStringValue( attributeType, val ) );
+
+ return nbAdded;
}
else
{
- String message = "The value must be a String, as its AttributeType is H/R";
- LOG.warn( message );
- return add( new ServerBinaryValue( attributeType, StringTools.getBytesUtf8( val ) ) );
- }
- }
-
-
- /**
- * @see EntryAttribute#add(String...)
- */
- public int add( String... vals ) throws NamingException
- {
- int nbAdded = 0;
-
- for ( String val:vals )
- {
- if ( add( val ) )
- {
- nbAdded ++;
- }
+ // We can't add String values into a Binary serverAttribute
+ return 0;
}
-
- return nbAdded;
}
/**
- * @see EntryAttribute#add(byte[])
+ * <p>
+ * Adds some values to this attribute. If the new values are already present in
+ * the attribute values, the method has no effect.
+ * </p>
+ * <p>
+ * The new values are added at the end of list of values.
+ * </p>
+ * <p>
+ * This method returns the number of values that were added.
+ * </p>
+ * <p>
+ * If the value's type is different from the attribute's type,
+ * the value is not added.
+ * </p>
+ * It's the responsibility of the caller to check if the stored
+ * values are consistent with the attribute's type.
+ * <p>
+ *
+ * @param val some new values to be added which may be null
+ * @return the number of added values, or 0 if none has been added
*/
- public boolean add( byte[] val ) throws InvalidAttributeValueException, NamingException
+ public int add( byte[]... vals )
{
- if ( ! attributeType.getSyntax().isHumanReadable() )
+ if ( !isHR )
{
- return add( new ServerBinaryValue( attributeType, val ) );
+ int nbAdded = 0;
+
+ for ( byte[] val:vals )
+ {
+ if ( add( new ServerBinaryValue( attributeType, val ) ) != 0 )
+ {
+ nbAdded++;
+ }
+ else
+ {
+ LOG.error( "The value '" + val + "' is incorrect, it hasn't been added" );
+ }
+ }
+
+ return nbAdded;
}
else
{
- String message = "The value must be a String, as its AttributeType is H/R";
- LOG.error( message );
- throw new InvalidAttributeValueException( message );
+ // We can't add Binary values into a String serverAttribute
+ return 0;
}
- }
-
-
- /**
- * @see EntryAttribute#add(byte[]...)
- */
- public int add( byte[]... vals ) throws InvalidAttributeValueException, NamingException
- {
- int nbAdded = 0;
-
- for ( byte[] val:vals )
- {
- if ( add( val ) )
- {
- nbAdded ++;
- }
- }
-
- return nbAdded;
}
@@ -463,231 +805,195 @@
/**
- * @see EntryAttribute#contains(org.apache.directory.shared.ldap.entry.Value)
- */
- public boolean contains( Value<?> val )
- {
- return values.contains( val );
- }
-
-
- /**
- * @see EntryAttribute#contains(org.apache.directory.shared.ldap.entry.Value...)
+ * <p>
+ * Indicates whether the specified values are some of the attribute's values.
+ * </p>
+ * <p>
+ * If the Attribute is HR, te metho will only accept String Values. Otherwise,
+ * it will only accept Binary values.
+ * </p>
+ *
+ * @param vals the values
+ * @return true if this attribute contains all the values, otherwise false
*/
public boolean contains( Value<?>... vals )
{
// Iterate through all the values, and quit if we
- // don't find one in the values
- for ( Value<?> val:vals )
+ // don't find one in the values. We have to separate the check
+ // depending on the isHR flag value.
+ if ( isHR )
{
- if ( !values.contains( val ) )
+ for ( Value<?> val:vals )
{
- return false;
+ if ( val instanceof ServerStringValue )
+ {
+ if ( !values.contains( val ) )
+ {
+ return false;
+ }
+ }
+ else if ( val instanceof ClientStringValue )
+ {
+ ServerStringValue serverValue = new ServerStringValue( attributeType, (String)val.get() );
+
+ if ( !values.contains( serverValue ) )
+ {
+ return false;
+ }
+ }
+ else
+ {
+ // Not a String value
+ return false;
+ }
}
}
-
- return true;
- }
-
-
- /**
- * @see EntryAttribute#contains(String)
- */
- public boolean contains( String val )
- {
- try
+ else
{
- if ( !attributeType.getSyntax().isHumanReadable() )
+ for ( Value<?> val:vals )
{
- return false;
+ if ( val instanceof ClientBinaryValue )
+ {
+ if ( !values.contains( val ) )
+ {
+ return false;
+ }
+ }
+ else
+ {
+ // Not a Binary value
+ return false;
+ }
}
}
- catch ( NamingException ne )
- {
- // We have had a pb while getting the syntax...
- return false;
- }
- ServerStringValue value = new ServerStringValue( attributeType, val );
-
- return values.contains( value );
+ return true;
}
/**
- * @see EntryAttribute#contains(String...)
+ * <p>
+ * Indicates whether all the specified values are attribute's values. If
+ * at least one value is not an attribute's value, this method will return
+ * <code>false</code>
+ * </p>
+ * <p>
+ * If the Attribute is not HR, this method will returns <code>false</code>
+ * </p>
+ *
+ * @param vals the values
+ * @return true if this attribute contains all the values, otherwise false
*/
public boolean contains( String... vals )
{
- // Iterate through all the values, and quit if we
- // don't find one in the values
- for ( String val:vals )
- {
- if ( !contains( val ) )
- {
- return false;
- }
- }
-
- return true;
- }
-
-
- /**
- * @see EntryAttribute#contains(byte[])
- */
- public boolean contains( byte[] val )
- {
- try
+ if ( isHR )
{
- if ( attributeType.getSyntax().isHumanReadable() )
+ // Iterate through all the values, and quit if we
+ // don't find one in the values
+ for ( String val:vals )
{
- return false;
+ ServerStringValue value = new ServerStringValue( attributeType, val );
+
+ if ( !values.contains( value ) )
+ {
+ return false;
+ }
}
+
+ return true;
}
- catch ( NamingException ne )
+ else
{
- // We have had a pb while getting the syntax...
return false;
}
-
- ServerBinaryValue sbv = new ServerBinaryValue( attributeType, val );
- return values.contains( sbv );
- }
-
-
- /**
- * @see EntryAttribute#contains(byte[]...)
- */
- public boolean contains( byte[]... vals )
- {
- // Iterate through all the values, and quit if we
- // don't find one in the values
- for ( byte[] val:vals )
- {
- if ( !contains( val ) )
- {
- return false;
- }
- }
-
- return true;
}
/**
- * Get the first value of this attribute. If there is none,
- * null is returned.
- *
- * Note : as we are storing values into a Set, one can't assume
- * the values to be ordered in any way. This method is meant to
- * be used if the attribute hold only one value.
- *
- * @return The first value for this attribute.
- */
- public Value<?> get()
- {
- if ( values.isEmpty() )
- {
- return null;
- }
-
- return values.iterator().next();
- }
-
-
- /**
- * Get the String value, if and only if the value is known to be a String,
- * otherwise a InvalidAttributeValueException will be thrown
+ * <p>
+ * Indicates whether all the specified values are attribute's values. If
+ * at least one value is not an attribute's value, this method will return
+ * <code>false</code>
+ * </p>
+ * <p>
+ * If the Attribute is HR, this method will returns <code>false</code>
+ * </p>
*
- * @return The value as a String
- * @throws InvalidAttributeValueException If the value is a byte[]
+ * @param vals the values
+ * @return true if this attribute contains all the values, otherwise false
*/
- public String getString() throws InvalidAttributeValueException
- {
- Value<?> value = get();
-
- if ( value instanceof ServerStringValue )
- {
- return (String)value.get();
- }
- else
- {
- String message = "The value is expected to be a String";
- LOG.error( message );
- throw new InvalidAttributeValueException( message );
- }
- }
-
-
- /**
- * Get the byte[] value, if and only if the value is known to be Binary,
- * otherwise a InvalidAttributeValueException will be thrown
- *
- * @return The value as a String
- * @throws InvalidAttributeValueException If the value is a String
- */
- public byte[] getBytes() throws InvalidAttributeValueException
+ public boolean contains( byte[]... vals )
{
- Value<?> value = get();
-
- if ( value instanceof ClientBinaryValue )
+ if ( !isHR )
{
- return (byte[])value.get();
+ // Iterate through all the values, and quit if we
+ // don't find one in the values
+ for ( byte[] val:vals )
+ {
+ ServerBinaryValue value = new ServerBinaryValue( attributeType, val );
+
+ if ( !values.contains( value ) )
+ {
+ return false;
+ }
+ }
+
+ return true;
}
else
{
- String message = "The value is expected to be a byte[]";
- LOG.error( message );
- throw new InvalidAttributeValueException( message );
+ return false;
}
}
-
-
- /**
- * Get all the stored values.
- *
- * @return An iterator over the values stored into the attribute
- */
- public Iterator<Value<?>> getAll()
- {
- return iterator();
- }
-
-
- /**
- * Get the number or values stored in the attribute
- *
- * @return the number of stored values. As 'null' can be a valid
- * value, it is counted as one result, not 0.
- */
- public int size()
- {
- return values.size();
- }
-
-
- /**
- * @see EntryAttribute#remove(org.apache.directory.shared.ldap.entry.Value)
- */
- public boolean remove( Value<?> val )
- {
- return values.remove( val );
- }
-
-
+
+
/**
* @see EntryAttribute#remove(org.apache.directory.shared.ldap.entry.Value...)
*/
public boolean remove( Value<?>... vals )
{
- boolean removed = false;
+ boolean removed = true;
// Loop through all the values to remove. If one of
// them is not present, the method will return false.
- for ( Value<?> val:vals )
+ // As the attribute may be HR or not, we have two separated treatments
+ if ( isHR )
+ {
+ for ( Value<?> val:vals )
+ {
+ if ( val instanceof ClientStringValue )
+ {
+ ServerStringValue ssv = new ServerStringValue( attributeType, (String)val.get() );
+ removed &= values.remove( ssv );
+ }
+ else if ( val instanceof ServerStringValue )
+ {
+ removed &= values.remove( val );
+ }
+ else
+ {
+ removed = false;
+ }
+ }
+ }
+ else
{
- removed &= values.remove( val );
+ for ( Value<?> val:vals )
+ {
+ if ( val instanceof ClientBinaryValue )
+ {
+ ServerBinaryValue sbv = new ServerBinaryValue( attributeType, (byte[])val.get() );
+ removed &= values.remove( sbv );
+ }
+ else if ( val instanceof ServerBinaryValue )
+ {
+ removed &= values.remove( val );
+ }
+ else
+ {
+ removed = false;
+ }
+ }
}
return removed;
@@ -695,20 +1001,15 @@
/**
- * @see EntryAttribute#remove(byte[])
- */
- public boolean remove( byte[] val )
- {
- ServerBinaryValue sbv = new ServerBinaryValue( attributeType, val );
- return values.remove( sbv );
- }
-
-
- /**
* @see EntryAttribute#remove(byte[]...)
*/
public boolean remove( byte[]... vals )
{
+ if ( isHR )
+ {
+ return false;
+ }
+
boolean removed = true;
for ( byte[] val:vals )
@@ -722,20 +1023,15 @@
/**
- * @see EntryAttribute#remove(String)
- */
- public boolean remove( String val )
- {
- ServerStringValue ssv = new ServerStringValue( attributeType, val );
- return values.remove( ssv );
- }
-
-
- /**
* @see EntryAttribute#remove(String...)
*/
public boolean remove( String... vals )
{
+ if ( !isHR )
+ {
+ return false;
+ }
+
boolean removed = true;
for ( String val:vals )
@@ -748,14 +1044,64 @@
}
+ //-------------------------------------------------------------------------
+ // Overloaded Object classes
+ //-------------------------------------------------------------------------
/**
- * An iterator on top of the stored values.
- *
- * @return an iterator over the stored values.
+ * The hashCode is based on the id, the isHR flag and
+ * on the internal values.
+ *
+ * @see Object#hashCode()
+ */
+ public int hashCode()
+ {
+ int h = super.hashCode();
+
+ if ( attributeType != null )
+ {
+ h = h*17 + attributeType.hashCode();
+ }
+
+ return h;
+ }
+
+
+ /**
+ * @see Object#equals(Object)
*/
- public Iterator<Value<?>> iterator()
+ public boolean equals( Object obj )
{
- return values.iterator();
+ if ( obj == this )
+ {
+ return true;
+ }
+
+ if ( ! (obj instanceof ServerAttribute ) )
+ {
+ return false;
+ }
+
+ ServerAttribute other = (ServerAttribute)obj;
+
+ if ( !attributeType.equals( other.getAttributeType() ) )
+ {
+ return false;
+ }
+
+ if ( values.size() != other.size() )
+ {
+ return false;
+ }
+
+ for ( Value<?> val:values )
+ {
+ if ( ! other.contains( val ) )
+ {
+ return false;
+ }
+ }
+
+ return true;
}
Modified: directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/DefaultServerEntry.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/DefaultServerEntry.java?rev=637318&r1=637317&r2=637318&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/DefaultServerEntry.java (original)
+++ directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/DefaultServerEntry.java Fri Mar 14 17:56:26 2008
@@ -359,16 +359,9 @@
public boolean hasObjectClass( String objectClass )
{
- try
- {
- ServerAttribute oc = serverAttributeMap.get( OBJECT_CLASS_AT );
-
- return oc.contains( objectClass );
- }
- catch ( NamingException ne )
- {
- return false;
- }
+ ServerAttribute oc = serverAttributeMap.get( OBJECT_CLASS_AT );
+
+ return oc.contains( objectClass );
}
public boolean isValid()
@@ -513,7 +506,7 @@
throw new IllegalArgumentException( message );
}
- ServerAttribute removed = serverAttributeMap.put( serverAttribute.getType(), serverAttribute );
+ ServerAttribute removed = serverAttributeMap.put( serverAttribute.getAttributeType(), serverAttribute );
if ( removed != null )
{
@@ -569,9 +562,9 @@
for ( ServerAttribute serverAttribute:serverAttributes )
{
- if ( serverAttributeMap.containsKey( serverAttribute.getType() ) )
+ if ( serverAttributeMap.containsKey( serverAttribute.getAttributeType() ) )
{
- serverAttributeMap.remove( serverAttribute.getType() );
+ serverAttributeMap.remove( serverAttribute.getAttributeType() );
removedAttributes.add( serverAttribute );
}
}
@@ -1531,7 +1524,7 @@
for ( ServerAttribute attribute:other )
{
- ServerAttribute attr = this.get( attribute.getType() );
+ ServerAttribute attr = this.get( attribute.getAttributeType() );
if ( attr == null )
{
@@ -1561,7 +1554,7 @@
{
for ( ServerAttribute attribute:serverAttributeMap.values() )
{
- if ( !attribute.getType().equals( OBJECT_CLASS_AT ) )
+ if ( !attribute.getAttributeType().equals( OBJECT_CLASS_AT ) )
{
sb.append( attribute );
}
Modified: directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerAttribute.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerAttribute.java?rev=637318&r1=637317&r2=637318&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerAttribute.java (original)
+++ directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerAttribute.java Fri Mar 14 17:56:26 2008
@@ -36,35 +36,79 @@
public interface ServerAttribute extends ClientAttribute
{
/**
- * Gets the attribute type associated with this ServerAttribute.
+ * Get the attribute type associated with this ServerAttribute.
*
* @return the attributeType associated with this entry attribute
*/
- AttributeType getType();
+ AttributeType getAttributeType();
/**
+ * <p>
+ * Set the attribute type associated with this ServerAttribute.
+ * </p>
+ * <p>
+ * The current attributeType will be replaced. It is the responsibility of
+ * the caller to insure that the existing values are compatible with the new
+ * AttributeType
+ * </p>
+ *
+ * @param the attributeType associated with this entry attribute
+ */
+ void setAttributeType( AttributeType attributeType );
+
+
+ /**
+ * <p>
+ * Check if the current attribute type is of the expected attributeType
+ * </p>
+ * <p>
+ * This method won't tell if the current attribute is a descendant of
+ * the attributeType. For instance, the "CN" serverAttribute will return
+ * false if we ask if it's an instance of "Name".
+ * </p>
+ *
+ * @param attributeId The AttributeType ID to check
+ * @return True if the current attribute is of the expected attributeType
+ * @throws InvalidAttributeValueException If there is no AttributeType
+ */
+ boolean instanceOf( String attributeId ) throws InvalidAttributeValueException;
+
+
+ /**
+ * <p>
* Set the user provided ID. If we have none, the upId is assigned
* the attributetype's name. If it does not have any name, we will
* use the OID.
+ * </p>
* <p>
* If we have an upId and an AttributeType, they must be compatible. :
* - if the upId is an OID, it must be the AttributeType's OID
* - otherwise, its normalized form must be equals to ones of
* the attributeType's names.
- *
+ * </p>
+ * <p>
+ * In any case, the ATtributeType will be changed. The caller is responsible for
+ * the present values to be compatoble with the new AttributeType.
+ * </p>
+ *
* @param upId The attribute ID
* @param attributeType The associated attributeType
*/
public void setUpId( String upId, AttributeType attributeType );
-
+
/**
- * Check if the current attribute type is of the expected attributeType
- *
- * @param attributeId The AttributeType ID to check
- * @return True if the current attribute is of the expected attributeType
- * @throws InvalidAttributeValueException If there is no AttributeType
+ * <p>
+ * Checks to see if this attribute is valid along with the values it contains.
+ * </p>
+ * <p>
+ * An attribute is valid if :
+ * <li>All of its values are valid with respect to the attributeType's syntax checker</li>
+ * <li>If the attributeType is SINGLE-VALUE, then no more than a value should be present</li>
+ *</p>
+ * @return true if the attribute and it's values are valid, false otherwise
+ * @throws NamingException if there is a failure to check syntaxes of values
*/
- boolean isA( String attributeId ) throws InvalidAttributeValueException;
+ boolean isValid() throws NamingException;
}
Modified: directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerEntrySerializer.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerEntrySerializer.java?rev=637318&r1=637317&r2=637318&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerEntrySerializer.java (original)
+++ directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerEntrySerializer.java Fri Mar 14 17:56:26 2008
@@ -101,7 +101,7 @@
for ( ServerAttribute attribute:entry )
{
// We store the OID, as the AttributeType might have no name
- out.writeUTF( attribute.getType().getOid() );
+ out.writeUTF( attribute.getAttributeType().getOid() );
// And store the attribute.
// Store the UP id
Modified: directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerEntryUtils.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerEntryUtils.java?rev=637318&r1=637317&r2=637318&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerEntryUtils.java (original)
+++ directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerEntryUtils.java Fri Mar 14 17:56:26 2008
@@ -128,17 +128,35 @@
{
Object value = values.nextElement();
- if ( value instanceof String )
+ if ( serverAttribute.isHR() )
{
- serverAttribute.add( (String)value );
- }
- else if ( value instanceof byte[] )
- {
- serverAttribute.add( (byte[])value );
+ if ( value instanceof String )
+ {
+ serverAttribute.add( (String)value );
+ }
+ else if ( value instanceof byte[] )
+ {
+ serverAttribute.add( StringTools.utf8ToString( (byte[])value ) );
+ }
+ else
+ {
+ return null;
+ }
}
else
{
- return null;
+ if ( value instanceof String )
+ {
+ serverAttribute.add( StringTools.getBytesUtf8( (String)value ) );
+ }
+ else if ( value instanceof byte[] )
+ {
+ serverAttribute.add( (byte[])value );
+ }
+ else
+ {
+ return null;
+ }
}
}
@@ -366,7 +384,7 @@
{
return (ServerAttribute)attr0.clone();
}
- else if ( !attr0.getType().equals( attr1.getType() ) )
+ else if ( !attr0.getAttributeType().equals( attr1.getAttributeType() ) )
{
throw new IllegalArgumentException( "Cannot take union of attributes with different IDs!" );
}
@@ -488,7 +506,7 @@
{
ServerAttribute attribute = (ServerAttribute)modification.getAttribute();
- if ( attribute.getType() == type )
+ if ( attribute.getAttributeType() == type )
{
return modification;
}
Modified: directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerModification.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerModification.java?rev=637318&r1=637317&r2=637318&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerModification.java (original)
+++ directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerModification.java Fri Mar 14 17:56:26 2008
@@ -104,7 +104,7 @@
/**
* @return the attribute containing the modifications
*/
- public EntryAttribute<?> getAttribute()
+ public EntryAttribute getAttribute()
{
return attribute;
}
@@ -115,7 +115,7 @@
*
* @param attribute The modified attribute
*/
- public void setAttribute( EntryAttribute<?> attribute )
+ public void setAttribute( EntryAttribute attribute )
{
this.attribute = (ServerAttribute)attribute;
}
Modified: directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerStringValue.java
URL: http://svn.apache.org/viewvc/directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerStringValue.java?rev=637318&r1=637317&r2=637318&view=diff
==============================================================================
--- directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerStringValue.java (original)
+++ directory/apacheds/branches/bigbang/core-entry/src/main/java/org/apache/directory/server/core/entry/ServerStringValue.java Fri Mar 14 17:56:26 2008
@@ -517,7 +517,13 @@
// If the normalized value is null, will default to wrapped
// which cannot be null at this point.
int h = 17;
- h = h*37 + getNormalizedValue().hashCode();
+
+ String normalized = getNormalizedValue();
+
+ if ( normalized != null )
+ {
+ h = h*37 + normalized.hashCode();
+ }
// Add the OID hashcode
h = h*37 + attributeType.getOid().hashCode();