|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.jconfig.utils; |
|
8 |
| import java.io.BufferedInputStream; |
|
9 |
| import java.io.File; |
|
10 |
| import java.io.FileInputStream; |
|
11 |
| import java.io.IOException; |
|
12 |
| import java.io.InputStream; |
|
13 |
| import java.net.URL; |
|
14 |
| import java.net.URLDecoder; |
|
15 |
| import java.util.StringTokenizer; |
|
16 |
| import java.util.Vector; |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| public class ResourceLocator { |
|
31 |
| |
|
32 |
| private String name; |
|
33 |
| private File file; |
|
34 |
| private URL url; |
|
35 |
| |
|
36 |
125
| public ResourceLocator(String name) throws IOException {
|
|
37 |
125
| this.name = name;
|
|
38 |
125
| SecurityException exception = null;
|
|
39 |
| |
|
40 |
125
| try {
|
|
41 |
| |
|
42 |
| |
|
43 |
125
| if (tryClasspath(name)) {
|
|
44 |
120
| return;
|
|
45 |
| } |
|
46 |
| } catch (SecurityException e) { |
|
47 |
0
| exception = e;
|
|
48 |
| } |
|
49 |
| |
|
50 |
5
| try {
|
|
51 |
| |
|
52 |
| |
|
53 |
5
| if (tryLoader(name)) {
|
|
54 |
0
| return;
|
|
55 |
| } |
|
56 |
| } catch (SecurityException e) { |
|
57 |
0
| exception = e;
|
|
58 |
| } |
|
59 |
| |
|
60 |
| |
|
61 |
5
| String msg = "";
|
|
62 |
5
| if (exception != null) {
|
|
63 |
0
| msg = ": " + exception;
|
|
64 |
| } |
|
65 |
| |
|
66 |
5
| throw new IOException(
|
|
67 |
| "Resource '" |
|
68 |
| + name |
|
69 |
| + "' could not be found in " |
|
70 |
| + "the CLASSPATH (" |
|
71 |
| + System.getProperty("java.class.path") |
|
72 |
| + "), nor could it be located by the classloader responsible for the " |
|
73 |
| + "web application (WEB-INF/classes)" |
|
74 |
| + msg); |
|
75 |
| } |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
2
| public InputStream findResource(String fileName) {
|
|
83 |
2
| return getClass().getClassLoader().getResourceAsStream(fileName);
|
|
84 |
| } |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
1
| public String getName() {
|
|
90 |
1
| return name;
|
|
91 |
| } |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
116
| public InputStream getInputStream() throws IOException {
|
|
97 |
116
| if (file != null) {
|
|
98 |
116
| return new BufferedInputStream(new FileInputStream(file));
|
|
99 |
0
| } else if (url != null) {
|
|
100 |
0
| return new BufferedInputStream(url.openStream());
|
|
101 |
| } |
|
102 |
0
| return null;
|
|
103 |
| } |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
0
| public long lastModified() {
|
|
114 |
0
| if (file != null) {
|
|
115 |
0
| return file.lastModified();
|
|
116 |
0
| } else if (url != null) {
|
|
117 |
0
| try {
|
|
118 |
0
| return url.openConnection().getLastModified();
|
|
119 |
| } catch (IOException e) { |
|
120 |
0
| return Long.MAX_VALUE;
|
|
121 |
| } |
|
122 |
| } |
|
123 |
0
| return 0;
|
|
124 |
| } |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
0
| public String getDirectory() {
|
|
133 |
0
| if (file != null) {
|
|
134 |
0
| return file.getParent();
|
|
135 |
0
| } else if (url != null) {
|
|
136 |
0
| return null;
|
|
137 |
| } |
|
138 |
0
| return null;
|
|
139 |
| } |
|
140 |
| |
|
141 |
| |
|
142 |
125
| private boolean tryClasspath(String filename) {
|
|
143 |
125
| String classpath = System.getProperty("java.class.path");
|
|
144 |
| |
|
145 |
| |
|
146 |
125
| String[] paths = split(classpath,System.getProperty("path.separator"));
|
|
147 |
125
| file = searchDirectories(paths, filename);
|
|
148 |
125
| return (file != null);
|
|
149 |
| } |
|
150 |
| |
|
151 |
125
| private static File searchDirectories(String[] paths, String filename) {
|
|
152 |
125
| SecurityException exception = null;
|
|
153 |
125
| for (int i = 0; i < paths.length; i++) {
|
|
154 |
400
| try {
|
|
155 |
| |
|
156 |
| |
|
157 |
400
| File file = new File(paths[i], filename);
|
|
158 |
| |
|
159 |
400
| if (file.exists() && !file.isDirectory()) {
|
|
160 |
120
| return file;
|
|
161 |
| } |
|
162 |
280
| file = new File(paths[i]);
|
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| } catch (SecurityException e) { |
|
168 |
| |
|
169 |
| |
|
170 |
0
| exception = e;
|
|
171 |
| } |
|
172 |
| } |
|
173 |
| |
|
174 |
5
| if (exception != null) {
|
|
175 |
0
| throw exception;
|
|
176 |
| } else { |
|
177 |
5
| return null;
|
|
178 |
| } |
|
179 |
| } |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
125
| private static String[] split(String str, String delim) {
|
|
185 |
| |
|
186 |
125
| Vector v = new Vector();
|
|
187 |
| |
|
188 |
| |
|
189 |
125
| StringTokenizer tokenizer = new StringTokenizer(str, delim);
|
|
190 |
125
| while (tokenizer.hasMoreTokens()) {
|
|
191 |
1000
| v.addElement(tokenizer.nextToken());
|
|
192 |
| } |
|
193 |
| |
|
194 |
125
| String[] ret = new String[v.size()];
|
|
195 |
125
| v.copyInto(ret);
|
|
196 |
125
| return ret;
|
|
197 |
| } |
|
198 |
| |
|
199 |
| |
|
200 |
5
| private boolean tryLoader(String name) {
|
|
201 |
5
| name = "/" + name;
|
|
202 |
5
| URL res = ResourceLocator.class.getResource(name);
|
|
203 |
5
| if (res == null) {
|
|
204 |
5
| return false;
|
|
205 |
| } |
|
206 |
| |
|
207 |
| |
|
208 |
0
| File resFile = urlToFile(res);
|
|
209 |
0
| if (resFile != null) {
|
|
210 |
0
| file = resFile;
|
|
211 |
| } else { |
|
212 |
0
| url = res;
|
|
213 |
| } |
|
214 |
0
| return true;
|
|
215 |
| } |
|
216 |
| |
|
217 |
0
| private static File urlToFile(URL res) {
|
|
218 |
| |
|
219 |
| |
|
220 |
0
| String externalForm = URLDecoder.decode(res.toExternalForm());
|
|
221 |
0
| if (externalForm.startsWith("file:")) {
|
|
222 |
0
| return new File(externalForm.substring(5));
|
|
223 |
| } |
|
224 |
0
| return null;
|
|
225 |
| } |
|
226 |
| |
|
227 |
0
| public String toString() {
|
|
228 |
0
| return "[Resource: File: " + file + " URL: " + url + "]";
|
|
229 |
| } |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
1
| public File getFile() {
|
|
236 |
1
| return file;
|
|
237 |
| } |
|
238 |
| |
|
239 |
| } |