001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.tool.archive;
024:
025: // ToolBox
026: import org.enhydra.tool.archive.wizard.ArchiveType;
027: import org.enhydra.tool.archive.wizard.E3Type; //Zeljko Kovacevic 28.10 2003 comment this
028: /*
029: import org.enhydra.tool.archive.wizard.EarType;
030: import org.enhydra.tool.archive.wizard.EjbType;
031: */
032:
033: import org.enhydra.tool.archive.wizard.WarType;
034: import org.enhydra.tool.common.PathHandle;
035:
036: // JDK
037: import java.io.BufferedOutputStream;
038: import java.io.IOException;
039: import java.io.File;
040: import java.io.FileNotFoundException;
041: import java.io.FileOutputStream;
042: import java.util.Properties;
043: import java.util.jar.JarFile;
044: import java.util.jar.Manifest;
045:
046: //
047: public class PropertyWriter implements PropertyKeys, ManifestKeys {
048:
049: //
050: private final String[] types = {
051: //Zeljko Kovacevic 28.10 2003 comment this
052: /*
053: EarType.EXT, EjbType.EXT,
054: */
055: WarType.EXT, E3Type.EXT, };
056: private File file = null;
057: private Properties props = new Properties();
058: private int count = 0;
059:
060: public PropertyWriter() {
061: }
062:
063: public void write(JarPlan[] plans, File out) {
064: FileOutputStream fileStream = null;
065: BufferedOutputStream bufStream = null;
066:
067: file = out;
068: write(plans);
069: try {
070: fileStream = new FileOutputStream(file);
071: bufStream = new BufferedOutputStream(fileStream);
072: props.store(bufStream, "Archive Properties");
073: fileStream.flush();
074: bufStream.flush();
075: bufStream.close();
076: fileStream.close();
077: } catch (FileNotFoundException e) {
078: e.printStackTrace(System.err);
079: } catch (IOException e) {
080: e.printStackTrace(System.err);
081: }
082: }
083:
084: public Properties write(JarPlan[] plans) {
085: for (int i = 0; i < plans.length; i++) {
086: write(plans[i]);
087: }
088: return props;
089: }
090:
091: public Properties write(String[] jars) {
092:
093: // TODO, add parameter to save file paths relative to
094: // TODO, move relative stuff from Kelp to common.
095: for (int i = 0; i < jars.length; i++) {
096: write(jars[i]);
097: }
098: return props;
099: }
100:
101: public Properties write(JarPlan plan) {
102: String value = null;
103:
104: value = plan.getArchivePath();
105: props.setProperty(PATH + count, pathValue(value));
106: if (plan instanceof WarPlan) {
107: writeWarProps((WarPlan) plan);
108: //Zeljko Kovacevic 28.10 2003 comment this
109: /*
110: } else if (plan instanceof EarPlan) {
111: writeEarProps((EarPlan) plan);
112: } else if (plan instanceof EjbPlan) {
113: writeEjbProps((EjbPlan) plan);
114: */
115: } else {
116: writeJarProps(plan);
117: }
118: count++;
119: return props;
120: }
121:
122: private void write(String archive) {
123: ArchiveType type = null;
124: PathHandle ph = null;
125: JarPlan plan = null;
126:
127: ph = PathHandle.createPathHandle(archive);
128: type = getType(ph.getPath());
129: if (type != null) {
130: type.readArchive(ph.getPath());
131: try {
132: plan = type.getPlan();
133: } catch (ArchiveException e) {
134: e.printStackTrace(System.err);
135: }
136: write(plan);
137: }
138: }
139:
140: private void writeWarProps(WarPlan plan) {
141: String[] files = new String[0];
142: String root = null;
143: String dd = null;
144:
145: writeJarProps(plan);
146:
147: // content
148: files = plan.getContentFiles();
149: root = plan.getContentRoot();
150: props.setProperty(CONTENT_ROOT_PROP + count, pathValue(root));
151: if (files != null) {
152: for (int i = 0; i < files.length; i++) {
153: props.setProperty(CONTENT + count + "." + i,
154: pathValue(files[i]));
155: }
156: }
157:
158: // libs
159: files = plan.getLibFiles();
160: for (int i = 0; i < files.length; i++) {
161: props.setProperty(LIBRARY_PROP + count + "." + i,
162: pathValue(files[i]));
163: }
164: }
165:
166: //Zeljko Kovacevic 28.10 2003 comment this
167: /*
168: private void writeEarProps(EarPlan plan) {
169: String[] files = new String[0];
170: Descriptor[] dd = new Descriptor[0];
171: WebApplication[] webApps = new WebApplication[0];
172: Boolean b = null;
173:
174: // app name
175: props.setProperty(APP_NAME_PROP + count, plan.getAppName());
176:
177: // descriptiion
178: props.setProperty(APP_DESC_PROP + count, plan.getDescription());
179:
180: // alt-dd
181: b = new Boolean(plan.isAlt());
182: props.setProperty(ALT_DD_PROP + count, b.toString());
183:
184: // copy-client
185: b = new Boolean(plan.isCopyClient());
186: props.setProperty(COPY_CLIENT_PROP + count, b.toString());
187:
188: // enterprise mods
189: files = plan.getEnterpriseModules();
190: for (int i = 0; i < files.length; i++) {
191: props.setProperty(ENTERPRISE_MODULE_PROP + count + "." + i,
192: pathValue(files[i]));
193: }
194:
195: // web apps
196: webApps = plan.getWebApplications();
197: for (int i = 0; i < webApps.length; i++) {
198: props.setProperty(WEBAPP_MODULE_PROP + count + "." + i,
199: pathValue(webApps[i].getArchive()));
200: props.setProperty(WEBAPP_CONTEXT_PROP + count + "." + i,
201: webApps[i].getContextRoot());
202: }
203:
204: // meta-dd
205: dd = plan.getDescriptors();
206: for (int i = 0; i < dd.length; i++) {
207: props.setProperty(META_DD_PROP + count + "." + i,
208: pathValue(dd[i].getPath()));
209: }
210:
211: // libs
212: files = plan.getLibFiles();
213: for (int i = 0; i < files.length; i++) {
214: props.setProperty(LIBRARY_PROP + count + "." + i,
215: pathValue(files[i]));
216: }
217:
218: // TODO: root-context
219: }
220:
221: private void writeEjbProps(EjbPlan plan) {
222: Boolean b = null;
223:
224: writeJarProps(plan);
225:
226: // copy-client
227: b = new Boolean(plan.isCreateClient());
228: props.setProperty(CREATE_CLIENT_PROP + count, b.toString());
229: }
230: */
231: private void writeJarProps(JarPlan plan) {
232: String[] files = new String[0];
233: Descriptor[] dd = new Descriptor[0];
234: String root = null;
235:
236: // classes
237: root = plan.getClassRoot();
238: files = plan.getClassFiles();
239: props.setProperty(CLASS_ROOT_PROP + count, pathValue(root));
240: if (files != null) {
241: for (int i = 0; i < files.length; i++) {
242: props.setProperty(CLASS + count + "." + i,
243: pathValue(files[i]));
244: }
245: }
246:
247: // deployment descriptors
248: dd = plan.getDescriptors();
249: for (int i = 0; i < dd.length; i++) {
250: props.setProperty(dd[i].getType() + "." + count,
251: pathValue(dd[i].getPath()));
252: }
253: }
254:
255: private ArchiveType getType(String archive) {
256: PathHandle ph = null;
257: JarFile jar = null;
258: Manifest manifest = null;
259: ArchiveType type = null;
260: String value = null;
261:
262: ph = PathHandle.createPathHandle(archive);
263: if (ph.isFile() && ph.getFile().canRead()
264: && ph.hasExtension(types)) {
265: try {
266: jar = new JarFile(ph.getFile());
267: manifest = jar.getManifest();
268: value = manifest.getMainAttributes().getValue(
269: KELP_VERSION);
270: if (value == null) {
271: System.err
272: .println("Properties not found in manifest of: "
273: + ph.getPath());
274: } else if (ph.hasExtension(WarType.EXT)) {
275: type = new WarType();
276: //Zeljko Kovacevic 28.10 2003 comment this
277: /*
278: } else if (ph.hasExtension(EarType.EXT)) {
279: type = new EarType();
280:
281: } else {
282: value =
283: manifest.getMainAttributes().getValue(CREATE_CLIENT);
284: if (value == null) {
285: type = new E3Type();
286: } else {
287: type = new EjbType();
288: }
289: */
290: }
291:
292: } catch (IOException e) {
293: e.printStackTrace(System.err);
294: }
295: }
296: return type;
297: }
298:
299: private String pathValue(String in) {
300: String out = null;
301:
302: if (file == null) {
303: out = in;
304: } else {
305: PathHandle propPh = null;
306: PathHandle inPh = null;
307:
308: propPh = PathHandle.createPathHandle(file);
309: inPh = PathHandle.createPathHandle(in);
310: out = propPh.getRelativePath(inPh);
311: }
312: return out;
313: }
314:
315: }
|