001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.deployment;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026:
027: import javax.management.ObjectName;
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030: import javax.xml.parsers.ParserConfigurationException;
031: import javax.xml.transform.Source;
032: import javax.xml.transform.Templates;
033: import javax.xml.transform.Transformer;
034: import javax.xml.transform.TransformerException;
035: import javax.xml.transform.TransformerFactory;
036: import javax.xml.transform.dom.DOMResult;
037: import javax.xml.transform.dom.DOMSource;
038: import javax.xml.transform.stream.StreamSource;
039:
040: import org.jboss.mx.util.MBeanProxyExt;
041: import org.jboss.system.server.ServerConfigUtil;
042: import org.jboss.util.xml.DOMWriter;
043: import org.jboss.util.xml.JBossEntityResolver;
044: import org.jboss.util.xml.JBossErrorHandler;
045: import org.w3c.dom.Document;
046: import org.xml.sax.SAXException;
047:
048: /**
049: * XSLSubDeployer
050: *
051: * @author <a href="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
052: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>
053: * @author <a href="mailto:adrian@jboss.org">Adrian Brock</a>
054: * @version <tt>$Revision: 57205 $</tt>
055: */
056: public class XSLSubDeployer extends SubDeployerSupport implements
057: XSLSubDeployerMBean {
058: protected String xslUrl;
059:
060: protected String packageSuffix;
061:
062: protected String ddSuffix;
063:
064: protected DocumentBuilderFactory dbf;
065:
066: private Templates templates;
067:
068: protected ObjectName delegateName = SARDeployerMBean.OBJECT_NAME;
069:
070: protected SubDeployer delegate;
071:
072: /** A flag indicating if deployment descriptors should be validated */
073: private boolean validateDTDs;
074:
075: public XSLSubDeployer() {
076:
077: }
078:
079: public void setXslUrl(final String xslUrl) {
080: this .xslUrl = xslUrl;
081: }
082:
083: public String getXslUrl() {
084: return xslUrl;
085: }
086:
087: public void setPackageSuffix(final String packageSuffix) {
088: this .packageSuffix = packageSuffix;
089: }
090:
091: public String getPackageSuffix() {
092: return packageSuffix;
093: }
094:
095: public void setDdSuffix(final String ddSuffix) {
096: this .ddSuffix = ddSuffix;
097: }
098:
099: public String getDdSuffix() {
100: return ddSuffix;
101: }
102:
103: public void setDelegateName(final ObjectName delegateName) {
104: this .delegateName = delegateName;
105: }
106:
107: public ObjectName getDelegateName() {
108: return delegateName;
109: }
110:
111: public boolean getValidateDTDs() {
112: return validateDTDs;
113: }
114:
115: public void setValidateDTDs(boolean validate) {
116: this .validateDTDs = validate;
117: }
118:
119: protected void createService() throws Exception {
120: super .createService();
121: delegate = (SubDeployer) MBeanProxyExt.create(
122: SubDeployer.class, delegateName, server);
123:
124: TransformerFactory tf = TransformerFactory.newInstance();
125: dbf = DocumentBuilderFactory.newInstance();
126: dbf.setNamespaceAware(true);
127: dbf.setValidating(validateDTDs);
128:
129: InputStream is = Thread.currentThread().getContextClassLoader()
130: .getResourceAsStream(xslUrl);
131: StreamSource ss = new StreamSource(is);
132: templates = tf.newTemplates(ss);
133: log.debug("Created templates: " + templates);
134: }
135:
136: protected void destroyService() throws Exception {
137: templates = null;
138: super .destroyService();
139: }
140:
141: public boolean accepts(DeploymentInfo di) {
142: String urlStr = di.url.toString();
143: return (packageSuffix != null && (urlStr
144: .endsWith(packageSuffix) || urlStr
145: .endsWith(packageSuffix + "/")))
146: || (ddSuffix != null && urlStr.endsWith(ddSuffix));
147: }
148:
149: public void init(DeploymentInfo di) throws DeploymentException {
150: if (di.document == null)
151: findDd(di);
152:
153: try {
154: Transformer trans = templates.newTransformer();
155: String urlStr = di.url.toString();
156: String shortURL = ServerConfigUtil
157: .shortUrlFromServerHome(urlStr);
158: trans
159: .setErrorListener(new JBossErrorHandler(shortURL,
160: null));
161: Source s = new DOMSource(di.document);
162: DOMResult r = new DOMResult();
163: setParameters(trans);
164:
165: trans.transform(s, r);
166:
167: di.document = (Document) r.getNode();
168: log.debug("transformed into doc: " + di.document);
169: if (log.isDebugEnabled()) {
170: String docStr = DOMWriter.printNode(di.document, true);
171: log.debug("transformed into doc: " + docStr);
172: }
173: } catch (TransformerException ce) {
174: throw new DeploymentException(
175: "Problem with xsl transformation", ce);
176: }
177: delegate.init(di);
178: }
179:
180: public void create(DeploymentInfo di) throws DeploymentException {
181: delegate.create(di);
182: }
183:
184: public void start(DeploymentInfo di) throws DeploymentException {
185: delegate.start(di);
186: }
187:
188: public void stop(DeploymentInfo di) throws DeploymentException {
189: delegate.stop(di);
190: }
191:
192: public void destroy(DeploymentInfo di) throws DeploymentException {
193: delegate.destroy(di);
194: }
195:
196: protected void setParameters(Transformer trans)
197: throws TransformerException {
198: //override to set document names etc.
199: }
200:
201: protected void findDd(DeploymentInfo di) throws DeploymentException {
202: try {
203: DocumentBuilder db = dbf.newDocumentBuilder();
204: String urlStr = di.url.toString();
205: String shortURL = ServerConfigUtil
206: .shortUrlFromServerHome(urlStr);
207: JBossEntityResolver resolver = new JBossEntityResolver();
208: db.setEntityResolver(resolver);
209: db
210: .setErrorHandler(new JBossErrorHandler(shortURL,
211: resolver));
212:
213: if (ddSuffix != null && urlStr.endsWith(ddSuffix))
214: di.document = db.parse(di.url.openStream());
215: } catch (SAXException se) {
216: throw new DeploymentException("Could not parse dd", se);
217: } catch (IOException ioe) {
218: throw new DeploymentException("Could not read dd", ioe);
219: } catch (ParserConfigurationException pce) {
220: throw new DeploymentException(
221: "Could not create document builder for dd", pce);
222: }
223: }
224: }
|