1 23 24 25 package com.sun.jdo.api.persistence.enhancer.util; 26 27 33 import java.util.zip.ZipFile ; 34 import java.util.zip.ZipEntry ; 35 import java.io.*; 36 import java.util.*; 37 40 45 public class ClassFileSource 47 extends Support { 48 49 50 private String theOriginalExpectedClassName = null; 51 52 53 private String theExpectedClassName = null; 54 55 56 private File classFile = null; 57 58 59 private ZipFile zipFile = null; 60 61 62 private InputStream byteCodeStream = null; 63 64 65 private ClassPathElement sourceElement; 66 67 68 private long cachedModDate; 69 71 75 80 81 82 83 86 public boolean sameAs(ClassFileSource other) { 87 88 99 100 if (isZipped()) 102 return (other.isZipped() && 103 other.zipFile.getName().equals(zipFile.getName())); 104 else if (other.isZipped()) 105 return false; 106 else if (other.classFile != null && classFile != null) 107 return other.classFile.getPath().equals(classFile.getPath()); 108 else if (byteCodeStream != null) 110 return byteCodeStream.equals(other.byteCodeStream); 111 return false; 112 } 113 114 117 public boolean isZipped() { 118 return zipFile != null; 119 } 120 121 124 public boolean isStreamed() { 126 return byteCodeStream != null; 127 } 128 129 133 public String expectedClassName() { 134 return theExpectedClassName; 135 } 136 137 140 public void setExpectedClassName(String name) { 141 theExpectedClassName = name; 142 } 143 144 147 public String containingFilePath() { 148 if (isZipped()) 149 return zipFile.getName(); 150 else if (classFile != null) 151 return classFile.getPath(); 152 else 153 return null; 154 } 155 156 162 public ClassFileSource(String className, File classFile) { 163 theExpectedClassName = className; 166 theOriginalExpectedClassName = className; 167 this.classFile = classFile; 168 } 169 170 177 184 185 191 public ClassFileSource(String className, ZipFile zipFile) { 192 theExpectedClassName = className; 195 theOriginalExpectedClassName = className; 196 this.zipFile = zipFile; 197 } 198 199 205 public ClassFileSource(String className, InputStream byteCodeStream) { 207 theExpectedClassName = className; 210 theOriginalExpectedClassName = className; 211 this.byteCodeStream = byteCodeStream; 212 } 213 214 217 public ClassFileSource nextSource(String className) { 218 if (sourceElement != null && sourceElement.next() != null) 219 return ClassPath.findClass(className, sourceElement.next()); 220 return null; 221 } 222 223 230 public ClassFileSource friendSource(String className) { 231 237 238 if (isZipped()) 239 return new ClassFileSource(className, zipFile); 240 else { 241 String fullPath = FilePath.getAbsolutePath(classFile); 242 File dir = new File (fullPath.substring( 243 0, fullPath.lastIndexOf(File.separatorChar)+1)); 244 File f = new File (dir, unpackagedName(className) + ".class"); return new ClassFileSource(className, f); 246 } 247 } 248 249 254 public DataInputStream classFileContents() 255 throws IOException, FileNotFoundException { 256 262 if (isZipped()) { 264 ZipEntry entry = 265 zipFile.getEntry(ClassPath.zipFileNameOf(theExpectedClassName)); 266 if (entry == null) 267 throw new FileNotFoundException( 268 "The zip file member " + theExpectedClassName + " was not found."); 269 return new DataInputStream(zipFile.getInputStream(entry)); 270 } 271 if (isStreamed()) { 273 return new DataInputStream(byteCodeStream); 274 } 275 return new DataInputStream( 276 new BufferedInputStream( 277 new FileInputStream(classFile))); 278 } 279 280 286 public long modificationDate() throws FileNotFoundException { 287 if (cachedModDate == 0) { 288 294 295 if (isZipped()) { 296 ZipEntry entry = 297 zipFile.getEntry(ClassPath.zipFileNameOf(theOriginalExpectedClassName)); 298 if (entry == null) 299 throw new FileNotFoundException("The zip file member was not found."); 300 cachedModDate = entry.getTime(); 301 } 302 else if (classFile != null) 303 cachedModDate = classFile.lastModified(); 304 } 305 306 return cachedModDate; 307 } 308 309 313 public void setModificationDate(long date) { 314 cachedModDate = date; 315 } 316 317 320 void setSourceElement(ClassPathElement cpathElement) { 321 sourceElement = cpathElement; 322 } 323 324 329 private File computeDestinationDir(File rootDestDir) throws IOException, FileNotFoundException { 330 331 StringBuffer buf = new StringBuffer (rootDestDir.getPath()); 332 String prevToken = null; 333 StringTokenizer parser 334 = new StringTokenizer(theExpectedClassName, "/", false); while (parser.hasMoreTokens()) { 336 if (prevToken != null) { 337 buf.append(File.separatorChar); 338 buf.append(prevToken); 339 340 File currDir = new File (buf.toString()); 341 if (!currDir.isDirectory()) { 342 if (!currDir.mkdir()) 343 throw new UserException( 345 getI18N("enhancer.unable_to_create_dir", currDir.getPath())); 347 } 348 } 349 prevToken = parser.nextToken(); 350 } 351 return new File (buf.toString()); 352 } 353 354 360 public File computeDestination(File destDir) 361 throws IOException, FileNotFoundException { 362 if (destDir != null) { 363 File finalDestDir = computeDestinationDir(destDir); 364 String theFinalClassComponent = ""; StringTokenizer parser = 366 new StringTokenizer(theExpectedClassName, "/", false); while (parser.hasMoreTokens()) 368 theFinalClassComponent = parser.nextToken(); 369 return new File (finalDestDir, theFinalClassComponent + ".class"); } 371 else 372 375 return classFile; 376 } 377 378 382 public DataOutputStream getOutputStream(File dest) throws IOException, FileNotFoundException { 383 389 390 391 return new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest))); 392 } 393 394 397 private String unpackagedName(String className) { 398 int idx = className.lastIndexOf((int) '/'); 399 if (idx < 0) 400 return className; 401 return className.substring(idx+1); 402 } 403 } 404 | Popular Tags |