org.ldaptive.beans.generate
Class BeanGenerator

java.lang.Object
  extended by 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();
 

Version:
$Revision: 3189 $ $Date: 2016-11-01 17:21:43 -0400 (Tue, 01 Nov 2016) $
Author:
Middleware Services

Constructor Summary
BeanGenerator()
          Default constructor.
BeanGenerator(org.ldaptive.schema.Schema s, String name, String[] oc)
          Creates a new bean generator.
 
Method Summary
protected  com.sun.codemodel.JDefinedClass createClass(String classPackage, String className)
          Creates a class in the supplied package.
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.
 void generate()
          Generates a class for each configured 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.
 org.ldaptive.schema.Schema getSchema()
          Returns the schema.
protected  Class<?> getSyntaxType(org.ldaptive.schema.AttributeType type, org.ldaptive.schema.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.
 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 a BeanGenerator.
 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(org.ldaptive.schema.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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BeanGenerator

public BeanGenerator()
Default constructor.


BeanGenerator

public BeanGenerator(org.ldaptive.schema.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 generation
name - package name to place the generated classes in
oc - object classes to generate beans for
Method Detail

getSchema

public org.ldaptive.schema.Schema getSchema()
Returns the schema.

Returns:
schema

setSchema

public void setSchema(org.ldaptive.schema.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(org.ldaptive.schema.AttributeType type,
                                 org.ldaptive.schema.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 type
syntax - associated with the attribute type
Returns:
syntax type

generate

public void generate()
Generates a class for each configured object class. See objectClasses. write(String) must be invoked to write the classes to disk.


createClass

protected com.sun.codemodel.JDefinedClass createClass(String classPackage,
                                                      String className)
Creates a class in the supplied package.

Parameters:
classPackage - to place the class in
className - 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 on
name - of the property
syntaxType - of the property
multivalue - whether this property is a collection

write

public void write()
           throws IOException
Writes the generated classes to disk. Invokes write(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 a BeanGenerator. Expects two arguments:
  1. path to a configuration property file
  2. 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


Copyright © 2003-2016 Virginia Tech. All Rights Reserved.