001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.aspect.session.echo2;
018:
019: import java.util.Collection;
020: import java.util.List;
021: import java.util.Locale;
022: import java.util.Map;
023: import java.util.WeakHashMap;
024:
025: import javax.servlet.http.HttpServletRequest;
026: import javax.servlet.http.HttpSession;
027: import javax.servlet.http.HttpSessionEvent;
028: import javax.servlet.http.HttpSessionListener;
029:
030: import nextapp.echo2.webrender.Connection;
031: import nextapp.echo2.webrender.WebRenderServlet;
032:
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035: import org.romaframework.aspect.session.SessionAspectAbstract;
036: import org.romaframework.aspect.session.SessionInfo;
037: import org.romaframework.aspect.session.SessionListener;
038: import org.romaframework.aspect.view.echo2.Echo2ApplicationContext;
039: import org.romaframework.core.flow.Controller;
040:
041: /**
042: * Uses Echo2 to identify user session aspects by current thread.
043: *
044: * @author Luca Garulli (luca.garulli@assetdata.it)
045: */
046: public class Echo2SessionAspect extends SessionAspectAbstract implements
047: HttpSessionListener {
048:
049: private Map<HttpSession, SessionInfo> sessions;
050: private static Log log = LogFactory
051: .getLog(Echo2SessionAspect.class);
052:
053: public Echo2SessionAspect() {
054: super ();
055: sessions = new WeakHashMap<HttpSession, SessionInfo>();
056: }
057:
058: public SessionInfo getActiveSessionInfo() {
059: // GET THREAD'S CURRENT CONTEXT
060: Connection connection = WebRenderServlet.getActiveConnection();
061:
062: if (connection == null)
063: return null;
064:
065: // RETURN SESSION INFO, IF ANY
066: return sessions.get(connection.getRequest().getSession());
067: }
068:
069: public SessionInfo getSession(Object iSystemSession) {
070: return sessions.get(iSystemSession);
071: }
072:
073: public SessionInfo addSession(Object iSession) {
074: // CREATE ACTIVE SESSION OBJECT
075: SessionInfo activeSession = new SessionInfo(iSession);
076:
077: HttpServletRequest req = WebRenderServlet.getActiveConnection()
078: .getRequest();
079:
080: activeSession.setSystemSession(req.getSession());
081: activeSession.setSource(req.getRemoteHost() + ":"
082: + req.getRemotePort());
083:
084: synchronized (sessions) {
085: sessions.put(
086: (HttpSession) activeSession.getSystemSession(),
087: activeSession);
088: }
089:
090: if (log.isDebugEnabled())
091: log
092: .debug("[Echo2SessionAspect.addSession] User session created id='"
093: + WebRenderServlet.getActiveConnection()
094: .getRequest().getSession().getId()
095: + "'");
096:
097: List<SessionListener> listeners = Controller.getInstance()
098: .getListeners(SessionListener.class);
099:
100: synchronized (listeners) {
101: for (SessionListener listener : listeners) {
102: listener.onSessionCreating(activeSession);
103: }
104: }
105:
106: return activeSession;
107: }
108:
109: public SessionInfo removeSession(Object iSession) {
110: SessionInfo sessInfo = null;
111: synchronized (sessions) {
112: sessInfo = sessions.remove(iSession);
113: }
114:
115: if (sessInfo != null) {
116: if (log.isDebugEnabled())
117: log
118: .debug("[Echo2SessionAspect.removeSession] Removed session created: account="
119: + sessInfo.getAccount()
120: + ", source="
121: + sessInfo.getSource()
122: + ", created="
123: + sessInfo.getCreated());
124: } else
125: return null;
126:
127: List<SessionListener> listeners = Controller.getInstance()
128: .getListeners(SessionListener.class);
129:
130: synchronized (listeners) {
131: for (SessionListener listener : listeners) {
132: listener.onSessionDestroying(sessInfo);
133: }
134: }
135:
136: return sessInfo;
137: }
138:
139: public SessionInfo removeSessionBySystem(Object iSystemSession) {
140: if (log.isInfoEnabled())
141: log
142: .info("[Echo2SessionAspect.removeSessionBySystem] User session destroyed id='"
143: + ((HttpSession) iSystemSession).getId()
144: + "'");
145:
146: return removeSession(iSystemSession);
147: }
148:
149: public Collection<SessionInfo> getSessionInfos() {
150: return sessions.values();
151: }
152:
153: public Locale getActiveLocale() {
154: Echo2ApplicationContext context = Echo2ApplicationContext
155: .getApp();
156: if (context == null)
157: return null;
158: return context.getLocale();
159: }
160:
161: public void logout() {
162: shutdown(getActiveSessionInfo().getSystemSession());
163: }
164:
165: public void shutdown(Object iSystemSession) {
166: ((HttpSession) iSystemSession).invalidate();
167: }
168:
169: /**
170: * Read the property from active HttpSession
171: */
172: public Object getProperty(String iKey) {
173: return getProperty(getActiveSessionInfo().getSystemSession(),
174: iKey);
175: }
176:
177: /**
178: * Read the property from a HttpSession
179: */
180: public Object getProperty(Object iSession, String iKey) {
181: HttpSession sess = (HttpSession) iSession;
182: if (sess != null)
183: return sess.getAttribute(iKey);
184: return null;
185: }
186:
187: /**
188: * Set the property inside current HttpSession
189: */
190: public void setProperty(String iKey, Object iValue) {
191: setProperty(getActiveSessionInfo().getSystemSession(), iKey,
192: iValue);
193: }
194:
195: /**
196: * Set the property inside a HttpSession
197: */
198: public void setProperty(Object iSession, String iKey, Object iValue) {
199: HttpSession sess = (HttpSession) iSession;
200: if (sess != null)
201: sess.setAttribute(iKey, iValue);
202: }
203:
204: public void sessionCreated(HttpSessionEvent iEvent) {
205: }
206:
207: public void sessionDestroyed(HttpSessionEvent iEvent) {
208: removeSessionBySystem(iEvent.getSession());
209: }
210: }
|