1 22 package org.jboss.system; 23 24 import java.io.IOException ; 25 import java.io.StringWriter ; 26 import java.io.Writer ; 27 import java.util.ArrayList ; 28 import java.util.Collection ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import javax.management.Attribute ; 34 import javax.management.InstanceNotFoundException ; 35 import javax.management.MBeanAttributeInfo ; 36 import javax.management.MBeanInfo ; 37 import javax.management.MBeanServer ; 38 import javax.management.ObjectName ; 39 import javax.xml.parsers.DocumentBuilder ; 40 import javax.xml.parsers.DocumentBuilderFactory ; 41 import javax.xml.transform.Transformer ; 42 import javax.xml.transform.TransformerException ; 43 import javax.xml.transform.TransformerFactory ; 44 import javax.xml.transform.dom.DOMSource ; 45 import javax.xml.transform.stream.StreamResult ; 46 47 import org.jboss.deployment.DeploymentException; 48 import org.jboss.logging.Logger; 49 import org.jboss.mx.util.JMXExceptionDecoder; 50 import org.jboss.system.metadata.ServiceAttributeMetaData; 51 import org.jboss.system.metadata.ServiceMetaData; 52 import org.jboss.system.metadata.ServiceMetaDataParser; 53 import org.jboss.system.metadata.ServiceValueContext; 54 import org.jboss.util.xml.DOMWriter; 55 import org.w3c.dom.Document ; 56 import org.w3c.dom.Element ; 57 import org.w3c.dom.Node ; 58 import org.w3c.dom.NodeList ; 59 60 71 public class ServiceConfigurator 72 { 73 74 private final MBeanServer server; 75 76 77 private final ServiceController serviceController; 78 79 80 private final ServiceCreator serviceCreator; 81 82 83 private ServiceBinding serviceBinding; 84 85 86 private static final Logger log = Logger.getLogger(ServiceConfigurator.class); 87 88 98 public static void configure(MBeanServer server, ServiceController controller, ObjectName objectName, ObjectName classLoaderName, Collection <ServiceAttributeMetaData> attrs) throws Exception 99 { 100 server = checkMBeanServer(server, controller); 101 ClassLoader cl = server.getClassLoader(classLoaderName); 102 configure(server, controller, objectName, cl, attrs); 103 } 104 105 115 public static void configure(MBeanServer server, ServiceController controller, ObjectName objectName, ClassLoader cl, Collection <ServiceAttributeMetaData> attrs) throws Exception 116 { 117 ServiceValueContext valueContext = new ServiceValueContext(server, controller, cl); 118 server = checkMBeanServer(server, controller); 119 120 HashMap <String , MBeanAttributeInfo > attributeMap = getAttributeMap(server, objectName); 121 122 for (ServiceAttributeMetaData attribute : attrs) 123 { 124 String attributeName = attribute.getName(); 125 if (attributeName == null || attributeName.length() == 0) 126 throw new DeploymentException("No or empty attribute name for " + objectName); 127 MBeanAttributeInfo attributeInfo = attributeMap.get(attributeName); 128 if (attributeInfo == null) 129 throw new DeploymentException("No Attribute found with name: " + attributeName + " for " + objectName); 130 131 valueContext.setAttributeInfo(attributeInfo); 132 Object value = attribute.getValue(valueContext); 133 try 134 { 135 log.debug(attributeName + " set to " + value + " in " + objectName); 136 server.setAttribute(objectName, new Attribute (attributeName, value)); 137 } 138 catch (Throwable t) 139 { 140 throw new DeploymentException("Exception setting attribute " + attributeName + " on mbean " + objectName, JMXExceptionDecoder.decode(t)); 141 } 142 } 143 } 144 145 152 private static MBeanServer checkMBeanServer(MBeanServer server, ServiceController controller) 153 { 154 if (server != null) 155 return server; 156 157 if (controller != null) 158 return controller.getMBeanServer(); 159 160 throw new IllegalArgumentException ("Either the server or controller must be passed"); 161 } 162 163 172 public static void applyServiceConfig(MBeanServer server, ObjectName objectName, ServiceBinding serviceBinding) throws Exception 173 { 174 try 175 { 176 serviceBinding.applyServiceConfig(objectName); 177 } 178 catch (Throwable e) 179 { 180 Throwable t = JMXExceptionDecoder.decode(e); 182 log.warn("Failed to apply service binding override for " + objectName, t); 183 } 184 } 185 186 194 private static HashMap <String , MBeanAttributeInfo > getAttributeMap(MBeanServer server, ObjectName objectName) throws Exception 195 { 196 MBeanInfo info; 197 try 198 { 199 info = server.getMBeanInfo(objectName); 200 } 201 catch (InstanceNotFoundException e) 202 { 203 throw new DeploymentException("Trying to configure nonexistent mbean: " + objectName); 205 } 206 catch (Exception e) 207 { 208 throw new DeploymentException("Could not get mbeanInfo", JMXExceptionDecoder.decode(e)); 209 } 210 if (info == null) 211 throw new DeploymentException("MBeanInfo is null for mbean: " + objectName); 212 MBeanAttributeInfo [] attributes = info.getAttributes(); 213 HashMap <String , MBeanAttributeInfo > attributeMap = new HashMap <String , MBeanAttributeInfo >(); 214 for (int i = 0; i < attributes.length; i++) 215 { 216 MBeanAttributeInfo attr = attributes[i]; 217 attributeMap.put(attr.getName(), attr); 218 } 219 220 return attributeMap; 221 } 222 223 231 public ServiceConfigurator(MBeanServer server, ServiceController serviceController, ServiceCreator serviceCreator) 232 { 233 if (server == null) 234 throw new IllegalArgumentException ("Null server"); 235 if (serviceCreator == null) 236 throw new IllegalArgumentException ("Null serverCreator"); 237 238 this.server = server; 239 this.serviceController = serviceController; 240 this.serviceCreator = serviceCreator; 241 if (serviceController != null) 242 this.serviceBinding = serviceController.getServiceBinding(); 243 } 244 245 251 public void setServiceBinding(ServiceBinding serviceBinding) 252 { 253 this.serviceBinding = serviceBinding; 254 } 255 256 268 public List <ObjectName > install(Element config, ObjectName loaderName) throws DeploymentException 269 { 270 ServiceMetaDataParser parser = new ServiceMetaDataParser(config); 272 List <ServiceMetaData> metaDatas = parser.parse(); 273 274 List <ObjectName > result = new ArrayList <ObjectName >(metaDatas.size()); 276 277 for (ServiceMetaData metaData : metaDatas) 279 { 280 ObjectName objectName = metaData.getObjectName(); 281 Collection <ServiceAttributeMetaData> attrs = metaData.getAttributes(); 282 try 284 { 285 ServiceCreator.install(server, objectName, metaData, null); 286 result.add(objectName); 287 configure(server, null, objectName, loaderName, attrs); 288 if (serviceBinding != null) 289 applyServiceConfig(server, objectName, serviceBinding); 290 } 291 catch (Throwable t) 292 { 293 for (ObjectName name : result) 295 { 296 try 297 { 298 serviceCreator.remove(name); 299 } 300 catch (Exception e) 301 { 302 log.error("Error removing mbean after failed deployment: " + name, e); 303 } 304 } 305 DeploymentException.rethrowAsDeploymentException("Error during install", t); 306 } 307 } 308 return result; 309 } 310 311 322 public static String getConfiguration(MBeanServer server, ServiceController serviceController, ObjectName [] objectNames) throws Exception 323 { 324 Writer out = new StringWriter (); 325 326 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 327 DocumentBuilder builder = factory.newDocumentBuilder(); 328 Document doc = builder.newDocument(); 329 330 Element serverElement = doc.createElement("server"); 331 332 for (int j = 0; j < objectNames.length; j++) 334 { 335 Element mbeanElement = internalGetConfiguration(doc, server, serviceController, objectNames[j]); 336 serverElement.appendChild(mbeanElement); 337 } 338 339 doc.appendChild(serverElement); 340 341 new DOMWriter(out).setPrettyprint(true).print(doc); 343 344 out.close(); 345 346 return out.toString(); 348 } 349 350 private static Element internalGetConfiguration(Document doc, MBeanServer server, ServiceController serviceController, ObjectName name) throws Exception 351 { 352 Element mbeanElement = doc.createElement("mbean"); 353 mbeanElement.setAttribute("name", name.toString()); 354 355 MBeanInfo info = server.getMBeanInfo(name); 356 mbeanElement.setAttribute("code", info.getClassName()); 357 MBeanAttributeInfo [] attributes = info.getAttributes(); 358 boolean trace = log.isTraceEnabled(); 359 for (int i = 0; i < attributes.length; i++) 360 { 361 if (trace) 362 log.trace("considering attribute: " + attributes[i]); 363 if (attributes[i].isReadable() && attributes[i].isWritable()) 364 { 365 Element attributeElement = null; 366 if (attributes[i].getType().equals("javax.management.ObjectName")) 367 { 368 attributeElement = doc.createElement("depends"); 369 attributeElement.setAttribute("optional-attribute-name", attributes[i].getName()); 370 } 371 else 372 { 373 attributeElement = doc.createElement("attribute"); 374 attributeElement.setAttribute("name", attributes[i].getName()); 375 } 376 Object value = server.getAttribute(name, attributes[i].getName()); 377 378 if (value != null) 379 { 380 if (value instanceof Element ) 381 { 382 attributeElement.appendChild(doc.importNode((Element ) value, true)); 383 } 384 else 385 { 386 attributeElement.appendChild(doc.createTextNode(value.toString())); 387 } 388 } 389 mbeanElement.appendChild(attributeElement); 390 } 391 } 392 393 ServiceContext sc = serviceController.getServiceContext(name); 394 for (Iterator i = sc.iDependOn.iterator(); i.hasNext();) 395 { 396 ServiceContext needs = (ServiceContext) i.next(); 397 Element dependsElement = doc.createElement("depends"); 398 dependsElement.appendChild(doc.createTextNode(needs.objectName.toString())); 399 mbeanElement.appendChild(dependsElement); 400 } 401 402 return mbeanElement; 403 } 404 405 414 public String getConfiguration(ObjectName [] objectNames) throws Exception 415 { 416 return getConfiguration(server, serviceController, objectNames); 417 } 418 419 431 public static StringBuffer getElementContent(Element element) throws IOException , TransformerException 432 { 433 NodeList children = element.getChildNodes(); 434 Element content = null; 435 for (int n = 0; n < children.getLength(); n++) 436 { 437 Node node = children.item(n); 438 if (node.getNodeType() == Node.ELEMENT_NODE) 439 { 440 content = (Element )node; 441 break; 442 } 443 } 444 if (content == null) 445 return null; 446 447 DOMSource source = new DOMSource (content); 449 TransformerFactory tFactory = TransformerFactory.newInstance(); 450 Transformer transformer = tFactory.newTransformer(); 451 StringWriter sw = new StringWriter (); 452 StreamResult result = new StreamResult (sw); 453 transformer.transform(source, result); 454 sw.close(); 455 return sw.getBuffer(); 456 } 457 } | Popular Tags |