Class 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();
     
    • 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.
      • typeMappings

        private Map<String,​Class<?>> typeMappings
        Mapping to determine attribute value type.
      • 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 generation
        name - package name to place the generated classes in
        oc - 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 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.
      • getAttributeNames

        private Set<String> getAttributeNames​(ObjectClass objectClass)
        Returns the attribute names to use for the supplied object class. See getAttributeNames(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 from
        processed - 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 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
      • createHashCode

        private void createHashCode​(com.sun.codemodel.JDefinedClass clazz)
        Creates the hashCode method on the supplied class. Leverages LdapUtils.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. Leverages LdapUtils.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​(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
      • builder

        public static BeanGenerator.Builder builder()
        Creates a builder for this class.
        Returns:
        new builder