KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jahia.clipbuilder.sql.util;
2
3 import java.io.*;
4 import java.util.zip.*;
5 import java.util.jar.JarOutputStream 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 /*
23  * Copyright 2000-2004 The Apache Software Foundation.
24  *
25  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
26  * use this file except in compliance with the License. You may obtain a copy of
27  * the License at
28  *
29  * http://www.apache.org/licenses/LICENSE-2.0
30  *
31  * Unless required by applicable law or agreed to in writing, software
32  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
33  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
34  * License for the specific language governing permissions and limitations under
35  * the License.
36  */

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

44 public class DeployUtilities {
45
46     private final byte[] buffer = new byte[4096];
47     private static org.apache.log4j.Logger logger =
48             org.apache.log4j.Logger.getLogger(DeployUtilities.class);
49
50
51     /**
52      * Constructor for the TestDeploy object
53      */

54
55     /**
56      * Description of the Method
57      *
58      *@param inputName Description of Parameter
59      *@param outputName Description of Parameter
60      *@param portletName Description of Parameter
61      *@param portletDescription Description of Parameter
62      *@param driver Description of Parameter
63      *@param url Description of Parameter
64      *@param databaseName Description of Parameter
65      *@param username Description of Parameter
66      *@param password Description of Parameter
67      *@param title Description of Parameter
68      *@param query Description of Parameter
69      *@exception Exception Description of Exception
70      */

71     public void deploy(String JavaDoc inputName, String JavaDoc outputName, String JavaDoc portletName,
72             String JavaDoc portletDescription, String JavaDoc driver, String JavaDoc url,
73             String JavaDoc databaseName, String JavaDoc username, String JavaDoc password,
74             String JavaDoc title, String JavaDoc query) throws Exception JavaDoc {
75         File JavaDoc tempFile = null;
76         JarFile JavaDoc jin = null;
77         JarOutputStream JavaDoc jout = null;
78         FileChannel JavaDoc srcChannel = null;
79         FileChannel JavaDoc dstChannel = null;
80
81         try {
82             String JavaDoc portletApplicationName = getPortletApplicationName(outputName);
83             tempFile = File.createTempFile(portletApplicationName, "");
84             tempFile.deleteOnExit();
85
86             jin = new JarFile JavaDoc(inputName);
87             jout = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(tempFile));
88
89             // copy over all of the files in the input war to the output
90
// war except for web.xml, portlet.xml, and context.xml which
91
// we parse for use later
92
Document webXml = null;
93             Document portletXml = null;
94             ZipEntry JavaDoc src;
95             InputStream JavaDoc source;
96             Enumeration JavaDoc zipEntries = jin.entries();
97             while (zipEntries.hasMoreElements()) {
98                 src = (ZipEntry JavaDoc) zipEntries.nextElement();
99                 source = jin.getInputStream(src);
100                 try {
101                     String JavaDoc target = src.getName();
102                     if ("WEB-INF/web.xml".equals(target)) {
103                         System.out.println("Found web.xml");
104                         webXml = parseXml(source);
105                     }
106                     else if ("WEB-INF/portlet.xml".equals(target)) {
107                         System.out.println("Found WEB-INF/portlet.xml");
108                         portletXml = parseXml(source);
109
110                         Element rootElement = portletXml.getRootElement();
111                         Element portletEle = rootElement.getChild("portlet", rootElement.getNamespace());
112                         //portlet name and description
113
Element portletNameEle = portletEle.getChild("portlet-name", rootElement.getNamespace());
114                         portletNameEle.setText(portletName);
115                         Element displayNameEle = portletEle.getChild("display-name", rootElement.getNamespace());
116                         displayNameEle.setText(portletName);
117                         Element descriptionEle = portletEle.getChild("description", rootElement.getNamespace());
118                         descriptionEle.setText(portletDescription);
119
120                         //portlet preferences
121
Element portletPreferencesEle = portletEle.getChild("portlet-preferences", rootElement.getNamespace());
122
123                         //driver
124
setDriverPref(driver, portletPreferencesEle);
125
126                         //url
127
setUrlPref(url, portletPreferencesEle);
128
129                         //databaseName
130
setDatabaseNamePref(databaseName, portletPreferencesEle);
131
132                         //user name
133
setUserNamePref(username, portletPreferencesEle);
134
135                         //user password
136
setUserpassPref(password, portletPreferencesEle);
137
138                         //title
139
setTitlePref(title, portletPreferencesEle);
140
141                         //sql query
142
setSqlQueryPref(query, portletPreferencesEle);
143
144                     }
145
146                     else {
147
148                         addFile(target, source, jout);
149                     }
150                 }
151                 finally {
152                     source.close();
153                 }
154             }
155
156             if (webXml == null) {
157                 throw new IllegalArgumentException JavaDoc("WEB-INF/web.xml");
158             }
159             if (portletXml == null) {
160                 throw new IllegalArgumentException JavaDoc("WEB-INF/portlet.xml");
161             }
162
163             // write the web.xml, portlet.xml, and context.xml files
164
addFile("WEB-INF/web.xml", webXml, jout);
165             addFile("WEB-INF/portlet.xml", portletXml, jout);
166
167             jout.close();
168             jin.close();
169             jin = null;
170             jout = null;
171
172             System.out.println("Creating war " + outputName + " ...");
173             System.out.flush();
174             // Now copy the new war to its destination
175
srcChannel = new FileInputStream JavaDoc(tempFile).getChannel();
176             dstChannel = new FileOutputStream JavaDoc(outputName).getChannel();
177             dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
178             srcChannel.close();
179             srcChannel = null;
180             dstChannel.close();
181             dstChannel = null;
182             tempFile.delete();
183             tempFile = null;
184             System.out.println("War " + outputName + " created");
185             System.out.flush();
186         }
187         finally {
188             if (srcChannel != null && srcChannel.isOpen()) {
189                 try {
190                     srcChannel.close();
191                 }
192                 catch (IOException JavaDoc e1) {
193                     // ignore
194
}
195             }
196             if (dstChannel != null && dstChannel.isOpen()) {
197                 try {
198                     dstChannel.close();
199                 }
200                 catch (IOException JavaDoc e1) {
201                     // ignore
202
}
203             }
204             if (jin != null) {
205                 try {
206                     jin.close();
207                     jin = null;
208                 }
209                 catch (IOException JavaDoc e1) {
210                     // ignore
211
}
212             }
213             if (jout != null) {
214                 try {
215                     jout.close();
216                     jout = null;
217                 }
218                 catch (IOException JavaDoc e1) {
219                     // ignore
220
}
221             }
222             if (tempFile != null && tempFile.exists()) {
223                 tempFile.delete();
224             }
225         }
226     }
227
228
229     /**
230      * Gets the PortletApplicationName attribute of the TestDeploy object
231      *
232      *@param path Description of Parameter
233      *@return The PortletApplicationName value
234      */

