1 23 24 95 96 package com.sun.enterprise.admin.config; 97 98 import java.util.Enumeration ; 99 import java.util.Hashtable ; 100 import java.text.CharacterIterator ; 101 import java.text.StringCharacterIterator ; 102 103 import java.lang.reflect.Method ; 104 import java.lang.reflect.Modifier ; 105 106 import javax.management.*; 108 import javax.management.modelmbean.ModelMBeanAttributeInfo ; 109 import javax.management.modelmbean.ModelMBeanNotificationInfo ; 110 import javax.management.modelmbean.ModelMBeanConstructorInfo ; 111 import javax.management.modelmbean.ModelMBeanOperationInfo ; 112 import javax.management.modelmbean.ModelMBeanInfoSupport ; 113 import javax.management.modelmbean.ModelMBeanInfo ; 114 115 import com.sun.org.apache.commons.modeler.AttributeInfo; 116 117 import com.sun.enterprise.admin.meta.naming.MBeanNamingInfo; 119 120 import com.sun.enterprise.config.ConfigException; 122 import com.sun.enterprise.config.ConfigBean; 123 import com.sun.enterprise.config.ConfigBeansFactory; 124 import com.sun.enterprise.config.ConfigContext; 125 import com.sun.enterprise.config.serverbeans.ElementProperty; 126 import com.sun.enterprise.config.serverbeans.PropertyResolver; 127 129 public class ConfigBeanHelper 130 { 131 private static final String XPATH_SEPARATOR = "/"; 132 public final String PROPERTY_NAME_PREFIX = "property."; 133 134 private ConfigBean m_baseConfigBean; 135 private BaseConfigMBean m_mbean; 136 public ConfigBeanHelper(BaseConfigMBean mbean, ConfigBean cb) 137 { 138 m_baseConfigBean = cb; 139 m_mbean = mbean; 140 } 144 145 150 151 152 public String getAttributeValue(String name) throws MBeanException,AttributeNotFoundException 153 { 154 if(m_baseConfigBean==null) 156 return null; 157 if(name.startsWith(PROPERTY_NAME_PREFIX)) 158 { 159 return (String )getPropertyElementValue(name.substring(PROPERTY_NAME_PREFIX.length())); 161 } 162 else 163 return m_baseConfigBean.getAttributeValue(name); 164 } 165 public String setAttributeValue(String name, String value) throws MBeanException,AttributeNotFoundException 166 { 167 if(m_baseConfigBean==null) 169 return null; 170 if(name.startsWith(PROPERTY_NAME_PREFIX)) 171 { 172 setPropertyElementValue(new Attribute(name.substring(PROPERTY_NAME_PREFIX.length()), value), false); 174 } 175 else 176 m_baseConfigBean.setAttributeValue(name, value); 177 return value; 178 } 179 180 181 182 185 protected static boolean isDebugEnabled() { 186 return true; 187 } 188 189 protected static void debug(String s) { 190 System.out.println(s); 192 } 193 protected static void info(String s) { 194 System.out.println(s); 196 } 197 protected static void error(String s) { 198 System.out.println(s); 200 } 201 202 203 204 211 public Object getPropertyElementValue(String propertyName) throws MBeanException,AttributeNotFoundException 212 { 213 214 Class cl = m_baseConfigBean.getClass(); 215 ElementProperty prop; 216 try 217 { 218 Method method = cl.getDeclaredMethod("getElementPropertyByName", new Class []{Class.forName("java.lang.String")}); 219 prop = (ElementProperty)method.invoke(m_baseConfigBean, new Object []{propertyName}); 220 } 221 catch (Exception e) 222 { 223 String msg = ( "admin.server.core.mbean.config.getattribute.undefined_properties_in_base_element"+ propertyName ); 224 throw new MBeanException(new MBeanConfigException( msg )); 225 } 226 if(prop==null) { 227 String msg = ( "admin.server.core.mbean.config.getattribute_properties_not_found_in_base_element"+ propertyName ); 228 throw new MBeanException(new MBeanConfigException( msg )); 229 } 230 return prop.getValue(); 231 } 232 238 public void setPropertyElementValue(Attribute attr, boolean bAllowsEmptyValue) throws MBeanException,AttributeNotFoundException 239 { 240 String propertyName = attr.getName(); 241 String value = (String )attr.getValue(); 242 243 Class cl = m_baseConfigBean.getClass(); 244 ElementProperty prop; 245 try 246 { 247 Method method = cl.getDeclaredMethod("getElementPropertyByName", new Class []{Class.forName("java.lang.String")}); 248 prop = (ElementProperty)method.invoke(m_baseConfigBean, new Object []{propertyName}); 249 } 250 catch (Exception e) 251 { 252 String msg = ( "admin.server.core.mbean.config.setattribute_undefined_properties_in_base_element"+ propertyName ); 253 throw new MBeanException(new MBeanConfigException( msg )); 254 } 255 if(prop==null && value!=null && (bAllowsEmptyValue || !value.equals(""))) 256 { 257 prop = new ElementProperty(); 258 prop.setName(propertyName); 259 prop.setValue(value); 260 try 261 { 262 Method method = cl.getDeclaredMethod("addElementProperty", new Class []{prop.getClass()}); 263 method.invoke(m_baseConfigBean, new Object []{prop}); 264 } 265 catch (Exception e) 266 { 267 String msg = ( "admin.server.core.mbean.config.setproperty_invoke_error"+propertyName ); 268 throw new MBeanException(new MBeanConfigException( msg )); 269 } 270 } 271 else 272 { 273 if(value==null || (!bAllowsEmptyValue && value.equals(""))) 274 { 275 try 276 { 277 Method method = cl.getDeclaredMethod("removeElementProperty", new Class []{prop.getClass()}); 278 method.invoke(m_baseConfigBean, new Object []{prop}); 279 } 280 catch (Exception e) 281 { 282 String msg = ( "admin.server.core.mbean.config.setproperty_could_not_remove_propery"+ propertyName ); 283 throw new MBeanException(new MBeanConfigException( msg )); 284 } 285 } 286 else 287 prop.setValue(value); 288 } 289 290 299 } 300 301 static private ElementProperty[] getElementProperties(ConfigBean element) 302 { 303 304 Class cl = element.getClass(); 305 ElementProperty[] props = null; 306 try 307 { 308 Method method = cl.getDeclaredMethod("getElementProperty"); 309 props = (ElementProperty[])method.invoke(element); 310 } 311 catch (Exception e) 312 { 313 } 314 return props; 315 } 316 317 324 static public boolean checkIfAttributesAndPropertiesAreResolvable(ConfigBean element, String instanceName) 325 throws ConfigException 326 { 327 PropertyResolver resolver = new PropertyResolver(element.getConfigContext(), instanceName); 328 StringBuffer buf = new StringBuffer (); 329 String [] attrNames = element.getAttributeNames(); 330 if(attrNames!=null) 332 { 333 for(int i=0; i<attrNames.length; i++) 334 { 335 String value = element.getAttributeValue(attrNames[i]); 336 if(value!=null && !resolver.isResolvable(value, true)) 337 { 338 return false; 339 } 340 } 341 } 342 ElementProperty[] props = getElementProperties(element); 344 if(props!=null) 345 { 346 for(int i=0; i<props.length; i++) 347 { 348 String value = props[i].getValue(); 349 if(value!=null && !resolver.isResolvable(value, true)) 350 { 351 return false; 352 } 353 } 354 } 355 356 return true; 357 } 358 } 359 | Popular Tags |