Service configuration helper.
| Method from org.jboss.system.ServiceConfigurator Detail: |
public static void applyServiceConfig(MBeanServer server,
ObjectName objectName,
ServiceBinding serviceBinding) throws Exception {
try
{
serviceBinding.applyServiceConfig(objectName);
}
catch (Throwable e)
{
// serviceBinding is most probably a dynamic mbean proxy
Throwable t = JMXExceptionDecoder.decode(e);
log.warn("Failed to apply service binding override for " + objectName, t);
}
}
Apply any service binding overrides |
public static void configure(MBeanServer server,
ServiceController controller,
ObjectName objectName,
ObjectName classLoaderName,
Collection attrs) throws Exception {
server = checkMBeanServer(server, controller);
ClassLoader cl = server.getClassLoader(classLoaderName);
configure(server, controller, objectName, cl, attrs);
}
|
public static void configure(MBeanServer server,
ServiceController controller,
ObjectName objectName,
ClassLoader cl,
Collection attrs) throws Exception {
ServiceValueContext valueContext = new ServiceValueContext(server, controller, cl);
server = checkMBeanServer(server, controller);
HashMap< String, MBeanAttributeInfo > attributeMap = getAttributeMap(server, objectName);
for (ServiceAttributeMetaData attribute : attrs)
{
String attributeName = attribute.getName();
if (attributeName == null || attributeName.length() == 0)
throw new DeploymentException("No or empty attribute name for " + objectName);
MBeanAttributeInfo attributeInfo = attributeMap.get(attributeName);
if (attributeInfo == null)
{
throw new DeploymentException("No Attribute found with name: " + attributeName + " for " + objectName
+", attributes: "+attributeMap.keySet());
}
valueContext.setAttributeInfo(attributeInfo);
Object value = null;
ClassLoader previous = SecurityActions.setContextClassLoader(cl);
try
{
value = attribute.getValue(valueContext);
}
finally
{
SecurityActions.resetContextClassLoader(previous);
}
try
{
log.debug(attributeName + " set to " + value + " in " + objectName);
server.setAttribute(objectName, new Attribute(attributeName, value));
}
catch (Throwable t)
{
throw new DeploymentException("Exception setting attribute " + attributeName + " on mbean " + objectName, JMXExceptionDecoder.decode(t));
}
}
}
|
public String getConfiguration(ObjectName[] objectNames) throws Exception {
return getConfiguration(server, serviceController, objectNames);
}
Builds a string that consists of the configuration elements of the
currently running MBeans registered in the server.
TODO replace with more sophisticated mbean persistence mechanism. |
public static String getConfiguration(MBeanServer server,
ServiceController serviceController,
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");
// Store attributes as XML
for (int j = 0; j < objectNames.length; j++)
{
Element mbeanElement = internalGetConfiguration(doc, server, serviceController, objectNames[j]);
serverElement.appendChild(mbeanElement);
}
doc.appendChild(serverElement);
// Write configuration
new DOMWriter(out).setPrettyprint(true).print(doc);
out.close();
// Return configuration
return out.toString();
}
Builds a string that consists of the configuration elements of the
currently running MBeans registered in the server. |
public static StringBuffer getElementContent(Element element) throws TransformerException, IOException {
NodeList children = element.getChildNodes();
Element content = null;
for (int n = 0; n < children.getLength(); n++)
{
Node node = children.item(n);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
content = (Element)node;
break;
}
}
if (content == null)
return null;
// Get a parsable representation of this elements content
DOMSource source = new DOMSource(content);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
transformer.transform(source, result);
sw.close();
return sw.getBuffer();
}
A utility method that transforms the contents of the argument element into
a StringBuffer representation that can be reparsed.
[FIXME] This is not a general DOMUtils method because of its funny contract. It does not
support multiple child elements neither can it deal with text content. |
public List install(Element config,
ObjectName loaderName) throws DeploymentException {
// Parse the xml
ServiceMetaDataParser parser = new ServiceMetaDataParser(config);
List< ServiceMetaData > metaDatas = parser.parse();
// Track the registered object names
List< ObjectName > result = new ArrayList< ObjectName >(metaDatas.size());
// Go through each mbean in the passed xml
for (ServiceMetaData metaData : metaDatas)
{
ObjectName objectName = metaData.getObjectName();
Collection< ServiceAttributeMetaData > attrs = metaData.getAttributes();
// Install and configure the mbean
try
{
ServiceCreator.install(server, objectName, metaData, null);
result.add(objectName);
configure(server, null, objectName, loaderName, attrs);
if (serviceBinding != null)
applyServiceConfig(server, objectName, serviceBinding);
}
catch (Throwable t)
{
// Something went wrong
for (ObjectName name : result)
{
try
{
serviceCreator.remove(name);
}
catch (Exception e)
{
log.error("Error removing mbean after failed deployment: " + name, e);
}
}
DeploymentException.rethrowAsDeploymentException("Error during install", t);
}
}
return result;
} Deprecated! the - service controller no longer uses the service configurator and vice-versa
The install method iterates through the mbean tags in the
supplied xml configuration and creates and configures the mbeans shown.
The mbean configuration can be nested. |
public void setServiceBinding(ServiceBinding serviceBinding) {
this.serviceBinding = serviceBinding;
}
Dynamically plug-in a ServiceBinding policy
to possibly override the configuration of a service. |