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.deployment;
23
24 import java.io.IOException;
25 import java.io.InputStream;
26
27 import javax.management.ObjectName;
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.transform.Source;
32 import javax.xml.transform.Templates;
33 import javax.xml.transform.Transformer;
34 import javax.xml.transform.TransformerException;
35 import javax.xml.transform.TransformerFactory;
36 import javax.xml.transform.dom.DOMResult;
37 import javax.xml.transform.dom.DOMSource;
38 import javax.xml.transform.stream.StreamSource;
39
40 import org.jboss.mx.util.MBeanProxyExt;
41 import org.jboss.system.server.ServerConfigUtil;
42 import org.jboss.util.xml.DOMWriter;
43 import org.jboss.util.xml.JBossEntityResolver;
44 import org.jboss.util.xml.JBossErrorHandler;
45 import org.w3c.dom.Document;
46 import org.xml.sax.SAXException;
47
48 /**
49 * XSLSubDeployer
50 *
51 * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
52 * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>
53 * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
54 * @version <tt>$Revision: 57108 $</tt>
55 */
56 public class XSLSubDeployer extends SubDeployerSupport implements XSLSubDeployerMBean
57 {
58 protected String xslUrl;
59
60 protected String packageSuffix;
61
62 protected String ddSuffix;
63
64 protected DocumentBuilderFactory dbf;
65
66 private Templates templates;
67
68 protected ObjectName delegateName = SARDeployerMBean.OBJECT_NAME;
69
70 protected SubDeployer delegate;
71
72 /** A flag indicating if deployment descriptors should be validated */
73 private boolean validateDTDs;
74
75 public XSLSubDeployer()
76 {
77
78 }
79
80 public void setXslUrl(final String xslUrl)
81 {
82 this.xslUrl = xslUrl;
83 }
84
85 public String getXslUrl()
86 {
87 return xslUrl;
88 }
89
90 public void setPackageSuffix(final String packageSuffix)
91 {
92 this.packageSuffix = packageSuffix;
93 }
94
95 public String getPackageSuffix()
96 {
97 return packageSuffix;
98 }
99
100 public void setDdSuffix(final String ddSuffix)
101 {
102 this.ddSuffix = ddSuffix;
103 }
104
105 public String getDdSuffix()
106 {
107 return ddSuffix;
108 }
109
110 public void setDelegateName(final ObjectName delegateName)
111 {
112 this.delegateName = delegateName;
113 }
114
115 public ObjectName getDelegateName()
116 {
117 return delegateName;
118 }
119 public boolean getValidateDTDs()
120 {
121 return validateDTDs;
122 }
123
124 public void setValidateDTDs(boolean validate)
125 {
126 this.validateDTDs = validate;
127 }
128
129 protected void createService() throws Exception
130 {
131 super.createService();
132 delegate = (SubDeployer) MBeanProxyExt.create(SubDeployer.class, delegateName, server);
133
134 TransformerFactory tf = TransformerFactory.newInstance();
135 dbf = DocumentBuilderFactory.newInstance();
136 dbf.setNamespaceAware(true);
137 dbf.setValidating(validateDTDs);
138
139 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(xslUrl);
140 StreamSource ss = new StreamSource(is);
141 templates = tf.newTemplates(ss);
142 log.debug("Created templates: " + templates);
143 }
144
145 protected void destroyService() throws Exception
146 {
147 templates = null;
148 super.destroyService();
149 }
150
151 public boolean accepts(DeploymentInfo di)
152 {
153 String urlStr = di.url.toString();
154 return (packageSuffix != null && (urlStr.endsWith(packageSuffix) || urlStr.endsWith(packageSuffix + "/")))
155 || (ddSuffix != null && urlStr.endsWith(ddSuffix));
156 }
157
158 public void init(DeploymentInfo di) throws DeploymentException
159 {
160 if (di.document == null)
161 findDd(di);
162
163 try
164 {
165 Transformer trans = templates.newTransformer();
166 String urlStr = di.url.toString();
167 String shortURL = ServerConfigUtil.shortUrlFromServerHome(urlStr);
168 trans.setErrorListener(new JBossErrorHandler(shortURL, null));
169 Source s = new DOMSource(di.document);
170 DOMResult r = new DOMResult();
171 setParameters(trans);
172
173 trans.transform(s, r);
174
175 di.document = (Document) r.getNode();
176 log.debug("transformed into doc: " + di.document);
177 if (log.isDebugEnabled())
178 {
179 String docStr = DOMWriter.printNode(di.document, true);
180 log.debug("transformed into doc: " + docStr);
181 }
182 }
183 catch (TransformerException ce)
184 {
185 throw new DeploymentException("Problem with xsl transformation", ce);
186 }
187 delegate.init(di);
188 }
189
190 public void create(DeploymentInfo di) throws DeploymentException
191 {
192 delegate.create(di);
193 }
194
195 public void start(DeploymentInfo di) throws DeploymentException
196 {
197 delegate.start(di);
198 }
199
200 public void stop(DeploymentInfo di) throws DeploymentException
201 {
202 delegate.stop(di);
203 }
204
205 public void destroy(DeploymentInfo di) throws DeploymentException
206 {
207 delegate.destroy(di);
208 }
209
210 protected void setParameters(Transformer trans) throws TransformerException
211 {
212 //override to set document names etc.
213 }
214
215 protected void findDd(DeploymentInfo di) throws DeploymentException
216 {
217 try
218 {
219 DocumentBuilder db = dbf.newDocumentBuilder();
220 String urlStr = di.url.toString();
221 String shortURL = ServerConfigUtil.shortUrlFromServerHome(urlStr);
222 JBossEntityResolver resolver = new JBossEntityResolver();
223 db.setEntityResolver(resolver);
224 db.setErrorHandler(new JBossErrorHandler(shortURL, resolver));
225
226 if (ddSuffix != null && urlStr.endsWith(ddSuffix))
227 di.document = db.parse(di.url.openStream());
228 }
229 catch (SAXException se)
230 {
231 throw new DeploymentException("Could not parse dd", se);
232 }
233 catch (IOException ioe)
234 {
235 throw new DeploymentException("Could not read dd", ioe);
236 }
237 catch (ParserConfigurationException pce)
238 {
239 throw new DeploymentException("Could not create document builder for dd", pce);
240 }
241 }
242 }