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
29
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
58
59
60 public PluginServletContext getServletContext() {
61 return servletContext;
62 }
63
64
65
66
67 protected HttpSession getWrappedSession() {
68 return wrappedSession;
69 }
70
71
72
73
74
75 public long getCreationTime() {
76 return wrappedSession.getCreationTime();
77 }
78
79
80
81
82
83 public String getId() {
84 return wrappedSession.getId();
85 }
86
87
88
89
90
91 public long getLastAccessedTime() {
92 return wrappedSession.getLastAccessedTime();
93 }
94
95
96
97
98
99 public void setMaxInactiveInterval(int interval) {
100 wrappedSession.setMaxInactiveInterval(interval);
101 }
102
103
104
105
106
107 public int getMaxInactiveInterval() {
108 return wrappedSession.getMaxInactiveInterval();
109 }
110
111
112
113
114
115
116 public HttpSessionContext getSessionContext() {
117 return wrappedSession.getSessionContext();
118 }
119
120
121
122
123
124
125 public Object getAttribute(String name) {
126 return wrappedSession.getAttribute(name);
127 }
128
129
130
131
132
133
134
135 public Object getValue(String name) {
136 return wrappedSession.getValue(name);
137 }
138
139
140
141
142
143 public Enumeration<?> getAttributeNames() {
144 return wrappedSession.getAttributeNames();
145 }
146
147
148
149
150
151
152 public String[] getValueNames() {
153 return wrappedSession.getValueNames();
154 }
155
156
157
158
159
160
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
181
182
183
184
185
186 public void putValue(String name, Object value) {
187 wrappedSession.putValue(name, value);
188 }
189
190
191
192
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
206
207
208
209 public void removeValue(String name) {
210 wrappedSession.removeValue(name);
211 }
212
213
214
215
216
217 public void invalidate() {
218 wrappedSession.invalidate();
219 }
220
221
222
223
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
242
243
244
245
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 }