KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.StringReader JavaDoc;
27
28 import javax.management.Attribute JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import javax.xml.transform.Result JavaDoc;
32 import javax.xml.transform.Source JavaDoc;
33 import javax.xml.transform.Transformer JavaDoc;
34 import javax.xml.transform.TransformerFactory JavaDoc;
35 import javax.xml.transform.stream.StreamResult JavaDoc;
36 import javax.xml.transform.stream.StreamSource JavaDoc;
37
38 import org.jboss.logging.Logger;
39 import org.jboss.system.server.ServerConfig;
40 import org.jboss.metadata.MetaData;
41 import org.jboss.util.StringPropertyReplacer;
42 import org.w3c.dom.Element JavaDoc;
43 import org.w3c.dom.NodeList JavaDoc;
44
45 /**
46  * An implementation of the ServicesConfigDelegate
47  * that transforms an xml file used by a service.
48  *
49  * It retrieves the file location from the service,
50  * tranforms the file and saves it on a temporary location,
51  * which will be then applied to the service.
52  *
53  * It excpects a delegate-config element of the following form:
54  *
55  * <delegate-config>
56  * <xslt-config configName="jmx_filename_attribute"><![CDATA[
57  * XSL document contents...
58  * ]]></xslt-config>
59  * </delegate-config>
60  *
61  * The configName attribute specifies the JMX attribute,
62  * which defines the XML file to be transformed.
63  *
64  * @author wonne.keysers@realsoftware.be
65  * @author Scott.Stark@jboss.org
66  * @version $Revision: 41289 $
67  */

68 public class XSLTFileDelegate implements ServicesConfigDelegate
69 {
70    private static Logger log = Logger.getLogger(XSLTFileDelegate.class);
71
72    /** Transform the file specified in the given config,
73     * transform it, temporarily save the result and apply it onto the service
74     * specified in the config using JMX via the given server.
75     @param config, the service name and its config bindings
76     @param server, the JMX server to use to apply the config
77     */

78    public void applyConfig(ServiceConfig config, MBeanServer JavaDoc server)
79       throws Exception JavaDoc
80    {
81       Element JavaDoc delegateConfig =
82          (Element JavaDoc) config.getServiceConfigDelegateConfig();
83       if (delegateConfig == null)
84       {
85          throw new IllegalArgumentException JavaDoc("ServiceConfig.ServiceConfigDelegateConfig is null");
86       }
87
88       Element JavaDoc xslConfigElement =
89          (Element JavaDoc) delegateConfig.getElementsByTagName("xslt-config").item(0);
90       if (xslConfigElement == null)
91       {
92          throw new IllegalArgumentException JavaDoc("No valid xslt config found");
93       }
94
95       String JavaDoc configName = xslConfigElement.getAttribute("configName");
96       log.debug("configName = " + configName);
97
98       if (configName.length() == 0)
99       {
100          throw new IllegalArgumentException JavaDoc("No valid configName attribute found");
101       }
102
103       ObjectName JavaDoc serviceName = new ObjectName JavaDoc(config.getServiceName());
104       log.debug("serviceName = " + serviceName);
105
106       String JavaDoc oldValue = (String JavaDoc) server.getAttribute(serviceName, configName);
107       log.debug("oldValue = " + oldValue);
108
109       String JavaDoc tmpName = System.getProperty(ServerConfig.SERVER_TEMP_DIR);
110       File JavaDoc tempDirectory = new File JavaDoc(tmpName);
111       File JavaDoc targetFile = File.createTempFile("server", ".xml", tempDirectory);
112       targetFile.deleteOnExit();
113       log.debug("targetFile: " + targetFile.getCanonicalPath());
114
115       ServiceBinding[] bindings = config.getBindings();
116       if (bindings == null || bindings.length == 0)
117       {
118          throw new IllegalArgumentException JavaDoc("No port binding specified");
119       }
120
121       int port = bindings[0].getPort();
122       String JavaDoc host = bindings[0].getHostName();
123
124       try
125       {
126          String JavaDoc xslText = xslConfigElement.getFirstChild().getNodeValue();
127          log.trace("XSL text:" + xslText);
128          Source JavaDoc xslSource = new StreamSource JavaDoc(new StringReader JavaDoc(xslText));
129
130          Source JavaDoc xmlSource =
131             new StreamSource JavaDoc(getClass().getClassLoader().getResourceAsStream(oldValue));
132
133          Result JavaDoc xmlResult =
134             new StreamResult JavaDoc(new FileOutputStream JavaDoc(targetFile));
135
136          TransformerFactory JavaDoc factory = TransformerFactory.newInstance();
137          Transformer JavaDoc transformer = factory.newTransformer(xslSource);
138
139          transformer.setParameter("port", new Integer JavaDoc(port));
140          log.debug("set port parameter to:"+port);
141          if (host != null)
142          {
143             transformer.setParameter("host", host);
144             log.debug("set host parameter to:"+host);
145          }
146
147          // Check for any arbitrary attributes
148
NodeList JavaDoc attributes = delegateConfig.getElementsByTagName("xslt-param");
149          // xslt-param are transform parameters
150
for(int a = 0; a < attributes.getLength(); a ++)
151          {
152             Element JavaDoc attr = (Element JavaDoc) attributes.item(a);
153             String JavaDoc name = attr.getAttribute("name");
154             if( name.length() == 0 )
155                throw new IllegalArgumentException JavaDoc("attribute element #"
156                             +a+" has no name attribute");
157             String JavaDoc attrExp = MetaData.getElementContent(attr);
158             String JavaDoc attrValue = StringPropertyReplacer.replaceProperties(attrExp);
159             transformer.setParameter(name, attrValue);
160
161             log.debug("set "+name+" parameter to:"+attrValue);
162          }
163
164          transformer.transform(xmlSource, xmlResult);
165
166          Attribute JavaDoc mbeanConfigAttr =
167             new Attribute JavaDoc(configName, targetFile.getCanonicalPath());
168
169          server.setAttribute(serviceName, mbeanConfigAttr);
170       }
171       catch (Exception JavaDoc ex)
172       {
173          log.error("Error while transforming xml", ex);
174       }
175
176    }
177
178 }
179
Popular Tags