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.varia.deployment.convertor;
023:
024: import java.io.ByteArrayInputStream;
025: import java.io.ByteArrayOutputStream;
026: import java.io.File;
027: import java.io.FileFilter;
028: import java.io.FileInputStream;
029: import java.io.FileOutputStream;
030: import java.io.InputStream;
031: import java.io.IOException;
032: import java.io.OutputStream;
033: import java.util.Properties;
034:
035: import org.jboss.logging.Logger;
036:
037: /**
038: * JarTransformer is used to transform passed in jar file.
039: * Transformation algorithm:
040: * 1. open JarInputStream on passed in Jar file,
041: * open JarOutputStream for result;
042: * 2. read next Jar entry;
043: * 3. check whether Jar entry is an XML file
044: * - if it's not, copy Jar entry to result and go to step 2.
045: * 4. check whether there is an XSL file with name equal to XML file's
046: * in classpath.
047: * - if there isn't, copy Jar entry to result and go to step 2.
048: * 5. check whether there is a properties file with the name equal to
049: * XML file's name + "-output.properties"
050: * 6. set needed xsl parameters
051: * 7. transform Jar entry with xsl template and output properties
052: * (if were found)
053: * 8. check whether there is a property "newname" in output properties
054: * - if there is, write transformed entry to result with the value
055: * of "newname";
056: * - otherwise write transformed entry to result with the original
057: * Jar entry name
058: *
059: * @author <a href="mailto:aloubyansky@hotmail.com">Alex Loubyansky</a>
060: */
061: public class JarTransformer {
062: // Attributes --------------------------------------------------------
063: private static Logger log = Logger.getLogger(JarTransformer.class);
064:
065: // Public static methods ---------------------------------------------
066: /**
067: * Applies transformations to xml sources for passed in jar file
068: */
069: public static void transform(File root, Properties globalXslParams)
070: throws Exception {
071: // local xsl params
072: Properties xslParams = new Properties(globalXslParams);
073:
074: File metaInf = new File(root, "META-INF");
075: if (!metaInf.exists()) {
076: return;
077: //throw new Exception("No META-INF directory found");
078: }
079:
080: // set path to ejb-jar.xml in xslParams
081: File ejbjar = new File(metaInf, "ejb-jar.xml");
082: if (ejbjar.exists())
083: xslParams.setProperty("ejb-jar", ejbjar.getAbsolutePath());
084:
085: // list only xml files.
086: // Note: returns null only if the path name isn't a directory
087: // or I/O exception occured
088: File[] files = metaInf.listFiles(new FileFilter() {
089: public boolean accept(File file) {
090: if (file.getName().endsWith(".xml")
091: && !file.isDirectory())
092: return true;
093: return false;
094: }
095: });
096:
097: log.debug("list XML files: " + java.util.Arrays.asList(files));
098: for (int i = 0; i < files.length; i++) {
099: File file = files[i];
100:
101: // construct names for transformation resources
102: String xmlName = file.getName();
103: String xslName = xslParams.getProperty("resources_path")
104: + xmlName.substring(0, xmlName.length() - 3)
105: + "xsl";
106: String propsName = xslParams.getProperty("resources_path")
107: + xmlName.substring(0, xmlName.length() - 4)
108: + "-output.properties";
109:
110: // try to find XSL template and open InputStream on it
111: InputStream templateIs = null;
112: try {
113: templateIs = JarTransformer.class.getClassLoader()
114: .getResource(xslName).openStream();
115: } catch (Exception e) {
116: log.debug("xsl template wasn't found for '" + xmlName
117: + "'");
118: continue;
119: }
120:
121: log.debug("Attempt to transform '" + xmlName + "' with '"
122: + xslName + "'");
123:
124: // try to load output properties
125: Properties outputProps = loadProperties(propsName);
126:
127: // transform Jar entry and write transformed data to result
128: InputStream input = null;
129: OutputStream output = null;
130: try {
131: // transformation closes the input stream, so read entry to byte[]
132: input = new FileInputStream(file);
133: byte[] bytes = readBytes(input);
134: input.close();
135: bytes = transformBytes(bytes, templateIs, outputProps,
136: xslParams);
137:
138: // Determine the new name for the transformed entry
139: String entryname = null;
140: if (outputProps != null)
141: entryname = outputProps.getProperty("newname");
142: if (entryname == null)
143: entryname = file.getName();
144:
145: output = new FileOutputStream(new File(root, entryname));
146: writeBytes(output, bytes);
147:
148: log.debug("Entry '" + file.getName()
149: + "' transformed to '" + entryname + "'");
150: } catch (Exception e) {
151: log.debug("Exception while transforming entry '"
152: + file.getName(), e);
153: } finally {
154: if (templateIs != null)
155: try {
156: templateIs.close();
157: } catch (Exception e) {
158: }
159: if (input != null)
160: try {
161: input.close();
162: } catch (Exception e) {
163: }
164: if (output != null)
165: try {
166: output.close();
167: } catch (Exception e) {
168: }
169: }
170: }
171: }
172:
173: // Private static methods ------------------------------------------
174: /**
175: * Searches for, loads and returns properties from file
176: * <code>propsName</code>
177: */
178: private static Properties loadProperties(String propsName) {
179: Properties props = new Properties();
180: InputStream propsIs = null;
181: try {
182: propsIs = JarTransformer.class.getClassLoader()
183: .getResource(propsName).openStream();
184: props.load(propsIs);
185: log.debug("Loaded properties '" + propsName + "'");
186: } catch (Exception e) {
187: log.debug("Couldn't find properties '" + propsName + "'");
188: } finally {
189: if (propsIs != null)
190: try {
191: propsIs.close();
192: } catch (Exception e) {
193: }
194: }
195: return props;
196: }
197:
198: /**
199: * Returns byte array that is the result of transformation of
200: * the passed in byte array with xsl template and output properties
201: */
202: private static byte[] transformBytes(byte[] bytes,
203: InputStream xslIs, Properties outputprops) throws Exception {
204: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
205: ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
206: try {
207: XslTransformer.applyTransformation(bais, baos, xslIs,
208: outputprops);
209: } finally {
210: if (bais != null)
211: try {
212: bais.close();
213: } catch (Exception e) {
214: }
215: if (baos != null)
216: try {
217: baos.close();
218: } catch (Exception e) {
219: }
220: }
221: return baos.toByteArray();
222: }
223:
224: /**
225: * Returns byte array that is the result of transformation of
226: * the passed in byte array with xsl template, output properties
227: * and xsl parameters
228: */
229: private static byte[] transformBytes(byte[] bytes,
230: InputStream xslIs, Properties outputProps,
231: Properties xslParams) throws Exception {
232: ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
233: ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
234: try {
235: XslTransformer.applyTransformation(bais, baos, xslIs,
236: outputProps, xslParams);
237: } finally {
238: if (bais != null)
239: try {
240: bais.close();
241: } catch (Exception e) {
242: }
243: if (baos != null)
244: try {
245: baos.close();
246: } catch (Exception e) {
247: }
248: }
249: return baos.toByteArray();
250: }
251:
252: /**
253: * Writes byte array to OutputStream.
254: */
255: private static void writeBytes(OutputStream os, byte[] bytes)
256: throws Exception {
257: os.write(bytes, 0, bytes.length);
258: os.flush();
259: }
260:
261: /**
262: * Copies bytes from InputStream to OutputStream.
263: * Returns the number of bytes copied.
264: */
265: private static int copyBytes(InputStream is, OutputStream os)
266: throws Exception {
267: byte[] buffer = readBytes(is);
268: os.write(buffer, 0, buffer.length);
269: os.flush();
270: return buffer.length;
271: }
272:
273: /**
274: * Returns byte array read from InputStream
275: */
276: private static byte[] readBytes(InputStream is) throws IOException {
277: byte[] buffer = new byte[8192];
278: ByteArrayOutputStream baos = new ByteArrayOutputStream(2048);
279: int n;
280: baos.reset();
281: try {
282: while ((n = is.read(buffer, 0, buffer.length)) != -1)
283: baos.write(buffer, 0, n);
284: buffer = baos.toByteArray();
285: } finally {
286: if (baos != null)
287: try {
288: baos.close();
289: } catch (Exception e) {
290: }
291: }
292: return buffer;
293: }
294: }
|