001: /*
002: * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.portlet.cli;
006:
007: import java.io.File;
008: import java.io.FileInputStream;
009: import java.io.FileOutputStream;
010: import java.io.InputStream;
011: import java.io.IOException;
012: import java.io.FileNotFoundException;
013:
014: import java.util.ArrayList;
015: import java.util.List;
016: import java.util.Enumeration;
017: import java.util.Properties;
018:
019: import java.util.jar.Manifest;
020: import java.util.jar.JarEntry;
021: import java.util.jar.JarFile;
022: import java.util.jar.JarOutputStream;
023:
024: import org.jdom.Document;
025: import org.jdom.Element;
026: import org.jdom.Comment;
027: import org.jdom.JDOMException;
028: import org.jdom.IllegalAddException;
029: import org.jdom.input.SAXBuilder;
030: import org.jdom.output.XMLOutputter;
031:
032: /**
033: * PDWarUpdater is responsible for inserting the
034: * Context Params, listener classes and PAE servlet
035: * definition into the web.xml of the war file and inserting
036: * the PAE jar file into the war file.
037: */
038: public class PDWarUpdater {
039:
040: private File warFile = null;
041: private JarFile jar = null;
042: private Properties configProps = null;
043:
044: private static final String PAE_LOCATION = "PAEJarLocation";
045: private static final String TLD_LOCATION = "tldLocation";
046: private static final String WEB_INF_PREFIX = "WEB-INF" + "/";
047: private static final String WEB_XML = "web.xml";
048: private static final String WEB_XML_NAME = WEB_INF_PREFIX + WEB_XML;
049: private static final String PAE_PREFIX = WEB_INF_PREFIX + "lib"
050: + "/";
051: private static final String DD_LOCATION = "DDFileLocation";
052:
053: public PDWarUpdater(File warFile, Properties props)
054: throws IOException {
055: this .warFile = warFile;
056: jar = new JarFile(warFile);
057: configProps = props;
058: }
059:
060: public JarFile getJarFile() {
061: return jar;
062: }
063:
064: public File getUpdatedWarFile(File webXMLFile) throws IOException {
065:
066: //portletappengine jar.
067: String PAEFilename = configProps.getProperty(PAE_LOCATION);
068: File paeFile = new File(PAEFilename);
069:
070: //get tld file
071: String tldFileName = configProps.getProperty(TLD_LOCATION);
072: File tldFile = new File(tldFileName);
073:
074: //location of portlet tmp dir
075: String ddLocation = configProps.getProperty(DD_LOCATION);
076:
077: // Create temp war file.
078: File tempJarFile = new File(ddLocation, warFile.getName()
079: + ".tmp");
080: tempJarFile.deleteOnExit();
081:
082: // Initialize a flag that will indicate that the jar was updated.
083: boolean jarUpdated = false;
084:
085: try {
086: // Create a temp jar file with no manifest. (The manifest will
087: // be copied when the entries are copied.)
088: Manifest jarManifest = jar.getManifest();
089: JarOutputStream tempJar = new JarOutputStream(
090: new FileOutputStream(tempJarFile));
091:
092: // Allocate a buffer for reading entry data.
093: byte[] buffer = new byte[1024];
094: int bytesRead;
095:
096: try {
097: // Open the pae file.
098: FileInputStream file = new FileInputStream(paeFile);
099:
100: try {
101: // Create a jar entry and add it to the temp jar.
102: JarEntry entry = new JarEntry(PAE_PREFIX
103: + paeFile.getName());
104: tempJar.putNextEntry(entry);
105:
106: // Read the file and write it to the jar.
107: while ((bytesRead = file.read(buffer)) != -1) {
108: tempJar.write(buffer, 0, bytesRead);
109: }
110:
111: //System.out.println(entry.getName() + " added.");
112: } finally {
113: file.close();
114: }
115:
116: //open the web.xml file
117: FileInputStream webXMLin = new FileInputStream(
118: webXMLFile);
119:
120: try {
121: // Create a jar entry and add it to the temp jar.
122: JarEntry entry = new JarEntry(WEB_XML_NAME);
123: tempJar.putNextEntry(entry);
124:
125: // Read the file and write it to the jar.
126: while ((bytesRead = webXMLin.read(buffer)) != -1) {
127: tempJar.write(buffer, 0, bytesRead);
128: }
129:
130: //System.out.println(entry.getName() + " added.");
131: } finally {
132: webXMLin.close();
133: }
134:
135: // open the tld file.
136: FileInputStream tldin = new FileInputStream(tldFile);
137: try {
138: // Create a jar entry and add it to the temp jar.
139: JarEntry entry = new JarEntry(WEB_INF_PREFIX
140: + tldFile.getName());
141: tempJar.putNextEntry(entry);
142:
143: // Read the file and write it to the jar.
144: while ((bytesRead = tldin.read(buffer)) != -1) {
145: tempJar.write(buffer, 0, bytesRead);
146: }
147:
148: //System.out.println(entry.getName() + " added.");
149: } finally {
150: tldin.close();
151: }
152:
153: // Loop through the jar entries and add them to the temp jar,
154: // skipping the entry that was added to the temp jar already.
155:
156: for (Enumeration entries = jar.entries(); entries
157: .hasMoreElements();) {
158: // Get the next entry.
159: JarEntry entry = (JarEntry) entries.nextElement();
160:
161: // If the entry has not been added already, add it.
162: if ((!entry.getName().equals(WEB_XML_NAME))
163: && (!entry.getName().equals(
164: PAE_PREFIX + paeFile.getName()))
165: && (!entry.getName().equals(
166: WEB_INF_PREFIX + tldFile.getName()))) {
167: // Get an input stream for the entry.
168: InputStream entryStream = jar
169: .getInputStream(entry);
170:
171: // Read the entry and write it to the temp jar.
172: tempJar.putNextEntry(entry);
173:
174: while ((bytesRead = entryStream.read(buffer)) != -1) {
175: tempJar.write(buffer, 0, bytesRead);
176: }
177: }
178: }
179:
180: jarUpdated = true;
181: } catch (Exception ex) {
182: PortletDeployerLocalizer.error(ex.toString());
183:
184: // Add a stub entry here, so that the jar will close without an
185: // exception.
186: tempJar.putNextEntry(new JarEntry("stub"));
187: } finally {
188: tempJar.close();
189: }
190: } finally {
191: jar.close();
192: //System.out.println(warFile.getName() + " closed.");
193:
194: // If the jar was not updated, delete the temp jar file.
195:
196: if (!jarUpdated) {
197: tempJarFile.delete();
198: }
199: }
200:
201: // If the jar was updated, delete the original jar file and rename the
202: // temp jar file to the original name.
203:
204: if (jarUpdated) {
205: //System.out.println(warFile.getName() + " updated.");
206: return tempJarFile;
207: }
208: return null;
209: }
210:
211: }
|