1 17 18 package org.apache.jasper.compiler; 19 20 import java.io.File ; 21 import java.io.FileNotFoundException ; 22 import java.io.FilePermission ; 23 import java.net.URL ; 24 import java.net.URLClassLoader ; 25 import java.security.CodeSource ; 26 import java.security.PermissionCollection ; 27 import java.security.Policy ; 28 import java.security.cert.Certificate ; 29 import java.util.Iterator ; 30 import java.util.Map ; 31 import java.util.concurrent.ConcurrentHashMap ; 32 33 import javax.servlet.ServletContext ; 34 import javax.servlet.jsp.JspFactory ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.apache.jasper.Constants; 39 import org.apache.jasper.JspCompilationContext; 40 import org.apache.jasper.Options; 41 import org.apache.jasper.runtime.JspFactoryImpl; 42 import org.apache.jasper.security.SecurityClassLoad; 43 import org.apache.jasper.servlet.JspServletWrapper; 44 45 58 public final class JspRuntimeContext { 59 60 private Log log = LogFactory.getLog(JspRuntimeContext.class); 62 63 66 private int jspReloadCount; 67 68 72 static { 73 JspFactoryImpl factory = new JspFactoryImpl(); 74 SecurityClassLoad.securityClassLoad(factory.getClass().getClassLoader()); 75 JspFactory.setDefaultFactory(factory); 76 } 77 78 80 87 public JspRuntimeContext(ServletContext context, Options options) { 88 89 this.context = context; 90 this.options = options; 91 92 parentClassLoader = 94 (URLClassLoader ) Thread.currentThread().getContextClassLoader(); 95 if (parentClassLoader == null) { 96 parentClassLoader = 97 (URLClassLoader )this.getClass().getClassLoader(); 98 } 99 100 if (log.isDebugEnabled()) { 101 if (parentClassLoader != null) { 102 log.debug(Localizer.getMessage("jsp.message.parent_class_loader_is", 103 parentClassLoader.toString())); 104 } else { 105 log.debug(Localizer.getMessage("jsp.message.parent_class_loader_is", 106 "<none>")); 107 } 108 } 109 110 initClassPath(); 111 112 if (context instanceof org.apache.jasper.servlet.JspCServletContext) { 113 return; 114 } 115 116 if (System.getSecurityManager() != null) { 117 initSecurity(); 118 } 119 120 String appBase = context.getRealPath("/"); 123 if (!options.getDevelopment() 124 && appBase != null 125 && options.getCheckInterval() > 0) { 126 lastCheck = System.currentTimeMillis(); 127 } 128 } 129 130 132 135 private ServletContext context; 136 private Options options; 137 private URLClassLoader parentClassLoader; 138 private PermissionCollection permissionCollection; 139 private CodeSource codeSource; 140 private String classpath; 141 private long lastCheck = -1L; 142 143 146 private Map <String , JspServletWrapper> jsps = new ConcurrentHashMap <String , JspServletWrapper>(); 147 148 149 151 157 public void addWrapper(String jspUri, JspServletWrapper jsw) { 158 jsps.put(jspUri, jsw); 159 } 160 161 167 public JspServletWrapper getWrapper(String jspUri) { 168 return jsps.get(jspUri); 169 } 170 171 176 public void removeWrapper(String jspUri) { 177 jsps.remove(jspUri); 178 } 179 180 186 public int getJspCount() { 187 return jsps.size(); 188 } 189 190 196 public CodeSource getCodeSource() { 197 return codeSource; 198 } 199 200 205 public URLClassLoader getParentClassLoader() { 206 return parentClassLoader; 207 } 208 209 215 public PermissionCollection getPermissionCollection() { 216 return permissionCollection; 217 } 218 219 222 public void destroy() { 223 Iterator servlets = jsps.values().iterator(); 224 while (servlets.hasNext()) { 225 ((JspServletWrapper) servlets.next()).destroy(); 226 } 227 } 228 229 232 public synchronized void incrementJspReloadCount() { 233 jspReloadCount++; 234 } 235 236 241 public synchronized void setJspReloadCount(int count) { 242 this.jspReloadCount = count; 243 } 244 245 250 public int getJspReloadCount() { 251 return jspReloadCount; 252 } 253 254 255 259 public void checkCompile() { 260 261 if (lastCheck < 0) { 262 return; 264 } 265 long now = System.currentTimeMillis(); 266 if (now > (lastCheck + (options.getCheckInterval() * 1000L))) { 267 lastCheck = now; 268 } else { 269 return; 270 } 271 272 Object [] wrappers = jsps.values().toArray(); 273 for (int i = 0; i < wrappers.length; i++ ) { 274 JspServletWrapper jsw = (JspServletWrapper)wrappers[i]; 275 JspCompilationContext ctxt = jsw.getJspEngineContext(); 276 synchronized(jsw) { 279 try { 280 ctxt.compile(); 281 } catch (FileNotFoundException ex) { 282 ctxt.incrementRemoved(); 283 } catch (Throwable t) { 284 jsw.getServletContext().log("Background compile failed", 285 t); 286 } 287 } 288 } 289 290 } 291 292 295 public String getClassPath() { 296 return classpath; 297 } 298 299 300 302 303 306 private void initClassPath() { 307 308 URL [] urls = parentClassLoader.getURLs(); 309 StringBuffer cpath = new StringBuffer (); 310 String sep = System.getProperty("path.separator"); 311 312 for(int i = 0; i < urls.length; i++) { 313 318 if( urls[i].getProtocol().equals("file") ) { 319 cpath.append((String )urls[i].getFile()+sep); 320 } 321 } 322 323 cpath.append(options.getScratchDir() + sep); 324 325 String cp = (String ) context.getAttribute(Constants.SERVLET_CLASSPATH); 326 if (cp == null || cp.equals("")) { 327 cp = options.getClassPath(); 328 } 329 330 classpath = cpath.toString() + cp; 331 332 if(log.isDebugEnabled()) { 333 log.debug("Compilation classpath initialized: " + getClassPath()); 334 } 335 } 336 337 340 private void initSecurity() { 341 342 Policy policy = Policy.getPolicy(); 347 if( policy != null ) { 348 try { 349 String docBase = context.getRealPath("/"); 351 if( docBase == null ) { 352 docBase = options.getScratchDir().toString(); 353 } 354 String codeBase = docBase; 355 if (!codeBase.endsWith(File.separator)){ 356 codeBase = codeBase + File.separator; 357 } 358 File contextDir = new File (codeBase); 359 URL url = contextDir.getCanonicalFile().toURL(); 360 codeSource = new CodeSource (url,(Certificate [])null); 361 permissionCollection = policy.getPermissions(codeSource); 362 363 if (!docBase.endsWith(File.separator)){ 365 permissionCollection.add 366 (new FilePermission (docBase,"read")); 367 docBase = docBase + File.separator; 368 } else { 369 permissionCollection.add 370 (new FilePermission 371 (docBase.substring(0,docBase.length() - 1),"read")); 372 } 373 docBase = docBase + "-"; 374 permissionCollection.add(new FilePermission (docBase,"read")); 375 376 String workDir = options.getScratchDir().toString(); 379 if (!workDir.endsWith(File.separator)){ 380 permissionCollection.add 381 (new FilePermission (workDir,"read")); 382 workDir = workDir + File.separator; 383 } 384 workDir = workDir + "-"; 385 permissionCollection.add(new FilePermission (workDir,"read")); 386 387 permissionCollection.add( new RuntimePermission ( 389 "accessClassInPackage.org.apache.jasper.runtime") ); 390 391 if (parentClassLoader instanceof URLClassLoader ) { 392 URL [] urls = parentClassLoader.getURLs(); 393 String jarUrl = null; 394 String jndiUrl = null; 395 for (int i=0; i<urls.length; i++) { 396 if (jndiUrl == null 397 && urls[i].toString().startsWith("jndi:") ) { 398 jndiUrl = urls[i].toString() + "-"; 399 } 400 if (jarUrl == null 401 && urls[i].toString().startsWith("jar:jndi:") 402 ) { 403 jarUrl = urls[i].toString(); 404 jarUrl = jarUrl.substring(0,jarUrl.length() - 2); 405 jarUrl = jarUrl.substring(0, 406 jarUrl.lastIndexOf('/')) + "/-"; 407 } 408 } 409 if (jarUrl != null) { 410 permissionCollection.add( 411 new FilePermission (jarUrl,"read")); 412 permissionCollection.add( 413 new FilePermission (jarUrl.substring(4),"read")); 414 } 415 if (jndiUrl != null) 416 permissionCollection.add( 417 new FilePermission (jndiUrl,"read") ); 418 } 419 } catch(Exception e) { 420 context.log("Security Init for context failed",e); 421 } 422 } 423 } 424 425 426 } 427 | Popular Tags |