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.IOException;
008: import java.io.File;
009: import java.io.FileInputStream;
010: import java.io.FileOutputStream;
011: import java.io.InputStream;
012: import java.io.FileNotFoundException;
013:
014: import java.util.List;
015: import java.util.ArrayList;
016: import java.util.Map;
017: import java.util.HashMap;
018: import java.util.Properties;
019:
020: import java.util.jar.JarFile;
021: import java.util.jar.JarEntry;
022:
023: import java.util.zip.ZipEntry;
024:
025: import com.sun.portal.desktop.context.DSAMEAdminDPContext;
026:
027: /**
028: * PDDeploy is responsible for processing the command line options
029: * associated with the deploy subcommand by calling the PDWarUpdater
030: * for updating the war file and PDDPUpdater for updating the
031: * Display Profile with the provider entries created from portlet.xml
032: */
033: class PDDeploy {
034:
035: private static final String CONFIG_LOCATION_PROPERTY = "deployer.propertiesFile";
036: private static final String WEB_XML = "web.xml";
037: private static final String PORTLET_XML = "portlet.xml";
038: private static final String EXT_XML = "sun-portlet.xml";
039: private static final String WEB_INF_PREFIX = "WEB-INF" + "/";
040: private static final String DD_LOCATION = "DDFileLocation";
041: private static final String DD_SUFFIX = "_portlet.xml";
042:
043: private Properties configProps = new Properties();
044:
045: public PDDeploy() throws PortletDeployerException {
046: String configName = System
047: .getProperty(CONFIG_LOCATION_PROPERTY);
048: if (configName == null) {
049: throw new PortletDeployerException("errorConfigFile");
050: }
051: try {
052: configProps.load(new FileInputStream(configName));
053: } catch (FileNotFoundException fnfe) {
054: Object[] tokens = { fnfe.toString() };
055: throw new PortletDeployerException("errorConfigFile",
056: tokens);
057: } catch (IOException ioe) {
058: Object[] tokens = { ioe.toString() };
059: throw new PortletDeployerException("errorConfigFile",
060: tokens);
061: }
062:
063: }
064:
065: public String process(DSAMEAdminDPContext dadc, String dn,
066: boolean global, File warFile, String rolesfilename,
067: String userinfofilename, boolean verbose)
068: throws PortletDeployerException {
069:
070: //initialize PDWarUpdate
071: PDWarUpdater pdwr = null;
072: JarFile jar = null;
073: String warName = warFile.getName();
074: String portletAppName = warName.substring(0, warName
075: .indexOf('.'));
076: String ddLocation = configProps.getProperty(DD_LOCATION);
077:
078: if (verbose) {
079: PortletDeployerLocalizer.debug("dbgGettingJarFile");
080: }
081: try {
082: pdwr = new PDWarUpdater(warFile, configProps);
083: jar = pdwr.getJarFile();
084: } catch (IOException ioe) {
085: Object[] tokens = { ioe.toString() };
086: throw new PortletDeployerException("errorGettingJarFile",
087: tokens);
088: }
089:
090: if (verbose) {
091: PortletDeployerLocalizer.debug("dbgGettingPortletDD");
092: }
093: // get the portlet.xml as InputStream
094: InputStream in = null;
095: try {
096: ZipEntry portletXMLEntry = jar.getEntry(WEB_INF_PREFIX
097: + PORTLET_XML);
098: in = jar.getInputStream(portletXMLEntry);
099:
100: // copy portlet.xml file to a tmp directory
101: // to be used later for undeployment.
102: InputStream portletIn = jar.getInputStream(portletXMLEntry);
103: String ddName = portletAppName + DD_SUFFIX;
104: File portletFile = new File(ddLocation, ddName);
105: portletFile.createNewFile();
106: FileOutputStream fos = new FileOutputStream(portletFile);
107:
108: // Allocate a buffer for reading entry data.
109: byte[] buffer = new byte[1024];
110: int bytesRead;
111:
112: // Read the stream and write it to the file.
113: while ((bytesRead = portletIn.read(buffer)) != -1) {
114: fos.write(buffer, 0, bytesRead);
115: }
116:
117: } catch (IOException ioe) {
118: Object[] tokens = { ioe.toString() };
119: throw new PortletDeployerException("errorStreamRead",
120: tokens);
121: }
122: //System.out.println("PDDeploy: done getting stream from portlet file");
123:
124: //process roles file and userinfo file
125: Properties rolesProps = new Properties();
126: if (rolesfilename != null) {
127: try {
128: File rolesFile = new File(rolesfilename);
129: if (!rolesFile.exists() || !rolesFile.canRead()) {
130: throw new PortletDeployerException("errorFileRead");
131: }
132: FileInputStream rolesIn = new FileInputStream(rolesFile);
133: rolesProps.load(rolesIn);
134: rolesIn.close();
135: } catch (IOException ioe) {
136: Object[] tokens = { ioe.toString() };
137: throw new PortletDeployerException("errorFileRead",
138: tokens);
139: }
140: }
141: Properties uiProps = new Properties();
142: if (userinfofilename != null) {
143: try {
144: File uiFile = new File(userinfofilename);
145: if (!uiFile.exists() || !uiFile.canRead()) {
146: throw new PortletDeployerException("errorFileRead");
147: }
148: FileInputStream uiIn = new FileInputStream(uiFile);
149: uiProps.load(uiIn);
150: uiIn.close();
151: } catch (IOException ioe) {
152: Object[] tokens = { ioe.toString() };
153: throw new PortletDeployerException("errorFileRead",
154: tokens);
155: }
156: }
157:
158: if (verbose) {
159: PortletDeployerLocalizer.debug("dbgCreatingProviders");
160: }
161:
162: InputStream webXMLStream1 = null;
163: List roles = new ArrayList();
164: try {
165: ZipEntry webXMLEntry = jar.getEntry(WEB_INF_PREFIX
166: + WEB_XML);
167: webXMLStream1 = (InputStream) jar
168: .getInputStream(webXMLEntry);
169: roles = PDWebAppUpdater.getRoles(webXMLStream1);
170: } catch (IOException ioe) {
171: Object[] tokens = { ioe.toString() };
172: throw new PortletDeployerException("errorGettingRoles",
173: tokens);
174: }
175:
176: InputStream extStream = null;
177: try {
178: ZipEntry extXMLEntry = jar.getEntry(WEB_INF_PREFIX
179: + EXT_XML);
180: if (extXMLEntry != null) {
181: extStream = (InputStream) jar
182: .getInputStream(extXMLEntry);
183: }
184: } catch (IOException ioe) {
185: Object[] tokens = { ioe.toString() };
186: throw new PortletDeployerException("errorGettingExtension",
187: tokens);
188: }
189:
190: // Call deployment descriptor parser with portlet.xml and create provider elements.
191: PDProviderEntryGenerator providerGen = new PDProviderEntryGenerator(
192: in, extStream, configProps, portletAppName);
193: List providerElements = providerGen.createProviderElements(
194: rolesProps, uiProps, roles);
195:
196: // System.out.println("PDDeploy: done creating provider elements:" + providerElements);
197:
198: if (verbose) {
199: PortletDeployerLocalizer.debug("dbgUpdatingWebApp");
200: }
201:
202: File newWebXMLFile = null;
203: // update web.xml.
204: InputStream webXMLStream2 = null;
205: try {
206: ZipEntry webXMLEntry = jar.getEntry(WEB_INF_PREFIX
207: + WEB_XML);
208: webXMLStream2 = (InputStream) jar
209: .getInputStream(webXMLEntry);
210: //pdwr.updateWebApp(webXMLStream, portletAppName);
211: newWebXMLFile = PDWebAppUpdater.addWebAppParam(
212: webXMLStream2, configProps, portletAppName);
213: } catch (IOException ioe) {
214: Object[] tokens = { ioe.toString() };
215: throw new PortletDeployerException("errorUpdatingWebApp",
216: tokens);
217: }
218:
219: try {
220: webXMLStream1.close();
221: webXMLStream2.close();
222: in.close();
223: } catch (IOException ioe) {
224: Object[] tokens = { ioe.toString() };
225: throw new PortletDeployerException("errorStreamClose",
226: tokens);
227: }
228:
229: //get updated war file
230: try {
231: File newWarFile = pdwr.getUpdatedWarFile(newWebXMLFile);
232: if (newWarFile != null) {
233: File destFile = new File(ddLocation, warFile.getName());
234: copyFile(newWarFile, destFile, true, false);
235: }
236: } catch (IOException ioe) {
237: Object[] tokens = { ioe.toString() };
238: throw new PortletDeployerException("errorJarUpdate", tokens);
239: }
240:
241: if (verbose) {
242: PortletDeployerLocalizer.debug("dbgAddingProviders");
243: }
244:
245: // add provider definition using DPAPI.
246:
247: PDDPUpdater dpUpdater = new PDDPUpdater(dadc, dn, global,
248: verbose);
249: dpUpdater.addProviders(providerElements);
250:
251: //System.out.println("PDDeploy: done adding providers");
252: return PortletDeployerLocalizer
253: .getLocalizedString("msgSuccess");
254:
255: }
256:
257: /**
258: * Convienence method to copy a file from a source to a destination.
259: * Overwrite is prevented, and the last modified is kept.
260: *
261: * @throws IOException
262: */
263: public static void copyFile(String sourceFile, String destFile)
264: throws IOException {
265: copyFile(new File(sourceFile), new File(destFile), false, true);
266: }
267:
268: /**
269: * Method to copy a file from a source to a
270: * destination specifying if
271: * source files may overwrite newer destination files and the
272: * last modified time of <code>destFile</code> file should be made equal
273: * to the last modified time of <code>sourceFile</code>.
274: *
275: * @throws IOException
276: */
277: public static void copyFile(File sourceFile, File destFile,
278: boolean overwrite, boolean preserveLastModified)
279: throws IOException {
280:
281: if (overwrite || !destFile.exists()
282: || destFile.lastModified() < sourceFile.lastModified()) {
283:
284: if (destFile.exists() && destFile.isFile()) {
285: destFile.delete();
286: }
287:
288: // ensure that parent dir of dest file exists!
289: File parent = new File(destFile.getParent());
290: if (!parent.exists()) {
291: parent.mkdirs();
292: }
293:
294: FileInputStream in = new FileInputStream(sourceFile);
295: FileOutputStream out = new FileOutputStream(destFile);
296:
297: byte[] buffer = new byte[8 * 1024];
298: int count = 0;
299: do {
300: out.write(buffer, 0, count);
301: count = in.read(buffer, 0, buffer.length);
302: } while (count != -1);
303:
304: in.close();
305: out.close();
306:
307: if (preserveLastModified) {
308: destFile.setLastModified(sourceFile.lastModified());
309: }
310: }
311: }
312: }
|