KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > clipbuilder > html > util > DeployUtilities


1 package org.jahia.clipbuilder.html.util;
2 import java.io.*;
3 import java.util.zip.*;
4 import java.util.jar.JarOutputStream JavaDoc;
5 import java.util.jar.JarEntry JavaDoc;
6 import java.io.FileInputStream JavaDoc;
7 import java.io.FileOutputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.File JavaDoc;
11 import java.nio.channels.FileChannel JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.util.jar.JarFile JavaDoc;
14 import java.util.jar.JarOutputStream JavaDoc;
15 import java.util.zip.ZipEntry JavaDoc;
16
17 import org.jdom.*;
18 import org.jdom.input.SAXBuilder;
19 import org.jdom.output.Format;
20 import org.jdom.output.XMLOutputter;
21 /*
22  * Copyright 2000-2004 The Apache Software Foundation.
23  *
24  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
25  * use this file except in compliance with the License. You may obtain a copy of
26  * the License at
27  *
28  * http://www.apache.org/licenses/LICENSE-2.0
29  *
30  * Unless required by applicable law or agreed to in writing, software
31  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
32  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
33  * License for the specific language governing permissions and limitations under
34  * the License.
35  */

36 /**
37  * Makes a web application Deploy-ready for Jetspeed.
38  *
39  *@author <a HREF="mailto:taylor@apache.org">Dain Sundstrom </a>
40  *@author <a HREF="mailto:dsundstrom@gluecode.com">David Sean Taylor </a>
41  *@version $Id: DeployUtilities.java 102 2005-12-19 10:24:49Z ktlili $
42  */

43 public class DeployUtilities {
44
45     private final byte[] buffer = new byte[4096];
46     private static org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(DeployUtilities.class);
47
48
49     /**
50      * Constructor for the TestDeploy object
51      *
52      *@param inputName Description of Parameter
53      *@param outputName Description of Parameter
54      *@param portletName Description of Parameter
55      *@param portletDescription Description of Parameter
56      *@param clipperDocument Description of Parameter
57      *@exception Exception Description of Exception
58      */

59     public void deploy(String JavaDoc inputName, String JavaDoc outputName, String JavaDoc portletName, String JavaDoc portletDescription, Document clipperXML) throws Exception JavaDoc {
60         File JavaDoc tempFile = null;
61         JarFile JavaDoc jin = null;
62         JarOutputStream JavaDoc jout = null;
63         FileChannel JavaDoc srcChannel = null;
64         FileChannel JavaDoc dstChannel = null;
65
66         try {
67             String JavaDoc portletApplicationName = getPortletApplicationName(outputName);
68             tempFile = File.createTempFile(portletApplicationName, "");
69             tempFile.deleteOnExit();
70
71             jin = new JarFile JavaDoc(inputName);
72             jout = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(tempFile));
73
74             // copy over all of the files in the input war to the output
75
// war except for web.xml, portlet.xml, and context.xml which
76
// we parse for use later
77
Document webXml = null;
78             Document portletXml = null;
79             ZipEntry JavaDoc src;
80             InputStream JavaDoc source;
81             Enumeration JavaDoc zipEntries = jin.entries();
82             while (zipEntries.hasMoreElements()) {
83                 src = (ZipEntry JavaDoc) zipEntries.nextElement();
84                 source = jin.getInputStream(src);
85                 try {
86                     String JavaDoc target = src.getName();
87                     if ("WEB-INF/web.xml".equals(target)) {
88                         System.out.println("Found web.xml");
89                         webXml = parseXml(source);
90                     }
91                     else if ("WEB-INF/portlet.xml".equals(target)) {
92                         System.out.println("Found WEB-INF/portlet.xml");
93                         portletXml = parseXml(source);
94                         Element rootElement = portletXml.getRootElement();
95                         Element portletEle = rootElement.getChild("portlet", rootElement.getNamespace());
96                         Element portletNameEle = portletEle.getChild("portlet-name", rootElement.getNamespace());
97                         portletNameEle.setText(portletName);
98                         Element displayNameEle = portletEle.getChild("display-name", rootElement.getNamespace());
99                         displayNameEle.setText(portletName);
100                         Element descriptionEle = portletEle.getChild("description", rootElement.getNamespace());
101                         descriptionEle.setText(portletDescription);
102                     }
103
104                     else {
105
106                         addFile(target, source, jout);
107                     }
108                 }
109                 finally {
110                     source.close();
111                 }
112             }
113
114             if (webXml == null) {
115                 throw new IllegalArgumentException JavaDoc("WEB-INF/web.xml");
116             }
117             if (portletXml == null) {
118                 throw new IllegalArgumentException JavaDoc("WEB-INF/portlet.xml");
119             }
120
121             // write the web.xml, portlet.xml, and context.xml files
122
addFile("WEB-INF/web.xml", webXml, jout);
123             addFile("WEB-INF/portlet.xml", portletXml, jout);
124             addFile("WEB-INF/clipper.xml", clipperXML, jout);
125
126             jout.close();
127             jin.close();
128             jin = null;
129             jout = null;
130
131             System.out.println("Creating war " + outputName + " ...");
132             System.out.flush();
133             // Now copy the new war to its destination
134
srcChannel = new FileInputStream JavaDoc(tempFile).getChannel();
135             dstChannel = new FileOutputStream JavaDoc(outputName).getChannel();
136             dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
137             srcChannel.close();
138             srcChannel = null;
139             dstChannel.close();
140             dstChannel = null;
141             tempFile.delete();
142             tempFile = null;
143             System.out.println("War " + outputName + " created");
144             System.out.flush();
145         }
146         finally {
147             if (srcChannel != null && srcChannel.isOpen()) {
148                 try {
149                     srcChannel.close();
150                 }
151                 catch (IOException JavaDoc e1) {
152                     // ignore
153
}
154             }
155             if (dstChannel != null && dstChannel.isOpen()) {
156                 try {
157                     dstChannel.close();
158                 }
159                 catch (IOException JavaDoc e1) {
160                     // ignore
161
}
162             }
163             if (jin != null) {
164                 try {
165                     jin.close();
166                     jin = null;
167                 }
168                 catch (IOException JavaDoc e1) {
169                     // ignore
170
}
171             }
172             if (jout != null) {
173                 try {
174                     jout.close();
175                     jout = null;
176                 }
177                 catch (IOException JavaDoc e1) {
178                     // ignore
179
}
180             }
181             if (tempFile != null && tempFile.exists()) {
182                 tempFile.delete();
183             }
184         }
185     }
186
187
188     /**
189      * Gets the PortletApplicationName attribute of the TestDeploy object
190      *
191      *@param path Description of Parameter
192      *@return The PortletApplicationName value
193      */

