1 17 18 19 package org.apache.catalina.core; 20 21 22 import java.io.InputStream ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.net.MalformedURLException ; 26 import java.net.URL ; 27 import java.security.AccessController ; 28 import java.security.PrivilegedActionException ; 29 import java.security.PrivilegedExceptionAction ; 30 import java.util.Enumeration ; 31 import java.util.HashMap ; 32 import java.util.Set ; 33 34 import javax.servlet.RequestDispatcher ; 35 import javax.servlet.Servlet ; 36 import javax.servlet.ServletContext ; 37 import javax.servlet.ServletException ; 38 39 import org.apache.catalina.security.SecurityUtil; 40 41 42 50 51 public final class ApplicationContextFacade 52 implements ServletContext { 53 54 58 private HashMap classCache; 59 60 61 64 private HashMap objectCache; 65 66 67 69 70 76 public ApplicationContextFacade(ApplicationContext context) { 77 super(); 78 this.context = context; 79 80 classCache = new HashMap (); 81 objectCache = new HashMap (); 82 initClassCache(); 83 } 84 85 86 private void initClassCache(){ 87 Class [] clazz = new Class []{String .class}; 88 classCache.put("getContext", clazz); 89 classCache.put("getMimeType", clazz); 90 classCache.put("getResourcePaths", clazz); 91 classCache.put("getResource", clazz); 92 classCache.put("getResourceAsStream", clazz); 93 classCache.put("getRequestDispatcher", clazz); 94 classCache.put("getNamedDispatcher", clazz); 95 classCache.put("getServlet", clazz); 96 classCache.put("getInitParameter", clazz); 97 classCache.put("setAttribute", new Class []{String .class, Object .class}); 98 classCache.put("removeAttribute", clazz); 99 classCache.put("getRealPath", clazz); 100 classCache.put("getAttribute", clazz); 101 classCache.put("log", clazz); 102 } 103 104 105 107 108 111 private ApplicationContext context = null; 112 113 114 115 117 118 public ServletContext getContext(String uripath) { 119 ServletContext theContext = null; 120 if (SecurityUtil.isPackageProtectionEnabled()) { 121 theContext = (ServletContext ) 122 doPrivileged("getContext", new Object []{uripath}); 123 } else { 124 theContext = context.getContext(uripath); 125 } 126 if ((theContext != null) && 127 (theContext instanceof ApplicationContext)){ 128 theContext = ((ApplicationContext)theContext).getFacade(); 129 } 130 return (theContext); 131 } 132 133 134 public int getMajorVersion() { 135 return context.getMajorVersion(); 136 } 137 138 139 public int getMinorVersion() { 140 return context.getMinorVersion(); 141 } 142 143 144 public String getMimeType(String file) { 145 if (SecurityUtil.isPackageProtectionEnabled()) { 146 return (String )doPrivileged("getMimeType", new Object []{file}); 147 } else { 148 return context.getMimeType(file); 149 } 150 } 151 152 153 public Set getResourcePaths(String path) { 154 if (SecurityUtil.isPackageProtectionEnabled()){ 155 return (Set )doPrivileged("getResourcePaths", new Object []{path}); 156 } else { 157 return context.getResourcePaths(path); 158 } 159 } 160 161 162 public URL getResource(String path) 163 throws MalformedURLException { 164 if (System.getSecurityManager() != null) { 165 try { 166 return (URL ) invokeMethod(context, "getResource", 167 new Object []{path}); 168 } catch(Throwable t) { 169 if (t instanceof MalformedURLException ){ 170 throw (MalformedURLException )t; 171 } 172 return null; 173 } 174 } else { 175 return context.getResource(path); 176 } 177 } 178 179 180 public InputStream getResourceAsStream(String path) { 181 if (SecurityUtil.isPackageProtectionEnabled()) { 182 return (InputStream ) doPrivileged("getResourceAsStream", 183 new Object []{path}); 184 } else { 185 return context.getResourceAsStream(path); 186 } 187 } 188 189 190 public RequestDispatcher getRequestDispatcher(final String path) { 191 if (SecurityUtil.isPackageProtectionEnabled()) { 192 return (RequestDispatcher ) doPrivileged("getRequestDispatcher", 193 new Object []{path}); 194 } else { 195 return context.getRequestDispatcher(path); 196 } 197 } 198 199 200 public RequestDispatcher getNamedDispatcher(String name) { 201 if (SecurityUtil.isPackageProtectionEnabled()) { 202 return (RequestDispatcher ) doPrivileged("getNamedDispatcher", 203 new Object []{name}); 204 } else { 205 return context.getNamedDispatcher(name); 206 } 207 } 208 209 210 public Servlet getServlet(String name) 211 throws ServletException { 212 if (SecurityUtil.isPackageProtectionEnabled()) { 213 try { 214 return (Servlet ) invokeMethod(context, "getServlet", 215 new Object []{name}); 216 } catch (Throwable t) { 217 if (t instanceof ServletException ) { 218 throw (ServletException ) t; 219 } 220 return null; 221 } 222 } else { 223 return context.getServlet(name); 224 } 225 } 226 227 228 public Enumeration getServlets() { 229 if (SecurityUtil.isPackageProtectionEnabled()) { 230 return (Enumeration ) doPrivileged("getServlets", null); 231 } else { 232 return context.getServlets(); 233 } 234 } 235 236 237 public Enumeration getServletNames() { 238 if (SecurityUtil.isPackageProtectionEnabled()) { 239 return (Enumeration ) doPrivileged("getServletNames", null); 240 } else { 241 return context.getServletNames(); 242 } 243 } 244 245 246 public void log(String msg) { 247 if (SecurityUtil.isPackageProtectionEnabled()) { 248 doPrivileged("log", new Object []{msg} ); 249 } else { 250 context.log(msg); 251 } 252 } 253 254 255 public void log(Exception exception, String msg) { 256 if (SecurityUtil.isPackageProtectionEnabled()) { 257 doPrivileged("log", new Class []{Exception .class, String .class}, 258 new Object []{exception,msg}); 259 } else { 260 context.log(exception, msg); 261 } 262 } 263 264 265 public void log(String message, Throwable throwable) { 266 if (SecurityUtil.isPackageProtectionEnabled()) { 267 doPrivileged("log", new Class []{String .class, Throwable .class}, 268 new Object []{message, throwable}); 269 } else { 270 context.log(message, throwable); 271 } 272 } 273 274 275 public String getRealPath(String path) { 276 if (SecurityUtil.isPackageProtectionEnabled()) { 277 return (String ) doPrivileged("getRealPath", new Object []{path}); 278 } else { 279 return context.getRealPath(path); 280 } 281 } 282 283 284 public String getServerInfo() { 285 if (SecurityUtil.isPackageProtectionEnabled()) { 286 return (String ) doPrivileged("getServerInfo", null); 287 } else { 288 return context.getServerInfo(); 289 } 290 } 291 292 293 public String getInitParameter(String name) { 294 if (SecurityUtil.isPackageProtectionEnabled()) { 295 return (String ) doPrivileged("getInitParameter", 296 new Object []{name}); 297 } else { 298 return context.getInitParameter(name); 299 } 300 } 301 302 303 public Enumeration getInitParameterNames() { 304 if (SecurityUtil.isPackageProtectionEnabled()) { 305 return (Enumeration ) doPrivileged("getInitParameterNames", null); 306 } else { 307 return context.getInitParameterNames(); 308 } 309 } 310 311 312 public Object getAttribute(String name) { 313 if (SecurityUtil.isPackageProtectionEnabled()) { 314 return doPrivileged("getAttribute", new Object []{name}); 315 } else { 316 return context.getAttribute(name); 317 } 318 } 319 320 321 public Enumeration getAttributeNames() { 322 if (SecurityUtil.isPackageProtectionEnabled()) { 323 return (Enumeration ) doPrivileged("getAttributeNames", null); 324 } else { 325 return context.getAttributeNames(); 326 } 327 } 328 329 330 public void setAttribute(String name, Object object) { 331 if (SecurityUtil.isPackageProtectionEnabled()) { 332 doPrivileged("setAttribute", new Object []{name,object}); 333 } else { 334 context.setAttribute(name, object); 335 } 336 } 337 338 339 public void removeAttribute(String name) { 340 if (SecurityUtil.isPackageProtectionEnabled()) { 341 doPrivileged("removeAttribute", new Object []{name}); 342 } else { 343 context.removeAttribute(name); 344 } 345 } 346 347 348 public String getServletContextName() { 349 if (SecurityUtil.isPackageProtectionEnabled()) { 350 return (String ) doPrivileged("getServletContextName", null); 351 } else { 352 return context.getServletContextName(); 353 } 354 } 355 356 357 public String getContextPath() { 358 if (SecurityUtil.isPackageProtectionEnabled()) { 359 return (String ) doPrivileged("getContextPath", null); 360 } else { 361 return context.getContextPath(); 362 } 363 } 364 365 366 374 private Object doPrivileged(ApplicationContext appContext, 375 final String methodName, 376 final Object [] params) { 377 try{ 378 return invokeMethod(appContext, methodName, params ); 379 } catch (Throwable t){ 380 throw new RuntimeException (t.getMessage()); 381 } 382 383 } 384 385 386 393 private Object doPrivileged(final String methodName, final Object [] params){ 394 try{ 395 return invokeMethod(context, methodName, params); 396 }catch(Throwable t){ 397 throw new RuntimeException (t.getMessage()); 398 } 399 } 400 401 402 410 private Object invokeMethod(ApplicationContext appContext, 411 final String methodName, 412 Object [] params) 413 throws Throwable { 414 415 try{ 416 Method method = (Method )objectCache.get(methodName); 417 if (method == null){ 418 method = appContext.getClass() 419 .getMethod(methodName, (Class [])classCache.get(methodName)); 420 objectCache.put(methodName, method); 421 } 422 423 return executeMethod(method,appContext,params); 424 } catch (Exception ex){ 425 handleException(ex, methodName); 426 return null; 427 } finally { 428 params = null; 429 } 430 } 431 432 439 private Object doPrivileged(final String methodName, 440 final Class [] clazz, 441 Object [] params){ 442 443 try{ 444 Method method = context.getClass() 445 .getMethod(methodName, (Class [])clazz); 446 return executeMethod(method,context,params); 447 } catch (Exception ex){ 448 try{ 449 handleException(ex, methodName); 450 }catch (Throwable t){ 451 throw new RuntimeException (t.getMessage()); 452 } 453 return null; 454 } finally { 455 params = null; 456 } 457 } 458 459 460 467 private Object executeMethod(final Method method, 468 final ApplicationContext context, 469 final Object [] params) 470 throws PrivilegedActionException , 471 IllegalAccessException , 472 InvocationTargetException { 473 474 if (SecurityUtil.isPackageProtectionEnabled()){ 475 return AccessController.doPrivileged(new PrivilegedExceptionAction (){ 476 public Object run() throws IllegalAccessException , InvocationTargetException { 477 return method.invoke(context, params); 478 } 479 }); 480 } else { 481 return method.invoke(context, params); 482 } 483 } 484 485 486 491 private void handleException(Exception ex, String methodName) 492 throws Throwable { 493 494 Throwable realException; 495 496 if (ex instanceof PrivilegedActionException ) { 497 ex = ((PrivilegedActionException ) ex).getException(); 498 } 499 500 if (ex instanceof InvocationTargetException ) { 501 realException = 502 ((InvocationTargetException ) ex).getTargetException(); 503 } else { 504 realException = ex; 505 } 506 507 throw realException; 508 } 509 } 510 | Popular Tags |