KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > gui > web > ClasspathResource


1 /*
2   Copyright (C) 2001-2003 Laurent Martelli <laurent@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.gui.web;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.net.JarURLConnection JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.net.URLConnection JavaDoc;
28 import java.util.Date JavaDoc;
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 JavaDoc().getTime();
37     String JavaDoc path;
38     URLConnection JavaDoc connection;
39     File JavaDoc file;
40     boolean isFile;
41     public ClasspathResource(String JavaDoc path) throws IOException JavaDoc {
42         if (path.startsWith("/")) {
43             path = path.substring(1);
44         }
45         this.path = path;
46         URL JavaDoc url = getClass().getClassLoader().getResource(path);
47         if (url!=null) {
48             file = new File JavaDoc(url.getFile());
49             connection = url.openConnection();
50             if (connection instanceof JarURLConnection JavaDoc) {
51                 file = new File JavaDoc(((JarURLConnection JavaDoc)connection).getJarFileURL().getFile());
52             }
53             logger.debug("New classpath resource: "+url+" isFile="+file.isFile());
54         } else {
55             //throw new RuntimeException("Resource not found: "+path);
56
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 JavaDoc is = getInputStream();
84                 if (is!=null)
85                     return getInputStream().available();
86                 else
87                     return 0;
88             }
89         } catch(Exception JavaDoc e) {
90             logger.error("Failed to get length of resource: "+path,e);
91             return 0;
92         }
93     }
94     public URL JavaDoc getURL() {
95         return null;
96     }
97     public File JavaDoc getFile() {
98         return file;
99     }
100     public String JavaDoc getName() {
101         return path;
102     }
103     public InputStream JavaDoc getInputStream() {
104         logger.debug("getInputStream "+file);
105         if (isFile) {
106             try {
107                 return new FileInputStream JavaDoc(file);
108             } catch (IOException JavaDoc e) {
109                 logger.error("getInputStream "+path,e);
110                 return null;
111             }
112         } else {
113             return getClass().getClassLoader().getResourceAsStream(path);
114         }
115     }
116     public OutputStream JavaDoc 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 JavaDoc[] list() {
126         return ExtArrays.emptyStringArray;
127     }
128     public Resource addPath(String JavaDoc addedPath) throws IOException JavaDoc {
129         if (path==null)
130             return new ClasspathResource(addedPath);
131         else
132             return this;
133     }
134     public String JavaDoc toString() {
135         return path;
136     }
137
138 }
139
Popular Tags