KickJava   Java API By Example, From Geeks To Geeks.

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


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.common.PathHandle;
28
29 // JDK
30
import java.io.BufferedInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Properties JavaDoc;
36
37 //
38
public class PropertyReader implements PropertyKeys {
39     private Properties JavaDoc props = new Properties JavaDoc();
40     private File JavaDoc file = null;
41     private boolean validate = true;
42
43     public PropertyReader() {}
44
45     public boolean isValidate() {
46         return validate;
47     }
48
49     public void setValidate(boolean v) {
50         validate = v;
51     }
52
53     public JarPlan[] read(File JavaDoc in) throws IOException JavaDoc {
54         FileInputStream JavaDoc fileStream = null;
55         BufferedInputStream JavaDoc bufStream = null;
56
57         file = in;
58         fileStream = new FileInputStream JavaDoc(file);
59         bufStream = new BufferedInputStream JavaDoc(fileStream);
60         props.load(bufStream);
61         bufStream.close();
62         fileStream.close();
63         return read(props);
64     }
65
66     public JarPlan[] read(Properties JavaDoc p) {
67         ArrayList JavaDoc list = null;
68         JarPlan[] plans = new JarPlan[0];
69
70         props = p;
71         list = new ArrayList JavaDoc();
72         for (int i = 0; i < 100; i++) {
73             JarPlan cursor = null;
74             String JavaDoc value = null;
75
76             value = pathValue(props.getProperty(PATH + i));
77             if (isEmpty(value)) {
78                 break;
79             } else {
80                 try {
81                     cursor = createPlan(i);
82
83                     // TODO: relative root expansion
84
cursor.setArchivePath(value);
85                 } catch (ArchiveException e) {
86                     e.printStackTrace(System.err);
87                     cursor = null;
88                 }
89             }
90             if (cursor != null) {
91                 list.add(cursor);
92             }
93         }
94         list.trimToSize();
95         plans = new JarPlan[list.size()];
96         plans = (JarPlan[]) list.toArray(plans);
97         list.clear();
98         return plans;
99     }
100
101     private JarPlan createPlan(int count) throws ArchiveException {
102         String JavaDoc warValue = null;
103         String JavaDoc earValue = null;
104         String JavaDoc ejbValue = null;
105         String JavaDoc jarValue = null;
106         String JavaDoc serviceValue = null;
107         JarPlan plan = null;
108
109         earValue = props.getProperty(APP_NAME_PROP + count);
110         warValue = props.getProperty(CONTENT_ROOT_PROP + count);
111         ejbValue = props.getProperty(CREATE_CLIENT_PROP + count);
112         serviceValue = props.getProperty(Descriptor.ENHYDRA_SERVICE + '.'
113                                          + count);
114         jarValue = props.getProperty(CLASS_ROOT_PROP + count);
115         if (!isEmpty(earValue)) {
116             plan = createEarPlan(count);
117         } else if (!isEmpty(warValue)) {
118             plan = createWarPlan(count);
119         } else if (!isEmpty(ejbValue)) {
120             plan = createEjbPlan(count);
121         } else if (!isEmpty(serviceValue)) {
122             plan = createServicePlan(count);
123         } else if (!isEmpty(jarValue)) {
124             plan = createJarPlan(count);
125         }
126         return plan;
127     }
128
129     private JarPlan createJarPlan(int count) throws ArchiveException {
130         JarPlan plan = null;
131
132         plan = new JarPlan();
133         readJarProperties(plan, count);
134         return plan;
135     }
136
137     private ServicePlan createServicePlan(int count) throws ArchiveException {
138         ServicePlan plan = null;
139
140         plan = new ServicePlan();
141         readJarProperties(plan, count);
142         return plan;
143     }
144
145     private void readJarProperties(JarPlan plan,
146                                    int count) throws ArchiveException {
147         Descriptor[] dd = new Descriptor[0];
148         String JavaDoc value = null;
149         String JavaDoc[] files = new String JavaDoc[0];
150
151         plan.setValidate(isValidate());
152
153         // classes
154
value = pathValue(props.getProperty(CLASS_ROOT_PROP + count));
155         plan.setClassRoot(value);
156         files = readFiles(CLASS, count);
157         if (files.length > 0) {
158             plan.setClassFiles(files);
159         }
160
161         // dd
162
dd = plan.getDescriptors();
163         for (int i = 0; i < dd.length; i++) {
164             value = pathValue(props.getProperty(dd[i].getType() + "."
165                                                 + count));
166             dd[i].setPath(value);
167         }
168         plan.setDescriptors(dd);
169     }
170
171     private EjbPlan createEjbPlan(int count) throws ArchiveException {
172         EjbPlan plan = null;
173         Boolean JavaDoc b = null;
174         String JavaDoc value = null;
175
176         plan = new EjbPlan();
177         readJarProperties(plan, count);
178
179         // create client
180
value = props.getProperty(CREATE_CLIENT_PROP + count);
181         b = Boolean.valueOf(value);
182         plan.setCreateClient(b.booleanValue());
183         return plan;
184     }
185
186     private EarPlan createEarPlan(int count) throws ArchiveException {
187         EarPlan plan = null;
188         String JavaDoc value = null;
189         Boolean JavaDoc b = null;
190         String JavaDoc[] files = new String JavaDoc[0];
191         WebApplication[] webApps = new WebApplication[0];
192         Descriptor[] dd = new Descriptor[0];
193
194         plan = new EarPlan();
195         plan.setValidate(isValidate());
196
197         // app name
198
value = props.getProperty(APP_NAME_PROP + count);
199         plan.setAppName(value);
200
201         // description
202
value = props.getProperty(APP_DESC_PROP + count);
203         plan.setDescription(value);
204
205         // alt-dd
206
value = props.getProperty(ALT_DD_PROP + count);
207         b = Boolean.valueOf(value);
208         plan.setAlt(b.booleanValue());
209
210         // copy-client
211
value = props.getProperty(COPY_CLIENT_PROP + count);
212         b = Boolean.valueOf(value);
213         plan.setCopyClient(b.booleanValue());
214
215         // enterprise modules
216
files = readFiles(ENTERPRISE_MODULE_PROP, count);
217         if (files.length > 0) {
218             plan.setEnterpriseModules(files);
219         }
220
221         // webapps
222
webApps = readWebApps(count);
223         if (webApps.length > 0) {
224             plan.setWebApplications(webApps);
225         }
226
227         // libs
228
files = readFiles(LIBRARY_PROP, count);
229         if (files.length > 0) {
230             plan.setLibFiles(files);
231         }
232
233         // meta-dd
234
files = readFiles(META_DD_PROP, count);
235         if (files.length > 0) {
236             dd = new Descriptor[files.length];
237             for (int i = 0; i < dd.length; i++) {
238                 dd[i] = new Descriptor(files[i]);
239             }
240             plan.setDescriptors(dd);
241         }
242         return plan;
243     }
244
245     private WarPlan createWarPlan(int count) throws ArchiveException {
246         WarPlan plan = null;
247         String JavaDoc value = null;
248         String JavaDoc[] files = new String JavaDoc[0];
249         Descriptor[] dd = new Descriptor[0];
250
251         plan = new WarPlan();
252         readJarProperties(plan, count);
253
254         // content
255
value = pathValue(props.getProperty(CONTENT_ROOT_PROP + count));
256         plan.setContentRoot(value);
257         files = readFiles(CONTENT, count);
258         if (files.length > 0) {
259             plan.setContentFiles(files);
260         }
261
262         // libs
263
files = readFiles(LIBRARY_PROP, count);
264         if (files.length > 0) {
265             plan.setLibFiles(files);
266         }
267
268         //
269
return plan;
270     }
271
272     private String JavaDoc[] readFiles(String JavaDoc property, int count) {
273         ArrayList JavaDoc list = new ArrayList JavaDoc();
274         String JavaDoc[] files = new String JavaDoc[0];
275         String JavaDoc value = null;
276
277         for (int i = 0; i < 2500; i++) {
278             value = pathValue(props.getProperty(property + count + "." + i));
279             if (isEmpty(value)) {
280                 break;
281             } else {
282                 list.add(value);
283             }
284         }
285         list.trimToSize();
286         files = new String JavaDoc[list.size()];
287         files = (String JavaDoc[]) list.toArray(files);
288         return files;
289     }
290
291     private WebApplication[] readWebApps(int count) {
292         ArrayList JavaDoc list = new ArrayList JavaDoc();
293         WebApplication[] webApps = new WebApplication[0];
294         String JavaDoc value = null;
295
296         for (int i = 0; i < 2500; i++) {
297             value = pathValue(props.getProperty(WEBAPP_MODULE_PROP + count
298                                                 + "." + i));
299             if (isEmpty(value)) {
300                 break;
301             } else {
302                 WebApplication webApp = new WebApplication(value);
303
304                 value = props.getProperty(WEBAPP_CONTEXT_PROP + count + "."
305                                           + i);
306                 if (!isEmpty(value)) {
307                     webApp.setContextRoot(value);
308                 }
309                 list.add(webApp);
310             }
311         }
312         list.trimToSize();
313         webApps = new WebApplication[list.size()];
314         webApps = (WebApplication[]) list.toArray(webApps);
315         return webApps;
316     }
317
318     private boolean isEmpty(String JavaDoc value) {
319         boolean empty = false;
320
321         if (value == null) {
322             empty = true;
323         } else if (value.trim().length() == 0) {
324             empty = true;
325         }
326         return empty;
327     }
328
329     private String JavaDoc pathValue(String JavaDoc in) {
330         String JavaDoc out = null;
331
332         if (file == null) {
333             out = in;
334         } else if (isEmpty(in)) {
335             out = in;
336         } else {
337             PathHandle propPh = null;
338             PathHandle outPh = null;
339
340             propPh = PathHandle.createPathHandle(file);
341             outPh = propPh.expandRelativePath(in);
342             out = outPh.getPath();
343         }
344         return out;
345     }
346
347 }
348
Popular Tags