1 23 24 28 29 package com.sun.enterprise.admin.selfmanagement.configuration; 30 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.Field ; 33 import java.security.AccessController ; 34 import java.security.PrivilegedAction ; 35 import java.util.List ; 36 import java.util.ArrayList ; 37 import java.util.StringTokenizer ; 38 import java.util.logging.ErrorManager ; 39 import java.util.logging.Logger ; 40 import java.util.logging.Level ; 41 42 import com.sun.enterprise.config.serverbeans.ElementProperty; 43 44 45 49 public class JavaBeanConfigurator { 50 51 private static final JavaBeanConfigurator singleton = 52 new JavaBeanConfigurator( ); 53 54 55 private JavaBeanConfigurator() { 56 } 57 58 public static JavaBeanConfigurator getInstance( ) { 59 return singleton; 60 } 61 62 63 66 public Object configureBean( Object bean, ElementProperty[] properties ){ 67 setProperties( bean, properties ); 68 return bean; 69 } 70 71 72 75 private final Method [] getDeclaredMethods(final Class clz) { 76 return (Method []) AccessController.doPrivileged(new PrivilegedAction () { 77 public Object run() { 78 return clz.getDeclaredMethods(); 79 } 80 }); 81 } 82 83 87 private void setProperties( Object o, ElementProperty[] properties ) { 88 if( properties == null ) return; 89 Method [] methods = null; 90 try { 91 methods = getDeclaredMethods( o.getClass( ) ); 92 for( int i = 0; i < properties.length; i++ ) { 93 ElementProperty property = properties[i]; 94 String propertyName = property.getName( ).toLowerCase( ); 95 String propertyValue = property.getValue( ); 96 for( int j = 0; j < methods.length; j++ ) { 97 String methodName = methods[j].getName().toLowerCase(); 98 if ( ( methodName.startsWith( "set" ) ) 99 && ( methodName.endsWith( propertyName ) ) ) 100 { 101 Class [] parameterTypes = methods[j].getParameterTypes( ); 102 if( parameterTypes.length != 1 ) { 103 new ErrorManager ().error( 104 "Only one Parameter is allowed for the setter " + 105 " Method: " + methodName + 106 " has invalid signature", new Exception (), 107 ErrorManager.GENERIC_FAILURE ); 108 } 109 110 String parameterType = parameterTypes[0].getName(); 111 Object [] parameters = new Object [1]; 112 113 if( parameterType.equals( "java.lang.String") ) { 114 parameters[0] = propertyValue; 115 } else if( parameterType.equals( "byte" ) ) { 116 parameters[0] = 117 new Byte ( propertyValue.getBytes()[0]); 118 } else if( parameterType.equals( "int" ) ) { 119 parameters[0] = new Integer (propertyValue); 120 } else if( parameterType.equals( "float" ) ) { 121 parameters[0] = new Float (propertyValue); 122 } else if( parameterType.equals( "double") ) { 123 parameters[0] = new Double (propertyValue); 124 } else if( parameterType.equals( "char" ) ) { 125 parameters[0] = 126 new Character (propertyValue.charAt(0)); 127 } else if( parameterType.equals("boolean") ) { 128 parameters[0] = new Boolean (propertyValue); 129 } else if( parameterType.equals("long") ) { 130 parameters[0] = new Long (propertyValue); 131 } else if( parameterType.equals("short") ) { 132 parameters[0] = new Short (propertyValue); 133 } else { 134 new ErrorManager ().error( 135 "Only the basic primitive types can be set " + 136 "as properties to NotificationListener and " + 137 " NotificationFilter ", new Exception (), 138 ErrorManager.GENERIC_FAILURE ); 139 continue; 140 } 141 methods[j].invoke( o, parameters ); 142 } 143 } 144 } 145 } catch( Exception e ) { 146 new ErrorManager ().error( 147 "Error While Setting properties to Notification Listener or " + 148 " Filter ", e, ErrorManager.GENERIC_FAILURE ); 149 } 150 } 151 } 152 | Popular Tags |