1 16 package com.ibatis.common.resources; 17 18 import java.io.*; 19 import java.net.URL ; 20 import java.net.URLConnection ; 21 import java.util.Properties ; 22 23 26 public class Resources extends Object { 27 28 private static ClassLoader defaultClassLoader; 29 30 private Resources() { 31 } 32 33 38 public static ClassLoader getDefaultClassLoader() { 39 return defaultClassLoader; 40 } 41 42 47 public static void setDefaultClassLoader(ClassLoader defaultClassLoader) { 48 Resources.defaultClassLoader = defaultClassLoader; 49 } 50 51 58 public static URL getResourceURL(String resource) throws IOException { 59 return getResourceURL(getClassLoader(), resource); 60 } 61 62 70 public static URL getResourceURL(ClassLoader loader, String resource) throws IOException { 71 URL url = null; 72 if (loader != null) url = loader.getResource(resource); 73 if (url == null) url = ClassLoader.getSystemResource(resource); 74 if (url == null) throw new IOException("Could not find resource " + resource); 75 return url; 76 } 77 78 85 public static InputStream getResourceAsStream(String resource) throws IOException { 86 return getResourceAsStream(getClassLoader(), resource); 87 } 88 89 97 public static InputStream getResourceAsStream(ClassLoader loader, String resource) throws IOException { 98 InputStream in = null; 99 if (loader != null) in = loader.getResourceAsStream(resource); 100 if (in == null) in = ClassLoader.getSystemResourceAsStream(resource); 101 if (in == null) throw new IOException("Could not find resource " + resource); 102 return in; 103 } 104 105 112 public static Properties getResourceAsProperties(String resource) 113 throws IOException { 114 Properties props = new Properties (); 115 InputStream in = null; 116 String propfile = resource; 117 in = getResourceAsStream(propfile); 118 props.load(in); 119 in.close(); 120 return props; 121 } 122 123 131 public static Properties getResourceAsProperties(ClassLoader loader, String resource) 132 throws IOException { 133 Properties props = new Properties (); 134 InputStream in = null; 135 String propfile = resource; 136 in = getResourceAsStream(loader, propfile); 137 props.load(in); 138 in.close(); 139 return props; 140 } 141 142 149 public static Reader getResourceAsReader(String resource) throws IOException { 150 return new InputStreamReader(getResourceAsStream(resource)); 151 } 152 153 161 public static Reader getResourceAsReader(ClassLoader loader, String resource) throws IOException { 162 return new InputStreamReader(getResourceAsStream(loader, resource)); 163 } 164 165 172 public static File getResourceAsFile(String resource) throws IOException { 173 return new File(getResourceURL(resource).getFile()); 174 } 175 176 184 public static File getResourceAsFile(ClassLoader loader, String resource) throws IOException { 185 return new File(getResourceURL(loader, resource).getFile()); 186 } 187 188 195 public static InputStream getUrlAsStream(String urlString) throws IOException { 196 URL url = new URL (urlString); 197 URLConnection conn = url.openConnection(); 198 return conn.getInputStream(); 199 } 200 201 208 public static Reader getUrlAsReader(String urlString) throws IOException { 209 return new InputStreamReader(getUrlAsStream(urlString)); 210 } 211 212 219 public static Properties getUrlAsProperties(String urlString) throws IOException { 220 Properties props = new Properties (); 221 InputStream in = null; 222 String propfile = urlString; 223 in = getUrlAsStream(propfile); 224 props.load(in); 225 in.close(); 226 return props; 227 } 228 229 236 public static Class classForName(String className) throws ClassNotFoundException { 237 Class clazz = null; 238 try { 239 clazz = getClassLoader().loadClass(className); 240 } catch (Exception e) { 241 } 243 if (clazz == null) { 244 clazz = Class.forName(className); 245 } 246 return clazz; 247 } 248 249 258 public static Object instantiate(String className) 259 throws ClassNotFoundException , InstantiationException , IllegalAccessException { 260 return instantiate(classForName(className)); 261 } 262 263 271 public static Object instantiate(Class clazz) 272 throws InstantiationException , IllegalAccessException { 273 return clazz.newInstance(); 274 } 275 276 private static ClassLoader getClassLoader() { 277 if (defaultClassLoader != null) { 278 return defaultClassLoader; 279 } else { 280 return Thread.currentThread().getContextClassLoader(); 281 } 282 } 283 284 } 285 | Popular Tags |