KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > util > ResourceUtils


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.util;
18
19 import java.io.File JavaDoc;
20 import java.io.FileNotFoundException JavaDoc;
21 import java.net.MalformedURLException JavaDoc;
22 import java.net.URL JavaDoc;
23 import java.net.URLDecoder JavaDoc;
24
25 /**
26  * Utility methods for resolving resource locations to files in the
27  * file system. Mainly for internal use within the framework.
28  *
29  * <p>Consider using Spring's Resource abstraction in the core package
30  * for handling all kinds of file resources in a uniform manner.
31  * {@link org.springframework.core.io.ResourceLoader}'s <code>getResource</code>
32  * method can resolve any location to a {@link org.springframework.core.io.Resource}
33  * object, which in turn allows to obtain a <code>java.io.File</code> in the
34  * file system through its <code>getFile()</code> method.
35  *
36  * <p>The main reason for these utility methods for resource location handling
37  * is to support {@link Log4jConfigurer}, which must be able to resolve
38  * resource locations <i>before the logging system has been initialized</i>.
39  * Spring' Resource abstraction in the core package, on the other hand,
40  * already expects the logging system to be available.
41  *
42  * @author Juergen Hoeller
43  * @since 1.1.5
44  * @see org.springframework.core.io.Resource
45  * @see org.springframework.core.io.ClassPathResource
46  * @see org.springframework.core.io.FileSystemResource
47  * @see org.springframework.core.io.UrlResource
48  * @see org.springframework.core.io.ResourceLoader
49  */

50 public abstract class ResourceUtils {
51
52     /** Pseudo URL prefix for loading from the class path: "classpath:" */
53     public static final String JavaDoc CLASSPATH_URL_PREFIX = "classpath:";
54
55     /** URL prefix for loading from the file system: "file:" */
56     public static final String JavaDoc FILE_URL_PREFIX = "file:";
57
58     /** URL protocol for a file in the file system: "file" */
59     public static final String JavaDoc URL_PROTOCOL_FILE = "file";
60
61     /** URL protocol for an entry from a jar file: "jar" */
62     public static final String JavaDoc URL_PROTOCOL_JAR = "jar";
63
64     /** URL protocol for an entry from a zip file: "zip" */
65     public static final String JavaDoc URL_PROTOCOL_ZIP = "zip";
66
67     /** URL protocol for an entry from a WebSphere jar file: "wsjar" */
68     public static final String JavaDoc URL_PROTOCOL_WSJAR = "wsjar";
69
70     /** Separator between JAR URL and file path within the JAR */
71     public static final String JavaDoc JAR_URL_SEPARATOR = "!/";
72
73
74     /**
75      * Return whether the given resource location is a URL:
76      * either a special "classpath" pseudo URL or a standard URL.
77      * @see #CLASSPATH_URL_PREFIX
78      * @see java.net.URL
79      */

80     public static boolean isUrl(String JavaDoc resourceLocation) {
81         if (resourceLocation == null) {
82             return false;
83         }
84         if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
85             return true;
86         }
87         try {
88             new URL JavaDoc(resourceLocation);
89             return true;
90         }
91         catch (MalformedURLException JavaDoc ex) {
92             return false;
93         }
94     }
95
96     /**
97      * Resolve the given resource location to a <code>java.net.URL</code>.
98      * <p>Does not check whether the URL actually exists; simply returns
99      * the URL that the given location would correspond to.
100      * @param resourceLocation the resource location to resolve: either a
101      * "classpath:" pseudo URL, a "file:" URL, or a plain file path
102      * @return a corresponding URL object
103      * @throws FileNotFoundException if the resource cannot be resolved to a URL
104      */

105     public static URL JavaDoc getURL(String JavaDoc resourceLocation) throws FileNotFoundException JavaDoc {
106         Assert.notNull(resourceLocation, "Resource location must not be null");
107         if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
108             String JavaDoc path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
109             URL JavaDoc url = ClassUtils.getDefaultClassLoader().getResource(path);
110             if (url == null) {
111                 String JavaDoc description = "class path resource [" + path + "]";
112                 throw new FileNotFoundException JavaDoc(
113                         description + " cannot be resolved to URL because it does not exist");
114             }
115             return url;
116         }
117         try {
118             // try URL
119
return new URL JavaDoc(resourceLocation);
120         }
121         catch (MalformedURLException JavaDoc ex) {
122             // no URL -> treat as file path
123
try {
124                 return new URL JavaDoc(FILE_URL_PREFIX + resourceLocation);
125             }
126             catch (MalformedURLException JavaDoc ex2) {
127                 throw new FileNotFoundException JavaDoc("Resource location [" + resourceLocation +
128                         "] is neither a URL not a well-formed file path");
129             }
130         }
131     }
132
133     /**
134      * Resolve the given resource location to a <code>java.io.File</code>,
135      * i.e. to a file in the file system.
136      * <p>Does not check whether the fil actually exists; simply returns
137      * the File that the given location would correspond to.
138      * @param resourceLocation the resource location to resolve: either a
139      * "classpath:" pseudo URL, a "file:" URL, or a plain file path
140      * @return a corresponding File object
141      * @throws FileNotFoundException if the resource cannot be resolved to
142      * a file in the file system
143      */

