Package org.ldaptive.beans.generate
Class BeanGenerator
java.lang.Object
org.ldaptive.beans.generate.BeanGenerator
Utility class for creating Java POJOs from an LDAP schema. Sample usage:
Schema schema = new Schema(new DefaultConnectionFactory( "ldap://directory.ldaptive.org")); BeanGenerator generator = new BeanGenerator( schema, "com.my.package", new String[] {"inetOrgPerson"}); generator.generate(); generator.write();
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final com.sun.codemodel.JCodeModel
Code model for java class creation.private String[]
Excluded names.private boolean
Whether to include superior classes for each object class.Name mappings.private String[]
Object classes to build beans for.private String
Package to create beans in.private Schema
Schema to generate beans from.Mapping to determine attribute value type.private boolean
Whether to include operational attributes.private boolean
Whether to include optional attributes. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.BeanGenerator
(Schema s, String name, String[] oc) Creates a new bean generator. -
Method Summary
Modifier and TypeMethodDescriptionstatic BeanGenerator.Builder
builder()
Creates a builder for this class.protected com.sun.codemodel.JDefinedClass
createClass
(String classPackage, String className) Creates a class in the supplied package.private void
createEquals
(com.sun.codemodel.JDefinedClass clazz) Creates the equals method on the supplied class.private void
createHashCode
(com.sun.codemodel.JDefinedClass clazz) Creates the hashCode method on the supplied class.protected void
createMutators
(com.sun.codemodel.JDefinedClass clazz, String name, Class<?> syntaxType, boolean multivalue) Creates the getter and setter methods on the supplied class for the supplied name.private void
createToString
(com.sun.codemodel.JDefinedClass clazz) Creates the toString method on the supplied class.private String
formatAttributeName
(String name) Formats the supplied name for use as a Java property.void
generate()
Generates a class for each configured object class.getAttributeNames
(ObjectClass objectClass) Returns the attribute names to use for the supplied object class.getAttributeNames
(ObjectClass objectClass, Set<ObjectClass> processed) Returns the attribute names to use for the supplied object class.Returns the default syntax types used to determine attribute property type.String[]
Returns the attribute names to exclude from bean generation.Returns the mapping of directory attribute name to bean property.String[]
Returns the object classes.Returns the package name where beans will be generated.Returns the schema.protected Class<?>
getSyntaxType
(AttributeType type, Syntax syntax) Returns the class for the supplied attribute type and syntax.Returns the type mappings.boolean
Returns whether to include superior classes in bean generation.private boolean
isNameExcluded
(AttributeType type) Returns whether the supplied attribute type has a matching OID or name in the excluded names list.boolean
Returns whether to include operational attributes in bean generation.boolean
Returns whether to include optional attributes in bean generation.static void
Provides command line access to aBeanGenerator
.void
setExcludedNames
(String... names) Sets the attribute names to exclude from bean generation.void
setIncludeSuperiorClasses
(boolean b) Sets whether to include superior classes in bean generation.void
Sets the mapping of directory attribute name to bean property.void
setObjectClasses
(String... oc) Sets the object classes.void
setPackageName
(String name) Sets the package name where beans will be generated.void
Sets the schema.void
setTypeMappings
(Map<String, Class<?>> m) Sets the type mappings.void
setUseOperationalAttributes
(boolean b) Sets whether to include operational attributes in bean generation.void
setUseOptionalAttributes
(boolean b) Sets whether to include optional attributes in bean generation.void
write()
Writes the generated classes to disk.void
Writes the generated classes to disk at the supplied path.
-
Field Details
-
codeModel
private final com.sun.codemodel.JCodeModel codeModelCode model for java class creation. -
schema
Schema to generate beans from. -
packageName
Package to create beans in. -
objectClasses
Object classes to build beans for. -
useOptionalAttributes
private boolean useOptionalAttributesWhether to include optional attributes. -
useOperationalAttributes
private boolean useOperationalAttributesWhether to include operational attributes. -
includeSuperiorClasses
private boolean includeSuperiorClassesWhether to include superior classes for each object class. -
typeMappings
Mapping to determine attribute value type. -
nameMappings
Name mappings. -
excludedNames
Excluded names.
-
-
Constructor Details
-
BeanGenerator
public BeanGenerator()Default constructor. -
BeanGenerator
Creates a new bean generator. A bean will be generated for each supplied object class.- Parameters:
s
- schema containing directory data for generationname
- package name to place the generated classes inoc
- object classes to generate beans for
-
-
Method Details
-
getSchema
Returns the schema.- Returns:
- schema
-
setSchema
Sets the schema.- Parameters:
s
- schema
-
getPackageName
Returns the package name where beans will be generated.- Returns:
- package name
-
setPackageName
Sets the package name where beans will be generated.- Parameters:
name
- package name
-
getObjectClasses
Returns the object classes. A class is generated for each object class.- Returns:
- object classes
-
setObjectClasses
Sets the object classes. A class is generated for each object class.- Parameters:
oc
- object classes
-
isUseOptionalAttributes
public boolean isUseOptionalAttributes()Returns whether to include optional attributes in bean generation.- Returns:
- whether to include optional attributes
-
setUseOptionalAttributes
public void setUseOptionalAttributes(boolean b) Sets whether to include optional attributes in bean generation.- Parameters:
b
- whether to include optional attributes
-
isUseOperationalAttributes
public boolean isUseOperationalAttributes()Returns whether to include operational attributes in bean generation.- Returns:
- whether to include operational attributes
-
setUseOperationalAttributes
public void setUseOperationalAttributes(boolean b) Sets whether to include operational attributes in bean generation.- Parameters:
b
- whether to include operational attributes
-
isIncludeSuperiorClasses
public boolean isIncludeSuperiorClasses()Returns whether to include superior classes in bean generation.- Returns:
- whether to include superior classes attributes
-
setIncludeSuperiorClasses
public void setIncludeSuperiorClasses(boolean b) Sets whether to include superior classes in bean generation.- Parameters:
b
- whether to include superior classes
-
getTypeMappings
Returns the type mappings. Type mappings is syntax OID to class type and is used to determine field type in the generated POJOs.- Returns:
- type mappings
-
setTypeMappings
Sets the type mappings. Type mappings is syntax OID to class type and is used to determine field type in the generated POJOs.- Parameters:
m
- type mappings
-
getNameMappings
Returns the mapping of directory attribute name to bean property. This property is used to override the default schema name. For instance, you may prefer using 'countryName' to 'c', which would be set as 'c'=>'countryName'.- Returns:
- attribute name to bean property mapping
-
setNameMappings
Sets the mapping of directory attribute name to bean property.- Parameters:
m
- name mappings- Throws:
NullPointerException
- if m is null
-
getExcludedNames
Returns the attribute names to exclude from bean generation. Excludes an attribute from the generated POJO. For instance, you may not want 'userPassword' included in your bean.- Returns:
- attribute names to exclude
-
setExcludedNames
Sets the attribute names to exclude from bean generation.- Parameters:
names
- to exclude- Throws:
NullPointerException
- if names is null
-
getDefaultTypeMappings
Returns the default syntax types used to determine attribute property type.- Returns:
- map of syntax OID to class type
-
getSyntaxType
Returns the class for the supplied attribute type and syntax. If the attribute type syntax OID is found in the default type mapping it is used. Otherwise if the syntax is "X-NOT-HUMAN-READABLE", a byte array is used.- Parameters:
type
- attribute typesyntax
- associated with the attribute type- Returns:
- syntax type
-
generate
public void generate()Generates a class for each configured object class. SeeobjectClasses
.write(String)
must be invoked to write the classes to disk. -
getAttributeNames
Returns the attribute names to use for the supplied object class. SeegetAttributeNames(ObjectClass, Set)
.- Parameters:
objectClass
- to retrieve names from- Returns:
- set of all attribute names used for bean generation
-
getAttributeNames
Returns the attribute names to use for the supplied object class. This method is invoked recursively if superior classes are included.- Parameters:
objectClass
- to retrieve names fromprocessed
- object classes that have already been processed- Returns:
- set of all attribute names used for bean generation
-
formatAttributeName
Formats the supplied name for use as a Java property.- Parameters:
name
- to format- Returns:
- formatted name
-
isNameExcluded
Returns whether the supplied attribute type has a matching OID or name in the excluded names list.- Parameters:
type
- to compare- Returns:
- whether attribute type should be excluded from bean generation
-
createClass
Creates a class in the supplied package.- Parameters:
classPackage
- to place the class inclassName
- to create- Returns:
- class
- Throws:
IllegalArgumentException
- if the class already exists
-
createMutators
protected void createMutators(com.sun.codemodel.JDefinedClass clazz, String name, Class<?> syntaxType, boolean multivalue) Creates the getter and setter methods on the supplied class for the supplied name.- Parameters:
clazz
- to put getter and setter methods onname
- of the propertysyntaxType
- of the propertymultivalue
- whether this property is a collection
-
createHashCode
private void createHashCode(com.sun.codemodel.JDefinedClass clazz) Creates the hashCode method on the supplied class. LeveragesLdapUtils.computeHashCode(int, Object...)
.- Parameters:
clazz
- to put hashCode method on
-
createEquals
private void createEquals(com.sun.codemodel.JDefinedClass clazz) Creates the equals method on the supplied class. LeveragesLdapUtils.areEqual(Object, Object)
.- Parameters:
clazz
- to put equals method on
-
createToString
private void createToString(com.sun.codemodel.JDefinedClass clazz) Creates the toString method on the supplied class. Creates a string that contains every property on the generated bean.- Parameters:
clazz
- to put toString method on
-
write
Writes the generated classes to disk. Invokeswrite(String)
with ".".- Throws:
IOException
- if the write fails
-
write
Writes the generated classes to disk at the supplied path.- Parameters:
path
- to write the classes to- Throws:
IOException
- if the write fails
-
main
Provides command line access to aBeanGenerator
. Expects two arguments:- path to a configuration property file
- target directory to write files to
A sample configuration property file looks like:
org.ldaptive.packageName=my.package.ldap.beans org.ldaptive.objectClasses=eduPerson org.ldaptive.nameMappings=c=countryName,l=localityName org.ldaptive.excludedNames=userPassword org.ldaptive.ldapUrl=ldap://directory.ldaptive.org
- Parameters:
args
- command line arguments- Throws:
Exception
- if any error occurs
-
builder
Creates a builder for this class.- Returns:
- new builder
-