View Javadoc

1   package com.eyeq.pivot4j.pentaho.servlet;
2   
3   import java.lang.reflect.InvocationHandler;
4   import java.lang.reflect.InvocationTargetException;
5   import java.lang.reflect.Method;
6   import java.lang.reflect.Proxy;
7   import java.util.Enumeration;
8   
9   import javax.servlet.http.HttpSession;
10  import javax.servlet.http.HttpSessionBindingEvent;
11  import javax.servlet.http.HttpSessionContext;
12  import javax.servlet.http.HttpSessionEvent;
13  
14  import org.apache.commons.lang.ObjectUtils;
15  import org.apache.myfaces.webapp.StartupServletContextListener;
16  import org.pentaho.platform.api.engine.IPentahoSession;
17  import org.pentaho.platform.engine.core.system.PentahoSessionHolder;
18  import org.pentaho.platform.engine.core.system.PentahoSystem;
19  
20  @SuppressWarnings("deprecation")
21  public class PluginServletSession implements HttpSession {
22  
23  	private HttpSession wrappedSession;
24  
25  	private PluginServletContext servletContext;
26  
27  	/**
28  	 * @param servletContext
29  	 * @param session
30  	 */
31  	public PluginServletSession(PluginServletContext servletContext,
32  			HttpSession session) {
33  		this.servletContext = servletContext;
34  		this.wrappedSession = session;
35  
36  		IPentahoSession pentahoSession = (IPentahoSession) session
37  				.getAttribute(PentahoSystem.PENTAHO_SESSION_KEY);
38  
39  		if (!(pentahoSession instanceof PentahoSessionProxy)) {
40  			if (pentahoSession == null) {
41  				pentahoSession = PentahoSessionHolder.getSession();
42  			}
43  
44  			IPentahoSession proxy = (IPentahoSession) Proxy.newProxyInstance(
45  					getClass().getClassLoader(),
46  					new Class<?>[] { PentahoSessionProxy.class },
47  					new InvocationHandlerImpl(pentahoSession));
48  
49  			session.setAttribute(PentahoSystem.PENTAHO_SESSION_KEY, proxy);
50  
51  			servletContext.getListener().sessionCreated(
52  					new HttpSessionEvent(this));
53  		}
54  	}
55  
56  	/**
57  	 * @return the servletContext
58  	 * @see javax.servlet.http.HttpSession#getServletContext()
59  	 */
60  	public PluginServletContext getServletContext() {
61  		return servletContext;
62  	}
63  
64  	/**
65  	 * @return the wrappedSession
66  	 */
67  	protected HttpSession getWrappedSession() {
68  		return wrappedSession;
69  	}
70  
71  	/**
72  	 * @return
73  	 * @see javax.servlet.http.HttpSession#getCreationTime()
74  	 */
75  	public long getCreationTime() {
76  		return wrappedSession.getCreationTime();
77  	}
78  
79  	/**
80  	 * @return
81  	 * @see javax.servlet.http.HttpSession#getId()
82  	 */
83  	public String getId() {
84  		return wrappedSession.getId();
85  	}
86  
87  	/**
88  	 * @return
89  	 * @see javax.servlet.http.HttpSession#getLastAccessedTime()
90  	 */
91  	public long getLastAccessedTime() {
92  		return wrappedSession.getLastAccessedTime();
93  	}
94  
95  	/**
96  	 * @param interval
97  	 * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
98  	 */
99  	public void setMaxInactiveInterval(int interval) {
100 		wrappedSession.setMaxInactiveInterval(interval);
101 	}
102 
103 	/**
104 	 * @return
105 	 * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
106 	 */
107 	public int getMaxInactiveInterval() {
108 		return wrappedSession.getMaxInactiveInterval();
109 	}
110 
111 	/**
112 	 * @return
113 	 * @deprecated
114 	 * @see javax.servlet.http.HttpSession#getSessionContext()
115 	 */
116 	public HttpSessionContext getSessionContext() {
117 		return wrappedSession.getSessionContext();
118 	}
119 
120 	/**
121 	 * @param name
122 	 * @return
123 	 * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
124 	 */
125 	public Object getAttribute(String name) {
126 		return wrappedSession.getAttribute(name);
127 	}
128 
129 	/**
130 	 * @param name
131 	 * @return
132 	 * @deprecated
133 	 * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
134 	 */
135 	public Object getValue(String name) {
136 		return wrappedSession.getValue(name);
137 	}
138 
139 	/**
140 	 * @return
141 	 * @see javax.servlet.http.HttpSession#getAttributeNames()
142 	 */
143 	public Enumeration<?> getAttributeNames() {
144 		return wrappedSession.getAttributeNames();
145 	}
146 
147 	/**
148 	 * @return
149 	 * @deprecated
150 	 * @see javax.servlet.http.HttpSession#getValueNames()
151 	 */
152 	public String[] getValueNames() {
153 		return wrappedSession.getValueNames();
154 	}
155 
156 	/**
157 	 * @param name
158 	 * @param value
159 	 * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String,
160 	 *      java.lang.Object)
161 	 */
162 	public void setAttribute(String name, Object value) {
163 		wrappedSession.setAttribute(name, value);
164 		Object oldValue = getAttribute(name);
165 
166 		wrappedSession.setAttribute(name, value);
167 
168 		StartupServletContextListener listener = getServletContext()
169 				.getListener();
170 		if (oldValue == null) {
171 			listener.attributeAdded(new HttpSessionBindingEvent(this, name,
172 					value));
173 		} else if (!ObjectUtils.equals(oldValue, value)) {
174 			listener.attributeReplaced(new HttpSessionBindingEvent(this, name,
175 					value));
176 		}
177 	}
178 
179 	/**
180 	 * @param name
181 	 * @param value
182 	 * @deprecated
183 	 * @see javax.servlet.http.HttpSession#putValue(java.lang.String,
184 	 *      java.lang.Object)
185 	 */
186 	public void putValue(String name, Object value) {
187 		wrappedSession.putValue(name, value);
188 	}
189 
190 	/**
191 	 * @param name
192 	 * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
193 	 */
194 	public void removeAttribute(String name) {
195 		Object value = getAttribute(name);
196 
197 		wrappedSession.removeAttribute(name);
198 
199 		StartupServletContextListener listener = getServletContext()
200 				.getListener();
201 		listener.attributeRemoved(new HttpSessionBindingEvent(this, name, value));
202 	}
203 
204 	/**
205 	 * @param name
206 	 * @deprecated
207 	 * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
208 	 */
209 	public void removeValue(String name) {
210 		wrappedSession.removeValue(name);
211 	}
212 
213 	/**
214 	 * 
215 	 * @see javax.servlet.http.HttpSession#invalidate()
216 	 */
217 	public void invalidate() {
218 		wrappedSession.invalidate();
219 	}
220 
221 	/**
222 	 * @return
223 	 * @see javax.servlet.http.HttpSession#isNew()
224 	 */
225 	public boolean isNew() {
226 		return wrappedSession.isNew();
227 	}
228 
229 	private interface PentahoSessionProxy extends IPentahoSession {
230 	}
231 
232 	private final class InvocationHandlerImpl implements InvocationHandler {
233 
234 		private IPentahoSession session;
235 
236 		private InvocationHandlerImpl(IPentahoSession session) {
237 			this.session = session;
238 		}
239 
240 		/**
241 		 * @throws InvocationTargetException
242 		 * @throws IllegalAccessException
243 		 * @throws
244 		 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object,
245 		 *      java.lang.reflect.Method, java.lang.Object[])
246 		 */
247 		@Override
248 		public Object invoke(Object proxy, Method method, Object[] args)
249 				throws IllegalAccessException, InvocationTargetException {
250 			Object result = method.invoke(session, args);
251 
252 			if (method.getName().equals("destroy")) {
253 				servletContext.getListener().sessionDestroyed(
254 						new HttpSessionEvent(wrappedSession));
255 
256 				Enumeration<?> names = getAttributeNames();
257 
258 				while (names.hasMoreElements()) {
259 					removeAttribute((String) names.nextElement());
260 				}
261 			}
262 
263 			return result;
264 		}
265 	}
266 }