Package org.ldaptive.beans.generate
Class BeanGenerator
- java.lang.Object
-
- org.ldaptive.beans.generate.BeanGenerator
-
public class BeanGenerator extends Object
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 Modifier and Type Class Description static class
BeanGenerator.Builder
-
Field Summary
Fields Modifier and Type Field Description private com.sun.codemodel.JCodeModel
codeModel
Code model for java class creation.private String[]
excludedNames
Excluded names.private boolean
includeSuperiorClasses
Whether to include superior classes for each object class.private Map<String,String>
nameMappings
Name mappings.private String[]
objectClasses
Object classes to build beans for.private String
packageName
Package to create beans in.private Schema
schema
Schema to generate beans from.private Map<String,Class<?>>
typeMappings
Mapping to determine attribute value type.private boolean
useOperationalAttributes
Whether to include operational attributes.private boolean
useOptionalAttributes
Whether to include optional attributes.
-
Constructor Summary
Constructors Constructor Description BeanGenerator()
Default constructor.BeanGenerator(Schema s, String name, String[] oc)
Creates a new bean generator.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static 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.private Set<String>
getAttributeNames(ObjectClass objectClass)
Returns the attribute names to use for the supplied object class.private Set<String>
getAttributeNames(ObjectClass objectClass, Set<ObjectClass> processed)
Returns the attribute names to use for the supplied object class.protected static Map<String,Class<?>>
getDefaultTypeMappings()
Returns the default syntax types used to determine attribute property type.String[]
getExcludedNames()
Returns the attribute names to exclude from bean generation.Map<String,String>
getNameMappings()
Returns the mapping of directory attribute name to bean property.String[]
getObjectClasses()
Returns the object classes.String
getPackageName()
Returns the package name where beans will be generated.Schema
getSchema()
Returns the schema.protected Class<?>
getSyntaxType(AttributeType type, Syntax syntax)
Returns the class for the supplied attribute type and syntax.Map<String,Class<?>>
getTypeMappings()
Returns the type mappings.boolean
isIncludeSuperiorClasses()
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
isUseOperationalAttributes()
Returns whether to include operational attributes in bean generation.boolean
isUseOptionalAttributes()
Returns whether to include optional attributes in bean generation.static void
main(String[] args)
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
setNameMappings(Map<String,String> m)
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
setSchema(Schema s)
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
write(String path)
Writes the generated classes to disk at the supplied path.
-
-
-
Field Detail
-
codeModel
private final com.sun.codemodel.JCodeModel codeModel
Code model for java class creation.
-
schema
private Schema schema
Schema to generate beans from.
-
packageName
private String packageName
Package to create beans in.
-
objectClasses
private String[] objectClasses
Object classes to build beans for.
-
useOptionalAttributes
private boolean useOptionalAttributes
Whether to include optional attributes.
-
useOperationalAttributes
private boolean useOperationalAttributes
Whether to include operational attributes.
-
includeSuperiorClasses
private boolean includeSuperiorClasses
Whether to include superior classes for each object class.
-
excludedNames
private String[] excludedNames
Excluded names.
-
-
Constructor Detail
-
BeanGenerator
public BeanGenerator()
Default constructor.
-
BeanGenerator
public BeanGenerator(Schema s, String name, String[] oc)
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 Detail
-
getSchema
public Schema getSchema()
Returns the schema.- Returns:
- schema
-
setSchema
public void setSchema(Schema s)
Sets the schema.- Parameters:
s
- schema
-
getPackageName
public String getPackageName()
Returns the package name where beans will be generated.- Returns:
- package name
-
setPackageName
public void setPackageName(String name)
Sets the package name where beans will be generated.- Parameters:
name
- package name
-
getObjectClasses
public String[] getObjectClasses()
Returns the object classes. A class is generated for each object class.- Returns:
- object classes
-
setObjectClasses
public void setObjectClasses(String... oc)
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
public Map<String,Class<?>> 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
public void setTypeMappings(Map<String,Class<?>> m)
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
public Map<String,String> 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
public void setNameMappings(Map<String,String> m)
Sets the mapping of directory attribute name to bean property.- Parameters:
m
- name mappings- Throws:
NullPointerException
- if m is null
-
getExcludedNames
public String[] 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
public void setExcludedNames(String... names)
Sets the attribute names to exclude from bean generation.- Parameters:
names
- to exclude- Throws:
NullPointerException
- if names is null
-
getDefaultTypeMappings
protected static Map<String,Class<?>> getDefaultTypeMappings()
Returns the default syntax types used to determine attribute property type.- Returns:
- map of syntax OID to class type
-
getSyntaxType
protected Class<?> getSyntaxType(AttributeType type, Syntax syntax)
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
private Set<String> getAttributeNames(ObjectClass objectClass)
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
private Set<String> getAttributeNames(ObjectClass objectClass, Set<ObjectClass> processed)
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
private String formatAttributeName(String name)
Formats the supplied name for use as a Java property.- Parameters:
name
- to format- Returns:
- formatted name
-
isNameExcluded
private boolean isNameExcluded(AttributeType type)
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
protected com.sun.codemodel.JDefinedClass createClass(String classPackage, String className)
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
public void write() throws IOException
Writes the generated classes to disk. Invokeswrite(String)
with ".".- Throws:
IOException
- if the write fails
-
write
public void write(String path) throws IOException
Writes the generated classes to disk at the supplied path.- Parameters:
path
- to write the classes to- Throws:
IOException
- if the write fails
-
main
public static void main(String[] args) throws Exception
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
public static BeanGenerator.Builder builder()
Creates a builder for this class.- Returns:
- new builder
-
-