1 16 package org.mortbay.http; 17 18 import java.io.File ; 19 import java.io.FileOutputStream ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.net.URLClassLoader ; 25 import java.security.CodeSource ; 26 import java.security.PermissionCollection ; 27 import java.util.Arrays ; 28 import java.util.StringTokenizer ; 29 30 import org.apache.commons.logging.Log; 31 import org.mortbay.log.LogFactory; 32 import org.mortbay.util.IO; 33 import org.mortbay.util.Resource; 34 35 36 48 public class ContextLoader extends URLClassLoader 49 { 50 private static Log log= LogFactory.getLog(ContextLoader.class); 51 52 private boolean _java2compliant= false; 53 private ClassLoader _parent; 54 private PermissionCollection _permissions; 55 private String _urlClassPath; 56 private HttpContext _context; 57 58 59 65 public ContextLoader(HttpContext context, String classPath, ClassLoader parent, PermissionCollection permisions) 66 throws MalformedURLException , IOException 67 { 68 super(new URL [0], parent); 69 _context=context; 70 _permissions= permisions; 71 _parent= parent; 72 73 if (_parent == null) 74 _parent= getSystemClassLoader(); 75 76 if (classPath == null) 77 { 78 _urlClassPath= ""; 79 } 80 else 81 { 82 StringTokenizer tokenizer= new StringTokenizer (classPath, ",;"); 83 84 while (tokenizer.hasMoreTokens()) 85 { 86 Resource resource= Resource.newResource(tokenizer.nextToken()); 87 if (log.isDebugEnabled()) 88 log.debug("Path resource=" + resource); 89 90 File file= resource.getFile(); 92 93 if (file != null) 94 { 95 URL url= resource.getURL(); 96 addURL(url); 97 _urlClassPath= (_urlClassPath == null) ? url.toString() : (_urlClassPath + "," + url.toString()); 98 } 99 else 100 { 101 if (!resource.isDirectory() && file == null) 103 { 104 InputStream in= resource.getInputStream(); 105 File lib= new File (context.getTempDirectory(), "lib"); 106 if (!lib.exists()) 107 { 108 lib.mkdir(); 109 lib.deleteOnExit(); 110 } 111 File jar= File.createTempFile("Jetty-", ".jar", lib); 112 113 jar.deleteOnExit(); 114 if (log.isDebugEnabled()) 115 log.debug("Extract " + resource + " to " + jar); 116 FileOutputStream out = null; 117 try 118 { 119 out= new FileOutputStream (jar); 120 IO.copy(in, out); 121 } 122 finally 123 { 124 IO.close(out); 125 } 126 127 URL url= jar.toURL(); 128 addURL(url); 129 _urlClassPath= 130 (_urlClassPath == null) ? url.toString() : (_urlClassPath + "," + url.toString()); 131 } 132 else 133 { 134 URL url= resource.getURL(); 135 addURL(url); 136 _urlClassPath= 137 (_urlClassPath == null) ? url.toString() : (_urlClassPath + "," + url.toString()); 138 } 139 } 140 } 141 } 142 143 if (log.isDebugEnabled()) 144 { 145 if (log.isDebugEnabled()) 146 log.debug("ClassPath=" + _urlClassPath); 147 if (log.isDebugEnabled()) 148 log.debug("Permissions=" + _permissions); 149 if (log.isDebugEnabled()) 150 log.debug("URL=" + Arrays.asList(getURLs())); 151 } 152 } 153 154 155 158 public void setJava2Compliant(boolean compliant) 159 { 160 _java2compliant= compliant; 161 } 162 163 164 public boolean isJava2Compliant() 165 { 166 return _java2compliant; 167 } 168 169 170 public String toString() 171 { 172 return "ContextLoader@" + hashCode() + "(" + _urlClassPath + ")\n --parent--> " + _parent.toString(); 173 } 174 175 176 public PermissionCollection getPermissions(CodeSource cs) 177 { 178 PermissionCollection pc= (_permissions == null) ? super.getPermissions(cs) : _permissions; 179 if (log.isDebugEnabled()) 180 log.debug("loader.getPermissions(" + cs + ")=" + pc); 181 return pc; 182 } 183 184 185 public Class loadClass(String name) throws ClassNotFoundException 186 { 187 return loadClass(name, false); 188 } 189 190 191 protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException 192 { 193 Class c= findLoadedClass(name); 194 ClassNotFoundException ex= null; 195 boolean tried_parent= false; 196 if (c == null && (_java2compliant || isSystemPath(name)) && !isServerPath(name) && _parent!=null) 197 { 198 if (log.isTraceEnabled()) 199 log.trace("try loadClass " + name + " from " + _parent); 200 tried_parent= true; 201 try 202 { 203 c= _parent.loadClass(name); 204 if (log.isTraceEnabled()) 205 log.trace("p0 loaded " + c); 206 } 207 catch (ClassNotFoundException e) 208 { 209 ex= e; 210 } 211 } 212 213 if (c == null) 214 { 215 if (log.isTraceEnabled()) 216 log.trace("try findClass " + name + " from " + _urlClassPath); 217 try 218 { 219 c= this.findClass(name); 220 if (log.isTraceEnabled()) 221 log.trace("cx loaded " + c); 222 } 223 catch (ClassNotFoundException e) 224 { 225 ex= e; 226 } 227 } 228 229 if (c == null && !tried_parent && !isServerPath(name) && _parent!=null) 230 { 231 if (log.isTraceEnabled()) 232 log.trace("try loadClass " + name + " from " + _parent); 233 c= _parent.loadClass(name); 234 if (log.isTraceEnabled()) 235 log.trace("p1 loaded " + c); 236 } 237 238 if (c == null) 239 throw ex; 240 241 if (resolve) 242 resolveClass(c); 243 244 return c; 245 } 246 247 248 public URL getResource(String name) 249 { 250 URL url= null; 251 boolean tried_parent= false; 252 if (_parent!=null &&(_java2compliant || isSystemPath(name))) 253 { 254 if (log.isTraceEnabled()) 255 log.trace("try getResource " + name + " from " + _parent); 256 tried_parent= true; 257 url= _parent.getResource(name); 258 } 259 260 if (url == null) 261 { 262 if (log.isTraceEnabled()) 263 log.trace("try findResource " + name + " from " + _urlClassPath); 264 url= this.findResource(name); 265 266 if (url == null && name.startsWith("/")) 267 { 268 if (log.isDebugEnabled()) 269 log.debug("HACK leading / off " + name); 270 url= this.findResource(name.substring(1)); 271 } 272 } 273 274 if (_parent!=null && url == null && !tried_parent) 275 { 276 if (log.isTraceEnabled()) 277 log.trace("try getResource " + name + " from " + _parent); 278 url= _parent.getResource(name); 279 } 280 281 if (url != null) 282 if (log.isTraceEnabled()) 283 log.trace("found " + url); 284 285 return url; 286 } 287 288 289 public boolean isServerPath(String name) 290 { 291 name=name.replace('/','.'); 292 while(name.startsWith(".")) 293 name=name.substring(1); 294 295 String [] server_classes=_context.getServerClasses(); 296 297 if (server_classes!=null) 298 { 299 for (int i=0;i<server_classes.length;i++) 300 { 301 boolean result=true; 302 String c=server_classes[i]; 303 if (c.startsWith("-")) 304 { 305 c=c.substring(1); 306 result=false; 307 } 308 309 if (c.endsWith(".")) 310 { 311 if (name.startsWith(c)) 312 return result; 313 } 314 else if (name.equals(c)) 315 { 316 return result; 317 } 318 } 319 } 320 return false; 321 } 322 323 324 public boolean isSystemPath(String name) 325 { 326 name=name.replace('/','.'); 327 while(name.startsWith(".")) 328 name=name.substring(1); 329 330 String [] system_classes=_context.getSystemClasses(); 331 if (system_classes!=null) 332 { 333 for (int i=0;i<system_classes.length;i++) 334 { 335 boolean result=true; 336 String c=system_classes[i]; 337 if (c.startsWith("-")) 338 { 339 c=c.substring(1); 340 result=false; 341 } 342 343 if (c.endsWith(".")) 344 { 345 if (name.startsWith(c)) 346 return result; 347 } 348 else if (name.equals(c)) 349 return result; 350 } 351 } 352 353 return false; 354 } 355 356 357 public void destroy() 358 { 359 this._parent=null; 360 this._permissions=null; 361 this._urlClassPath=null; 362 } 363 364 365 368 String [] getServerClasses() 369 { 370 return _context.getServerClasses(); 371 } 372 373 374 377 void setServerClasses(String [] serverClasses) 378 { 379 _context.setServerClasses(serverClasses); 380 } 381 382 383 386 String [] getSystemClasses() 387 { 388 return _context.getSystemClasses(); 389 } 390 391 392 395 void setSystemClasses(String [] systemClasses) 396 { 397 _context.setSystemClasses(systemClasses); 398 } 399 } 400 | Popular Tags |