Source code: jac/aspects/gui/web/ClasspathResource.java
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 jac.aspects.gui.web;
19
20 import jac.util.Log;
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.net.URL;
26 import java.util.Date;
27 import org.mortbay.util.Resource;
28
29 public class ClasspathResource extends Resource {
30 long lastModified = new Date().getTime();
31 String path;
32 File file;
33 public ClasspathResource(String path) {
34 if (path.startsWith("/")) {
35 path = path.substring(1);
36 }
37 this.path = path;
38 URL url = getClass().getClassLoader().getResource(path);
39 file = new File(url.getFile());
40 }
41 public ClasspathResource() {
42 path = null;
43 }
44 public void release() {
45 }
46 public boolean exists() {
47 return getInputStream()!=null;
48 }
49 public boolean isDirectory() {
50 return false;
51 }
52 public long lastModified() {
53 if (file.exists())
54 return file.lastModified();
55 else
56 return lastModified;
57 }
58 public long length() {
59 try {
60 InputStream is = getInputStream();
61 if (is!=null)
62 return getInputStream().available();
63 else
64 return 0;
65 } catch(Exception e) {
66 Log.error("Failed to get length of resource: "+path);
67 e.printStackTrace();
68 return 0;
69 }
70 }
71 public URL getURL() {
72 return null;
73 }
74 public File getFile() {
75 return file;
76 }
77 public String getName() {
78 return path;
79 }
80 public InputStream getInputStream() {
81 return getClass().getClassLoader().getResourceAsStream(path);
82 }
83 public OutputStream getOutputStream() {
84 return null;
85 }
86 public boolean delete() {
87 return false;
88 }
89 public boolean renameTo(Resource newName) {
90 return false;
91 }
92 public String[] list() {
93 return new String[0];
94 }
95 public Resource addPath(String addedPath) {
96 if (path==null)
97 return new ClasspathResource(addedPath);
98 else
99 return this;
100 }
101 public String toString() {
102 return path;
103 }
104
105 }