KickJava   Java API By Example, From Geeks To Geeks.

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


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 Imports
27
import org.enhydra.tool.common.FileUtil;
28 import org.enhydra.tool.common.ResUtil;
29 import org.enhydra.tool.common.PathHandle;
30
31 // Standard imports
32
import java.io.File JavaDoc;
33 import java.io.FileFilter JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Arrays JavaDoc;
36 import java.util.ResourceBundle JavaDoc;
37
38 //
39
public class JarPlan {
40
41     // not to be resourced
42
static ResourceBundle JavaDoc res =
43         ResourceBundle.getBundle("org.enhydra.tool.archive.Res"); // nores
44

45     //
46
protected ArrayList JavaDoc fileList = new ArrayList JavaDoc();
47     private FileFilter JavaDoc fillFilter = null;
48     private String JavaDoc archivePath = new String JavaDoc();
49     private String JavaDoc manifestPath = null;
50     private String JavaDoc classRoot = null;
51     private String JavaDoc[] classFiles = null;
52     private String JavaDoc[] libFiles = new String JavaDoc[0];
53     private boolean validate = true;
54     private boolean classFiltering = false;
55     private Descriptor[] dd = new Descriptor[0];
56
57     public Descriptor[] getDescriptors() {
58         return dd;
59     }
60
61     public void setDescriptors(Descriptor[] d) {
62         dd = d;
63     }
64
65     public FileFilter JavaDoc getFillFilter() {
66         return fillFilter;
67     }
68
69     public void setFillFilter(FileFilter JavaDoc f) {
70         fillFilter = f;
71     }
72
73     public boolean isClassFiltering() {
74         return classFiltering;
75     }
76
77     public void setClassFiltering(boolean b) {
78         classFiltering = b;
79     }
80
81     public String JavaDoc getManifestPath() {
82         return manifestPath;
83     }
84
85     public void setManifestPath(String JavaDoc in) throws ArchiveException {
86         if (in == null) {
87             manifestPath = null;
88         } else {
89             PathHandle ph = PathHandle.createPathHandle(in);
90
91             if (ph.isDirectory()) {
92                 String JavaDoc exMess = "Manifest path is a directory: {0}";
93
94                 exMess = ResUtil.format(exMess, ph);
95                 throw new ArchiveException(exMess);
96             }
97             if (isValidate()) {
98                 if (!ph.isFile()) {
99                     String JavaDoc exMess = "Manifest not found: {0}";
100
101                     exMess = ResUtil.format(exMess, ph);
102                     throw new ArchiveException(exMess);
103                 }
104             }
105             manifestPath = ph.getPath();
106             setClassFiltering(true);
107         }
108     }
109
110     public boolean isValidate() {
111         return validate;
112     }
113
114     public void setValidate(boolean v) {
115         validate = v;
116     }
117
118     public String JavaDoc getArchivePath() {
119         return archivePath;
120     }
121
122     public void setArchivePath(String JavaDoc in) throws ArchiveException {
123         if (in == null) {
124             throw new ArchiveException(res.getString("_archive_file_is_null"));
125         } else {
126             PathHandle ph = PathHandle.createPathHandle(in);
127
128             if (ph.isDirectory()) {
129                 String JavaDoc exMess = res.getString("_archive_file_is_a");
130
131                 exMess = ResUtil.format(exMess, ph);
132                 throw new ArchiveException(exMess);
133             }
134             archivePath = ph.getPath();
135         }
136     }
137
138     public String JavaDoc getClassRoot() {
139         return classRoot;
140     }
141
142     public void setClassRoot(String JavaDoc in) throws ArchiveException {
143         if (in == null) {
144             throw new ArchiveException(res.getString("Classes_directory_is"));
145         } else {
146             PathHandle ph = PathHandle.createPathHandle(in);
147
148             if (isValidate()) {
149                 if (!ph.isDirectory()) {
150                     String JavaDoc exMess = res.getString("Classes_directory_not");
151
152                     exMess = ResUtil.format(exMess, ph);
153                     throw new ArchiveException(exMess);
154                 }
155             }
156             classRoot = ph.getPath();
157         }
158     }
159
160     public void setClassFiles(String JavaDoc[] classes) {
161         if (isEqual(getClassFileArray(), classes)) {
162             classFiles = null;
163         } else {
164             classFiles = classes;
165         }
166     }
167
168     public String JavaDoc[] getClassFiles() {
169         return classFiles;
170     }
171
172     public void setLibFiles(String JavaDoc[] libs) {
173         libFiles = libs;
174     }
175
176     public String JavaDoc[] getLibFiles() {
177         return libFiles;
178     }
179
180     public File JavaDoc[] getLibFileArray() {
181         File JavaDoc[] files = new File JavaDoc[0];
182
183         fileList.clear();
184         for (int i = 0; i < getLibFiles().length; i++) {
185             File JavaDoc cursor = new File JavaDoc(getLibFiles()[i]);
186
187             if (cursor.isFile()) {
188                 fileList.add(cursor);
189             }
190         }
191         fileList.trimToSize();
192         files = new File JavaDoc[fileList.size()];
193         files = (File JavaDoc[]) fileList.toArray(files);
194         fileList.clear();
195         return files;
196     }
197
198     public File JavaDoc[] getClassFileArray() {
199         File JavaDoc[] files = new File JavaDoc[0];
200         NFSFilter nfsFilter = new NFSFilter();
201
202         fileList.clear();
203         if (isClassFiltering()) {
204             fillFilter = new ClassFilter(getClassRoot());
205         } else {
206             fillFilter = null;
207         }
208         if (getClassRoot() == null) {
209
210             // done
211
} else if (classFiles == null) {
212             fillFileList(new File JavaDoc(getClassRoot()));
213         } else {
214             for (int i = 0; i < classFiles.length; i++) {
215                 File JavaDoc cursor = new File JavaDoc(classFiles[i]);
216
217                 if (fillFilter == null) {
218                     if (nfsFilter.accept(cursor)) {
219                         fileList.add(cursor);
220                     }
221                 } else if (fillFilter.accept(cursor)) {
222                     fileList.add(cursor);
223                 }
224             }
225         }
226         fileList.trimToSize();
227         files = new File JavaDoc[fileList.size()];
228         files = (File JavaDoc[]) fileList.toArray(files);
229         fileList.clear();
230         return files;
231     }
232
233     public boolean equals(Object JavaDoc comp) {
234         boolean equal = false;
235         JarPlan plan = null;
236
237         if (comp instanceof JarPlan) {
238             plan = (JarPlan) comp;
239             equal = true;
240             if (plan.getArchivePath() == null && getArchivePath() == null) {}
241             else if (plan.getArchivePath().equals(getArchivePath())) {}
242             else {
243                 equal = false;
244             }
245             if (equal) {
246                 if (plan.getClassRoot() == null && getClassRoot() == null) {}
247                 else if (plan.getClassRoot().equals(getClassRoot())) {}
248                 else {
249                     equal = false;
250                 }
251             }
252             if (equal) {
253                 if (!Arrays.equals(plan.getClassFiles(), getClassFiles())) {
254                     equal = false;
255                 }
256             }
257             if (equal) {
258                 if (!Arrays.equals(plan.getLibFiles(), getLibFiles())) {
259                     equal = false;
260                 }
261             }
262             if (equal) {
263                 if (!Arrays.equals(plan.getDescriptors(), getDescriptors())) {
264                     equal = false;
265                 }
266             }
267         }
268         return equal;
269     }
270
271     // /
272
protected boolean isEqual(File JavaDoc[] compFiles, String JavaDoc[] paths) {
273         boolean equal = false;
274
275         if (paths == null) {
276             equal = false;
277         } else if (compFiles.length == paths.length) {
278             String JavaDoc[] comp = new String JavaDoc[paths.length];
279
280             for (int i = 0; i < paths.length; i++) {
281                 paths[i] = PathHandle.createPathString(paths[i]);
282                 comp[i] = PathHandle.createPathString(compFiles[i]);
283             }
284             Arrays.sort(paths);
285             Arrays.sort(comp);
286             equal = Arrays.equals(paths, comp);
287         }
288         return equal;
289     }
290
291     protected void fillFileList(File JavaDoc root) {
292         if (root.isDirectory()) {
293             File JavaDoc[] children = new File JavaDoc[0];
294
295             if (fillFilter == null) {
296                 children = root.listFiles(new NFSFilter());
297             } else {
298                 children = root.listFiles(fillFilter);
299             }
300             for (int i = 0; i < children.length; i++) {
301                 if (children[i].isDirectory()) {
302                     fillFileList(children[i]);
303                 } else if (children[i].isFile()) {
304                     fileList.add(children[i]);
305                 }
306             }
307         }
308     }
309
310     private class NFSFilter implements FileFilter JavaDoc {
311
312         //
313
public NFSFilter() {}
314
315         public boolean accept(File JavaDoc file) {
316             boolean include = false;
317
318             if (file.isDirectory()) {
319                 include = true;
320             } else if (file.getName().toLowerCase().startsWith(ClassFilter.TEMP_NFS)) {
321                 include = false;
322             } else {
323                 include = true;
324             }
325             return include;
326         }
327
328     }
329 }
330
Popular Tags