1 17 18 package org.objectweb.jac.aspects.gui.web; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.net.JarURLConnection ; 26 import java.net.URL ; 27 import java.net.URLConnection ; 28 import java.util.Date ; 29 import org.apache.log4j.Logger; 30 import org.mortbay.util.Resource; 31 import org.objectweb.jac.util.ExtArrays; 32 33 public class ClasspathResource extends Resource { 34 static Logger logger = Logger.getLogger("web"); 35 36 long lastModified = new Date ().getTime(); 37 String path; 38 URLConnection connection; 39 File file; 40 boolean isFile; 41 public ClasspathResource(String path) throws IOException { 42 if (path.startsWith("/")) { 43 path = path.substring(1); 44 } 45 this.path = path; 46 URL url = getClass().getClassLoader().getResource(path); 47 if (url!=null) { 48 file = new File (url.getFile()); 49 connection = url.openConnection(); 50 if (connection instanceof JarURLConnection ) { 51 file = new File (((JarURLConnection )connection).getJarFileURL().getFile()); 52 } 53 logger.debug("New classpath resource: "+url+" isFile="+file.isFile()); 54 } else { 55 logger.debug("Resource not found: "+path); 57 } 58 } 59 public ClasspathResource() { 60 path = null; 61 } 62 public void release() { 63 } 64 public boolean exists() { 65 boolean exists = isFile ? file.exists() : getInputStream()!=null; 66 logger.debug("exists "+path+"? -> "+exists); 67 return exists; 68 } 69 public boolean isDirectory() { 70 return false; 71 } 72 public long lastModified() { 73 if (file.exists()) 74 return file.lastModified(); 75 else 76 return lastModified; 77 } 78 public long length() { 79 try { 80 if (isFile) { 81 return file.length(); 82 } else { 83 InputStream is = getInputStream(); 84 if (is!=null) 85 return getInputStream().available(); 86 else 87 return 0; 88 } 89 } catch(Exception e) { 90 logger.error("Failed to get length of resource: "+path,e); 91 return 0; 92 } 93 } 94 public URL getURL() { 95 return null; 96 } 97 public File getFile() { 98 return file; 99 } 100 public String getName() { 101 return path; 102 } 103 public InputStream getInputStream() { 104 logger.debug("getInputStream "+file); 105 if (isFile) { 106 try { 107 return new FileInputStream (file); 108 } catch (IOException e) { 109 logger.error("getInputStream "+path,e); 110 return null; 111 } 112 } else { 113 return getClass().getClassLoader().getResourceAsStream(path); 114 } 115 } 116 public OutputStream getOutputStream() { 117 return null; 118 } 119 public boolean delete() { 120 return false; 121 } 122 public boolean renameTo(Resource newName) { 123 return false; 124 } 125 public String [] list() { 126 return ExtArrays.emptyStringArray; 127 } 128 public Resource addPath(String addedPath) throws IOException { 129 if (path==null) 130 return new ClasspathResource(addedPath); 131 else 132 return this; 133 } 134 public String toString() { 135 return path; 136 } 137 138 } 139 | Popular Tags |