KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > deployment > XSLSubDeployer


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 JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 import javax.management.ObjectName JavaDoc;
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31 import javax.xml.transform.Source JavaDoc;
32 import javax.xml.transform.Templates JavaDoc;
33 import javax.xml.transform.Transformer JavaDoc;
34 import javax.xml.transform.TransformerException JavaDoc;
35 import javax.xml.transform.TransformerFactory JavaDoc;
36 import javax.xml.transform.dom.DOMResult JavaDoc;
37 import javax.xml.transform.dom.DOMSource JavaDoc;
38 import javax.xml.transform.stream.StreamSource JavaDoc;
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 JavaDoc;
46 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc xslUrl;
59
60    protected String JavaDoc packageSuffix;
61
62    protected String JavaDoc ddSuffix;
63
64    protected DocumentBuilderFactory JavaDoc dbf;
65
66    private Templates JavaDoc templates;
67
68    protected ObjectName JavaDoc 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 JavaDoc xslUrl)
81    {
82       this.xslUrl = xslUrl;
83    }
84
85    public String JavaDoc getXslUrl()
86    {
87       return xslUrl;
88    }
89
90    public void setPackageSuffix(final String JavaDoc packageSuffix)
91    {
92       this.packageSuffix = packageSuffix;
93    }
94
95    public String JavaDoc getPackageSuffix()
96    {
97       return packageSuffix;
98    }
99
100    public void setDdSuffix(final String JavaDoc ddSuffix)
101    {
102       this.ddSuffix = ddSuffix;
103    }
104
105    public String JavaDoc getDdSuffix()
106    {
107       return ddSuffix;
108    }
109
110    public void setDelegateName(final ObjectName JavaDoc delegateName)
111    {
112       this.delegateName = delegateName;
113    }
114
115    public ObjectName JavaDoc 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 JavaDoc
130    {
131       super.createService();
132       delegate = (SubDeployer) MBeanProxyExt.create(SubDeployer.class, delegateName, server);
133
134       TransformerFactory JavaDoc tf = TransformerFactory.newInstance();
135       dbf = DocumentBuilderFactory.newInstance();
136       dbf.setNamespaceAware(true);
137       dbf.setValidating(validateDTDs);
138       
139       InputStream JavaDoc is = Thread.currentThread().getContextClassLoader().getResourceAsStream(xslUrl);
140       StreamSource JavaDoc ss = new StreamSource JavaDoc(is);
141       templates = tf.newTemplates(ss);
142       log.debug("Created templates: " + templates);
143    }
144
145    protected void destroyService() throws Exception JavaDoc
146    {
147       templates = null;
148       super.destroyService();
149    }
150
151    public boolean accepts(DeploymentInfo di)
152    {
153       String JavaDoc 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 JavaDoc trans = templates.newTransformer();
166          String JavaDoc urlStr = di.url.toString();
167          String JavaDoc shortURL = ServerConfigUtil.shortUrlFromServerHome(urlStr);
168          trans.setErrorListener(new JBossErrorHandler(shortURL, null));
169          Source JavaDoc s = new DOMSource JavaDoc(di.document);
170          DOMResult JavaDoc r = new DOMResult JavaDoc();
171          setParameters(trans);
172          
173          trans.transform(s, r);
174          
175          di.document = (Document JavaDoc) r.getNode();
176          log.debug("transformed into doc: " + di.document);
177          if (log.isDebugEnabled())
178          {
179             String JavaDoc docStr = DOMWriter.printNode(di.document, true);
180             log.debug("transformed into doc: " + docStr);
181          }
182       }
183       catch (TransformerException JavaDoc 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 JavaDoc trans) throws TransformerException JavaDoc
211    {
212       //override to set document names etc.
213
}
214
215    protected void findDd(DeploymentInfo di) throws DeploymentException
216    {
217       try
218       {
219          DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
220          String JavaDoc urlStr = di.url.toString();
221          String JavaDoc 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 JavaDoc se)
230       {
231          throw new DeploymentException("Could not parse dd", se);
232       }
233       catch (IOException JavaDoc ioe)
234       {
235          throw new DeploymentException("Could not read dd", ioe);
236       }
237       catch (ParserConfigurationException JavaDoc pce)
238       {
239          throw new DeploymentException("Could not create document builder for dd", pce);
240       }
241    }
242 }
243
Popular Tags