1 22 package org.jboss.test.system.controller.legacy; 23 24 import java.beans.PropertyEditor ; 25 import java.beans.PropertyEditorManager ; 26 import java.lang.reflect.Constructor ; 27 import java.lang.reflect.UndeclaredThrowableException ; 28 import java.net.URL ; 29 30 import javax.management.MBeanServer ; 31 import javax.management.ObjectInstance ; 32 import javax.management.ObjectName ; 33 34 import org.jboss.deployment.DeploymentException; 35 import org.jboss.logging.Logger; 36 import org.jboss.mx.service.ServiceConstants; 37 import org.jboss.mx.util.JMXExceptionDecoder; 38 import org.jboss.system.ConfigurationException; 39 import org.jboss.util.Classes; 40 import org.jboss.util.StringPropertyReplacer; 41 import org.w3c.dom.Attr ; 42 import org.w3c.dom.Element ; 43 import org.w3c.dom.NodeList ; 44 45 54 public class OldServiceCreator 55 { 56 58 59 private static final String XMBEAN_CODE = "org.jboss.mx.modelmbean.XMBean"; 60 61 62 private static final Logger log = Logger.getLogger(OldServiceCreator.class); 63 64 66 67 private MBeanServer server; 68 69 71 76 public OldServiceCreator(final MBeanServer server) 77 { 78 this.server = server; 79 } 80 81 83 86 public void shutdown() 87 { 88 this.server = null; 89 } 90 91 101 public ObjectInstance install(ObjectName mbeanName, ObjectName loaderName, 102 Element mbeanElement) throws Exception 103 { 104 if (server.isRegistered(mbeanName)) 105 { 106 throw new DeploymentException("Trying to install an already registered mbean: " + mbeanName); 107 } 108 String code = mbeanElement.getAttribute("code"); 110 if ( code == null || "".equals(code)) 111 { 112 throw new ConfigurationException("missing 'code' attribute"); 113 } 114 115 ConstructorInfo constructor = ConstructorInfo.create(mbeanElement); 117 118 String xmbeandd = null; 120 Attr xmbeanddAttr = mbeanElement.getAttributeNode("xmbean-dd"); 121 if( xmbeanddAttr != null ) 122 xmbeandd = xmbeanddAttr.getValue(); 123 String xmbeanCode = mbeanElement.getAttribute("xmbean-code"); 124 if( xmbeanCode.length() == 0 ) 125 xmbeanCode = XMBEAN_CODE; 126 127 ObjectInstance instance = null; 129 try 130 { 131 if ( xmbeandd == null ) 132 { 133 Attr itfAttr = mbeanElement.getAttributeNode("interface"); 135 if (itfAttr != null) 136 { 137 ClassLoader classLoader = server.getClassLoader(loaderName); 139 140 String itf = itfAttr.getValue(); 142 Class itfClass = classLoader.loadClass(itf); 143 log.debug("About to create bean resource: " + mbeanName + " with code: " + code); 144 Object resource = server.instantiate(code, 145 loaderName, 146 constructor.params, 147 constructor.signature); 148 log.debug("About to register StandardMBean : " + mbeanName); 150 instance = server.createMBean("javax.management.StandardMBean", 151 mbeanName, 152 loaderName, 153 new Object []{resource,itfClass}, 154 new String []{Object .class.getName(),Class .class.getName()}); 155 } 156 else 157 { 158 log.debug("About to create bean: " + mbeanName + " with code: " + code); 160 instance = server.createMBean(code, 161 mbeanName, 162 loaderName, 163 constructor.params, 164 constructor.signature); 165 } 166 } else if( xmbeandd.length() == 0 ) 168 { 169 log.debug("About to create xmbean object: " + mbeanName 171 + " with code: " + code + " with embedded descriptor"); 172 Object resource = server.instantiate(code, loaderName, 174 constructor.params, constructor.signature); 175 176 NodeList mbeans = mbeanElement.getElementsByTagName("xmbean"); 177 if( mbeans.getLength() == 0 ) 178 throw new ConfigurationException("No nested mbean element given for xmbean"); 179 Element mbeanDescriptor = (Element ) mbeans.item(0); 180 Object [] args = {resource, mbeanDescriptor, 181 ServiceConstants.PUBLIC_JBOSSMX_XMBEAN_DTD_1_0}; 182 String [] sig = {Object .class.getName(), Element .class.getName(), 183 String .class.getName()}; 184 instance = server.createMBean(xmbeanCode, 185 mbeanName, 186 loaderName, 187 args, 188 sig); 189 } 190 else 191 { 192 log.debug("About to create xmbean object: " + mbeanName 194 + " with code: " + code + " with descriptor: "+xmbeandd); 195 Object resource = server.instantiate(code, loaderName, 197 constructor.params, constructor.signature); 198 URL xmbeanddUrl = null; 200 try 201 { 202 xmbeanddUrl = resource.getClass().getClassLoader().getResource(xmbeandd); 203 } 204 catch (Exception e) 205 { 206 } if (xmbeanddUrl == null) 208 { 209 xmbeanddUrl = new URL (xmbeandd); 210 } 212 Object [] args = {resource, xmbeanddUrl}; 214 String [] sig = {Object .class.getName(), URL .class.getName()}; 215 instance = server.createMBean(xmbeanCode, 216 mbeanName, 217 loaderName, 218 args, 219 sig); 220 } } 222 catch (Throwable e) 223 { 224 Throwable newE = JMXExceptionDecoder.decode(e); 225 226 try 228 { 229 server.unregisterMBean(mbeanName); 230 } 231 catch (Throwable ignore) 232 { 233 } 234 235 if (newE instanceof Exception ) 236 { 237 throw (Exception )newE; 238 } throw new UndeclaredThrowableException (newE); 240 } 241 242 log.debug("Created bean: "+mbeanName); 243 return instance; 244 } 245 246 public void remove(ObjectName name) throws Exception 247 { 248 String domain = name.getDomain(); 250 if (domain == null || "".equals(domain)) 251 { 252 name = new ObjectName (server.getDefaultDomain() + name); 253 } 254 255 server.unregisterMBean(name); 257 } 258 259 261 277 private static class ConstructorInfo 278 { 279 280 public static final Object EMPTY_PARAMS[] = {}; 281 282 283 public static final String EMPTY_SIGNATURE[] = {}; 284 285 286 public String [] signature = EMPTY_SIGNATURE; 287 288 289 public Object [] params = EMPTY_PARAMS; 290 291 299 public static ConstructorInfo create(Element element) 300 throws ConfigurationException 301 { 302 ConstructorInfo info = new ConstructorInfo(); 303 NodeList list = element.getElementsByTagName("constructor"); 304 if (list.getLength() > 1 && list.item(0).getParentNode() == element) 305 { 306 throw new ConfigurationException 307 ("only one <constructor> element may be defined"); 308 } 309 else if (list.getLength() == 1) 310 { 311 element = (Element )list.item(0); 312 313 list = element.getElementsByTagName("arg"); 315 int length = list.getLength(); 316 info.params = new Object [length]; 317 info.signature = new String [length]; 318 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 319 320 for (int j=0; j<length; j++) 322 { 323 Element arg = (Element )list.item(j); 324 String signature = arg.getAttribute("type"); 325 String value = arg.getAttribute("value"); 326 value = StringPropertyReplacer.replaceProperties(arg.getAttribute("value")); 328 Object realValue = value; 329 330 if( signature != null ) 331 { 332 Class typeClass = Classes.getPrimitiveTypeForName(signature); 334 if (typeClass == null) 335 { 336 try 338 { 339 typeClass = loader.loadClass(signature); 340 } 341 catch (ClassNotFoundException e) 342 { 343 throw new ConfigurationException 344 ("Class not found for type: " + signature, e); 345 } 346 } 347 348 PropertyEditor editor = PropertyEditorManager.findEditor(typeClass); 350 if (editor == null) 351 { 352 try 353 { 354 Class [] sig = {String .class}; 356 Constructor ctor = typeClass.getConstructor(sig); 357 Object [] args = {value}; 358 realValue = ctor.newInstance(args); 359 } 360 catch (Exception e) 361 { 362 throw new ConfigurationException("No property editor for type: " + typeClass); 363 } 364 } 365 else 366 { 367 editor.setAsText(value); 368 realValue = editor.getValue(); 369 } 370 } 371 info.signature[j] = signature; 372 info.params[j] = realValue; 373 } 374 } 375 376 return info; 377 } 378 } 379 380 } 381 | Popular Tags |