1 /* 2 * Copyright (c) 2003 The Visigoth Software Society. All rights 3 * reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in 14 * the documentation and/or other materials provided with the 15 * distribution. 16 * 17 * 3. The end-user documentation included with the redistribution, if 18 * any, must include the following acknowledgement: 19 * "This product includes software developed by the 20 * Visigoth Software Society (http://www.visigoths.org/)." 21 * Alternately, this acknowledgement may appear in the software itself, 22 * if and wherever such third-party acknowledgements normally appear. 23 * 24 * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the 25 * project contributors may be used to endorse or promote products derived 26 * from this software without prior written permission. For written 27 * permission, please contact visigoths@visigoths.org. 28 * 29 * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth" 30 * nor may "FreeMarker" or "Visigoth" appear in their names 31 * without prior written permission of the Visigoth Software Society. 32 * 33 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 34 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 35 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 36 * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR 37 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 40 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 41 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 42 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 43 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 44 * SUCH DAMAGE. 45 * ==================================================================== 46 * 47 * This software consists of voluntary contributions made by many 48 * individuals on behalf of the Visigoth Software Society. For more 49 * information on the Visigoth Software Society, please see 50 * http://www.visigoths.org/ 51 */ 52 53 package freemarker.ext.jsp; 54 55 import java.util.ArrayList; 56 import java.util.EventListener; 57 import java.util.Iterator; 58 import java.util.List; 59 60 import javax.servlet.ServletContext; 61 import javax.servlet.ServletContextAttributeEvent; 62 import javax.servlet.ServletContextAttributeListener; 63 import javax.servlet.ServletContextEvent; 64 import javax.servlet.ServletContextListener; 65 import javax.servlet.http.HttpSessionAttributeListener; 66 import javax.servlet.http.HttpSessionBindingEvent; 67 import javax.servlet.http.HttpSessionEvent; 68 import javax.servlet.http.HttpSessionListener; 69 70 import freemarker.log.Logger; 71 72 /** 73 * An instance of this class should be registered as a <tt><listener></tt> in 74 * the <tt>web.xml</tt> descriptor in order to correctly dispatch events to 75 * event listeners that are specified in TLD files. 76 * @author Attila Szegedi 77 * @version $Id: EventForwarding.java,v 1.5 2003/01/24 10:19:33 szegedia Exp $ 78 */ 79 public class EventForwarding 80 implements 81 ServletContextAttributeListener, 82 ServletContextListener, 83 HttpSessionListener, 84 HttpSessionAttributeListener 85 { 86 private static final Logger logger = Logger.getLogger("freemarker.jsp"); 87 88 private static final String ATTR_NAME = EventForwarding.class.getName(); 89 90 private final List servletContextAttributeListeners = new ArrayList(); 91 private final List servletContextListeners = new ArrayList(); 92 private final List httpSessionAttributeListeners = new ArrayList(); 93 private final List httpSessionListeners = new ArrayList(); 94 95 void addListeners(List listeners) 96 { 97 for (Iterator iter = listeners.iterator(); iter.hasNext();) 98 { 99 addListener((EventListener)iter.next()); 100 } 101 } 102 103 private void addListener(EventListener listener) 104 { 105 boolean added = false; 106 if(listener instanceof ServletContextAttributeListener) 107 { 108 addListener(servletContextAttributeListeners, listener); 109 added = true; 110 } 111 if(listener instanceof ServletContextListener) 112 { 113 addListener(servletContextListeners, listener); 114 added = true; 115 } 116 if(listener instanceof HttpSessionAttributeListener) 117 { 118 addListener(httpSessionAttributeListeners, listener); 119 added = true; 120 } 121 if(listener instanceof HttpSessionListener) 122 { 123 addListener(httpSessionListeners, listener); 124 added = true; 125 } 126 if(!added) { 127 logger.warn( 128 "Listener of class " + listener.getClass().getName() + 129 "wasn't registered as it doesn't implement any of the " + 130 "recognized listener interfaces."); 131 } 132 } 133 134 static EventForwarding getInstance(ServletContext context) 135 { 136 return (EventForwarding)context.getAttribute(ATTR_NAME); 137 } 138 private void addListener(List listeners, EventListener listener) 139 { 140 synchronized(listeners) 141 { 142 listeners.add(listener); 143 } 144 } 145 146 public void attributeAdded(ServletContextAttributeEvent arg0) 147 { 148 synchronized(servletContextAttributeListeners) 149 { 150 int s = servletContextAttributeListeners.size(); 151 for(int i = 0; i < s; ++i) 152 { 153 ((ServletContextAttributeListener)servletContextAttributeListeners.get(i)).attributeAdded(arg0); 154 } 155 } 156 } 157 158 public void attributeRemoved(ServletContextAttributeEvent arg0) 159 { 160 synchronized(servletContextAttributeListeners) 161 { 162 int s = servletContextAttributeListeners.size(); 163 for(int i = 0; i < s; ++i) 164 { 165 ((ServletContextAttributeListener)servletContextAttributeListeners.get(i)).attributeRemoved(arg0); 166 } 167 } 168 } 169 170 public void attributeReplaced(ServletContextAttributeEvent arg0) 171 { 172 synchronized(servletContextAttributeListeners) 173 { 174 int s = servletContextAttributeListeners.size(); 175 for(int i = 0; i < s; ++i) 176 { 177 ((ServletContextAttributeListener)servletContextAttributeListeners.get(i)).attributeReplaced(arg0); 178 } 179 } 180 } 181 182 public void contextInitialized(ServletContextEvent arg0) 183 { 184 arg0.getServletContext().setAttribute(ATTR_NAME, this); 185 186 synchronized(servletContextListeners) 187 { 188 int s = servletContextListeners.size(); 189 for(int i = 0; i < s; ++i) 190 { 191 ((ServletContextListener)servletContextListeners.get(i)).contextInitialized(arg0); 192 } 193 } 194 } 195 196 public void contextDestroyed(ServletContextEvent arg0) 197 { 198 synchronized(servletContextListeners) 199 { 200 int s = servletContextListeners.size(); 201 for(int i = s - 1; i >= 0; --i) 202 { 203 ((ServletContextListener)servletContextListeners.get(i)).contextDestroyed(arg0); 204 } 205 } 206 } 207 208 public void sessionCreated(HttpSessionEvent arg0) 209 { 210 synchronized(httpSessionListeners) 211 { 212 int s = httpSessionListeners.size(); 213 for(int i = 0; i < s; ++i) 214 { 215 ((HttpSessionListener)httpSessionListeners.get(i)).sessionCreated(arg0); 216 } 217 } 218 } 219 220 public void sessionDestroyed(HttpSessionEvent arg0) 221 { 222 synchronized(httpSessionListeners) 223 { 224 int s = httpSessionListeners.size(); 225 for(int i = s - 1; i >= 0; --i) 226 { 227 ((HttpSessionListener)httpSessionListeners.get(i)).sessionDestroyed(arg0); 228 } 229 } 230 } 231 232 public void attributeAdded(HttpSessionBindingEvent arg0) 233 { 234 synchronized(httpSessionAttributeListeners) 235 { 236 int s = httpSessionAttributeListeners.size(); 237 for(int i = 0; i < s; ++i) 238 { 239 ((HttpSessionAttributeListener)httpSessionAttributeListeners.get(i)).attributeAdded(arg0); 240 } 241 } 242 } 243 244 public void attributeRemoved(HttpSessionBindingEvent arg0) 245 { 246 synchronized(httpSessionAttributeListeners) 247 { 248 int s = httpSessionAttributeListeners.size(); 249 for(int i = 0; i < s; ++i) 250 { 251 ((HttpSessionAttributeListener)httpSessionAttributeListeners.get(i)).attributeRemoved(arg0); 252 } 253 } 254 } 255 256 public void attributeReplaced(HttpSessionBindingEvent arg0) 257 { 258 synchronized(httpSessionAttributeListeners) 259 { 260 int s = httpSessionAttributeListeners.size(); 261 for(int i = 0; i < s; ++i) 262 { 263 ((HttpSessionAttributeListener)httpSessionAttributeListeners.get(i)).attributeReplaced(arg0); 264 } 265 } 266 } 267 }