KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > web > server > WebContainerListener


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.web.server;
24
25 import java.util.*;
26 import java.security.AccessController JavaDoc;
27 import java.security.PrivilegedAction JavaDoc;
28 import java.text.MessageFormat JavaDoc;
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 //START OF IASRI 4660742
36
import java.util.logging.*;
37 import com.sun.logging.*;
38 import java.util.logging.Logger JavaDoc;
39 //END OF IASRI 4660742
40

41 /**
42  * This class implements the Tomcat ContainerListener interface and
43  * handles Context and Session related events.
44  * @author Tony Ng
45  */

46 public final class WebContainerListener
47     implements ContainerListener {
48
49     // START OF IASRI 4660742
50
static Logger JavaDoc _logger=LogDomains.getLogger(LogDomains.WEB_LOGGER);
51     // END OF IASRI 4660742
52

53     static private HashSet beforeEvents = new HashSet();
54     static private HashSet afterEvents = new HashSet();
55
56     static {
57         // preInvoke events
58
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         // postInvoke events
73
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 JavaDoc 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 JavaDoc ex) {
120             String JavaDoc msg = _logger.getResourceBundle().getString(
121                 "web_server.excep_handle_event");
122             msg = MessageFormat.format(msg, new Object JavaDoc[] { type });
123             throw new RuntimeException JavaDoc(msg, ex);
124         }
125
126     }
127
128     private void preInvoke(Context ctx) {
129         // set context class loader for use by RMIClassLoader
130
final ClassLoader JavaDoc cl = ctx.getLoader().getClassLoader();
131         AccessController.doPrivileged
132             (new PrivilegedAction JavaDoc() {
133                 public Object JavaDoc 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     /*
160      * Injects all injectable resources into the servlet context listener
161      * or filter instance associated with the given ContainerEvent.
162      *
163      * @param event The ContainerEvent to process
164      */

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