private void readMultiValuedField()

in server-config/src/main/java/org/apache/directory/server/config/ConfigPartitionReader.java [311:413]


    private void readMultiValuedField( AdsBaseBean bean, Field field, Attribute attribute )
        throws ConfigurationException
    {
        if ( attribute == null )
        {
            return;
        }

        Class<?> type = field.getType();

        String fieldName = field.getName();
        String addMethodName = "add" + Character.toUpperCase( fieldName.charAt( 0 ) ) + fieldName.substring( 1 );

        // loop on the values and inject them in the bean
        for ( Value value : attribute )
        {
            String valueStr = value.getString();

            try
            {
                if ( type == String.class )
                {
                    field.set( bean, valueStr );
                }
                else if ( type == int.class )
                {
                    field.setInt( bean, Integer.parseInt( valueStr ) );
                }
                else if ( type == long.class )
                {
                    field.setLong( bean, Long.parseLong( valueStr ) );
                }
                else if ( type == boolean.class )
                {
                    field.setBoolean( bean, Boolean.parseBoolean( valueStr ) );
                }
                else if ( type == Dn.class )
                {
                    try
                    {
                        Dn dn = new Dn( valueStr );
                        field.set( bean, dn );
                    }
                    catch ( LdapInvalidDnException lide )
                    {
                        String message = "The Dn '" + valueStr + "' for attribute " + attribute.getId()
                            + " is not a valid Dn";
                        LOG.error( message );
                        throw new ConfigurationException( message );
                    }
                }
                else if ( ( type == Set.class ) || ( type == List.class ) )
                {
                    Type genericFieldType = field.getGenericType();
                    Class<?> fieldArgClass = null;

                    if ( genericFieldType instanceof ParameterizedType )
                    {
                        ParameterizedType parameterizedType = ( ParameterizedType ) genericFieldType;
                        Type[] fieldArgTypes = parameterizedType.getActualTypeArguments();

                        for ( Type fieldArgType : fieldArgTypes )
                        {
                            fieldArgClass = ( Class<?> ) fieldArgType;
                        }
                    }

                    Method method = bean.getClass().getMethod( addMethodName,
                        Array.newInstance( fieldArgClass, 0 ).getClass() );

                    method.invoke( bean, new Object[] { new String[] { valueStr } } );
                }
            }
            catch ( IllegalArgumentException | IllegalAccessException e )
            {
                String message = "Cannot store '" + valueStr + "' into attribute " + attribute.getId();
                LOG.error( message );
                throw new ConfigurationException( message );
            }
            catch ( SecurityException e )
            {
                String message = "Cannot access to the class " + bean.getClass().getName();
                LOG.error( message );
                throw new ConfigurationException( message );
            }
            catch ( NoSuchMethodException nsme )
            {
                String message = "Cannot find a method " + addMethodName + " in the class " + bean.getClass().getName();
                LOG.error( message );
                throw new ConfigurationException( message );
            }
            catch ( InvocationTargetException ite )
            {
                String message = "Cannot invoke the class " + bean.getClass().getName() + ", " + ite.getMessage();
                LOG.error( message );
                throw new ConfigurationException( message );
            }
            catch ( NegativeArraySizeException nase )
            {
                // No way that can happen...
            }
        }
    }