144     public static File JavaDoc getFile(String JavaDoc resourceLocation) throws FileNotFoundException JavaDoc {
145         Assert.notNull(resourceLocation, "Resource location must not be null");
146         if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
147             String JavaDoc path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
148             String JavaDoc description = "class path resource [" + path + "]";
149             URL JavaDoc url = ClassUtils.getDefaultClassLoader().getResource(path);
150             if (url == null) {
151                 throw new FileNotFoundException JavaDoc(
152                         description + " cannot be resolved to absolute file path " +
153                         "because it does not reside in the file system");
154             }
155             return getFile(url, description);
156         }
157         try {
158             // try URL
159
return getFile(new URL JavaDoc(resourceLocation));
160         }
161         catch (MalformedURLException JavaDoc ex) {
162             // no URL -> treat as file path
163
return new File JavaDoc(resourceLocation);
164         }
165     }
166
167     /**
168      * Resolve the given resource URL to a <code>java.io.File</code>,
169      * i.e. to a file in the file system.
170      * @param resourceUrl the resource URL to resolve
171      * @return a corresponding File object
172      * @throws FileNotFoundException if the URL cannot be resolved to
173      * a file in the file system
174      */

175     public static File JavaDoc getFile(URL JavaDoc resourceUrl) throws FileNotFoundException JavaDoc {
176         return getFile(resourceUrl, "URL");
177     }
178
179     /**
180      * Resolve the given resource URL to a <code>java.io.File</code>,
181      * i.e. to a file in the file system.
182      * @param resourceUrl the resource URL to resolve
183      * @param description a description of the original resource that
184      * the URL was created for (for example, a class path location)
185      * @return a corresponding File object
186      * @throws FileNotFoundException if the URL cannot be resolved to
187      * a file in the file system
188      */

189     public static File JavaDoc getFile(URL JavaDoc resourceUrl, String JavaDoc description) throws FileNotFoundException JavaDoc {
190         Assert.notNull(resourceUrl, "Resource URL must not be null");
191         if (!URL_PROTOCOL_FILE.equals(resourceUrl.getProtocol())) {
192             throw new FileNotFoundException JavaDoc(
193                     description + " cannot be resolved to absolute file path " +
194                     "because it does not reside in the file system: " + resourceUrl);
195         }
196         return new File JavaDoc(URLDecoder.decode(resourceUrl.getFile()));
197     }
198
199     /**
200      * Determine whether the given URL points to a resource in a jar file,
201      * that is, has protocol "jar", "zip" or "wsjar".
202      * <p>"zip" and "wsjar" are used by BEA WebLogic Server and IBM WebSphere,
203      * respectively, but can be treated like jar files.
204      * @param url the URL to check
205      */

206     public static boolean isJarURL(URL JavaDoc url) {
207         String JavaDoc protocol = url.getProtocol();
208         return (URL_PROTOCOL_JAR.equals(protocol) ||
209                 URL_PROTOCOL_ZIP.equals(protocol) ||
210                 URL_PROTOCOL_WSJAR.equals(protocol));
211     }
212
213     /**
214      * Extract the URL for the actual jar file from the given URL
215      * (which may point to a resource in a jar file or to a jar file itself).
216      * @param jarUrl the original URL
217      * @return the URL for the actual jar file
218      * @throws MalformedURLException if no valid jar file URL could be extracted
219      */

220     public static URL JavaDoc extractJarFileURL(URL JavaDoc jarUrl) throws MalformedURLException JavaDoc {
221         String JavaDoc urlFile = jarUrl.getFile();
222         int separatorIndex = urlFile.indexOf(JAR_URL_SEPARATOR);
223         if (separatorIndex != -1) {
224             String JavaDoc jarFile = urlFile.substring(0, separatorIndex);
225             try {
226                 return new URL JavaDoc(jarFile);
227             }
228             catch (MalformedURLException JavaDoc ex) {
229                 // Probably no protocol in original jar URL, like "jar:C:/mypath/myjar.jar".
230
// This usually indicates that the jar file resides in the file system.
231
if (!jarFile.startsWith("/")) {
232                     jarFile = "/" + jarFile;
233                 }
234                 return new URL JavaDoc(FILE_URL_PREFIX + jarFile);
235             }
236         }
237         else {
238             return jarUrl;
239         }
240     }
241
242 }
243
Popular Tags