package org.jboss.system;
import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.HashMap;
import java.util.Properties;
import javax.management.Attribute;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanInfo;
import javax.management.MBeanServer;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jboss.deployment.DeploymentException;
import org.jboss.logging.Logger;
import org.jboss.mx.util.JMXExceptionDecoder;
import org.jboss.mx.util.MBeanProxyExt;
import org.jboss.mx.util.ObjectNameFactory;
import org.jboss.util.Classes;
import org.jboss.util.StringPropertyReplacer;
import org.jboss.util.propertyeditor.PropertyEditors;
import org.jboss.util.xml.DOMWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class ServiceConfigurator
{
static
{
Class c = org.jboss.util.propertyeditor.PropertyEditors.class;
}
private final MBeanServer server;
private final ServiceController serviceController;
private final ServiceCreator serviceCreator;
private final Logger log = Logger.getLogger(getClass());
public ServiceConfigurator(final MBeanServer server,
final ServiceController serviceController,
final ServiceCreator serviceCreator)
{
this.server = server;
this.serviceController = serviceController;
this.serviceCreator = serviceCreator;
}
public List install(Element config, ObjectName loaderName) throws DeploymentException
{
List mbeans = new ArrayList();
try
{
if (config.getTagName().equals("mbean"))
{
internalInstall(config, mbeans, loaderName, true);
}
else
{
NodeList nl = config.getChildNodes();
for (int i = 0; i < nl.getLength(); i++)
{
if (nl.item(i).getNodeType() == Node.ELEMENT_NODE)
{
Element element = (Element) nl.item(i);
if (element.getTagName().equals("mbean"))
{
Element mbean = (Element) nl.item(i);
internalInstall(mbean, mbeans, loaderName, true);
} } } } return mbeans;
}
catch (Exception e)
{
for (ListIterator li = mbeans.listIterator(mbeans.size()); li.hasPrevious();)
{
ObjectName mbean = (ObjectName) li.previous();
try
{
serviceCreator.remove(mbean);
}
catch (Exception n)
{
log.error("exception removing mbean after failed deployment: " + mbean, n);
}
}
if (e instanceof DeploymentException)
throw (DeploymentException) e;
throw new DeploymentException(e);
}
}
private ObjectName internalInstall(Element mbeanElement, List mbeans,
ObjectName loaderName, boolean replace) throws Exception
{
ObjectInstance instance = null;
ObjectName mbeanName = parseObjectName(mbeanElement, replace);
instance = serviceCreator.install(mbeanName, loaderName, mbeanElement);
mbeanName = instance.getObjectName();
mbeans.add(mbeanName);
if (mbeanName != null)
{
ServiceContext ctx = serviceController.createServiceContext(mbeanName);
try
{
configure(mbeanName, loaderName, mbeanElement, mbeans);
ctx.state = ServiceContext.CONFIGURED;
ctx.problem = null;
}
catch (Exception e)
{
ctx.state = ServiceContext.FAILED;
ctx.problem = e;
log.info("Problem configuring service " + mbeanName, e);
}
}
return mbeanName;
}
protected void configure(ObjectName objectName, ObjectName loaderName,
Element mbeanElement, List mbeans)
throws Exception
{
boolean debug = log.isDebugEnabled();
MBeanInfo info;
try
{
info = server.getMBeanInfo(objectName);
}
catch (InstanceNotFoundException e)
{
throw new DeploymentException("trying to configure nonexistent mbean: " + objectName);
}
catch (Exception e)
{
throw new DeploymentException("Could not get mbeanInfo", JMXExceptionDecoder.decode(e));
}
if (info == null)
{
throw new DeploymentException("MBeanInfo is null for mbean: " + objectName);
}
ClassLoader cl = Thread.currentThread().getContextClassLoader();
MBeanAttributeInfo[] attributes = info.getAttributes();
HashMap attributeMap = new HashMap();
for (int i = 0; i < attributes.length; i++)
{
MBeanAttributeInfo attr = attributes[i];
attributeMap.put(attr.getName(), attr);
}
NodeList attrs = mbeanElement.getChildNodes();
for (int j = 0; j < attrs.getLength(); j++)
{
if (attrs.item(j).getNodeType() != Node.ELEMENT_NODE)
{
continue;
}
Element element = (Element) attrs.item(j);
boolean replace = true;
if (element.getTagName().equals("attribute"))
{
String attributeName = element.getAttribute("name");
boolean trim = true;
String replaceAttr = element.getAttribute("replace");
if (replaceAttr.length() > 0)
replace = Boolean.valueOf(replaceAttr).booleanValue();
String trimAttr = element.getAttribute("trim");
if (trimAttr.length() > 0)
trim = Boolean.valueOf(trimAttr).booleanValue();
String serialDataType = element.getAttribute("serialDataType");
MBeanAttributeInfo attr = (MBeanAttributeInfo) attributeMap.get(attributeName);
if (attr == null)
throw new DeploymentException("No Attribute found with name: " + attributeName);
if (element.hasChildNodes())
{
Object value = null;
if (serialDataType.equals("javaBean"))
value = parseJavaBeanSerialData(attr, cl, element, replace, trim);
else
value = parseTextSerialData(attr, cl, element, replace, trim);
log.debug(attributeName + " set to " + value + " in " + objectName);
setAttribute(objectName, new Attribute(attributeName, value));
}
}
else if (element.getTagName().equals("depends"))
{
if (!element.hasChildNodes())
{
throw new DeploymentException("No ObjectName supplied for depends in " + objectName);
}
String mbeanRefName = element.getAttribute("optional-attribute-name");
if ("".equals(mbeanRefName))
mbeanRefName = null;
else
mbeanRefName = StringPropertyReplacer.replaceProperties(mbeanRefName);
String proxyType = element.getAttribute("proxy-type");
if ("".equals(proxyType))
proxyType = null;
else
proxyType = StringPropertyReplacer.replaceProperties(proxyType);
ObjectName dependsObjectName = processDependency(objectName, loaderName, element, mbeans, replace);
if (debug)
log.debug("considering " + ((mbeanRefName == null) ? "<anonymous>" : mbeanRefName.toString()) + " with object name " + dependsObjectName);
if (mbeanRefName != null)
{
Object attribute = dependsObjectName;
if (proxyType != null)
{
if (mbeanRefName == null)
throw new DeploymentException("You cannot use a proxy-type without an optional-attribute-name");
if (proxyType.equals("attribute"))
{
MBeanAttributeInfo attr = (MBeanAttributeInfo) attributeMap.get(mbeanRefName);
if (attr == null)
throw new DeploymentException("No Attribute found with name: " + mbeanRefName);
proxyType = attr.getType();
}
Class proxyClass = cl.loadClass(proxyType);
attribute = MBeanProxyExt.create(proxyClass, dependsObjectName, server);
}
setAttribute(objectName, new Attribute(mbeanRefName, attribute));
} }
else if (element.getTagName().equals("depends-list"))
{
String dependsListName = element.getAttribute("optional-attribute-name");
if ("".equals(dependsListName))
{
dependsListName = null;
}
NodeList dependsList = element.getChildNodes();
ArrayList dependsListNames = new ArrayList();
for (int l = 0; l < dependsList.getLength(); l++)
{
if (dependsList.item(l).getNodeType() != Node.ELEMENT_NODE)
{
continue;
}
Element dependsElement = (Element) dependsList.item(l);
if (dependsElement.getTagName().equals("depends-list-element"))
{
if (!dependsElement.hasChildNodes())
{
throw new DeploymentException("Empty depends-list-element!");
}
ObjectName dependsObjectName = processDependency(objectName, loaderName, dependsElement, mbeans, replace);
if (!dependsListNames.contains(dependsObjectName))
{
dependsListNames.add(dependsObjectName);
} }
} if (dependsListName != null)
{
setAttribute(objectName, new Attribute(dependsListName, dependsListNames));
} } }
try
{
ObjectName serviceBindingMgr = new ObjectName("jboss.system:service=ServiceBindingManager");
Object[] args = {objectName};
String[] sig = {"javax.management.ObjectName"};
server.invoke(serviceBindingMgr, "applyServiceConfig", args, sig);
}
catch (InstanceNotFoundException ignore)
{
}
catch (Exception e)
{
Throwable t = JMXExceptionDecoder.decode(e);
log.warn("Failed to apply service binding override", t);
}
}
private Object parseTextSerialData(MBeanAttributeInfo attr, ClassLoader cl,
Element element, boolean replace, boolean trim)
throws Exception
{
String attributeName = attr.getName();
String attributeText = getElementContent(element, trim, replace);
String typeName = attr.getType();
Class typeClass = Classes.getPrimitiveTypeForName(typeName);
if (typeClass == null)
{
try
{
typeClass = cl.loadClass(typeName);
}
catch (ClassNotFoundException e)
{
throw new DeploymentException
("Class not found for attribute: " + attributeName, e);
}
}
Object value = null;
if (typeClass.equals(Element.class))
{
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++)
{
Node n = nl.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE)
{
value = n;
break;
}
}
if (replace)
{
PropertyEditor editor = PropertyEditorManager.findEditor(typeClass);
if (editor == null)
{
log.warn("Cannot perform property replace on Element");
}
else
{
editor.setValue(value);
String text = editor.getAsText();
text = StringPropertyReplacer.replaceProperties(text);
editor.setAsText(text);
value = editor.getValue();
}
}
}
if (value == null)
{
PropertyEditor editor = PropertyEditorManager.findEditor(typeClass);
if (editor == null)
{
throw new DeploymentException
("No property editor for attribute: " + attributeName +
"; type=" + typeClass);
}
editor.setAsText(attributeText);
value = editor.getValue();
}
return value;
}
private Object parseJavaBeanSerialData(MBeanAttributeInfo attr, ClassLoader cl,
Element element, boolean replace, boolean trim)
throws Exception
{
String attributeClassName = element.getAttribute("attributeClass");
if( attributeClassName == null || attributeClassName.length() == 0 )
attributeClassName = attr.getType();
Class attributeClass = cl.loadClass(attributeClassName);
Object bean = attributeClass.newInstance();
NodeList properties = element.getElementsByTagName("property");
Properties beanProps = new Properties();
for(int n = 0; n < properties.getLength(); n ++)
{
Node node = properties.item(n);
if (node.getNodeType() != Node.ELEMENT_NODE)
{
continue;
}
Element property = (Element) node;
String name = property.getAttribute("name");
String value = getElementContent(property, trim, replace);
beanProps.setProperty(name, value);
}
PropertyEditors.mapJavaBeanProperties(bean, beanProps);
return bean;
}
private ObjectName processDependency(ObjectName container, ObjectName loaderName,
Element element, List mbeans, boolean replace)
throws Exception
{
ObjectName dependsObjectName = null;
NodeList nl = element.getChildNodes();
for (int i = 0; i < nl.getLength(); i++)
{
Node childNode = nl.item(i);
if (childNode.getNodeType() == Node.ELEMENT_NODE)
{
Element child = (Element) childNode;
if (child.getTagName().equals("mbean"))
{
dependsObjectName = internalInstall(child, mbeans, loaderName, replace);
break;
}
else
{
throw new DeploymentException("Non mbean child element in depends tag: " + child);
} } }
if (dependsObjectName == null)
{
String name = getElementContent(element, true, replace);
dependsObjectName = ObjectNameFactory.create(name);
}
if (dependsObjectName == null)
{
throw new DeploymentException("No object name found for attribute!");
}
serviceController.registerDependency(container, dependsObjectName);
return dependsObjectName;
}
private void setAttribute(ObjectName name, Attribute attr)
throws Exception
{
try
{
server.setAttribute(name, attr);
}
catch (Exception e)
{
throw new DeploymentException("Exception setting attribute " +
attr + " on mbean " + name, JMXExceptionDecoder.decode(e));
}
}
public String getConfiguration(ObjectName[] objectNames)
throws Exception
{
Writer out = new StringWriter();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
Element serverElement = doc.createElement("server");
for (int j = 0; j < objectNames.length; j++)
{
Element mbeanElement = internalGetConfiguration(doc, objectNames[j]);
serverElement.appendChild(mbeanElement);
}
doc.appendChild(serverElement);
(new DOMWriter(out, false)).print(doc, true);
out.close();
return out.toString();
}
Element getConfiguration(ObjectName name) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
return internalGetConfiguration(doc, name);
}
private Element internalGetConfiguration(Document doc, ObjectName name)
throws Exception
{
Element mbeanElement = doc.createElement("mbean");
mbeanElement.setAttribute("name", name.toString());
MBeanInfo info = server.getMBeanInfo(name);
mbeanElement.setAttribute("code", info.getClassName());
MBeanAttributeInfo[] attributes = info.getAttributes();
boolean trace = log.isTraceEnabled();
for (int i = 0; i < attributes.length; i++)
{
if (trace)
log.trace("considering attribute: " + attributes[i]);
if (attributes[i].isReadable() && attributes[i].isWritable())
{
Element attributeElement = null;
if (attributes[i].getType().equals("javax.management.ObjectName"))
{
attributeElement = doc.createElement("depends");
attributeElement.setAttribute("optional-attribute-name", attributes[i].getName());
}
else
{
attributeElement = doc.createElement("attribute");
attributeElement.setAttribute("name", attributes[i].getName());
}
Object value = server.getAttribute(name, attributes[i].getName());
if (value != null)
{
if (value instanceof Element)
{
attributeElement.appendChild(doc.importNode((Element) value, true));
}
else
{
attributeElement.appendChild(doc.createTextNode(value.toString()));
}
}
mbeanElement.appendChild(attributeElement);
}
}
ServiceContext sc = serviceController.getServiceContext(name);
for (Iterator i = sc.iDependOn.iterator(); i.hasNext();)
{
ServiceContext needs = (ServiceContext) i.next();
Element dependsElement = doc.createElement("depends");
dependsElement.appendChild(doc.createTextNode(needs.objectName.toString()));
mbeanElement.appendChild(dependsElement);
}
return mbeanElement;
}
private ObjectName parseObjectName(final Element element, boolean replace)
throws Exception
{
String name = element.getAttribute("name");
if (name == null || name.trim().equals(""))
{
throw new DeploymentException("MBean attribute 'name' must be given.");
}
if (replace)
name = StringPropertyReplacer.replaceProperties(name);
return new ObjectName(name);
}
private String getElementContent(Element element, boolean trim, boolean replace)
throws Exception
{
NodeList nl = element.getChildNodes();
String attributeText = "";
for (int i = 0; i < nl.getLength(); i++)
{
Node n = nl.item(i);
if (n instanceof Text)
{
attributeText += ((Text) n).getData();
}
} if (trim)
attributeText = attributeText.trim();
if (replace)
attributeText = StringPropertyReplacer.replaceProperties(attributeText);
return attributeText;
}
}