KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > archive > PropertyWriter


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.archive;
25
26 // ToolBox
27
import org.enhydra.tool.archive.wizard.ArchiveType;
28 import org.enhydra.tool.archive.wizard.E3Type;
29 import org.enhydra.tool.archive.wizard.EarType;
30 import org.enhydra.tool.archive.wizard.EjbType;
31 import org.enhydra.tool.archive.wizard.WarType;
32 import org.enhydra.tool.common.PathHandle;
33
34 // JDK
35
import java.io.BufferedOutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.File JavaDoc;
38 import java.io.FileNotFoundException JavaDoc;
39 import java.io.FileOutputStream JavaDoc;
40 import java.util.Properties JavaDoc;
41 import java.util.jar.JarFile JavaDoc;
42 import java.util.jar.Manifest JavaDoc;
43
44 //
45
public class PropertyWriter implements PropertyKeys, ManifestKeys {
46
47     //
48
private final String JavaDoc[] types = {
49         E3Type.EXT, EarType.EXT, EjbType.EXT, WarType.EXT
50     };
51     private File JavaDoc file = null;
52     private Properties JavaDoc props = new Properties JavaDoc();
53     private int count = 0;
54
55     public PropertyWriter() {}
56
57     public void write(JarPlan[] plans, File JavaDoc out) {
58         FileOutputStream JavaDoc fileStream = null;
59         BufferedOutputStream JavaDoc bufStream = null;
60
61         file = out;
62         write(plans);
63         try {
64             fileStream = new FileOutputStream JavaDoc(file);
65             bufStream = new BufferedOutputStream JavaDoc(fileStream);
66             props.store(bufStream, "Kelp Archive Properties");
67             fileStream.flush();
68             bufStream.flush();
69             bufStream.close();
70             fileStream.close();
71         } catch (FileNotFoundException JavaDoc e) {
72             e.printStackTrace(System.err);
73         } catch (IOException JavaDoc e) {
74             e.printStackTrace(System.err);
75         }
76     }
77
78     public Properties JavaDoc write(JarPlan[] plans) {
79         for (int i = 0; i < plans.length; i++) {
80             write(plans[i]);
81         }
82         return props;
83     }
84
85     public Properties JavaDoc write(String JavaDoc[] jars) {
86
87         // TODO, add parameter to save file paths relative to
88
// TODO, move relative stuff from Kelp to common.
89
for (int i = 0; i < jars.length; i++) {
90             write(jars[i]);
91         }
92         return props;
93     }
94
95     public Properties JavaDoc write(JarPlan plan) {
96         String JavaDoc value = null;
97
98         value = plan.getArchivePath();
99         props.setProperty(PATH + count, pathValue(value));
100         if (plan instanceof WarPlan) {
101             writeWarProps((WarPlan) plan);
102         } else if (plan instanceof EarPlan) {
103             writeEarProps((EarPlan) plan);
104         } else if (plan instanceof EjbPlan) {
105             writeEjbProps((EjbPlan) plan);
106         } else {
107             writeJarProps(plan);
108         }
109         count++;
110         return props;
111     }
112
113     private void write(String JavaDoc archive) {
114         ArchiveType type = null;
115         PathHandle ph = null;
116         JarPlan plan = null;
117
118         ph = PathHandle.createPathHandle(archive);
119         type = getType(ph.getPath());
120         if (type != null) {
121             type.readArchive(ph.getPath());
122             try {
123                 plan = type.getPlan();
124             } catch (ArchiveException e) {
125                 e.printStackTrace(System.err);
126             }
127             write(plan);
128         }
129     }
130
131     private void writeWarProps(WarPlan plan) {
132         String JavaDoc[] files = new String JavaDoc[0];
133         String JavaDoc root = null;
134         String JavaDoc dd = null;
135
136         writeJarProps(plan);
137
138         // content
139
files = plan.getContentFiles();
140         root = plan.getContentRoot();
141         props.setProperty(CONTENT_ROOT_PROP + count, pathValue(root));
142         if (files != null) {
143             for (int i = 0; i < files.length; i++) {
144                 props.setProperty(CONTENT + count + "." + i,
145                                   pathValue(files[i]));
146             }
147         }
148
149         // libs
150
files = plan.getLibFiles();
151         for (int i = 0; i < files.length; i++) {
152             props.setProperty(LIBRARY_PROP + count + "." + i,
153                               pathValue(files[i]));
154         }
155     }
156
157     private void writeEarProps(EarPlan plan) {
158         String JavaDoc[] files = new String JavaDoc[0];
159         Descriptor[] dd = new Descriptor[0];
160         WebApplication[] webApps = new WebApplication[0];
161         Boolean JavaDoc b = null;
162
163         // app name
164
props.setProperty(APP_NAME_PROP + count, plan.getAppName());
165
166         // descriptiion
167
props.setProperty(APP_DESC_PROP + count, plan.getDescription());
168
169         // alt-dd
170
b = new Boolean JavaDoc(plan.isAlt());
171         props.setProperty(ALT_DD_PROP + count, b.toString());
172
173         // copy-client
174
b = new Boolean JavaDoc(plan.isCopyClient());
175         props.setProperty(COPY_CLIENT_PROP + count, b.toString());
176
177         // enterprise mods
178
files = plan.getEnterpriseModules();
179         for (int i = 0; i < files.length; i++) {
180             props.setProperty(ENTERPRISE_MODULE_PROP + count + "." + i,
181                               pathValue(files[i]));
182         }
183
184         // web apps
185
webApps = plan.getWebApplications();
186         for (int i = 0; i < webApps.length; i++) {
187             props.setProperty(WEBAPP_MODULE_PROP + count + "." + i,
188                               pathValue(webApps[i].getArchive()));
189             props.setProperty(WEBAPP_CONTEXT_PROP + count + "." + i,
190                               webApps[i].getContextRoot());
191         }
192
193         // meta-dd
194
dd = plan.getDescriptors();
195         for (int i = 0; i < dd.length; i++) {
196             props.setProperty(META_DD_PROP + count + "." + i,
197                               pathValue(dd[i].getPath()));
198         }
199
200         // libs
201
files = plan.getLibFiles();
202         for (int i = 0; i < files.length; i++) {
203             props.setProperty(LIBRARY_PROP + count + "." + i,
204                               pathValue(files[i]));
205         }
206
207         // TODO: root-context
208
}
209
210     private void writeEjbProps(EjbPlan plan) {
211         Boolean JavaDoc b = null;
212
213         writeJarProps(plan);
214
215         // copy-client
216
b = new Boolean JavaDoc(plan.isCreateClient());
217         props.setProperty(CREATE_CLIENT_PROP + count, b.toString());
218     }
219
220     private void writeJarProps(JarPlan plan) {
221         String JavaDoc[] files = new String JavaDoc[0];
222         Descriptor[] dd = new Descriptor[0];
223         String JavaDoc root = null;
224
225         // classes
226
root = plan.getClassRoot();
227         files = plan.getClassFiles();
228         props.setProperty(CLASS_ROOT_PROP + count, pathValue(root));
229         if (files != null) {
230             for (int i = 0; i < files.length; i++) {
231                 props.setProperty(CLASS + count + "." + i,
232                                   pathValue(files[i]));
233             }
234         }
235
236         // deployment descriptors
237
dd = plan.getDescriptors();
238         for (int i = 0; i < dd.length; i++) {
239             props.setProperty(dd[i].getType() + "." + count,
240                               pathValue(dd[i].getPath()));
241         }
242     }
243
244     private ArchiveType getType(String JavaDoc archive) {
245         PathHandle ph = null;
246         JarFile JavaDoc jar = null;
247         Manifest JavaDoc manifest = null;
248         ArchiveType type = null;
249         String JavaDoc value = null;
250
251         ph = PathHandle.createPathHandle(archive);
252         if (ph.isFile() && ph.getFile().canRead() && ph.hasExtension(types)) {
253             try {
254                 jar = new JarFile JavaDoc(ph.getFile());
255                 manifest = jar.getManifest();
256                 value = manifest.getMainAttributes().getValue(KELP_VERSION);
257                 if (value == null) {
258                     System.err.println("Kelp properties not found in manifest of: "
259                                        + ph.getPath());
260                 } else if (ph.hasExtension(WarType.EXT)) {
261                     type = new WarType();
262                 } else if (ph.hasExtension(EarType.EXT)) {
263                     type = new EarType();
264                 } else {
265                     value =
266                         manifest.getMainAttributes().getValue(CREATE_CLIENT);
267                     if (value == null) {
268                         type = new E3Type();
269                     } else {
270                         type = new EjbType();
271                     }
272                 }
273             } catch (IOException JavaDoc e) {
274                 e.printStackTrace(System.err);
275             }
276         }
277         return type;
278     }
279
280     private String JavaDoc pathValue(String JavaDoc in) {
281         String JavaDoc out = null;
282
283         if (file == null) {
284             out = in;
285         } else {
286             PathHandle propPh = null;
287             PathHandle inPh = null;
288
289             propPh = PathHandle.createPathHandle(file);
290             inPh = PathHandle.createPathHandle(in);
291             out = propPh.getRelativePath(inPh);
292         }
293         return out;
294     }
295
296 }
297
Popular Tags