1 45 package org.openejb.util; 46 47 import org.openejb.OpenEJBException; 48 49 import java.io.File ; 50 import java.util.HashMap ; 51 import java.util.Properties ; 52 53 public class SafeToolkit { 54 55 private String systemLocation; 56 public static final Messages messages = new Messages("org.openejb.util.resources"); 57 public static final HashMap codebases = new HashMap (); 58 59 62 protected SafeToolkit(String systemLocation) { 63 this.systemLocation = systemLocation; 64 } 65 66 69 public static SafeToolkit getToolkit(String systemLocation) { 70 return new SafeToolkit(systemLocation); 71 } 72 73 80 public Class forName(String className) throws OpenEJBException { 81 Class clazz = null; 82 try { 83 clazz = Class.forName(className); 84 } catch (ClassNotFoundException cnfe) { 85 OpenEJBErrorHandler.classNotFound(systemLocation, className); 86 } 87 return clazz; 88 } 89 90 100 public Class forName(String className, String codebase) throws OpenEJBException { 101 ClassLoader cl = getContextClassLoader(); 103 104 if (codebase != null) { 108 try { 109 java.net.URL [] urlCodebase = new java.net.URL [1]; 110 urlCodebase[0] = new java.net.URL (codebase); 111 cl = new java.net.URLClassLoader (urlCodebase, cl); 112 } catch (java.net.MalformedURLException mue) { 113 OpenEJBErrorHandler.classCodebaseNotFound(systemLocation, className, codebase, mue); 114 } catch (SecurityException se) { 115 OpenEJBErrorHandler.classCodebaseNotFound(systemLocation, className, codebase, se); 116 } 117 } 118 119 Class clazz = null; 120 try { 121 clazz = Class.forName(className, true, cl); 122 } catch (ClassNotFoundException cnfe) { 123 OpenEJBErrorHandler.classNotFound(systemLocation, className); 124 } 125 return clazz; 126 } 127 128 135 public Object newInstance(String className) throws OpenEJBException { 136 return newInstance(forName(className)); 137 } 138 139 146 public Object newInstance(String className, String codebase) throws OpenEJBException { 147 return newInstance(forName(className, codebase)); 148 } 149 150 157 public Object newInstance(Class clazz) throws OpenEJBException { 158 Object instance = null; 159 try { 160 instance = clazz.newInstance(); 161 } catch (InstantiationException ie) { 162 OpenEJBErrorHandler.classNotIntantiateable(systemLocation, clazz.getName()); 163 } catch (IllegalAccessException iae) { 164 OpenEJBErrorHandler.classNotAccessible(systemLocation, clazz.getName()); 165 } 166 catch (Throwable exception) { 168 exception.printStackTrace(); 169 ClassLoader classLoader = clazz.getClassLoader(); 170 if (classLoader instanceof java.net.URLClassLoader ) { 171 OpenEJBErrorHandler.classNotIntantiateableFromCodebaseForUnknownReason(systemLocation, clazz.getName(), getCodebase((java.net.URLClassLoader ) classLoader), 172 exception.getClass().getName(), exception.getMessage()); 173 } else { 174 OpenEJBErrorHandler.classNotIntantiateableForUnknownReason(systemLocation, clazz.getName(), exception.getClass().getName(), exception.getMessage()); 175 } 176 } 177 return instance; 178 179 } 180 181 188 public SafeProperties getSafeProperties(Properties props) throws OpenEJBException { 189 return new SafeProperties(props, systemLocation); 190 } 191 192 202 public static Class loadClass(String className, String codebase) throws OpenEJBException { 203 return loadClass(className, codebase, true); 204 } 205 206 public static Class loadClass(String className, String codebase, boolean cache) throws OpenEJBException { 207 208 ClassLoader cl = (cache) ? getCodebaseClassLoader(codebase) : getClassLoader(codebase); 209 Class clazz = null; 210 try { 211 clazz = cl.loadClass(className); 212 } catch (ClassNotFoundException cnfe) { 213 throw new OpenEJBException(messages.format("cl0007", className, codebase)); 214 } 215 return clazz; 216 } 217 218 227 public static ClassLoader getCodebaseClassLoader(String codebase) throws OpenEJBException { 228 if (codebase == null) codebase = "CLASSPATH"; 229 230 ClassLoader cl = (ClassLoader ) codebases.get(codebase); 231 if (cl == null) { 232 synchronized (codebases) { 233 cl = (ClassLoader ) codebases.get(codebase); 234 if (cl == null) { 235 try { 236 java.net.URL [] urlCodebase = new java.net.URL [1]; 237 urlCodebase[0] = new java.net.URL ("file", null, codebase); 238 cl = new java.net.URLClassLoader (urlCodebase, SafeToolkit.class.getClassLoader()); 240 codebases.put(codebase, cl); 242 } catch (java.net.MalformedURLException mue) { 243 throw new OpenEJBException(messages.format("cl0001", codebase, mue.getMessage())); 244 } catch (SecurityException se) { 245 throw new OpenEJBException(messages.format("cl0002", codebase, se.getMessage())); 246 } 247 } 248 } 249 } 250 return cl; 251 } 252 253 262 public static ClassLoader getClassLoader(String codebase) throws OpenEJBException { 263 ClassLoader cl = null; 264 try { 265 java.net.URL [] urlCodebase = new java.net.URL [1]; 266 urlCodebase[0] = new java.net.URL ("file", null, codebase); 267 cl = new java.net.URLClassLoader (urlCodebase, SafeToolkit.class.getClassLoader()); 269 } catch (java.net.MalformedURLException mue) { 270 throw new OpenEJBException(messages.format("cl0001", codebase, mue.getMessage())); 271 } catch (SecurityException se) { 272 throw new OpenEJBException(messages.format("cl0002", codebase, se.getMessage())); 273 } 274 return cl; 275 } 276 277 280 private static String getCodebase(java.net.URLClassLoader urlClassLoader) { 281 StringBuffer codebase = new StringBuffer (); 282 java.net.URL urlList[] = urlClassLoader.getURLs(); 283 codebase.append(urlList[0].toString()); 284 for (int i = 1; i < urlList.length; ++i) { 285 codebase.append(';'); 286 codebase.append(urlList[i].toString()); 287 } 288 return codebase.toString(); 289 } 290 291 public static ClassLoader getContextClassLoader() { 292 return (ClassLoader ) java.security.AccessController.doPrivileged(new java.security.PrivilegedAction () { 293 public Object run() { 294 return Thread.currentThread().getContextClassLoader(); 295 } 296 }); 297 } 298 299 300 } | Popular Tags |