1 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 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 } 268 | Popular Tags |