|
|||||||||||||||||||
| 30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ResourceHelper.java | 31.2% | 26.7% | 30% | 28.1% |
|
||||||||||||||
| 1 |
/**
|
|
| 2 |
* JFoxSOAF, Service-Oriented Application Framework
|
|
| 3 |
*
|
|
| 4 |
* Copyright (C) www.huihoo.org
|
|
| 5 |
*
|
|
| 6 |
* Distributable under GNU LGPL
|
|
| 7 |
*
|
|
| 8 |
* For more information, please visit: http://www.huihoo.org/jfox/jfoxsoaf
|
|
| 9 |
*/
|
|
| 10 |
|
|
| 11 |
package org.huihoo.jfox.soaf.util.resource;
|
|
| 12 |
|
|
| 13 |
import java.io.File;
|
|
| 14 |
import java.io.IOException;
|
|
| 15 |
import java.io.InputStream;
|
|
| 16 |
import java.io.InputStreamReader;
|
|
| 17 |
import java.io.Reader;
|
|
| 18 |
import java.net.URL;
|
|
| 19 |
import java.net.URLConnection;
|
|
| 20 |
import java.util.Properties;
|
|
| 21 |
|
|
| 22 |
/**
|
|
| 23 |
* A class to simplify access to resources through the classloader.
|
|
| 24 |
*
|
|
| 25 |
* @author <a href="mailto:founder_chen@yahoo.com.cn">Peter Cheng </a>
|
|
| 26 |
* @author <a href="mailto:clinton.begin@ibatis.com">Clinton Begin </a>
|
|
| 27 |
* @version $Revision: 1.1 $ $Date: 2004/10/28 05:12:31 $
|
|
| 28 |
* @version Revision: 1.0
|
|
| 29 |
*/
|
|
| 30 |
|
|
| 31 |
public class ResourceHelper { |
|
| 32 |
|
|
| 33 |
private static ClassLoader defaultClassLoader; |
|
| 34 |
|
|
| 35 | 0 |
private ResourceHelper() {
|
| 36 |
} |
|
| 37 |
|
|
| 38 |
/**
|
|
| 39 |
* Returns the default classloader (may be null).
|
|
| 40 |
*
|
|
| 41 |
* @return The default classloader
|
|
| 42 |
*/
|
|
| 43 | 0 |
public static ClassLoader getDefaultClassLoader() { |
| 44 | 0 |
return defaultClassLoader;
|
| 45 |
} |
|
| 46 |
|
|
| 47 |
/**
|
|
| 48 |
* Sets the default classloader
|
|
| 49 |
*
|
|
| 50 |
* @param defaultClassLoader -
|
|
| 51 |
* the new default ClassLoader
|
|
| 52 |
*/
|
|
| 53 | 0 |
public static void setDefaultClassLoader(ClassLoader defaultClassLoader) { |
| 54 | 0 |
ResourceHelper.defaultClassLoader = defaultClassLoader; |
| 55 |
} |
|
| 56 |
|
|
| 57 |
/**
|
|
| 58 |
* Returns the URL of the resource on the classpath
|
|
| 59 |
*
|
|
| 60 |
* @param resource
|
|
| 61 |
* The resource to find
|
|
| 62 |
* @return The resource
|
|
| 63 |
* @throws IOException
|
|
| 64 |
* If the resource cannot be found or read
|
|
| 65 |
*/
|
|
| 66 | 0 |
public static URL getResourceURL(String resource) throws IOException { |
| 67 | 0 |
return getResourceURL(getClassLoader(), resource);
|
| 68 |
} |
|
| 69 |
|
|
| 70 |
/**
|
|
| 71 |
* Returns the URL of the resource on the classpath
|
|
| 72 |
*
|
|
| 73 |
* @param loader
|
|
| 74 |
* The classloader used to load the resource
|
|
| 75 |
* @param resource
|
|
| 76 |
* The resource to find
|
|
| 77 |
* @return The resource
|
|
| 78 |
* @throws IOException
|
|
| 79 |
* If the resource cannot be found or read
|
|
| 80 |
*/
|
|
| 81 | 0 |
public static URL getResourceURL(ClassLoader loader, String resource) |
| 82 |
throws IOException {
|
|
| 83 | 0 |
URL url = null;
|
| 84 | 0 |
if (loader != null) |
| 85 | 0 |
url = loader.getResource(resource); |
| 86 | 0 |
if (url == null) |
| 87 | 0 |
url = ClassLoader.getSystemResource(resource); |
| 88 | 0 |
if (url == null) |
| 89 | 0 |
throw new IOException("Could not find resource " + resource); |
| 90 | 0 |
return url;
|
| 91 |
} |
|
| 92 |
|
|
| 93 |
/**
|
|
| 94 |
* Returns a resource on the classpath as a Stream object
|
|
| 95 |
*
|
|
| 96 |
* @param resource
|
|
| 97 |
* The resource to find
|
|
| 98 |
* @return The resource
|
|
| 99 |
* @throws IOException
|
|
| 100 |
* If the resource cannot be found or read
|
|
| 101 |
*/
|
|
| 102 | 13 |
public static InputStream getResourceAsStream(String resource) |
| 103 |
throws IOException {
|
|
| 104 | 13 |
return getResourceAsStream(getClassLoader(), resource);
|
| 105 |
} |
|
| 106 |
|
|
| 107 |
/**
|
|
| 108 |
* Returns a resource on the classpath as a Stream object
|
|
| 109 |
*
|
|
| 110 |
* @param loader
|
|
| 111 |
* The classloader used to load the resource
|
|
| 112 |
* @param resource
|
|
| 113 |
* The resource to find
|
|
| 114 |
* @return The resource
|
|
| 115 |
* @throws IOException
|
|
| 116 |
* If the resource cannot be found or read
|
|
| 117 |
*/
|
|
| 118 | 13 |
public static InputStream getResourceAsStream(ClassLoader loader, |
| 119 |
String resource) throws IOException {
|
|
| 120 | 13 |
InputStream in = null;
|
| 121 | 13 |
if (loader != null) |
| 122 | 13 |
in = loader.getResourceAsStream(resource); |
| 123 | 13 |
if (in == null) |
| 124 | 0 |
in = ClassLoader.getSystemResourceAsStream(resource); |
| 125 | 13 |
if (in == null) |
| 126 | 0 |
throw new IOException("Could not find resource " + resource); |
| 127 | 13 |
return in;
|
| 128 |
} |
|
| 129 |
|
|
| 130 |
/**
|
|
| 131 |
* Returns a resource on the classpath as a Properties object
|
|
| 132 |
*
|
|
| 133 |
* @param resource
|
|
| 134 |
* The resource to find
|
|
| 135 |
* @return The resource
|
|
| 136 |
* @throws IOException
|
|
| 137 |
* If the resource cannot be found or read
|
|
| 138 |
*/
|
|
| 139 | 0 |
public static Properties getResourceAsProperties(String resource) |
| 140 |
throws IOException {
|
|
| 141 | 0 |
Properties props = new Properties();
|
| 142 | 0 |
InputStream in = null;
|
| 143 | 0 |
String propfile = resource; |
| 144 | 0 |
in = getResourceAsStream(propfile); |
| 145 | 0 |
props.load(in); |
| 146 | 0 |
in.close(); |
| 147 | 0 |
return props;
|
| 148 |
} |
|
| 149 |
|
|
| 150 |
/**
|
|
| 151 |
* Returns a resource on the classpath as a Properties object
|
|
| 152 |
*
|
|
| 153 |
* @param loader
|
|
| 154 |
* The classloader used to load the resource
|
|
| 155 |
* @param resource
|
|
| 156 |
* The resource to find
|
|
| 157 |
* @return The resource
|
|
| 158 |
* @throws IOException
|
|
| 159 |
* If the resource cannot be found or read
|
|
| 160 |
*/
|
|
| 161 | 0 |
public static Properties getResourceAsProperties(ClassLoader loader, |
| 162 |
String resource) throws IOException {
|
|
| 163 | 0 |
Properties props = new Properties();
|
| 164 | 0 |
InputStream in = null;
|
| 165 | 0 |
String propfile = resource; |
| 166 | 0 |
in = getResourceAsStream(loader, propfile); |
| 167 | 0 |
props.load(in); |
| 168 | 0 |
in.close(); |
| 169 | 0 |
return props;
|
| 170 |
} |
|
| 171 |
|
|
| 172 |
/**
|
|
| 173 |
* Returns a resource on the classpath as a Reader object
|
|
| 174 |
*
|
|
| 175 |
* @param resource
|
|
| 176 |
* The resource to find
|
|
| 177 |
* @return The resource
|
|
| 178 |
* @throws IOException
|
|
| 179 |
* If the resource cannot be found or read
|
|
| 180 |
*/
|
|
| 181 | 0 |
public static Reader getResourceAsReader(String resource) |
| 182 |
throws IOException {
|
|
| 183 | 0 |
return new InputStreamReader(getResourceAsStream(resource)); |
| 184 |
} |
|
| 185 |
|
|
| 186 |
/**
|
|
| 187 |
* Returns a resource on the classpath as a Reader object
|
|
| 188 |
*
|
|
| 189 |
* @param loader
|
|
| 190 |
* The classloader used to load the resource
|
|
| 191 |
* @param resource
|
|
| 192 |
* The resource to find
|
|
| 193 |
* @return The resource
|
|
| 194 |
* @throws IOException
|
|
| 195 |
* If the resource cannot be found or read
|
|
| 196 |
*/
|
|
| 197 | 0 |
public static Reader getResourceAsReader(ClassLoader loader, String resource) |
| 198 |
throws IOException {
|
|
| 199 | 0 |
return new InputStreamReader(getResourceAsStream(loader, resource)); |
| 200 |
} |
|
| 201 |
|
|
| 202 |
/**
|
|
| 203 |
* Returns a resource on the classpath as a File object
|
|
| 204 |
*
|
|
| 205 |
* @param resource
|
|
| 206 |
* The resource to find
|
|
| 207 |
* @return The resource
|
|
| 208 |
* @throws IOException
|
|
| 209 |
* If the resource cannot be found or read
|
|
| 210 |
*/
|
|
| 211 | 0 |
public static File getResourceAsFile(String resource) throws IOException { |
| 212 | 0 |
return new File(getResourceURL(resource).getFile()); |
| 213 |
} |
|
| 214 |
|
|
| 215 |
/**
|
|
| 216 |
* Returns a resource on the classpath as a File object
|
|
| 217 |
*
|
|
| 218 |
* @param loader -
|
|
| 219 |
* the classloader used to load the resource
|
|
| 220 |
* @param resource -
|
|
| 221 |
* the resource to find
|
|
| 222 |
* @return The resource
|
|
| 223 |
* @throws IOException
|
|
| 224 |
* If the resource cannot be found or read
|
|
| 225 |
*/
|
|
| 226 | 0 |
public static File getResourceAsFile(ClassLoader loader, String resource) |
| 227 |
throws IOException {
|
|
| 228 | 0 |
return new File(getResourceURL(loader, resource).getFile()); |
| 229 |
} |
|
| 230 |
|
|
| 231 |
/**
|
|
| 232 |
* Gets a URL as an input stream
|
|
| 233 |
*
|
|
| 234 |
* @param urlString -
|
|
| 235 |
* the URL to get
|
|
| 236 |
* @return An input stream with the data from the URL
|
|
| 237 |
* @throws IOException
|
|
| 238 |
* If the resource cannot be found or read
|
|
| 239 |
*/
|
|
| 240 | 0 |
public static InputStream getUrlAsStream(String urlString) |
| 241 |
throws IOException {
|
|
| 242 | 0 |
URL url = new URL(urlString);
|
| 243 | 0 |
URLConnection conn = url.openConnection(); |
| 244 | 0 |
return conn.getInputStream();
|
| 245 |
} |
|
| 246 |
|
|
| 247 |
/**
|
|
| 248 |
* Gets a URL as a Reader
|
|
| 249 |
*
|
|
| 250 |
* @param urlString -
|
|
| 251 |
* the URL to get
|
|
| 252 |
* @return A Reader with the data from the URL
|
|
| 253 |
* @throws IOException
|
|
| 254 |
* If the resource cannot be found or read
|
|
| 255 |
*/
|
|
| 256 | 0 |
public static Reader getUrlAsReader(String urlString) throws IOException { |
| 257 | 0 |
return new InputStreamReader(getUrlAsStream(urlString)); |
| 258 |
} |
|
| 259 |
|
|
| 260 |
/**
|
|
| 261 |
* Gets a URL as a Properties object
|
|
| 262 |
*
|
|
| 263 |
* @param urlString -
|
|
| 264 |
* the URL to get
|
|
| 265 |
* @return A Properties object with the data from the URL
|
|
| 266 |
* @throws IOException
|
|
| 267 |
* If the resource cannot be found or read
|
|
| 268 |
*/
|
|
| 269 | 0 |
public static Properties getUrlAsProperties(String urlString) |
| 270 |
throws IOException {
|
|
| 271 | 0 |
Properties props = new Properties();
|
| 272 | 0 |
InputStream in = null;
|
| 273 | 0 |
String propfile = urlString; |
| 274 | 0 |
in = getUrlAsStream(propfile); |
| 275 | 0 |
props.load(in); |
| 276 | 0 |
in.close(); |
| 277 | 0 |
return props;
|
| 278 |
} |
|
| 279 |
|
|
| 280 |
/**
|
|
| 281 |
* Loads a class
|
|
| 282 |
*
|
|
| 283 |
* @param className -
|
|
| 284 |
* the class to load
|
|
| 285 |
* @return The loaded class
|
|
| 286 |
* @throws ClassNotFoundException
|
|
| 287 |
* If the class cannot be found (duh!)
|
|
| 288 |
*/
|
|
| 289 | 21 |
public static Class classForName(String className) |
| 290 |
throws ClassNotFoundException {
|
|
| 291 | 21 |
Class clazz = null;
|
| 292 | 21 |
try {
|
| 293 | 21 |
clazz = getClassLoader().loadClass(className); |
| 294 |
} catch (Exception e) {
|
|
| 295 |
// Ignore. Failsafe below.
|
|
| 296 |
} |
|
| 297 | 21 |
if (clazz == null) { |
| 298 | 0 |
clazz = Class.forName(className); |
| 299 |
} |
|
| 300 | 21 |
return clazz;
|
| 301 |
} |
|
| 302 |
|
|
| 303 |
/**
|
|
| 304 |
* Creates an instance of a class
|
|
| 305 |
*
|
|
| 306 |
* @param className -
|
|
| 307 |
* the class to create
|
|
| 308 |
* @return An instance of the class
|
|
| 309 |
* @throws ClassNotFoundException
|
|
| 310 |
* If the class cannot be found (duh!)
|
|
| 311 |
* @throws InstantiationException
|
|
| 312 |
* If the class cannot be instantiaed
|
|
| 313 |
* @throws IllegalAccessException
|
|
| 314 |
* If the class is not public, or other access problems arise
|
|
| 315 |
*/
|
|
| 316 | 5 |
public static Object instantiate(String className) |
| 317 |
throws ClassNotFoundException, InstantiationException,
|
|
| 318 |
IllegalAccessException {
|
|
| 319 | 5 |
return instantiate(classForName(className));
|
| 320 |
} |
|
| 321 |
|
|
| 322 |
/**
|
|
| 323 |
* Creates an instance of a class
|
|
| 324 |
*
|
|
| 325 |
* @param clazz -
|
|
| 326 |
* the class to create
|
|
| 327 |
* @return An instance of the class
|
|
| 328 |
* @throws InstantiationException
|
|
| 329 |
* If the class cannot be instantiaed
|
|
| 330 |
* @throws IllegalAccessException
|
|
| 331 |
* If the class is not public, or other access problems arise
|
|
| 332 |
*/
|
|
| 333 | 5 |
public static Object instantiate(Class clazz) |
| 334 |
throws InstantiationException, IllegalAccessException {
|
|
| 335 | 5 |
return clazz.newInstance();
|
| 336 |
} |
|
| 337 |
|
|
| 338 | 34 |
private static ClassLoader getClassLoader() { |
| 339 | 34 |
if (defaultClassLoader != null) { |
| 340 | 0 |
return defaultClassLoader;
|
| 341 |
} else {
|
|
| 342 | 34 |
return Thread.currentThread().getContextClassLoader();
|
| 343 |
} |
|
| 344 |
} |
|
| 345 |
|
|
| 346 |
} |
|
||||||||||