FileDocCategorySizeDatePackage
JavaBeanConfigurator.javaAPI DocGlassfish v2 API7280Fri May 04 22:33:44 BST 2007com.sun.enterprise.admin.selfmanagement.configuration

JavaBeanConfigurator

public class JavaBeanConfigurator extends Object
author
Sun Micro Systems, Inc

Fields Summary
private static final JavaBeanConfigurator
singleton
Constructors Summary
private JavaBeanConfigurator()
Creates a new instance of JavaBeanConfigurator

    
           
       
    
Methods Summary
public java.lang.ObjectconfigureBean(java.lang.Object bean, com.sun.enterprise.config.serverbeans.ElementProperty[] properties)
A Utility method to instantiate a class and set the properties.

         setProperties( bean, properties );
         return bean;
    
private final java.lang.reflect.Method[]getDeclaredMethods(java.lang.Class clz)
Utility method to get the declared fields.

        return (Method[]) AccessController.doPrivileged(new PrivilegedAction() {
       public Object run() {
                return clz.getDeclaredMethods();
            }
        });
    
public static com.sun.enterprise.admin.selfmanagement.configuration.JavaBeanConfiguratorgetInstance()

        return singleton;
    
private voidsetProperties(java.lang.Object o, com.sun.enterprise.config.serverbeans.ElementProperty[] properties)
Utility method to set properties to the instantiated Object. _REVISIT_: We can just do java.beans.statement possibly

        if( properties == null ) return;
        Method[] methods = null;
        try {
            methods = getDeclaredMethods( o.getClass( ) );
            for( int i = 0; i < properties.length; i++ ) {
                ElementProperty property = properties[i];
                String propertyName = property.getName( ).toLowerCase( );
                String propertyValue = property.getValue( );
                for( int j = 0; j < methods.length; j++ ) {
                    String methodName = methods[j].getName().toLowerCase();
                    if ( ( methodName.startsWith( "set" ) )
                       && ( methodName.endsWith( propertyName ) ) )
                    {
                        Class[] parameterTypes = methods[j].getParameterTypes( );
                        if( parameterTypes.length != 1 ) {
                            new ErrorManager().error(
                                "Only one Parameter is allowed for the setter " +
                                " Method: " + methodName +
                                " has invalid signature", new Exception(),
                                ErrorManager.GENERIC_FAILURE );
                        }
                                                                                     
                        String parameterType = parameterTypes[0].getName();
                        Object[] parameters = new Object[1];
                                                                                     
                        if( parameterType.equals( "java.lang.String") ) {
                            parameters[0] = propertyValue;
                        } else if( parameterType.equals( "byte" ) ) {
                            parameters[0] =
                                new Byte( propertyValue.getBytes()[0]);
                        } else if( parameterType.equals( "int" ) ) {
                            parameters[0] = new Integer(propertyValue);
                        } else if( parameterType.equals( "float" ) ) {
                            parameters[0] = new Float(propertyValue);
                        } else if( parameterType.equals( "double") ) {
                            parameters[0] = new Double(propertyValue);
                        } else if( parameterType.equals( "char" ) ) {
                            parameters[0] =
                                new Character(propertyValue.charAt(0));
                        } else if( parameterType.equals("boolean") ) {
                            parameters[0] = new Boolean(propertyValue);
                        } else if( parameterType.equals("long") ) {
                            parameters[0] = new Long(propertyValue);
                        } else if( parameterType.equals("short") ) {
                             parameters[0] = new Short(propertyValue);
                        } else {
                            new ErrorManager().error(
                                "Only the basic primitive types can be set " +
                                "as properties to NotificationListener and " +
                                " NotificationFilter ", new Exception(),
                                ErrorManager.GENERIC_FAILURE );
                            continue;
                        }
                        methods[j].invoke( o,  parameters );
                    }
                }
            }
        } catch( Exception e ) {
            new ErrorManager().error(
                "Error While Setting properties to Notification Listener or " +
                " Filter ", e, ErrorManager.GENERIC_FAILURE );
        }