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 Imports
026: import org.enhydra.tool.common.FileUtil;
027: import org.enhydra.tool.common.ResUtil;
028: import org.enhydra.tool.common.PathHandle;
029:
030: // Standard imports
031: import java.io.File;
032: import java.io.FileFilter;
033: import java.util.ArrayList;
034: import java.util.Arrays;
035: import java.util.ResourceBundle;
036:
037: //
038: public class JarPlan {
039:
040: // not to be resourced
041: static ResourceBundle res = ResourceBundle
042: .getBundle("org.enhydra.tool.archive.Res"); // nores
043:
044: //
045: protected ArrayList fileList = new ArrayList();
046: private FileFilter fillFilter = null;
047: private String archivePath = new String();
048: private String manifestPath = null;
049: private String classRoot = null;
050: private String[] classFiles = null;
051: private String[] libFiles = new String[0];
052: private boolean validate = true;
053: private boolean classFiltering = false;
054: private Descriptor[] dd = new Descriptor[0];
055:
056: public Descriptor[] getDescriptors() {
057: return dd;
058: }
059:
060: public void setDescriptors(Descriptor[] d) {
061: dd = d;
062: }
063:
064: public FileFilter getFillFilter() {
065: return fillFilter;
066: }
067:
068: public void setFillFilter(FileFilter f) {
069: fillFilter = f;
070: }
071:
072: public boolean isClassFiltering() {
073: return classFiltering;
074: }
075:
076: public void setClassFiltering(boolean b) {
077: classFiltering = b;
078: }
079:
080: public String getManifestPath() {
081: return manifestPath;
082: }
083:
084: public void setManifestPath(String in) throws ArchiveException {
085: if (in == null) {
086: manifestPath = null;
087: } else {
088: PathHandle ph = PathHandle.createPathHandle(in);
089:
090: if (ph.isDirectory()) {
091: String exMess = "Manifest path is a directory: {0}";
092:
093: exMess = ResUtil.format(exMess, ph);
094: throw new ArchiveException(exMess);
095: }
096: if (isValidate()) {
097: if (!ph.isFile()) {
098: String exMess = "Manifest not found: {0}";
099:
100: exMess = ResUtil.format(exMess, ph);
101: throw new ArchiveException(exMess);
102: }
103: }
104: manifestPath = ph.getPath();
105: setClassFiltering(true);
106: }
107: }
108:
109: public boolean isValidate() {
110: return validate;
111: }
112:
113: public void setValidate(boolean v) {
114: validate = v;
115: }
116:
117: public String getArchivePath() {
118: return archivePath;
119: }
120:
121: public void setArchivePath(String in) throws ArchiveException {
122: if (in == null) {
123: throw new ArchiveException(res
124: .getString("_archive_file_is_null"));
125: } else {
126: PathHandle ph = PathHandle.createPathHandle(in);
127:
128: if (ph.isDirectory()) {
129: String 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 getClassRoot() {
139: return classRoot;
140: }
141:
142: public void setClassRoot(String in) throws ArchiveException {
143: if (in == null) {
144: throw new ArchiveException(res
145: .getString("Classes_directory_is"));
146: } else {
147: PathHandle ph = PathHandle.createPathHandle(in);
148:
149: if (isValidate()) {
150: if (!ph.isDirectory()) {
151: String exMess = res
152: .getString("Classes_directory_not");
153:
154: exMess = ResUtil.format(exMess, ph);
155: throw new ArchiveException(exMess);
156: }
157: }
158: classRoot = ph.getPath();
159: }
160: }
161:
162: public void setClassFiles(String[] classes) {
163: if (isEqual(getClassFileArray(), classes)) {
164: classFiles = null;
165: } else {
166: classFiles = classes;
167: }
168: }
169:
170: public String[] getClassFiles() {
171: return classFiles;
172: }
173:
174: public void setLibFiles(String[] libs) {
175: libFiles = libs;
176: }
177:
178: public String[] getLibFiles() {
179: return libFiles;
180: }
181:
182: public File[] getLibFileArray() {
183: File[] files = new File[0];
184:
185: fileList.clear();
186: for (int i = 0; i < getLibFiles().length; i++) {
187: File cursor = new File(getLibFiles()[i]);
188:
189: if (cursor.isFile()) {
190: fileList.add(cursor);
191: }
192: }
193: fileList.trimToSize();
194: files = new File[fileList.size()];
195: files = (File[]) fileList.toArray(files);
196: fileList.clear();
197: return files;
198: }
199:
200: public File[] getClassFileArray() {
201: File[] files = new File[0];
202: NFSFilter nfsFilter = new NFSFilter();
203:
204: fileList.clear();
205: if (isClassFiltering()) {
206: fillFilter = new ClassFilter(getClassRoot());
207: } else {
208: fillFilter = null;
209: }
210: if (getClassRoot() == null) {
211:
212: // done
213: } else if (classFiles == null) {
214: fillFileList(new File(getClassRoot()));
215: } else {
216: for (int i = 0; i < classFiles.length; i++) {
217: File cursor = new File(classFiles[i]);
218:
219: if (fillFilter == null) {
220: if (nfsFilter.accept(cursor)) {
221: fileList.add(cursor);
222: }
223: } else if (fillFilter.accept(cursor)) {
224: fileList.add(cursor);
225: }
226: }
227: }
228: fileList.trimToSize();
229: files = new File[fileList.size()];
230: files = (File[]) fileList.toArray(files);
231: fileList.clear();
232: return files;
233: }
234:
235: public boolean equals(Object comp) {
236: boolean equal = false;
237: JarPlan plan = null;
238:
239: if (comp instanceof JarPlan) {
240: plan = (JarPlan) comp;
241: equal = true;
242: if (plan.getArchivePath() == null
243: && getArchivePath() == null) {
244: } else if (plan.getArchivePath().equals(getArchivePath())) {
245: } else {
246: equal = false;
247: }
248: if (equal) {
249: if (plan.getClassRoot() == null
250: && getClassRoot() == null) {
251: } else if (plan.getClassRoot().equals(getClassRoot())) {
252: } else {
253: equal = false;
254: }
255: }
256: if (equal) {
257: if (!Arrays.equals(plan.getClassFiles(),
258: getClassFiles())) {
259: equal = false;
260: }
261: }
262: if (equal) {
263: if (!Arrays.equals(plan.getLibFiles(), getLibFiles())) {
264: equal = false;
265: }
266: }
267: if (equal) {
268: if (!Arrays.equals(plan.getDescriptors(),
269: getDescriptors())) {
270: equal = false;
271: }
272: }
273: }
274: return equal;
275: }
276:
277: // /
278: protected boolean isEqual(File[] compFiles, String[] paths) {
279: boolean equal = false;
280:
281: if (paths == null) {
282: equal = false;
283: } else if (compFiles.length == paths.length) {
284: String[] comp = new String[paths.length];
285:
286: for (int i = 0; i < paths.length; i++) {
287: paths[i] = PathHandle.createPathString(paths[i]);
288: comp[i] = PathHandle.createPathString(compFiles[i]);
289: }
290: Arrays.sort(paths);
291: Arrays.sort(comp);
292: equal = Arrays.equals(paths, comp);
293: }
294: return equal;
295: }
296:
297: protected void fillFileList(File root) {
298: if (root.isDirectory()) {
299: File[] children = new File[0];
300:
301: if (fillFilter == null) {
302: children = root.listFiles(new NFSFilter());
303: } else {
304: children = root.listFiles(fillFilter);
305: }
306: for (int i = 0; i < children.length; i++) {
307: if (children[i].isDirectory()) {
308: fillFileList(children[i]);
309: } else if (children[i].isFile()) {
310: fileList.add(children[i]);
311: }
312: }
313: }
314: }
315:
316: private class NFSFilter implements FileFilter {
317:
318: //
319: public NFSFilter() {
320: }
321:
322: public boolean accept(File file) {
323: boolean include = false;
324:
325: if (file.isDirectory()) {
326: include = true;
327: } else if (file.getName().toLowerCase().startsWith(
328: ClassFilter.TEMP_NFS)) {
329: include = false;
330: } else {
331: include = true;
332: }
333: return include;
334: }
335:
336: }
337: }
|