235     protected String JavaDoc getPortletApplicationName(String JavaDoc path) {
236         File JavaDoc file = new File JavaDoc(path);
237         String JavaDoc name = file.getName();
238         String JavaDoc portletApplicationName = name;
239
240         int index = name.lastIndexOf(".");
241         if (index > -1) {
242             portletApplicationName = name.substring(0, index);
243         }
244         return portletApplicationName;
245     }
246
247
248     /**
249      * Description of the Method
250      *
251      *@param source Description of Parameter
252      *@return Description of the Returned Value
253      *@exception Exception Description of Exception
254      */

255     protected Document parseXml(InputStream JavaDoc source) throws Exception JavaDoc {
256         // Parse using the local dtds instead of remote dtds. This
257
// allows to deploy the application offline
258
SAXBuilder saxBuilder = new SAXBuilder();
259         Document document = saxBuilder.build(source);
260         return document;
261     }
262
263
264     /**
265      * Adds a feature to the File attribute of the TestDeploy object
266      *
267      *@param path The feature to be added to the File attribute
268      *@param source The feature to be added to the File attribute
269      *@param jos The feature to be added to the File attribute
270      *@exception IOException Description of Exception
271      */

272     protected void addFile(String JavaDoc path, InputStream JavaDoc source, JarOutputStream JavaDoc jos) throws
273             IOException JavaDoc {
274         jos.putNextEntry(new ZipEntry JavaDoc(path));
275         try {
276             int count;
277             while ((count = source.read(buffer)) > 0) {
278                 jos.write(buffer, 0, count);
279             }
280         }
281         finally {
282             jos.closeEntry();
283         }
284     }
285
286
287     /**
288      * Adds a feature to the File attribute of the TestDeploy object
289      *
290      *@param path The feature to be added to the File attribute
291      *@param source The feature to be added to the File attribute
292      *@param jos The feature to be added to the File attribute
293      *@exception IOException Description of Exception
294      */

295     protected void addFile(String JavaDoc path, Document source, JarOutputStream JavaDoc jos) throws
296             IOException JavaDoc {
297         if (source != null) {
298             jos.putNextEntry(new ZipEntry JavaDoc(path));
299             XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat());
300             try {
301                 xmlOutputter.output(source, jos);
302             }
303             finally {
304                 jos.closeEntry();
305             }
306         }
307     }
308
309
310     /**
311      * Sets the SqlQueryPref attribute of the DeployUtilities object
312      *
313      *@param query The new SqlQueryPref value
314      *@param portletPreferencesEle The new SqlQueryPref value
315      */

316     private void setSqlQueryPref(String JavaDoc query, Element portletPreferencesEle) {
317         Element queryPrefEle = new Element("preference");
318         Element queryPrefNameEle = new Element("name");
319         queryPrefNameEle.setText("query");
320         Element queryPrefValueEle = new Element("value");
321         queryPrefValueEle.setText(query);
322         portletPreferencesEle.addContent(queryPrefEle);
323         queryPrefEle.addContent(queryPrefNameEle);
324         queryPrefEle.addContent(queryPrefValueEle);
325     }
326
327
328     /**
329      * Sets the TitlePref attribute of the DeployUtilities object
330      *
331      *@param title The new TitlePref value
332      *@param portletPreferencesEle The new TitlePref value
333      */

