From: Scott M S. <st...@us...> - 2002-06-22 12:59:32
|
User: starksm Date: 02/06/22 05:59:31 Modified: varia/src/main/org/jboss/services/binding AttributeMappingDelegate.java Log: - Relax the constraint on needing the hostName and portName attributes. If they are not specified then the corresponding binding will not be applied. - Add support for setting arbitrary attributes that are a function of the host and port. Revision Changes Path 1.3 +69 -15 contrib/varia/src/main/org/jboss/services/binding/AttributeMappingDelegate.java Index: AttributeMappingDelegate.java =================================================================== RCS file: /cvsroot/jboss/contrib/varia/src/main/org/jboss/services/binding/AttributeMappingDelegate.java,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- AttributeMappingDelegate.java 21 Jun 2002 05:53:36 -0000 1.2 +++ AttributeMappingDelegate.java 22 Jun 2002 12:59:31 -0000 1.3 @@ -12,19 +12,23 @@ import javax.management.ObjectName; import org.w3c.dom.Element; +import org.w3c.dom.NodeList; import org.jboss.logging.Logger; +import org.jboss.metadata.MetaData; -/** An implementation of the ServicesConfigDelegate that expects a config - of the form: - <config portName="portAttrName" - hostName="hostAttrName" /> +/** An implementation of the ServicesConfigDelegate that expects a delegate-config + element of the form: + <delegate-config portName="portAttrName" hostName="hostAttrName"> + <attribute name="mbeanAttrName">host-port-expr</attribute> + ... + </delegate-config> where the portAttrName is the attribute name of the mbean service to which the (int port) value should be applied and the hostAttrName is the attribute name of the mbean service to which the (String virtualHost) value should be applied. -@version $Revision: 1.2 $ +@version $Revision: 1.3 $ @author Sco...@jb... */ public class AttributeMappingDelegate @@ -42,12 +46,16 @@ Element delegateConfig = 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( delegateConfig == null ) - throw new IllegalArgumentException("No portName found in ServiceConfigDelegateConfig"); + if( portAttrName.length() == 0 ) + portAttrName = null; String hostAttrName = delegateConfig.getAttribute("hostName"); - if( hostAttrName == null ) - throw new IllegalArgumentException("No hostName found in ServiceConfigDelegateConfig"); + 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(); @@ -56,13 +64,59 @@ int port = bindings[0].getPort(); String host = bindings[0].getHostName(); ObjectName serviceName = new ObjectName(config.getServiceName()); - Attribute portAttr = new Attribute(portAttrName, new Integer(port)); - log.info("setPort, name='"+portAttrName+"' value="+port); - server.setAttribute(serviceName, portAttr); - Attribute hostAttr = new Attribute(hostAttrName, host); - log.info("setHost, name='"+hostAttrName+"' value="+host); - server.setAttribute(serviceName, hostAttr); + // 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 = new Attribute(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); + String attrValue = replaceHostAndPort(attrExp, host, ""+port); + log.debug("setAttribute, name='"+name+"' value="+attrValue); + Attribute theAttr = new Attribute(name, attrValue); + server.setAttribute(serviceName, theAttr); + } } } + /** Loop over text and replace any ${host} and ${port} strings. + */ + private String replaceHostAndPort(String text, String host, String port) + { + StringBuffer replacement = new StringBuffer(text); + if( host == null ) + host = "localhost"; + // Simple looping should be replaced with regex package + 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 replacement.toString(); + } } |