001: /*
002: * Copyright (c) 2003 The Visigoth Software Society. All rights
003: * reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * 1. Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * 2. Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in
014: * the documentation and/or other materials provided with the
015: * distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowledgement:
019: * "This product includes software developed by the
020: * Visigoth Software Society (http://www.visigoths.org/)."
021: * Alternately, this acknowledgement may appear in the software itself,
022: * if and wherever such third-party acknowledgements normally appear.
023: *
024: * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
025: * project contributors may be used to endorse or promote products derived
026: * from this software without prior written permission. For written
027: * permission, please contact visigoths@visigoths.org.
028: *
029: * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
030: * nor may "FreeMarker" or "Visigoth" appear in their names
031: * without prior written permission of the Visigoth Software Society.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of the Visigoth Software Society. For more
049: * information on the Visigoth Software Society, please see
050: * http://www.visigoths.org/
051: */
052:
053: package freemarker.ext.jsp;
054:
055: import java.util.ArrayList;
056: import java.util.EventListener;
057: import java.util.Iterator;
058: import java.util.List;
059:
060: import javax.servlet.ServletContext;
061: import javax.servlet.ServletContextAttributeEvent;
062: import javax.servlet.ServletContextAttributeListener;
063: import javax.servlet.ServletContextEvent;
064: import javax.servlet.ServletContextListener;
065: import javax.servlet.http.HttpSessionAttributeListener;
066: import javax.servlet.http.HttpSessionBindingEvent;
067: import javax.servlet.http.HttpSessionEvent;
068: import javax.servlet.http.HttpSessionListener;
069:
070: import freemarker.log.Logger;
071:
072: /**
073: * An instance of this class should be registered as a <tt><listener></tt> in
074: * the <tt>web.xml</tt> descriptor in order to correctly dispatch events to
075: * event listeners that are specified in TLD files.
076: * @author Attila Szegedi
077: * @version $Id: EventForwarding.java,v 1.5 2003/01/24 10:19:33 szegedia Exp $
078: */
079: public class EventForwarding implements
080: ServletContextAttributeListener, ServletContextListener,
081: HttpSessionListener, HttpSessionAttributeListener {
082: private static final Logger logger = Logger
083: .getLogger("freemarker.jsp");
084:
085: private static final String ATTR_NAME = EventForwarding.class
086: .getName();
087:
088: private final List servletContextAttributeListeners = new ArrayList();
089: private final List servletContextListeners = new ArrayList();
090: private final List httpSessionAttributeListeners = new ArrayList();
091: private final List httpSessionListeners = new ArrayList();
092:
093: void addListeners(List listeners) {
094: for (Iterator iter = listeners.iterator(); iter.hasNext();) {
095: addListener((EventListener) iter.next());
096: }
097: }
098:
099: private void addListener(EventListener listener) {
100: boolean added = false;
101: if (listener instanceof ServletContextAttributeListener) {
102: addListener(servletContextAttributeListeners, listener);
103: added = true;
104: }
105: if (listener instanceof ServletContextListener) {
106: addListener(servletContextListeners, listener);
107: added = true;
108: }
109: if (listener instanceof HttpSessionAttributeListener) {
110: addListener(httpSessionAttributeListeners, listener);
111: added = true;
112: }
113: if (listener instanceof HttpSessionListener) {
114: addListener(httpSessionListeners, listener);
115: added = true;
116: }
117: if (!added) {
118: logger
119: .warn("Listener of class "
120: + listener.getClass().getName()
121: + "wasn't registered as it doesn't implement any of the "
122: + "recognized listener interfaces.");
123: }
124: }
125:
126: static EventForwarding getInstance(ServletContext context) {
127: return (EventForwarding) context.getAttribute(ATTR_NAME);
128: }
129:
130: private void addListener(List listeners, EventListener listener) {
131: synchronized (listeners) {
132: listeners.add(listener);
133: }
134: }
135:
136: public void attributeAdded(ServletContextAttributeEvent arg0) {
137: synchronized (servletContextAttributeListeners) {
138: int s = servletContextAttributeListeners.size();
139: for (int i = 0; i < s; ++i) {
140: ((ServletContextAttributeListener) servletContextAttributeListeners
141: .get(i)).attributeAdded(arg0);
142: }
143: }
144: }
145:
146: public void attributeRemoved(ServletContextAttributeEvent arg0) {
147: synchronized (servletContextAttributeListeners) {
148: int s = servletContextAttributeListeners.size();
149: for (int i = 0; i < s; ++i) {
150: ((ServletContextAttributeListener) servletContextAttributeListeners
151: .get(i)).attributeRemoved(arg0);
152: }
153: }
154: }
155:
156: public void attributeReplaced(ServletContextAttributeEvent arg0) {
157: synchronized (servletContextAttributeListeners) {
158: int s = servletContextAttributeListeners.size();
159: for (int i = 0; i < s; ++i) {
160: ((ServletContextAttributeListener) servletContextAttributeListeners
161: .get(i)).attributeReplaced(arg0);
162: }
163: }
164: }
165:
166: public void contextInitialized(ServletContextEvent arg0) {
167: arg0.getServletContext().setAttribute(ATTR_NAME, this );
168:
169: synchronized (servletContextListeners) {
170: int s = servletContextListeners.size();
171: for (int i = 0; i < s; ++i) {
172: ((ServletContextListener) servletContextListeners
173: .get(i)).contextInitialized(arg0);
174: }
175: }
176: }
177:
178: public void contextDestroyed(ServletContextEvent arg0) {
179: synchronized (servletContextListeners) {
180: int s = servletContextListeners.size();
181: for (int i = s - 1; i >= 0; --i) {
182: ((ServletContextListener) servletContextListeners
183: .get(i)).contextDestroyed(arg0);
184: }
185: }
186: }
187:
188: public void sessionCreated(HttpSessionEvent arg0) {
189: synchronized (httpSessionListeners) {
190: int s = httpSessionListeners.size();
191: for (int i = 0; i < s; ++i) {
192: ((HttpSessionListener) httpSessionListeners.get(i))
193: .sessionCreated(arg0);
194: }
195: }
196: }
197:
198: public void sessionDestroyed(HttpSessionEvent arg0) {
199: synchronized (httpSessionListeners) {
200: int s = httpSessionListeners.size();
201: for (int i = s - 1; i >= 0; --i) {
202: ((HttpSessionListener) httpSessionListeners.get(i))
203: .sessionDestroyed(arg0);
204: }
205: }
206: }
207:
208: public void attributeAdded(HttpSessionBindingEvent arg0) {
209: synchronized (httpSessionAttributeListeners) {
210: int s = httpSessionAttributeListeners.size();
211: for (int i = 0; i < s; ++i) {
212: ((HttpSessionAttributeListener) httpSessionAttributeListeners
213: .get(i)).attributeAdded(arg0);
214: }
215: }
216: }
217:
218: public void attributeRemoved(HttpSessionBindingEvent arg0) {
219: synchronized (httpSessionAttributeListeners) {
220: int s = httpSessionAttributeListeners.size();
221: for (int i = 0; i < s; ++i) {
222: ((HttpSessionAttributeListener) httpSessionAttributeListeners
223: .get(i)).attributeRemoved(arg0);
224: }
225: }
226: }
227:
228: public void attributeReplaced(HttpSessionBindingEvent arg0) {
229: synchronized (httpSessionAttributeListeners) {
230: int s = httpSessionAttributeListeners.size();
231: for (int i = 0; i < s; ++i) {
232: ((HttpSessionAttributeListener) httpSessionAttributeListeners
233: .get(i)).attributeReplaced(arg0);
234: }
235: }
236: }
237: }
|