private void addBean()

in server-config/src/main/java/org/apache/directory/server/config/ConfigWriter.java [316:438]


    private void addBean( Dn rootDn, SchemaManager schemaManager, AdsBaseBean bean, List<LdifEntry> entries,
        LdifEntry parentEntry, String attributeTypeForParentEntry )
        throws Exception
    {
        if ( bean != null )
        {
            // Getting the class of the bean
            Class<?> beanClass = bean.getClass();

            // Creating the entry to hold the bean and adding it to the list
            LdifEntry entry = new LdifEntry();
            entry.setDn( getDn( rootDn, bean ) );
            addObjectClassAttribute( schemaManager, entry, getObjectClassNameForBean( beanClass ) );
            entries.add( entry );

            // A flag to know when we reached the 'AdsBaseBean' class when 
            // looping on the class hierarchy of the bean
            boolean adsBaseBeanClassFound = false;

            // Looping until the 'AdsBaseBean' class has been found
            while ( !adsBaseBeanClassFound )
            {
                // Checking if we reached the 'AdsBaseBean' class
                if ( beanClass == AdsBaseBean.class )
                {
                    adsBaseBeanClassFound = true;
                }

                // Looping on all fields of the bean
                Field[] fields = beanClass.getDeclaredFields();
                for ( Field field : fields )
                {
                    // Making the field accessible (we get an exception if we don't do that)
                    field.setAccessible( true );

                    // Getting the class of the field
                    Class<?> fieldClass = field.getType();
                    Object fieldValue = field.get( bean );

                    // Looking for the @ConfigurationElement annotation
                    ConfigurationElement configurationElement = field.getAnnotation( ConfigurationElement.class );
                    if ( configurationElement != null )
                    {
                        // Getting the annotation's values
                        String attributeType = configurationElement.attributeType();
                        String objectClass = configurationElement.objectClass();
                        String container = configurationElement.container();
                        boolean isOptional = configurationElement.isOptional();
                        String defaultValue = configurationElement.defaultValue();

                        // Checking if we have a value for the attribute type
                        if ( ( attributeType != null ) && ( !"".equals( attributeType ) ) )
                        {
                            // Checking if the field is optional and if the default value matches
                            if ( isOptional 
                                    && ( defaultValue != null ) && ( fieldValue != null )
                                    && ( defaultValue.equalsIgnoreCase( fieldValue.toString() ) ) )
                            {
                                // Skipping the addition of the value
                                continue;
                            }

                            // Adding values to the entry
                            addAttributeTypeValues( configurationElement.attributeType(), fieldValue, entry );
                        }
                        // Checking if we have a value for the object class
                        else if ( ( objectClass != null ) && ( !"".equals( objectClass ) ) )
                        {
                            // Checking if we're dealing with a container
                            if ( ( container != null ) && ( !"".equals( container ) ) )
                            {
                                // Creating the entry for the container and adding it to the list
                                LdifEntry containerEntry = new LdifEntry();
                                containerEntry.setDn( entry.getDn().add( new Rdn( SchemaConstants.OU_AT, container ) ) );
                                addObjectClassAttribute( schemaManager, containerEntry,
                                    SchemaConstants.ORGANIZATIONAL_UNIT_OC );
                                addAttributeTypeValues( SchemaConstants.OU_AT, container, containerEntry );
                                entries.add( containerEntry );

                                if ( Collection.class.isAssignableFrom( fieldClass ) )
                                {
                                    // Looping on the Collection's objects
                                    @SuppressWarnings("unchecked")
                                    Collection<Object> collection = ( Collection<Object> ) fieldValue;
                                    if ( collection != null )
                                    {
                                        for ( Object object : collection )
                                        {
                                            if ( object instanceof AdsBaseBean )
                                            {
                                                // Adding the bean
                                                addBean( containerEntry.getDn(), schemaManager, ( AdsBaseBean ) object,
                                                    entries, entry, attributeType );
                                            }
                                            else
                                            {
                                                // TODO throw an error, if we have a container, the type must be a subtype of AdsBaseBean
                                                throw new Exception();
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    // TODO throw an error, if we have a container, the type must be a subtype of Collection
                                    throw new Exception();
                                }
                            }
                            else
                            {
                                // Adding the bean
                                addBean( entry.getDn(), schemaManager, ( AdsBaseBean ) fieldValue, entries, entry,
                                    attributeType );
                            }
                        }
                    }
                }

                // Moving to the upper class in the class hierarchy
                beanClass = beanClass.getSuperclass();
            }
        }
    }