Package org.ldaptive.beans.generate
Class BeanGenerator
- java.lang.Object
-
- org.ldaptive.beans.generate.BeanGenerator
-
public class BeanGenerator extends java.lang.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();
- Author:
- Middleware Services
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BeanGenerator.Builder
-
Constructor Summary
Constructors Constructor Description BeanGenerator()
Default constructor.BeanGenerator(Schema s, java.lang.String name, java.lang.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(java.lang.String classPackage, java.lang.String className)
Creates a class in the supplied package.protected void
createMutators(com.sun.codemodel.JDefinedClass clazz, java.lang.String name, java.lang.Class<?> syntaxType, boolean multivalue)
Creates the getter and setter methods on the supplied class for the supplied name.void
generate()
Generates a class for each configured object class.protected static java.util.Map<java.lang.String,java.lang.Class<?>>
getDefaultTypeMappings()
Returns the default syntax types used to determine attribute property type.java.lang.String[]
getExcludedNames()
Returns the attribute names to exclude from bean generation.java.util.Map<java.lang.String,java.lang.String>
getNameMappings()
Returns the mapping of directory attribute name to bean property.java.lang.String[]
getObjectClasses()
Returns the object classes.java.lang.String
getPackageName()
Returns the package name where beans will be generated.Schema
getSchema()
Returns the schema.protected java.lang.Class<?>
getSyntaxType(AttributeType type, Syntax syntax)
Returns the class for the supplied attribute type and syntax.java.util.Map<java.lang.String,java.lang.Class<?>>
getTypeMappings()
Returns the type mappings.boolean
isIncludeSuperiorClasses()
Returns whether to include superior classes in bean generation.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(java.lang.String[] args)
Provides command line access to aBeanGenerator
.void
setExcludedNames(java.lang.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(java.util.Map<java.lang.String,java.lang.String> m)
Sets the mapping of directory attribute name to bean property.void
setObjectClasses(java.lang.String... oc)
Sets the object classes.void
setPackageName(java.lang.String name)
Sets the package name where beans will be generated.void
setSchema(Schema s)
Sets the schema.void
setTypeMappings(java.util.Map<java.lang.String,java.lang.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(java.lang.String path)
Writes the generated classes to disk at the supplied path.
-
-
-
Constructor Detail
-
BeanGenerator
public BeanGenerator()
Default constructor.
-
BeanGenerator
public BeanGenerator(Schema s, java.lang.String name, java.lang.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 java.lang.String getPackageName()
Returns the package name where beans will be generated.- Returns:
- package name
-
setPackageName
public void setPackageName(java.lang.String name)
Sets the package name where beans will be generated.- Parameters:
name
- package name
-
getObjectClasses
public java.lang.String[] getObjectClasses()
Returns the object classes. A class is generated for each object class.- Returns:
- object classes
-
setObjectClasses
public void setObjectClasses(java.lang.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 java.util.Map<java.lang.String,java.lang.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(java.util.Map<java.lang.String,java.lang.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 java.util.Map<java.lang.String,java.lang.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(java.util.Map<java.lang.String,java.lang.String> m)
Sets the mapping of directory attribute name to bean property.- Parameters:
m
- name mappings- Throws:
java.lang.NullPointerException
- if m is null
-
getExcludedNames
public java.lang.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(java.lang.String... names)
Sets the attribute names to exclude from bean generation.- Parameters:
names
- to exclude- Throws:
java.lang.NullPointerException
- if names is null
-
getDefaultTypeMappings
protected static java.util.Map<java.lang.String,java.lang.Class<?>> getDefaultTypeMappings()
Returns the default syntax types used to determine attribute property type.- Returns:
- map of syntax OID to class type
-
getSyntaxType
protected java.lang.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.
-
createClass
protected com.sun.codemodel.JDefinedClass createClass(java.lang.String classPackage, java.lang.String className)
Creates a class in the supplied package.- Parameters:
classPackage
- to place the class inclassName
- to create- Returns:
- class
- Throws:
java.lang.IllegalArgumentException
- if the class already exists
-
createMutators
protected void createMutators(com.sun.codemodel.JDefinedClass clazz, java.lang.String name, java.lang.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
-
write
public void write() throws java.io.IOException
Writes the generated classes to disk. Invokeswrite(String)
with ".".- Throws:
java.io.IOException
- if the write fails
-
write
public void write(java.lang.String path) throws java.io.IOException
Writes the generated classes to disk at the supplied path.- Parameters:
path
- to write the classes to- Throws:
java.io.IOException
- if the write fails
-
main
public static void main(java.lang.String[] args) throws java.lang.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:
java.lang.Exception
- if any error occurs
-
builder
public static BeanGenerator.Builder builder()
Creates a builder for this class.- Returns:
- new builder
-
-