1 2 24 package org.enhydra.tool.archive; 25 26 import org.enhydra.tool.common.FileUtil; 28 import org.enhydra.tool.common.ResUtil; 29 import org.enhydra.tool.common.PathHandle; 30 31 import java.io.File ; 33 import java.io.FileFilter ; 34 import java.util.ArrayList ; 35 import java.util.Arrays ; 36 import java.util.ResourceBundle ; 37 38 public class JarPlan { 40 41 static ResourceBundle res = 43 ResourceBundle.getBundle("org.enhydra.tool.archive.Res"); 45 protected ArrayList fileList = new ArrayList (); 47 private FileFilter fillFilter = null; 48 private String archivePath = new String (); 49 private String manifestPath = null; 50 private String classRoot = null; 51 private String [] classFiles = null; 52 private String [] libFiles = new String [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 getFillFilter() { 66 return fillFilter; 67 } 68 69 public void setFillFilter(FileFilter 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 getManifestPath() { 82 return manifestPath; 83 } 84 85 public void setManifestPath(String 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 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 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 getArchivePath() { 119 return archivePath; 120 } 121 122 public void setArchivePath(String 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 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.getString("Classes_directory_is")); 145 } else { 146 PathHandle ph = PathHandle.createPathHandle(in); 147 148 if (isValidate()) { 149 if (!ph.isDirectory()) { 150 String 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 [] classes) { 161 if (isEqual(getClassFileArray(), classes)) { 162 classFiles = null; 163 } else { 164 classFiles = classes; 165 } 166 } 167 168 public String [] getClassFiles() { 169 return classFiles; 170 } 171 172 public void setLibFiles(String [] libs) { 173 libFiles = libs; 174 } 175 176 public String [] getLibFiles() { 177 return libFiles; 178 } 179 180 public File [] getLibFileArray() { 181 File [] files = new File [0]; 182 183 fileList.clear(); 184 for (int i = 0; i < getLibFiles().length; i++) { 185 File cursor = new File (getLibFiles()[i]); 186 187 if (cursor.isFile()) { 188 fileList.add(cursor); 189 } 190 } 191 fileList.trimToSize(); 192 files = new File [fileList.size()]; 193 files = (File []) fileList.toArray(files); 194 fileList.clear(); 195 return files; 196 } 197 198 public File [] getClassFileArray() { 199 File [] files = new File [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 } else if (classFiles == null) { 212 fillFileList(new File (getClassRoot())); 213 } else { 214 for (int i = 0; i < classFiles.length; i++) { 215 File cursor = new File (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 [fileList.size()]; 228 files = (File []) fileList.toArray(files); 229 fileList.clear(); 230 return files; 231 } 232 233 public boolean equals(Object 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 protected boolean isEqual(File [] compFiles, String [] paths) { 273 boolean equal = false; 274 275 if (paths == null) { 276 equal = false; 277 } else if (compFiles.length == paths.length) { 278 String [] comp = new String [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 root) { 292 if (root.isDirectory()) { 293 File [] children = new File [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 { 311 312 public NFSFilter() {} 314 315 public boolean accept(File 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 |