KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > jsp > WingsSession


1 /*
2  * Copyright (c) 2005 Your Corporation. All Rights Reserved.
3  */

4 package org.wings.jsp;
5
6 import org.wings.session.Session;
7 import org.wings.session.SessionManager;
8 import org.wings.externalizer.ExternalizedResource;
9 import org.wings.event.SRequestEvent;
10 import org.wings.event.SRequestListener;
11 import org.wings.*;
12 import org.wings.plaf.css.Utils;
13 import org.wings.io.StringBufferDevice;
14
15 import javax.servlet.http.HttpServletResponse JavaDoc;
16 import javax.servlet.http.HttpServletRequest JavaDoc;
17 import javax.servlet.ServletConfig JavaDoc;
18 import javax.servlet.ServletException JavaDoc;
19 import javax.servlet.jsp.JspWriter JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 /**
27  * @author hengels
28  * @version $Revision: 1.5 $
29  */

30 public class WingsSession
31         extends Session
32 {
33     public static WingsSession getSession(ServletConfig JavaDoc servletConfig, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
34         synchronized (request.getSession()) {
35             String JavaDoc key = "Session:" + request.getSession().getServletContext().getServletContextName();
36             WingsSession wingsSession = (WingsSession)request.getSession().getAttribute(key);
37             if (wingsSession == null) {
38                 wingsSession = new WingsSession();
39                 request.getSession().setAttribute(key, wingsSession);
40                 SessionManager.setSession(wingsSession);
41
42                 wingsSession.init(servletConfig, request);
43                 RequestURL requestURL = new RequestURL("", response.encodeURL("foo").substring(3));
44                 wingsSession.setProperty("request.url", requestURL);
45             }
46
47             SessionManager.setSession(wingsSession);
48             wingsSession.setServletRequest(request);
49             wingsSession.setServletResponse(response);
50
51             return wingsSession;
52         }
53     }
54
55 protected static WingsSession getSession(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
56 // at this point, the session should already exist.
57
// by that, calling this method with a null pointer argument
58
// shouldn't do any harm. (volker)
59
return getSession(null, request, response);
60 }
61
62     public static void removeSession() {
63         SessionManager.removeSession();
64     }
65
66     public static SFrame getFrame(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
67         String JavaDoc path = request.getServletPath();
68         int pos = path.lastIndexOf('/');
69         path = path.substring(pos + 1);
70
71         synchronized (request.getSession()) {
72             WingsSession wingsSession = getSession(request, response);
73             Map JavaDoc frames = getFrames(wingsSession);
74             SFrame frame = (SFrame)frames.get(path);
75             if (frame == null) {
76                 frame = new SFrame(path);
77                 frame.setTargetResource(path);
78                 frame.show();
79                 frames.put(path, frame);
80             }
81             SessionManager.removeSession();
82             return frame;
83         }
84     }
85
86     private static Map JavaDoc getFrames(WingsSession wingsSession) {
87         Map JavaDoc frames = (Map JavaDoc) wingsSession.getProperty("frames");
88         if (frames == null) {
89             frames = new HashMap JavaDoc();
90             wingsSession.setProperty("frames", frames);
91         }
92         return frames;
93     }
94
95     public static void dispatchEvents(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc {
96         synchronized (request.getSession()) {
97             WingsSession wingsSession = getSession(request, response);
98             SForm.clearArmedComponents();
99
100             Enumeration JavaDoc en = request.getParameterNames();
101             if (en.hasMoreElements()) {
102                 wingsSession.fireRequestEvent(SRequestEvent.DISPATCH_START);
103
104                 while (en.hasMoreElements()) {
105                     String JavaDoc paramName = (String JavaDoc) en.nextElement();
106                     String JavaDoc[] value = request.getParameterValues(paramName);
107                     wingsSession.getDispatcher().dispatch(paramName, value);
108                 }
109                 SForm.fireEvents();
110
111                 wingsSession.fireRequestEvent(SRequestEvent.DISPATCH_DONE);
112             }
113
114             wingsSession.getReloadManager().invalidateResources();
115             wingsSession.getReloadManager().clear();
116             SessionManager.removeSession();
117         }
118     }
119
120     public void addComponent(String JavaDoc name, SComponent component) throws ServletException JavaDoc {
121         SFrame frame = getFrame(getServletRequest(), getServletResponse());
122         frame.getContentPane().add(component, name);
123         setProperty(name, component);
124     }
125
126     public SComponent getComponent(String JavaDoc name) {
127         return (SComponent)getProperty(name);
128     }
129
130     public SComponent removeComponent(String JavaDoc name) throws ServletException JavaDoc {
131         SFrame frame = getFrame(getServletRequest(), getServletResponse());
132         SComponent component = (SComponent) getProperty(name);
133         frame.getContentPane().remove(component);
134         return component;
135     }
136
137     public static void writeHeaders(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, JspWriter JavaDoc out) throws IOException JavaDoc, ServletException JavaDoc {
138         synchronized (request.getSession()) {
139             SFrame frame = getFrame(request, response);
140             WingsSession wingsSession = getSession(request, response);
141
142             StringBufferDevice headerdev = new StringBufferDevice();
143             for (Iterator JavaDoc iterator = frame.headers().iterator(); iterator.hasNext();) {
144                 Object JavaDoc next = iterator.next();
145                 if (next instanceof Renderable) {
146                     ((Renderable) next).write(headerdev);
147                 } else {
148                     Utils.write(headerdev, next.toString());
149                 }
150                 headerdev.write("\n".getBytes());
151             }
152             out.print(headerdev);
153
154             SessionManager.removeSession();
155         }
156     }
157
158     public static void writeComponent(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, JspWriter JavaDoc out, SComponent component) throws IOException JavaDoc, ServletException JavaDoc {
159         synchronized (request.getSession()) {
160             WingsSession wingsSession = getSession(request, response);
161             StringBufferDevice outdev = new StringBufferDevice();
162             component.write(outdev);
163             out.print(outdev);
164             SessionManager.removeSession();
165         }
166     }
167
168     public void setServletRequest(HttpServletRequest JavaDoc servletRequest) {
169         this.servletRequest = servletRequest;
170     }
171
172     public void setServletResponse(HttpServletResponse JavaDoc servletResponse) {
173         this.servletResponse = servletResponse;
174     }
175
176     public void fireRequestEvent(int type) {
177         fireRequestEvent(type, null);
178     }
179
180     public void fireRequestEvent(int type, ExternalizedResource resource) {
181         SRequestEvent event = null;
182
183         Object JavaDoc[] listeners = listenerList.getListenerList();
184         for (int i = listeners.length - 2; i >= 0; i -= 2) {
185             if (listeners[i] == SRequestListener.class) {
186                 // Lazily create the event:
187
if (event == null) {
188                     event = new SRequestEvent(this, type, resource);
189                 }
190                 ((SRequestListener) listeners[i + 1]).processRequest(event);
191             }
192         }
193     }
194 }
195
Popular Tags