1 package org.openejb.util; 2 3 import java.io.BufferedInputStream ; 4 import java.io.File ; 5 import java.io.IOException ; 6 import java.io.InputStream ; 7 import java.net.JarURLConnection ; 8 import java.net.MalformedURLException ; 9 import java.net.URL ; 10 import java.util.ArrayList ; 11 import java.util.Enumeration ; 12 import java.util.HashMap ; 13 import java.util.List ; 14 import java.util.Map ; 15 import java.util.Properties ; 16 import java.util.Iterator ; 17 import java.util.jar.JarEntry ; 18 import java.util.jar.JarFile ; 19 20 public class ResourceFinder { 21 22 private final String path; 23 private final ClassLoader classLoader; 24 25 public ResourceFinder(String path) { 26 this(path, Thread.currentThread().getContextClassLoader()); 27 } 28 29 public ResourceFinder(String path, ClassLoader classLoader) { 30 this.path = path; 31 this.classLoader = classLoader; 32 } 33 34 35 41 public String findString(String key) throws IOException { 42 String uri = path + key; 43 44 URL resource = classLoader.getResource(uri); 45 if (resource == null) { 46 throw new IOException ("Could not find command in : " + uri); 47 } 48 49 return readContents(resource); 50 } 51 52 public List findAllStrings(String key) throws IOException { 53 String uri = path + key; 54 55 List strings = new ArrayList (); 56 57 Enumeration resources = classLoader.getResources(uri); 58 while (resources.hasMoreElements()) { 59 URL url = (URL ) resources.nextElement(); 60 String string = readContents(url); 61 strings.add(string); 62 } 63 return strings; 64 } 65 66 public List findAvailableStrings(String key) throws IOException { 67 String uri = path + key; 68 69 List strings = new ArrayList (); 70 71 Enumeration resources = classLoader.getResources(uri); 72 while (resources.hasMoreElements()) { 73 try { 74 URL url = (URL ) resources.nextElement(); 75 String string = readContents(url); 76 strings.add(string); 77 } catch (Exception notAvailable) { 78 } 79 } 80 return strings; 81 } 82 83 public Map mapAllStrings(String key) throws IOException { 84 Map strings = new HashMap (); 85 Map resourcesMap = getResourcesMap(key); 86 for (Iterator iterator = resourcesMap.entrySet().iterator(); iterator.hasNext();) { 87 Map.Entry entry = (Map.Entry ) iterator.next(); 88 String name = (String ) entry.getKey(); 89 URL url = (URL ) entry.getValue(); 90 String value = readContents(url); 91 strings.put(name,value); 92 } 93 return strings; 94 } 95 96 public Map mapAvailableStrings(String key) throws IOException { 97 Map strings = new HashMap (); 98 Map resourcesMap = getResourcesMap(key); 99 for (Iterator iterator = resourcesMap.entrySet().iterator(); iterator.hasNext();) { 100 try { 101 Map.Entry entry = (Map.Entry ) iterator.next(); 102 String name = (String ) entry.getKey(); 103 URL url = (URL ) entry.getValue(); 104 String value = readContents(url); 105 strings.put(name,value); 106 } catch (Exception notAvailable) { 107 } 108 } 109 return strings; 110 } 111 112 113 119 public Class findClass(String key) throws IOException , ClassNotFoundException { 120 String className = findString(key); 121 Class clazz = classLoader.loadClass(className); 122 return clazz; 123 } 124 125 public List findAllClasses(String key) throws IOException , ClassNotFoundException { 126 List classes = new ArrayList (); 127 List strings = findAllStrings(key); 128 for (int i = 0; i < strings.size(); i++) { 129 String className = (String ) strings.get(i); 130 Class clazz = classLoader.loadClass(className); 131 classes.add(clazz); 132 } 133 return classes; 134 } 135 136 public List findAvailableClasses(String key) throws IOException { 137 List classes = new ArrayList (); 138 List strings = findAvailableStrings(key); 139 for (int i = 0; i < strings.size(); i++) { 140 String className = (String ) strings.get(i); 141 try { 142 Class clazz = classLoader.loadClass(className); 143 classes.add(clazz); 144 } catch (Exception notAvailable) { 145 } 146 } 147 return classes; 148 } 149 150 public Map mapAllClasses(String key) throws IOException , ClassNotFoundException { 151 Map classes = new HashMap (); 152 Map map = mapAllStrings(key); 153 for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) { 154 Map.Entry entry = (Map.Entry ) iterator.next(); 155 String string = (String ) entry.getKey(); 156 String className = (String ) entry.getValue(); 157 Class clazz = classLoader.loadClass(className); 158 classes.put(string, clazz); 159 } 160 return classes; 161 } 162 163 public Map mapAvailableClasses(String key) throws IOException { 164 Map classes = new HashMap (); 165 Map map = mapAvailableStrings(key); 166 for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) { 167 try { 168 Map.Entry entry = (Map.Entry ) iterator.next(); 169 String string = (String ) entry.getKey(); 170 String className = (String ) entry.getValue(); 171 Class clazz = classLoader.loadClass(className); 172 classes.put(string, clazz); 173 } catch (Exception notAvailable) { 174 } 175 } 176 return classes; 177 } 178 179 185 public Class findImplementation(Class interfase) throws IOException , ClassNotFoundException { 186 String className = findString(interfase.getName()); 187 Class impl = classLoader.loadClass(className); 188 if (!interfase.isAssignableFrom(impl)) { 189 throw new ClassCastException ("Class not of type: " + interfase.getName()); 190 } 191 return impl; 192 } 193 194 public List findAllImplementations(Class interfase) throws IOException , ClassNotFoundException { 195 List implementations = new ArrayList (); 196 List strings = findAllStrings(interfase.getName()); 197 for (int i = 0; i < strings.size(); i++) { 198 String className = (String ) strings.get(i); 199 Class impl = classLoader.loadClass(className); 200 if (!interfase.isAssignableFrom(impl)) { 201 throw new ClassCastException ("Class not of type: " + interfase.getName()); 202 } 203 implementations.add(impl); 204 } 205 return implementations; 206 } 207 208 public List findAvailableImplementations(Class interfase) throws IOException { 209 List implementations = new ArrayList (); 210 List strings = findAvailableStrings(interfase.getName()); 211 for (int i = 0; i < strings.size(); i++) { 212 String className = (String ) strings.get(i); 213 try { 214 Class impl = classLoader.loadClass(className); 215 if (interfase.isAssignableFrom(impl)) { 216 implementations.add(impl); 217 } 218 } catch (Exception notAvailable) { 219 } 220 } 221 return implementations; 222 } 223 224 public Map mapAllImplementations(Class interfase) throws IOException , ClassNotFoundException { 225 Map implementations = new HashMap (); 226 Map map = mapAllStrings(interfase.getName()); 227 for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) { 228 Map.Entry entry = (Map.Entry ) iterator.next(); 229 String string = (String ) entry.getKey(); 230 String className = (String ) entry.getValue(); 231 Class impl = classLoader.loadClass(className); 232 if (!interfase.isAssignableFrom(impl)) { 233 throw new ClassCastException ("Class not of type: " + interfase.getName()); 234 } 235 implementations.put(string, impl); 236 } 237 return implementations; 238 } 239 240 public Map mapAvailableImplementations(Class interfase) throws IOException { 241 Map implementations = new HashMap (); 242 Map map = mapAvailableStrings(interfase.getName()); 243 for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) { 244 try { 245 Map.Entry entry = (Map.Entry ) iterator.next(); 246 String string = (String ) entry.getKey(); 247 String className = (String ) entry.getValue(); 248 Class impl = classLoader.loadClass(className); 249 if (interfase.isAssignableFrom(impl)) { 250 implementations.put(string, impl); 251 } 252 } catch (Exception notAvailable) { 253 } 254 } 255 return implementations; 256 } 257 258 259 265 public Properties findProperties(String key) throws IOException { 266 String uri = path + key; 267 268 URL resource = classLoader.getResource(uri); 269 if (resource == null) { 270 throw new IOException ("Could not find command in : " + uri); 271 } 272 273 return loadProperties(resource); 274 } 275 276 public List findAllProperties(String key) throws IOException { 277 String uri = path + key; 278 279 List properties = new ArrayList (); 280 281 Enumeration resources = classLoader.getResources(uri); 282 while (resources.hasMoreElements()) { 283 URL url = (URL ) resources.nextElement(); 284 Properties props = loadProperties(url); 285 properties.add(props); 286 } 287 return properties; 288 } 289 290 public List findAvailableProperties(String key) throws IOException { 291 String uri = path + key; 292 293 List properties = new ArrayList (); 294 295 Enumeration resources = classLoader.getResources(uri); 296 while (resources.hasMoreElements()) { 297 try { 298 URL url = (URL ) resources.nextElement(); 299 Properties props = loadProperties(url); 300 properties.add(props); 301 } catch (Exception notAvailable) { 302 } 303 } 304 return properties; 305 } 306 307 public Map mapAllProperties(String key) throws IOException { 308 Map propertiesMap = new HashMap (); 309 Map map = getResourcesMap(key); 310 for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) { 311 Map.Entry entry = (Map.Entry ) iterator.next(); 312 String string = (String ) entry.getKey(); 313 URL url = (URL ) entry.getValue(); 314 Properties properties = loadProperties(url); 315 propertiesMap.put(string, properties); 316 } 317 return propertiesMap; 318 } 319 320 public Map mapAvailableProperties(String key) throws IOException { 321 Map propertiesMap = new HashMap (); 322 Map map = getResourcesMap(key); 323 for (Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) { 324 try { 325 Map.Entry entry = (Map.Entry ) iterator.next(); 326 String string = (String ) entry.getKey(); 327 URL url = (URL ) entry.getValue(); 328 Properties properties = loadProperties(url); 329 propertiesMap.put(string, properties); 330 } catch (Exception notAvailable) { 331 } 332 } 333 return propertiesMap; 334 } 335 336 342 public Map getResourcesMap(String key) throws IOException { 343 String basePath = path + key; 344 345 if (!basePath.endsWith("/")){ 346 basePath += "/"; 347 } 348 349 Map resources = new HashMap (); 350 Enumeration urls = classLoader.getResources(basePath); 351 352 while (urls.hasMoreElements()) { 353 URL location = (URL ) urls.nextElement(); 354 355 try { 356 if (location.getProtocol().equals("jar")) { 357 358 readJarEntries(location, basePath, resources); 359 360 } else if (location.getProtocol().equals("file")) { 361 362 readDirectoryEntries(location, resources); 363 364 } 365 } catch (Exception e) { 366 } 367 } 368 369 return resources; 370 } 371 372 private static void readDirectoryEntries(URL location, Map resources) throws MalformedURLException { 373 File dir = new File (location.getPath()); 374 if (dir.isDirectory()) { 375 File [] files = dir.listFiles(); 376 for (int i = 0; i < files.length; i++) { 377 File file = files[i]; 378 if (!file.isDirectory()){ 379 String name = file.getName(); 380 URL url = file.toURL(); 381 resources.put(name, url); 382 } 383 } 384 } 385 } 386 387 private static void readJarEntries(URL location, String basePath, Map resources) throws IOException { 388 JarURLConnection conn = (JarURLConnection ) location.openConnection(); 389 JarFile jarfile = conn.getJarFile(); 390 391 Enumeration entries = jarfile.entries(); 392 while (entries != null && entries.hasMoreElements()) { 393 JarEntry entry = (JarEntry ) entries.nextElement(); 394 String name = entry.getName(); 395 396 if (entry.isDirectory() || !name.startsWith(basePath) || name.length() == basePath.length()) { 397 continue; 398 } 399 400 name = name.substring(basePath.length()); 401 402 if (name.indexOf("/") != -1) { 403 continue; 404 } 405 406 URL resource = new URL (location, name); 407 resources.put(name, resource); 408 } 409 } 410 411 private Properties loadProperties(URL resource) throws IOException { 412 InputStream in = resource.openStream(); 413 414 BufferedInputStream reader = null; 415 try { 416 reader = new BufferedInputStream (in); 417 Properties properties = new Properties (); 418 properties.load(reader); 419 420 return properties; 421 } finally { 422 try { 423 in.close(); 424 reader.close(); 425 } catch (Exception e) { 426 } 427 } 428 } 429 430 private String readContents(URL resource) throws IOException { 431 InputStream in = resource.openStream(); 432 BufferedInputStream reader = null; 433 StringBuffer sb = new StringBuffer (); 434 435 try { 436 reader = new BufferedInputStream (in); 437 438 int b = reader.read(); 439 while (b != -1) { 440 sb.append((char) b); 441 b = reader.read(); 442 } 443 444 return sb.toString().trim(); 445 } finally { 446 try { 447 in.close(); 448 reader.close(); 449 } catch (Exception e) { 450 } 451 } 452 } 453 } | Popular Tags |