1 22 package org.jboss.services.binding; 23 24 import java.io.StringReader ; 25 import javax.management.Attribute ; 26 import javax.management.MBeanServer ; 27 import javax.management.ObjectName ; 28 import javax.xml.transform.Transformer ; 29 import javax.xml.transform.TransformerFactory ; 30 import javax.xml.transform.dom.DOMResult ; 31 import javax.xml.transform.dom.DOMSource ; 32 import javax.xml.transform.stream.StreamSource ; 33 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.Element ; 36 import org.w3c.dom.NodeList ; 37 import org.w3c.dom.Node ; 38 39 import org.jboss.logging.Logger; 40 import org.jboss.metadata.MetaData; 41 import org.jboss.util.StringPropertyReplacer; 42 43 63 public class XSLTConfigDelegate implements ServicesConfigDelegate 64 { 65 private static Logger log = Logger.getLogger(XSLTConfigDelegate.class); 66 67 72 public void applyConfig(ServiceConfig config, MBeanServer server) throws Exception 73 { 74 Element delegateConfig = (Element ) config.getServiceConfigDelegateConfig(); 75 if( delegateConfig == null ) 76 throw new IllegalArgumentException ("ServiceConfig.ServiceConfigDelegateConfig is null"); 77 78 Element xslConfigElement = (Element ) delegateConfig.getElementsByTagName("xslt-config").item(0); 80 String configName = xslConfigElement.getAttribute("configName"); 81 Node xslContent = xslConfigElement.getFirstChild(); 82 if( configName.length() == 0 ) 83 throw new IllegalArgumentException ("No valid configName attribute found"); 84 85 ObjectName serviceName = new ObjectName (config.getServiceName()); 87 Element mbeanConfig = (Element ) server.getAttribute(serviceName, configName); 88 if( mbeanConfig == null ) 89 { 90 log.debug("No value found for config attribute: "+configName); 91 return; 92 } 93 94 String xslText = xslContent.getNodeValue(); 96 log.trace("XSL text:"+xslText); 97 StreamSource xslSource = new StreamSource (new StringReader (xslText)); 98 TransformerFactory factory = TransformerFactory.newInstance(); 99 Transformer transformer = factory.newTransformer(xslSource); 100 101 ServiceBinding[] bindings = config.getBindings(); 103 if( bindings != null && bindings.length > 0 ) 104 { 105 int port = bindings[0].getPort(); 106 String host = bindings[0].getHostName(); 107 if( host != null ) 109 { 110 transformer.setParameter("host", host); 111 log.debug("set host parameter to:"+host); 112 } 113 transformer.setParameter("port", new Integer (port)); 114 log.debug("set port parameter to:"+port); 115 116 NodeList attributes = delegateConfig.getElementsByTagName("xslt-param"); 118 for(int a = 0; a < attributes.getLength(); a ++) 120 { 121 Element attr = (Element ) attributes.item(a); 122 String name = attr.getAttribute("name"); 123 if( name.length() == 0 ) 124 throw new IllegalArgumentException ("attribute element #" 125 +a+" has no name attribute"); 126 String attrExp = MetaData.getElementContent(attr); 127 String attrValue = StringPropertyReplacer.replaceProperties(attrExp); 128 transformer.setParameter(name, attrValue); 129 130 log.debug("set "+name+" parameter to:"+attrValue); 131 } 132 133 DOMSource src = new DOMSource (mbeanConfig); 135 DOMResult result = new DOMResult (); 136 transformer.transform(src, result); 137 Document newMbeanDoc = (Document ) result.getNode(); 139 Element newMbeanConfig = newMbeanDoc.getDocumentElement(); 140 log.debug("Updating DOM attribute to: "+newMbeanConfig); 141 Attribute mbeanConfigAttr = new Attribute (configName, newMbeanConfig); 142 server.setAttribute(serviceName, mbeanConfigAttr); 143 } 144 } 145 146 } 147 | Popular Tags |