1 23 package com.sun.web.server; 24 25 import java.util.*; 26 import java.security.AccessController ; 27 import java.security.PrivilegedAction ; 28 import java.security.AccessControlException ; 29 import java.security.Policy ; 30 import java.security.ProtectionDomain ; 31 import java.security.Principal ; 32 import javax.security.auth.Subject ; 33 import javax.transaction.Transaction ; 34 import javax.servlet.Servlet ; 35 import javax.servlet.ServletRequest ; import javax.servlet.ServletRequestWrapper ; 37 import javax.servlet.http.HttpServletRequest ; 38 import org.apache.catalina.Realm; 39 import org.apache.catalina.InstanceEvent; 40 import org.apache.catalina.InstanceListener; 41 import org.apache.catalina.Context; 42 import org.apache.catalina.Wrapper; 43 import org.apache.jasper.servlet.JspServlet; 44 import org.apache.catalina.servlets.DefaultServlet; 45 import org.apache.coyote.tomcat5.CoyoteRequestFacade; 46 import com.sun.enterprise.*; 47 import com.sun.enterprise.deployment.*; 48 import com.sun.enterprise.appverification.factory.AppVerification; 49 import com.sun.enterprise.security.LoginContext; 50 import com.sun.enterprise.security.SecurityContext; import com.sun.enterprise.log.Log; 52 import com.sun.web.security.RealmAdapter; 53 import com.sun.web.security.WebPrincipal; 54 55 56 import java.util.logging.*; 58 import com.sun.logging.*; 59 61 67 public final class J2EEInstanceListener implements InstanceListener { 68 69 static Logger _logger=LogDomains.getLogger(LogDomains.WEB_LOGGER); 71 73 74 private InvocationManager im; 75 private J2EETransactionManager tm; 76 private InjectionManager injectionMgr; 77 79 public J2EEInstanceListener() { 80 im = Switch.getSwitch().getInvocationManager(); 81 tm = Switch.getSwitch().getTransactionManager(); 82 injectionMgr = Switch.getSwitch().getInjectionManager(); 83 } 85 86 public void instanceEvent(InstanceEvent event) { 87 String eventType = event.getType(); 88 if(_logger.isLoggable(Level.FINEST)) { 89 _logger.log(Level.FINEST,"*** InstanceEvent: " + eventType); 90 } 91 if (eventType.equals(InstanceEvent.BEFORE_SERVICE_EVENT) || 92 eventType.equals(InstanceEvent.BEFORE_FILTER_EVENT) || 93 eventType.equals(InstanceEvent.BEFORE_INIT_EVENT) || 94 eventType.equals(InstanceEvent.BEFORE_DESTROY_EVENT) || 95 eventType.equals(InstanceEvent.BEFORE_DISPATCH_EVENT)) { 96 97 handleBeforeEvent(event, eventType); 98 } else if (eventType.equals(InstanceEvent.AFTER_SERVICE_EVENT) || 99 eventType.equals(InstanceEvent.AFTER_FILTER_EVENT) || 100 eventType.equals(InstanceEvent.AFTER_INIT_EVENT) || 101 eventType.equals(InstanceEvent.AFTER_DESTROY_EVENT) || 102 eventType.equals(InstanceEvent.AFTER_DISPATCH_EVENT)) { 103 handleAfterEvent(event, eventType); 104 } else { 105 } 109 } 110 111 private void handleBeforeEvent(InstanceEvent event, String eventType) { 112 113 Object instance = null; 114 if (eventType.equals(InstanceEvent.BEFORE_FILTER_EVENT)) { 115 instance = event.getFilter(); 116 } else { 117 instance = event.getServlet(); 118 } 119 Context context = (Context) event.getWrapper().getParent(); 120 121 final ClassLoader cl = context.getLoader().getClassLoader(); 123 AccessController.doPrivileged 124 (new PrivilegedAction () { 125 public Object run() { 126 Thread.currentThread().setContextClassLoader(cl); 127 return null; 128 } 129 }); 130 131 Realm ra = context.getRealm(); 135 152 if (ra != null) { 154 155 ServletRequest request = 156 (ServletRequest ) event.getRequest(); 157 if (request != null && request instanceof HttpServletRequest ) { 158 159 HttpServletRequest hreq = (HttpServletRequest )request; 160 HttpServletRequest base = hreq; 161 162 Principal prin = hreq.getUserPrincipal(); 163 Principal basePrincipal = prin; 164 165 boolean wrapped = false; 166 167 while (prin != null && base != null) { 168 169 if (base instanceof ServletRequestWrapper ) { 170 ServletRequest sr = 172 ((ServletRequestWrapper ) base).getRequest(); 173 174 if (sr instanceof HttpServletRequest ) { 175 176 base = (HttpServletRequest ) sr; 177 wrapped = true; 178 continue; 179 } 180 } 181 182 if (wrapped) { 183 basePrincipal = base.getUserPrincipal(); 184 } 185 186 else if (base instanceof CoyoteRequestFacade) { 187 if (base.getClass() != CoyoteRequestFacade.class) { 190 basePrincipal = ((CoyoteRequestFacade)base). 191 getUnwrappedCoyoteRequest().getUserPrincipal(); 192 } 193 } else { 194 basePrincipal = base.getUserPrincipal(); 195 } 196 197 break; 198 } 199 200 if (prin != null && prin == basePrincipal && 201 prin instanceof WebPrincipal) { 202 203 SecurityContext.setCurrent 204 (getSecurityContextForPrincipal(prin)); 205 206 } else if (prin != basePrincipal) { 207 208 212 checkObjectForDoAsPermission(hreq); 213 214 SecurityContext.setCurrent 215 (getSecurityContextForPrincipal(prin)); 216 } 217 } 218 } 219 224 230 ComponentInvocation inv = new ComponentInvocation(instance, context); 231 try { 232 im.preInvoke(inv); 233 if (eventType.equals(InstanceEvent.BEFORE_SERVICE_EVENT)) { 234 Transaction tran = null; 236 if ((tran = tm.getTransaction()) != null) { 237 inv.setTransaction(tran); 238 } 239 tm.enlistComponentResources(); 240 } else if(eventType.equals(InstanceEvent.BEFORE_INIT_EVENT)) { 241 242 JndiNameEnvironment desc = (JndiNameEnvironment) 247 Switch.getSwitch().getDescriptorFor(context); 248 249 if( desc != null 253 && instance.getClass() != DefaultServlet.class 254 && instance.getClass() != JspServlet.class) { 255 injectionMgr.injectInstance(instance, desc); 256 } 257 258 } 259 } catch (Exception ex) { 260 throw new RuntimeException ( 261 _logger.getResourceBundle().getString( 262 "web_server.excep_handle_before_event"), 263 ex); 264 } 265 } 266 267 private static javax.security.auth.AuthPermission doAsPrivilegedPerm = 268 new javax.security.auth.AuthPermission ("doAsPrivileged"); 269 270 private static void 271 checkObjectForDoAsPermission(final Object o) throws AccessControlException { 272 if (System.getSecurityManager() != null) { 273 AccessController.doPrivileged(new PrivilegedAction () { 274 public Object run() { 275 ProtectionDomain pD = o.getClass().getProtectionDomain(); 276 Policy p = Policy.getPolicy(); 277 if (!p.implies(pD,doAsPrivilegedPerm)) { 278 throw new AccessControlException 279 ("permission required to override getUserPrincipal", 280 doAsPrivilegedPerm); 281 } 282 return null; 283 } 284 }); 285 } 286 } 287 288 private static SecurityContext 289 getSecurityContextForPrincipal(final Principal p) { 290 if (p == null) { 291 return null; 292 } else if (p instanceof WebPrincipal) { 293 return ((WebPrincipal) p).getSecurityContext(); 294 } else { 295 return (SecurityContext) 296 AccessController.doPrivileged(new PrivilegedAction () { 297 public Object run() { 298 Subject s = new Subject (); 299 s.getPrincipals().add(p); 300 return new SecurityContext(p.getName(),s); 301 } 302 }); 303 } 304 } 305 306 private void handleAfterEvent(InstanceEvent event, String eventType) { 307 308 if (AppVerification.doInstrument() 309 && (eventType.equals(InstanceEvent.AFTER_SERVICE_EVENT) 310 || eventType.equals(InstanceEvent.AFTER_INIT_EVENT) 311 || eventType.equals(InstanceEvent.AFTER_DISPATCH_EVENT))) { 312 313 AppVerification.getInstrumentLogger().doInstrumentForWeb(event); 314 } 315 316 Object instance = null; 317 if (eventType.equals(InstanceEvent.AFTER_FILTER_EVENT)) { 318 instance = event.getFilter(); 319 } else { 320 instance = event.getServlet(); 321 } 322 Context context = (Context) event.getWrapper().getParent(); 323 ComponentInvocation inv = new ComponentInvocation(instance, context); 324 try { 325 im.postInvoke(inv); 326 } catch (Exception ex) { 327 throw new RuntimeException ( 328 _logger.getResourceBundle().getString( 329 "web_server.excep_handle_after_event"), 330 ex); 331 } finally { 332 if (eventType.equals(InstanceEvent.AFTER_DESTROY_EVENT)) { 333 tm.componentDestroyed(instance); 334 } 335 if (eventType.equals(InstanceEvent.AFTER_FILTER_EVENT) || 336 eventType.equals(InstanceEvent.AFTER_SERVICE_EVENT)) { 337 if (im.getCurrentInvocation() == null) { 340 try { 342 Realm ra = context.getRealm(); 344 if (ra != null && (ra instanceof RealmAdapter)) { 345 ((RealmAdapter)ra).logout(); 346 } 347 } catch (Exception ex) { 348 351 _logger.log(Level.SEVERE, 353 "web_server.excep_handle_after_event", 354 ex); 355 } 357 try { 358 if (tm.getTransaction() != null) { 359 tm.rollback(); 360 } 361 tm.cleanTxnTimeout(); 362 } catch (Exception ex) {} 363 } 364 tm.componentDestroyed(instance); 365 } 366 } 367 } 368 } 369 370 | Popular Tags |