1 15 package org.apache.hivemind.impl; 16 17 import java.net.URL ; 18 19 import org.apache.hivemind.ApplicationRuntimeException; 20 import org.apache.hivemind.ClassResolver; 21 22 28 public class DefaultClassResolver implements ClassResolver 29 { 30 private ClassLoader _loader; 31 32 35 36 public DefaultClassResolver() 37 { 38 this(Thread.currentThread().getContextClassLoader()); 39 } 40 41 public DefaultClassResolver(ClassLoader loader) 42 { 43 _loader = loader; 44 } 45 46 public URL getResource(String name) 47 { 48 String stripped = removeLeadingSlash(name); 49 50 URL result = _loader.getResource(stripped); 51 52 return result; 53 } 54 55 private String removeLeadingSlash(String name) 56 { 57 if (name.startsWith("/")) 58 return name.substring(1); 59 60 return name; 61 } 62 63 75 76 public Class findClass(String type) 77 { 78 try 79 { 80 return lookupClass(type); 81 } 82 catch (Throwable t) 83 { 84 throw new ApplicationRuntimeException(ImplMessages.unableToLoadClass(type, _loader, t), 85 t); 86 } 87 } 88 89 private Class lookupClass(String type) throws ClassNotFoundException 90 { 91 Class result = JavaTypeUtils.getPrimtiveClass(type); 92 93 if (result != null) 94 return result; 95 96 99 String jvmName = JavaTypeUtils.getJVMClassName(type); 100 101 return Class.forName(jvmName, true, _loader); 102 } 103 104 public Class checkForClass(String type) 105 { 106 try 107 { 108 return lookupClass(type); 109 } 110 catch (ClassNotFoundException ex) 111 { 112 return null; 113 } 114 catch (Throwable t) 115 { 116 throw new ApplicationRuntimeException(ImplMessages.unableToLoadClass(type, _loader, t), 117 t); 118 } 119 120 } 121 122 public ClassLoader getClassLoader() 123 { 124 return _loader; 125 } 126 127 } | Popular Tags |