1 23 24 package com.sun.enterprise.admin.server.core.mbean.config; 25 26 import java.beans.*; 28 import java.lang.reflect.*; 29 30 import java.util.logging.Logger ; 32 import com.sun.logging.LogDomains; 33 34 import com.sun.enterprise.config.*; 36 import com.sun.enterprise.config.serverbeans.*; 37 import com.sun.enterprise.admin.util.ArgChecker; 38 39 public final class ConfigBeanIntrospector 40 { 41 private static final Logger sLogger = 42 LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 43 44 public static Object instantiate(Class bean, Object [] params) 45 throws Exception 46 { 47 ArgChecker.checkValid(bean, "bean"); ArgChecker.checkValid(params, "params"); 50 Class [] paramTypes = new Class [params.length]; 51 for (int i = 0; i < params.length; i++) 52 { 53 paramTypes[i] = params[i].getClass(); 54 } 55 Constructor ctor = bean.getConstructor(paramTypes); 56 Object inst = ctor.newInstance(params); 57 return inst; 58 } 59 60 public static boolean isAttributeSupported(Class beanClass, 61 String attributeName) 62 { 63 boolean isSupported = false; 64 try 65 { 66 PropertyDescriptor pd = getPropertyDescriptor(beanClass, 67 attributeName); 68 isSupported = (pd != null); 69 } 70 catch (Exception e) 71 { 72 sLogger.throwing(ConfigBeanIntrospector.class.getName(), 73 "isAttributeSupported", e); 74 isSupported = false; 75 } 76 return isSupported; 77 } 78 79 public static boolean isAttributeReadable(Class beanClass, 80 String attributeName) 81 { 82 boolean isReadable = false; 83 try 84 { 85 Method m = getGetter(beanClass, attributeName); 86 isReadable = (m != null); 87 } 88 catch (Exception e) 89 { 90 sLogger.throwing(ConfigBeanIntrospector.class.getName(), 91 "isAttributeReadable", e); 92 isReadable = false; 93 } 94 return isReadable; 95 } 96 97 public static boolean isAttributeWritable(Class beanClass, 98 String attributeName) 99 { 100 boolean isWritable = false; 101 try 102 { 103 Method m = getSetter(beanClass, attributeName); 104 isWritable = (m != null); 105 } 106 catch (Exception e) 107 { 108 sLogger.throwing(ConfigBeanIntrospector.class.getName(), 109 "isAttributeWritable", e); 110 isWritable = false; 111 } 112 return isWritable; 113 } 114 115 public static Object invokeGetter(Object target, String attributeName) 116 throws Exception 117 { 118 ArgChecker.checkValid(target, "target"); ArgChecker.checkValid(attributeName, "attributeName"); 121 Object ret = null; 122 Class beanClass = target.getClass(); 123 Method getter = getGetter(beanClass, attributeName); 124 if (getter != null) 125 { 126 ret = getter.invoke(target, null); 127 } 128 return ret; 129 } 130 131 public static void invokeSetter(Object target, 132 String attributeName, 133 Object value) 134 throws Exception 135 { 136 ArgChecker.checkValid(target, "target"); ArgChecker.checkValid(attributeName, "attributeName"); 139 Class beanClass = target.getClass(); 140 Method setter = getSetter(beanClass, attributeName); 141 if (setter != null) 142 { 143 setter.invoke(target, new Object [] {value}); 144 } 145 } 146 147 private static Method getGetter(Class beanClass, String attributeName) 148 throws Exception 149 { 150 Method m = null; 151 PropertyDescriptor pd = getPropertyDescriptor(beanClass, attributeName); 152 if (pd != null) 153 { 154 m = pd.getReadMethod(); 155 } 156 return m; 157 } 158 159 private static Method getSetter(Class beanClass, String attributeName) 160 throws Exception 161 { 162 Method m = null; 163 PropertyDescriptor pd = getPropertyDescriptor(beanClass, attributeName); 164 if (pd != null) 165 { 166 m = pd.getWriteMethod(); 167 } 168 return m; 169 } 170 171 private static PropertyDescriptor getPropertyDescriptor(Class beanClass, 172 String attributeName) 173 throws Exception 174 { 175 PropertyDescriptor descriptor = null; 176 BeanInfo javaBeanInfo = Introspector.getBeanInfo(beanClass); 177 PropertyDescriptor[] pds = javaBeanInfo.getPropertyDescriptors(); 178 for (int i = 0; i < pds.length; i++) 179 { 180 PropertyDescriptor pd = pds[i]; 181 if (pd.getName().equals(attributeName)) 182 { 183 descriptor = pd; 184 break; 185 } 186 } 187 return descriptor; 188 } 189 190 public static void main(String [] args) throws Exception 191 { 192 ConfigContext ctx = ConfigFactory.createConfigContext( 193 "/u/ramakant/server.xml", true); 194 Object obj = null; 195 Config config = (Config)ConfigBeansFactory.getConfigBeanByXPath(ctx, ServerXPathHelper.XPATH_CONFIG); 197 198 IiopService iiopService = config.getIiopService(); 199 obj = iiopService.getIiopListenerById("orb-listener-1"); 200 201 Object value = ConfigBeanIntrospector.invokeGetter(obj, "id"); 203 sLogger.info(value.toString()); 204 try 205 { 206 ConfigBeanIntrospector.invokeGetter(obj, "abcd"); 207 } 208 catch (Exception e) 209 { 210 sLogger.info("OK " + e.getMessage()); 211 } 212 ConfigBeanIntrospector.invokeSetter(obj, "id", "surya10"); 214 ConfigBeanIntrospector.invokeSetter(obj, "port", 215 new Integer (8888).toString()); 216 217 ctx.flush(true); 218 } 219 } 220 | Popular Tags |