Class DefaultObjectEncoder

  • All Implemented Interfaces:
    java.io.Serializable

    @NotMutable
    @ThreadSafety(level=COMPLETELY_THREADSAFE)
    public final class DefaultObjectEncoder
    extends ObjectEncoder
    This class provides the default implementation of an ObjectEncoder object that will be used when encoding and decoding fields to be written to or read from an LDAP directory server.

    The following basic types will be supported, with the following encodings:
    • Any kind of enumeration -- Encoded using the name of the enum value
    • java.util.concurrent.atomic.AtomicInteger -- Encoded using the string representation of the value
    • java.util.concurrent.atomic.AtomicLong -- Encoded using the string representation of the value
    • java.math.BigDecimal -- Encoded using the string representation of the value
    • java.math.BigInteger -- Encoded using the string representation of the value
    • boolean -- Encoded as either "TRUE" or "FALSE"
    • java.lang.Boolean -- Encoded as either "TRUE" or "FALSE"
    • byte[] -- Encoded as the raw bytes contained in the array
    • char[] -- Encoded as a string containing the characters in the array
    • java.util.Date -- Encoded using the generalized time syntax
    • com.unboundid.ldap.sdk.DN -- Encoded using the string representation of the value
    • double -- Encoded using the string representation of the value
    • java.lang.Double -- Encoded using the string representation of the value
    • com.unboundid.ldap.sdk.Filter -- Encoded using the string representation of the value
    • float -- Encoded using the string representation of the value
    • java.lang.Float -- Encoded using the string representation of the value
    • int -- Encoded using the string representation of the value
    • java.lang.Integer -- Encoded using the string representation of the value
    • com.unboundid.ldap.sdk.LDAPURL -- Encoded using the string representation of the value
    • long -- Encoded using the string representation of the value
    • java.lang.Long -- Encoded using the string representation of the value
    • com.unboundid.ldap.sdk.RDN -- Encoded using the string representation of the value
    • short -- Encoded using the string representation of the value
    • java.lang.Short -- Encoded using the string representation of the value
    • java.lang.String -- Encoded using the value
    • java.lang.StringBuffer -- Encoded using the string representation of the value
    • java.lang.StringBuilder -- Encoded using the string representation of the value
    • java.net.URI -- Encoded using the string representation of the value.
    • java.net.URL -- Encoded using the string representation of the value.
    • java.util.UUID -- Encoded using the string representation of the value
    Serializable objects are also supported, in which case the raw bytes that comprise the serialized representation will be used. This may be undesirable, because the value may only be interpretable by Java-based clients. If you wish to better control the encoding for serialized objects, have them implement custom writeObject, readObject, and readObjectNoData methods that use the desired encoding. Alternately, you may create a custom ObjectEncoder implementation for that object type, or use getter/setter methods that convert between string/byte[] representations and the desired object types.

    In addition, arrays of all of the above types are also supported, in which case each element of the array will be a separate value in the corresponding LDAP attribute. Lists (including ArrayList, LinkedList, and CopyOnWriteArrayList) and sets (including HashSet, LinkedHashSet, TreeSet, and CopyOnWriteArraySet) of the above types are also supported.

    Note that you should be careful when using primitive types, since they cannot be unassigned and therefore will always have a value. When using an LDAP entry to initialize an object any fields with primitive types which are associated with LDAP attributes not present in the entry will have the default value assigned to them in the zero-argument constructor, or will have the JVM-supplied default value if no value was assigned to it in the constructor. If the associated object is converted back to an LDAP entry, then those fields will be included in the entry that is generated, even if they were not present in the original entry. To avoid this problem, you can use the object types rather than the primitive types (e.g., java.lang.Boolean instead of the boolean primitive), in which case any fields associated with attributes that are not present in the entry being de-serialized will be explicitly set to null.
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      DefaultObjectEncoder()
      Creates a new instance of this encoder.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      AttributeTypeDefinition constructAttributeType​(java.lang.reflect.Field f, OIDAllocator a)
      Constructs a definition for an LDAP attribute type which may be added to the directory server schema to allow it to hold the value of the specified field.
      AttributeTypeDefinition constructAttributeType​(java.lang.reflect.Method m, OIDAllocator a)
      Constructs a definition for an LDAP attribute type which may be added to the directory server schema to allow it to hold the value returned by the specified method.
      void decodeField​(java.lang.reflect.Field field, java.lang.Object object, Attribute attribute)
      Updates the provided object to assign a value for the specified field from the contents of the given attribute.
      Attribute encodeFieldValue​(java.lang.reflect.Field field, java.lang.Object value, java.lang.String name)
      Encodes the provided field to an LDAP attribute.
      Attribute encodeMethodValue​(java.lang.reflect.Method method, java.lang.Object value, java.lang.String name)
      Encodes the provided method to an LDAP attribute.
      void invokeSetter​(java.lang.reflect.Method method, java.lang.Object object, Attribute attribute)
      Updates the provided object to invoke the specified method to set a value from the contents of the given attribute.
      boolean supportsMultipleValues​(java.lang.reflect.Field field)
      Indicates whether the provided field can hold multiple values.
      boolean supportsMultipleValues​(java.lang.reflect.Method method)
      Indicates whether the provided setter method takes an argument that can hold multiple values.
      boolean supportsType​(java.lang.reflect.Type t)
      Indicates whether this object encoder may be used to encode or decode objects of the specified type.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • supportsType

        public boolean supportsType​(java.lang.reflect.Type t)
        Indicates whether this object encoder may be used to encode or decode objects of the specified type.
        Specified by:
        supportsType in class ObjectEncoder
        Parameters:
        t - The type of object for which to make the determination.
        Returns:
        true if this object encoder may be used for objects of the specified type, or false if not.
      • constructAttributeType

        public AttributeTypeDefinition constructAttributeType​(java.lang.reflect.Field f,
                                                              OIDAllocator a)
                                                       throws LDAPPersistException
        Constructs a definition for an LDAP attribute type which may be added to the directory server schema to allow it to hold the value of the specified field.
        Specified by:
        constructAttributeType in class ObjectEncoder
        Parameters:
        f - The field for which to construct an LDAP attribute type definition. It will include the LDAPField annotation type.
        a - The OID allocator to use to generate the object identifier. It must not be null.
        Returns:
        The constructed attribute type definition.
        Throws:
        LDAPPersistException - If this object encoder does not support encoding values for the associated field type.
      • constructAttributeType

        public AttributeTypeDefinition constructAttributeType​(java.lang.reflect.Method m,
                                                              OIDAllocator a)
                                                       throws LDAPPersistException
        Constructs a definition for an LDAP attribute type which may be added to the directory server schema to allow it to hold the value returned by the specified method. Note that the object identifier used for the constructed attribute type definition is not required to be valid or unique.
        Specified by:
        constructAttributeType in class ObjectEncoder
        Parameters:
        m - The method for which to construct an LDAP attribute type definition. It will include the LDAPGetter annotation type.
        a - The OID allocator to use to generate the object identifier. It must not be null.
        Returns:
        The constructed attribute type definition.
        Throws:
        LDAPPersistException - If this object encoder does not support encoding values for the associated method type.
      • supportsMultipleValues

        public boolean supportsMultipleValues​(java.lang.reflect.Field field)
        Indicates whether the provided field can hold multiple values.
        Specified by:
        supportsMultipleValues in class ObjectEncoder
        Parameters:
        field - The field for which to make the determination. It must be marked with the LDAPField annotation.
        Returns:
        true if the provided field can hold multiple values, or false if not.
      • supportsMultipleValues

        public boolean supportsMultipleValues​(java.lang.reflect.Method method)
        Indicates whether the provided setter method takes an argument that can hold multiple values.
        Specified by:
        supportsMultipleValues in class ObjectEncoder
        Parameters:
        method - The setter method for which to make the determination. It must be marked with the LDAPSetter annotation type and conform to the constraints associated with that annotation.
        Returns:
        true if the provided method takes an argument that can hold multiple values, or false if not.
      • encodeFieldValue

        public Attribute encodeFieldValue​(java.lang.reflect.Field field,
                                          java.lang.Object value,
                                          java.lang.String name)
                                   throws LDAPPersistException
        Encodes the provided field to an LDAP attribute.
        Specified by:
        encodeFieldValue in class ObjectEncoder
        Parameters:
        field - The field to be encoded.
        value - The value for the field in the object to be encoded.
        name - The name to use for the constructed attribute.
        Returns:
        The attribute containing the encoded representation of the provided field.
        Throws:
        LDAPPersistException - If a problem occurs while attempting to construct an attribute for the field.
      • encodeMethodValue

        public Attribute encodeMethodValue​(java.lang.reflect.Method method,
                                           java.lang.Object value,
                                           java.lang.String name)
                                    throws LDAPPersistException
        Encodes the provided method to an LDAP attribute.
        Specified by:
        encodeMethodValue in class ObjectEncoder
        Parameters:
        method - The method to be encoded.
        value - The value returned by the method in the object to be encoded.
        name - The name to use for the constructed attribute.
        Returns:
        The attribute containing the encoded representation of the provided method value.
        Throws:
        LDAPPersistException - If a problem occurs while attempting to construct an attribute for the method.
      • decodeField

        public void decodeField​(java.lang.reflect.Field field,
                                java.lang.Object object,
                                Attribute attribute)
                         throws LDAPPersistException
        Updates the provided object to assign a value for the specified field from the contents of the given attribute.
        Specified by:
        decodeField in class ObjectEncoder
        Parameters:
        field - The field to update in the provided object.
        object - The object to be updated.
        attribute - The attribute whose value(s) should be used to update the specified field in the given object.
        Throws:
        LDAPPersistException - If a problem occurs while attempting to assign a value to the specified field.
      • invokeSetter

        public void invokeSetter​(java.lang.reflect.Method method,
                                 java.lang.Object object,
                                 Attribute attribute)
                          throws LDAPPersistException
        Updates the provided object to invoke the specified method to set a value from the contents of the given attribute.
        Specified by:
        invokeSetter in class ObjectEncoder
        Parameters:
        method - The method to invoke in the provided object.
        object - The object to be updated.
        attribute - The attribute whose value(s) should be used to update the specified method in the given object.
        Throws:
        LDAPPersistException - If a problem occurs while attempting to determine the value or invoke the specified method.