334     private void setTitlePref(String JavaDoc title, Element portletPreferencesEle) {
335         Element titlePrefEle = new Element("preference");
336         Element titlePrefNameEle = new Element("name");
337         titlePrefNameEle.setText("title");
338         Element titlePrefValueEle = new Element("value");
339         titlePrefValueEle.setText(title);
340         portletPreferencesEle.addContent(titlePrefEle);
341         titlePrefEle.addContent(titlePrefNameEle);
342         titlePrefEle.addContent(titlePrefValueEle);
343     }
344
345
346     /**
347      * Sets the UserpassPref attribute of the DeployUtilities object
348      *
349      *@param password The new UserpassPref value
350      *@param portletPreferencesEle The new UserpassPref value
351      */

352     private void setUserpassPref(String JavaDoc password, Element portletPreferencesEle) {
353         Element userpasswordPrefEle = new Element("preference");
354         Element userpasswordPrefNameEle = new Element("name");
355         userpasswordPrefNameEle.setText("userpassword");
356         Element userpasswordPrefValueEle = new Element("value");
357         userpasswordPrefValueEle.setText(password);
358         portletPreferencesEle.addContent(userpasswordPrefEle);
359         userpasswordPrefEle.addContent(userpasswordPrefNameEle);
360         userpasswordPrefEle.addContent(userpasswordPrefValueEle);
361     }
362
363
364     /**
365      * Sets the UserNamePref attribute of the DeployUtilities object
366      *
367      *@param username The new UserNamePref value
368      *@param portletPreferencesEle The new UserNamePref value
369      */

370     private void setUserNamePref(String JavaDoc username, Element portletPreferencesEle) {
371         Element userNamePrefEle = new Element("preference");
372         Element userNamePrefNameEle = new Element("name");
373         userNamePrefNameEle.setText("username");
374         Element userNamePrefValueEle = new Element("value");
375         userNamePrefValueEle.setText(username);
376         portletPreferencesEle.addContent(userNamePrefEle);
377         userNamePrefEle.addContent(userNamePrefNameEle);
378         userNamePrefEle.addContent(userNamePrefValueEle);
379     }
380
381
382     /**
383      * Sets the DatabaseNamePref attribute of the DeployUtilities object
384      *
385      *@param databaseName The new DatabaseNamePref value
386      *@param portletPreferencesEle The new DatabaseNamePref value
387      */

388     private void setDatabaseNamePref(String JavaDoc databaseName,
389             Element portletPreferencesEle) {
390         Element databaseNamePrefEle = new Element("preference");
391         Element databaseNamePrefNameEle = new Element("name");
392         databaseNamePrefNameEle.setText("databasename");
393         Element databaseNamePrefValueEle = new Element("value");
394         databaseNamePrefValueEle.setText(databaseName);
395         portletPreferencesEle.addContent(databaseNamePrefEle);
396         databaseNamePrefEle.addContent(databaseNamePrefNameEle);
397         databaseNamePrefEle.addContent(databaseNamePrefValueEle);
398     }
399
400
401     /**
402      * Sets the UrlPref attribute of the DeployUtilities object
403      *
404      *@param url The new UrlPref value
405      *@param portletPreferencesEle The new UrlPref value
406      */

407     private void setUrlPref(String JavaDoc url, Element portletPreferencesEle) {
408         Element urlPrefEle = new Element("preference");
409         Element urlPrefNameEle = new Element("name");
410         urlPrefNameEle.setText("url");
411         Element urlPrefValueEle = new Element("value");
412         urlPrefValueEle.setText(url);
413         portletPreferencesEle.addContent(urlPrefEle);
414         urlPrefEle.addContent(urlPrefNameEle);
415         urlPrefEle.addContent(urlPrefValueEle);
416     }
417
418
419     /**
420      * Sets the DriverPref attribute of the DeployUtilities object
421      *
422      *@param driver The new DriverPref value
423      *@param portletPreferencesEle The new DriverPref value
424      */

425     private void setDriverPref(String JavaDoc driver, Element portletPreferencesEle) {
426         //driver
427
Element driverPrefEle = new Element("preference");
428         Element driverPrefNameEle = new Element("name");
429         driverPrefNameEle.setText("driver");
430         Element driverPrefValueEle = new Element("value");
431         driverPrefValueEle.setText(driver);
432         portletPreferencesEle.addContent(driverPrefEle);
433         driverPrefEle.addContent(driverPrefNameEle);
434         driverPrefEle.addContent(driverPrefValueEle);
435     }
436
437
438     /**
439      * Gets the Instance attribute of the DeployUtilities class
440      *
441      *@return The Instance value
442      */

443     public static DeployUtilities getInstance() {
444         return new DeployUtilities();
445     }
446
447 }
448
Popular Tags