package org.jboss.services.binding;
import java.util.HashMap;
import java.beans.PropertyEditorManager;
import java.beans.PropertyEditor;
import javax.management.Attribute;
import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.MBeanInfo;
import javax.management.MBeanAttributeInfo;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.jboss.logging.Logger;
import org.jboss.metadata.MetaData;
import org.jboss.util.StringPropertyReplacer;
import org.jboss.util.Classes;
import org.jboss.deployment.DeploymentException;
public class AttributeMappingDelegate
implements ServicesConfigDelegate
{
private static Logger log = Logger.getLogger(AttributeMappingDelegate.class);
public void applyConfig(ServiceConfig config, MBeanServer server)
throws Exception
{
Element delegateConfig = (Element) config.getServiceConfigDelegateConfig();
if( delegateConfig == null )
throw new IllegalArgumentException("ServiceConfig.ServiceConfigDelegateConfig is null");
String portAttrName = delegateConfig.getAttribute("portName");
if( portAttrName.length() == 0 )
portAttrName = null;
String hostAttrName = delegateConfig.getAttribute("hostName");
if( hostAttrName.length() == 0 )
hostAttrName = null;
NodeList attributes = delegateConfig.getElementsByTagName("attribute");
ServiceBinding[] bindings = config.getBindings();
if( bindings != null && bindings.length > 0 )
{
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();
if( portAttrName != null )
{
Attribute portAttr = new Attribute(portAttrName, new Integer(port));
log.debug("setPort, name='"+portAttrName+"' value="+port);
server.setAttribute(serviceName, portAttr);
}
if( hostAttrName != null )
{
Attribute hostAttr = createAtribute(port, host, attrTypeMap,
hostAttrName, host);
log.debug("setHost, name='"+hostAttrName+"' value="+host);
server.setAttribute(serviceName, hostAttr);
}
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);
}
}
}
private Attribute createAtribute(int port, String host,
HashMap attrTypeMap, String attrName, String attrExp)
throws Exception
{
String attrText = replaceHostAndPort(attrExp, host, ""+port);
String typeName = (String) attrTypeMap.get(attrName);
if( typeName == null )
{
throw new DeploymentException("No such attribute: " + attrName);
}
Class attrType = Classes.loadClass(typeName);
PropertyEditor editor = PropertyEditorManager.findEditor(attrType);
if( editor == null )
{
String msg = "No property editor for attribute: " + attrName +
"; type=" + typeName;
throw new DeploymentException(msg);
}
editor.setAsText(attrText);
Object attrValue = editor.getValue();
log.debug("setAttribute, name='"+attrName+"', text="+attrText
+", value="+attrValue);
Attribute theAttr = new Attribute(attrName, attrValue);
return theAttr;
}
private String replaceHostAndPort(String text, String host, String port)
{
if( text == null )
return null;
StringBuffer replacement = new StringBuffer(text);
if( host == null )
host = "localhost";
String test = replacement.toString();
int index;
while( (index = test.indexOf("${host}")) >= 0 )
{
replacement.replace(index, index+7, host);
test = replacement.toString();
}
while( (index = test.indexOf("${port}")) >= 0 )
{
replacement.replace(index, index+7, port);
test = replacement.toString();
}
return StringPropertyReplacer.replaceProperties(replacement.toString());
}
}