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.text.MessageFormat ; 29 import com.sun.enterprise.*; 30 import com.sun.enterprise.log.Log; 31 import com.sun.enterprise.deployment.JndiNameEnvironment; 32 import com.sun.enterprise.InjectionException; 33 import org.apache.catalina.*; 34 35 import java.util.logging.*; 37 import com.sun.logging.*; 38 import java.util.logging.Logger ; 39 41 46 public final class WebContainerListener 47 implements ContainerListener { 48 49 static Logger _logger=LogDomains.getLogger(LogDomains.WEB_LOGGER); 51 53 static private HashSet beforeEvents = new HashSet(); 54 static private HashSet afterEvents = new HashSet(); 55 56 static { 57 beforeEvents.add("beforeContextInitialized"); 59 beforeEvents.add("beforeContextDestroyed"); 60 beforeEvents.add("beforeContextAttributeAdded"); 61 beforeEvents.add("beforeContextAttributeRemoved"); 62 beforeEvents.add("beforeContextAttributeReplaced"); 63 beforeEvents.add("beforeRequestInitialized"); 64 beforeEvents.add("beforeRequestDestroyed"); 65 beforeEvents.add("beforeSessionCreated"); 66 beforeEvents.add("beforeSessionDestroyed"); 67 beforeEvents.add("beforeSessionAttributeAdded"); 68 beforeEvents.add("beforeSessionAttributeRemoved"); 69 beforeEvents.add("beforeSessionAttributeReplaced"); 70 beforeEvents.add("beforeFilterInitialized"); 71 72 afterEvents.add("afterContextInitialized"); 74 afterEvents.add("afterContextDestroyed"); 75 afterEvents.add("afterContextAttributeAdded"); 76 afterEvents.add("afterContextAttributeRemoved"); 77 afterEvents.add("afterContextAttributeReplaced"); 78 afterEvents.add("afterRequestInitialized"); 79 afterEvents.add("afterRequestDestroyed"); 80 afterEvents.add("afterSessionCreated"); 81 afterEvents.add("afterSessionDestroyed"); 82 afterEvents.add("afterSessionAttributeAdded"); 83 afterEvents.add("afterSessionAttributeRemoved"); 84 afterEvents.add("afterSessionAttributeReplaced"); 85 afterEvents.add("afterFilterInitialized"); 86 } 87 88 private InvocationManager im; 89 private InjectionManager injectionMgr; 90 91 92 public WebContainerListener() { 93 injectionMgr = Switch.getSwitch().getInjectionManager(); 94 } 95 96 public void containerEvent(ContainerEvent event) { 97 if(_logger.isLoggable(Level.FINEST)) { 98 _logger.log(Level.FINEST,"**** ContainerEvent: " + 99 event.getType() + "," + 100 event.getContainer() + "," + 101 event.getData()); 102 } 103 104 String type = event.getType(); 105 106 try { 107 if ("afterListenerInstantiated".equals(type) 108 || "beforeFilterInitialized".equals(type)) { 109 preInvoke((Context) event.getContainer()); 110 injectInstance(event); 111 postInvoke((Context) event.getContainer()); 112 } 113 114 if (beforeEvents.contains(type)) { 115 preInvoke((Context) event.getContainer()); 116 } else if (afterEvents.contains(type)) { 117 postInvoke((Context) event.getContainer()); 118 } 119 } catch (Exception ex) { 120 String msg = _logger.getResourceBundle().getString( 121 "web_server.excep_handle_event"); 122 msg = MessageFormat.format(msg, new Object [] { type }); 123 throw new RuntimeException (msg, ex); 124 } 125 126 } 127 128 private void preInvoke(Context ctx) { 129 final ClassLoader cl = ctx.getLoader().getClassLoader(); 131 AccessController.doPrivileged 132 (new PrivilegedAction () { 133 public Object run() { 134 Thread.currentThread().setContextClassLoader(cl); 135 return null; 136 } 137 }); 138 ComponentInvocation inv = new ComponentInvocation(); 139 inv.instance = null; 140 inv.container = ctx; 141 142 if (im == null) { 143 im = Switch.getSwitch().getInvocationManager(); 144 } 145 im.preInvoke(inv); 146 } 147 148 private void postInvoke(Context ctx) { 149 ComponentInvocation inv = new ComponentInvocation(); 150 inv.instance = null; 151 inv.container = ctx; 152 if (im == null) { 153 im = Switch.getSwitch().getInvocationManager(); 154 } 155 im.postInvoke(inv); 156 } 157 158 159 165 private void injectInstance(ContainerEvent event) 166 throws InjectionException { 167 168 JndiNameEnvironment desc = (JndiNameEnvironment) 169 Switch.getSwitch().getDescriptorFor( 170 (Context) event.getContainer()); 171 if( desc != null ) { 172 injectionMgr.injectInstance(event.getData(), desc); 173 } 174 } 175 } 176 | Popular Tags |