KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > services > binding > XSLTConfigDelegate


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.services.binding;
23
24 import java.io.StringReader JavaDoc;
25 import javax.management.Attribute JavaDoc;
26 import javax.management.MBeanServer JavaDoc;
27 import javax.management.ObjectName JavaDoc;
28 import javax.xml.transform.Transformer JavaDoc;
29 import javax.xml.transform.TransformerFactory JavaDoc;
30 import javax.xml.transform.dom.DOMResult JavaDoc;
31 import javax.xml.transform.dom.DOMSource JavaDoc;
32 import javax.xml.transform.stream.StreamSource JavaDoc;
33
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.Element JavaDoc;
36 import org.w3c.dom.NodeList JavaDoc;
37 import org.w3c.dom.Node JavaDoc;
38
39 import org.jboss.logging.Logger;
40 import org.jboss.metadata.MetaData;
41 import org.jboss.util.StringPropertyReplacer;
42
43 /** An implementation of the ServicesConfigDelegate that expects a delegate-config
44  element of the form:
45     <delegate-config portName="portAttrName" hostName="hostAttrName">
46       <xslt-config configName="ConfigurationElement"><![CDATA[
47  XSL document contents...
48 ]]>
49       </xslt-config>
50       <xslt-param name="p1">value1</xslt-param>
51     </delegate-config>
52  The portAttrName and hostAttrName are currently unused. Perhaps these should
53  be used as the names of the host and port parameters in the XSL script. Currently
54  the host and port bindings are passed into the XSL script as the 'host' and
55  'port' global parameters.
56
57  The xslt-param elements specify arbitrary XSL script parameter name/value pairs
58  that will be set on the Transformer.
59
60 @version $Revision: 37459 $
61 @author Scott.Stark@jboss.org
62  */

63 public class XSLTConfigDelegate implements ServicesConfigDelegate
64 {
65    private static Logger log = Logger.getLogger(XSLTConfigDelegate.class);
66
67    /** Take the given config and map it onto the service specified in the
68     config using JMX via the given server.
69     @param config, the service name and its config bindings
70     @param server, the JMX server to use to apply the config
71     */

72    public void applyConfig(ServiceConfig config, MBeanServer JavaDoc server) throws Exception JavaDoc
73    {
74       Element JavaDoc delegateConfig = (Element JavaDoc) config.getServiceConfigDelegateConfig();
75       if( delegateConfig == null )
76          throw new IllegalArgumentException JavaDoc("ServiceConfig.ServiceConfigDelegateConfig is null");
77
78       // Get the XSL doc
79
Element JavaDoc xslConfigElement = (Element JavaDoc) delegateConfig.getElementsByTagName("xslt-config").item(0);
80       String JavaDoc configName = xslConfigElement.getAttribute("configName");
81       Node JavaDoc xslContent = xslConfigElement.getFirstChild();
82       if( configName.length() == 0 )
83          throw new IllegalArgumentException JavaDoc("No valid configName attribute found");
84
85       // Get the DOM config from the configName attribute
86
ObjectName JavaDoc serviceName = new ObjectName JavaDoc(config.getServiceName());
87       Element JavaDoc mbeanConfig = (Element JavaDoc) server.getAttribute(serviceName, configName);
88       if( mbeanConfig == null )
89       {
90          log.debug("No value found for config attribute: "+configName);
91          return;
92       }
93
94       // Create the XSL transformer
95
String JavaDoc xslText = xslContent.getNodeValue();
96       log.trace("XSL text:"+xslText);
97       StreamSource JavaDoc xslSource = new StreamSource JavaDoc(new StringReader JavaDoc(xslText));
98       TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
99       Transformer JavaDoc transformer = factory.newTransformer(xslSource);
100
101       // Only the first binding is used as only one (host,port) pair is mapped
102
ServiceBinding[] bindings = config.getBindings();
103       if( bindings != null && bindings.length > 0 )
104       {
105          int port = bindings[0].getPort();
106          String JavaDoc host = bindings[0].getHostName();
107          // Set the host an port params to that binding values
108
if( host != null )
109          {
110             transformer.setParameter("host", host);
111             log.debug("set host parameter to:"+host);
112          }
113          transformer.setParameter("port", new Integer JavaDoc(port));
114          log.debug("set port parameter to:"+port);
115
116          // Check for any arbitrary attributes
117
NodeList JavaDoc attributes = delegateConfig.getElementsByTagName("xslt-param");
118          // xslt-param are transform parameters
119
for(int a = 0; a < attributes.getLength(); a ++)
120          {
121             Element JavaDoc attr = (Element JavaDoc) attributes.item(a);
122             String JavaDoc name = attr.getAttribute("name");
123             if( name.length() == 0 )
124                throw new IllegalArgumentException JavaDoc("attribute element #"
125                             +a+" has no name attribute");
126             String JavaDoc attrExp = MetaData.getElementContent(attr);
127             String JavaDoc attrValue = StringPropertyReplacer.replaceProperties(attrExp);
128             transformer.setParameter(name, attrValue);
129
130             log.debug("set "+name+" parameter to:"+attrValue);
131          }
132
133          // Transform the current config element
134
DOMSource JavaDoc src = new DOMSource JavaDoc(mbeanConfig);
135          DOMResult JavaDoc result = new DOMResult JavaDoc();
136          transformer.transform(src, result);
137          // Write the transformed config back to the mbean
138
Document JavaDoc newMbeanDoc = (Document JavaDoc) result.getNode();
139          Element JavaDoc newMbeanConfig = newMbeanDoc.getDocumentElement();
140          log.debug("Updating DOM attribute to: "+newMbeanConfig);
141          Attribute JavaDoc mbeanConfigAttr = new Attribute JavaDoc(configName, newMbeanConfig);
142          server.setAttribute(serviceName, mbeanConfigAttr);
143       }
144    }
145
146 }
147
Popular Tags