public void applyConfig(ServiceConfig config,
MBeanServer server) throws Exception {
Element delegateConfig = (Element) config.getServiceConfigDelegateConfig();
if( delegateConfig == null )
throw new IllegalArgumentException("ServiceConfig.ServiceConfigDelegateConfig is null");
// Check for a port and host name
String portAttrName = delegateConfig.getAttribute("portName");
if( portAttrName.length() == 0 )
portAttrName = null;
String hostAttrName = delegateConfig.getAttribute("hostName");
if( hostAttrName.length() == 0 )
hostAttrName = null;
// Check for any arbitrary attributes
NodeList attributes = delegateConfig.getElementsByTagName("attribute");
// Only the first binding is used as only one (host,port) pair is mapped
ServiceBinding[] bindings = config.getBindings();
if( bindings != null && bindings.length > 0 )
{
// Build a mapping of the attribute names to their type name
ObjectName serviceName = new ObjectName(config.getServiceName());
MBeanInfo info = server.getMBeanInfo(serviceName);
MBeanAttributeInfo[] attrInfo = info.getAttributes();
HashMap attrTypeMap = new HashMap();
for(int a = 0; a < attrInfo.length; a ++)
{
MBeanAttributeInfo attr = attrInfo[a];
attrTypeMap.put(attr.getName(), attr.getType());
}
int port = bindings[0].getPort();
String host = bindings[0].getHostName();
// Apply the port setting override if the port name was given
if( portAttrName != null )
{
Attribute portAttr = new Attribute(portAttrName, new Integer(port));
log.debug("setPort, name='"+portAttrName+"' value="+port);
server.setAttribute(serviceName, portAttr);
}
// Apply the host setting override if the port name was given
if( hostAttrName != null )
{
Attribute hostAttr = createAtribute(port, host, attrTypeMap,
hostAttrName, host);
log.debug("setHost, name='"+hostAttrName+"' value="+host);
server.setAttribute(serviceName, hostAttr);
}
/* Apply any other host/port based attributes with replacement of
the ${host} and ${port} strings.
*/
for(int a = 0; a < attributes.getLength(); a ++)
{
Element attr = (Element) attributes.item(a);
String name = attr.getAttribute("name");
if( name.length() == 0 )
throw new IllegalArgumentException("attribute element #"+a+" has no name attribute");
String attrExp = MetaData.getElementContent(attr);
Attribute theAttr = createAtribute(port, host, attrTypeMap,
name, attrExp);
server.setAttribute(serviceName, theAttr);
}
}
else
{
/**
* Apply attributes even if not using port or host
*/
for(int a = 0; a < attributes.getLength(); a ++)
{
Element attr = (Element) attributes.item(a);
String name = attr.getAttribute("name");
if( name.length() == 0 )
throw new IllegalArgumentException("attribute element #"+a+" has no name attribute");
String attrExp = MetaData.getElementContent(attr);
Attribute attribute = new Attribute(name, attrExp);
ObjectName serviceName = new ObjectName(config.getServiceName());
server.setAttribute(serviceName, attribute);
}
}
}
Take the given config and map it onto the service specified in the
config using JMX via the given server. |