1 22 package org.jboss.services.binding; 23 24 import java.util.HashMap ; 25 import java.beans.PropertyEditorManager ; 26 import java.beans.PropertyEditor ; 27 import javax.management.Attribute ; 28 import javax.management.MBeanServer ; 29 import javax.management.ObjectName ; 30 import javax.management.MBeanInfo ; 31 import javax.management.MBeanAttributeInfo ; 32 33 import org.w3c.dom.Element ; 34 import org.w3c.dom.NodeList ; 35 36 import org.jboss.logging.Logger; 37 import org.jboss.metadata.MetaData; 38 import org.jboss.util.StringPropertyReplacer; 39 import org.jboss.util.Classes; 40 import org.jboss.deployment.DeploymentException; 41 42 60 public class AttributeMappingDelegate 61 implements ServicesConfigDelegate 62 { 63 private static Logger log = Logger.getLogger(AttributeMappingDelegate.class); 64 65 70 public void applyConfig(ServiceConfig config, MBeanServer server) 71 throws Exception 72 { 73 Element delegateConfig = (Element ) config.getServiceConfigDelegateConfig(); 74 if( delegateConfig == null ) 75 throw new IllegalArgumentException ("ServiceConfig.ServiceConfigDelegateConfig is null"); 76 String portAttrName = delegateConfig.getAttribute("portName"); 78 if( portAttrName.length() == 0 ) 79 portAttrName = null; 80 String hostAttrName = delegateConfig.getAttribute("hostName"); 81 if( hostAttrName.length() == 0 ) 82 hostAttrName = null; 83 84 NodeList attributes = delegateConfig.getElementsByTagName("attribute"); 86 87 ServiceBinding[] bindings = config.getBindings(); 89 if( bindings != null && bindings.length > 0 ) 90 { 91 ObjectName serviceName = new ObjectName (config.getServiceName()); 93 MBeanInfo info = server.getMBeanInfo(serviceName); 94 MBeanAttributeInfo [] attrInfo = info.getAttributes(); 95 HashMap attrTypeMap = new HashMap (); 96 for(int a = 0; a < attrInfo.length; a ++) 97 { 98 MBeanAttributeInfo attr = attrInfo[a]; 99 attrTypeMap.put(attr.getName(), attr.getType()); 100 } 101 102 int port = bindings[0].getPort(); 103 String host = bindings[0].getHostName(); 104 if( portAttrName != null ) 106 { 107 Attribute portAttr = new Attribute (portAttrName, new Integer (port)); 108 log.debug("setPort, name='"+portAttrName+"' value="+port); 109 server.setAttribute(serviceName, portAttr); 110 } 111 if( hostAttrName != null ) 113 { 114 Attribute hostAttr = createAtribute(port, host, attrTypeMap, 115 hostAttrName, host); 116 log.debug("setHost, name='"+hostAttrName+"' value="+host); 117 server.setAttribute(serviceName, hostAttr); 118 } 119 120 123 for(int a = 0; a < attributes.getLength(); a ++) 124 { 125 Element attr = (Element ) attributes.item(a); 126 String name = attr.getAttribute("name"); 127 if( name.length() == 0 ) 128 throw new IllegalArgumentException ("attribute element #"+a+" has no name attribute"); 129 String attrExp = MetaData.getElementContent(attr); 130 Attribute theAttr = createAtribute(port, host, attrTypeMap, 131 name, attrExp); 132 server.setAttribute(serviceName, theAttr); 133 } 134 } 135 else 136 { 137 140 for(int a = 0; a < attributes.getLength(); a ++) 141 { 142 Element attr = (Element ) attributes.item(a); 143 String name = attr.getAttribute("name"); 144 if( name.length() == 0 ) 145 throw new IllegalArgumentException ("attribute element #"+a+" has no name attribute"); 146 String attrExp = MetaData.getElementContent(attr); 147 Attribute attribute = new Attribute (name, attrExp); 148 ObjectName serviceName = new ObjectName (config.getServiceName()); 149 server.setAttribute(serviceName, attribute); 150 } 151 } 152 } 153 154 167 private Attribute createAtribute(int port, String host, 168 HashMap attrTypeMap, String attrName, String attrExp) 169 throws Exception 170 { 171 String attrText = replaceHostAndPort(attrExp, host, ""+port); 172 String typeName = (String ) attrTypeMap.get(attrName); 173 if( typeName == null ) 174 { 175 throw new DeploymentException("No such attribute: " + attrName); 176 } 177 Class attrType = Classes.loadClass(typeName); 179 PropertyEditor editor = PropertyEditorManager.findEditor(attrType); 180 if( editor == null ) 181 { 182 String msg = "No property editor for attribute: " + attrName + 183 "; type=" + typeName; 184 throw new DeploymentException(msg); 185 } 186 editor.setAsText(attrText); 187 Object attrValue = editor.getValue(); 188 log.debug("setAttribute, name='"+attrName+"', text="+attrText 189 +", value="+attrValue); 190 Attribute theAttr = new Attribute (attrName, attrValue); 191 return theAttr; 192 } 193 194 202 private String replaceHostAndPort(String text, String host, String port) 203 { 204 if( text == null ) 205 return null; 206 207 StringBuffer replacement = new StringBuffer (text); 208 if( host == null ) 209 host = "localhost"; 210 String test = replacement.toString(); 212 int index; 213 while( (index = test.indexOf("${host}")) >= 0 ) 214 { 215 replacement.replace(index, index+7, host); 216 test = replacement.toString(); 217 } 218 while( (index = test.indexOf("${port}")) >= 0 ) 219 { 220 replacement.replace(index, index+7, port); 221 test = replacement.toString(); 222 } 223 return StringPropertyReplacer.replaceProperties(replacement.toString()); 224 } 225 } 226 | Popular Tags |