Package com.unboundid.ldap.sdk.persist
Class DefaultObjectEncoder
- java.lang.Object
-
- com.unboundid.ldap.sdk.persist.ObjectEncoder
-
- com.unboundid.ldap.sdk.persist.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 anObjectEncoder
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 valuejava.util.concurrent.atomic.AtomicLong
-- Encoded using the string representation of the valuejava.math.BigDecimal
-- Encoded using the string representation of the valuejava.math.BigInteger
-- Encoded using the string representation of the valueboolean
-- Encoded as either "TRUE" or "FALSE"java.lang.Boolean
-- Encoded as either "TRUE" or "FALSE"byte[]
-- Encoded as the raw bytes contained in the arraychar[]
-- Encoded as a string containing the characters in the arrayjava.util.Date
-- Encoded using the generalized time syntaxcom.unboundid.ldap.sdk.DN
-- Encoded using the string representation of the valuedouble
-- Encoded using the string representation of the valuejava.lang.Double
-- Encoded using the string representation of the valuecom.unboundid.ldap.sdk.Filter
-- Encoded using the string representation of the valuefloat
-- Encoded using the string representation of the valuejava.lang.Float
-- Encoded using the string representation of the valueint
-- Encoded using the string representation of the valuejava.lang.Integer
-- Encoded using the string representation of the valuecom.unboundid.ldap.sdk.LDAPURL
-- Encoded using the string representation of the valuelong
-- Encoded using the string representation of the valuejava.lang.Long
-- Encoded using the string representation of the valuecom.unboundid.ldap.sdk.RDN
-- Encoded using the string representation of the valueshort
-- Encoded using the string representation of the valuejava.lang.Short
-- Encoded using the string representation of the valuejava.lang.String
-- Encoded using the valuejava.lang.StringBuffer
-- Encoded using the string representation of the valuejava.lang.StringBuilder
-- Encoded using the string representation of the valuejava.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
writeObject
,readObject
, andreadObjectNoData
methods that use the desired encoding. Alternately, you may create a customObjectEncoder
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 (includingArrayList
,LinkedList
, andCopyOnWriteArrayList
) and sets (includingHashSet
,LinkedHashSet
,TreeSet
, andCopyOnWriteArraySet
) 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 theboolean
primitive), in which case any fields associated with attributes that are not present in the entry being de-serialized will be explicitly set tonull
.- 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 com.unboundid.ldap.sdk.persist.ObjectEncoder
constructAttributeType, constructAttributeType, setNull, setNull
-
-
-
-
Constructor Detail
-
DefaultObjectEncoder
public DefaultObjectEncoder()
Creates a new instance of this encoder.
-
-
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 classObjectEncoder
- 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, orfalse
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 classObjectEncoder
- Parameters:
f
- The field for which to construct an LDAP attribute type definition. It will include theLDAPField
annotation type.a
- The OID allocator to use to generate the object identifier. It must not benull
.- 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 classObjectEncoder
- Parameters:
m
- The method for which to construct an LDAP attribute type definition. It will include theLDAPGetter
annotation type.a
- The OID allocator to use to generate the object identifier. It must not benull
.- 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 classObjectEncoder
- Parameters:
field
- The field for which to make the determination. It must be marked with theLDAPField
annotation.- Returns:
true
if the provided field can hold multiple values, orfalse
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 classObjectEncoder
- Parameters:
method
- The setter method for which to make the determination. It must be marked with theLDAPSetter
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, orfalse
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 classObjectEncoder
- 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 classObjectEncoder
- 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 classObjectEncoder
- 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 classObjectEncoder
- 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.
-
-