194     protected String JavaDoc getPortletApplicationName(String JavaDoc path) {
195         File JavaDoc file = new File JavaDoc(path);
196         String JavaDoc name = file.getName();
197         String JavaDoc portletApplicationName = name;
198
199         int index = name.lastIndexOf(".");
200         if (index > -1) {
201             portletApplicationName = name.substring(0, index);
202         }
203         return portletApplicationName;
204     }
205
206
207     /**
208      * Description of the Method
209      *
210      *@param source Description of Parameter
211      *@return Description of the Returned Value
212      *@exception Exception Description of Exception
213      */

214     protected Document parseXml(InputStream JavaDoc source) throws Exception JavaDoc {
215         // Parse using the local dtds instead of remote dtds. This
216
// allows to deploy the application offline
217
SAXBuilder saxBuilder = new SAXBuilder();
218         Document document = saxBuilder.build(source);
219         return document;
220     }
221
222
223     /**
224      * Adds a feature to the File attribute of the TestDeploy object
225      *
226      *@param path The feature to be added to the File attribute
227      *@param source The feature to be added to the File attribute
228      *@param jos The feature to be added to the File attribute
229      *@exception IOException Description of Exception
230      */

231     protected void addFile(String JavaDoc path, InputStream JavaDoc source, JarOutputStream JavaDoc jos) throws IOException JavaDoc {
232         jos.putNextEntry(new ZipEntry JavaDoc(path));
233         try {
234             int count;
235             while ((count = source.read(buffer)) > 0) {
236                 jos.write(buffer, 0, count);
237             }
238         }
239         finally {
240             jos.closeEntry();
241         }
242     }
243
244
245     /**
246      * Adds a feature to the File attribute of the TestDeploy object
247      *
248      *@param path The feature to be added to the File attribute
249      *@param source The feature to be added to the File attribute
250      *@param jos The feature to be added to the File attribute
251      *@exception IOException Description of Exception
252      */

253     protected void addFile(String JavaDoc path, Document source, JarOutputStream JavaDoc jos) throws IOException JavaDoc {
254         if (source != null) {
255             jos.putNextEntry(new ZipEntry JavaDoc(path));
256             XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
257             try {
258                 xmlOutputter.output(source, jos);
259             }
260             finally {
261                 jos.closeEntry();
262             }
263         }
264     }
265
266
267     /**
268      * Gets the Instance attribute of the DeployUtilities class
269      *
270      *@return The Instance value
271      */

272     public static DeployUtilities getInstance() {
273         return new DeployUtilities();
274     }
275
276 }
277
278
Popular Tags