KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > param > SessionParamPool


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.param;
14
15 import java.util.Collection JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import javax.servlet.http.HttpSession JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23
24 import org.apache.log4j.Logger;
25
26 import com.tonbeller.tbutils.res.Resources;
27
28 /**
29  * A pool that contains all {@link SessionParam} instances of a session.
30  * JSPs, SQL Queries and MDX Queries may fetch parameter values that
31  * they need for display or computation.
32  *
33  * <p/>
34  * Parameters may be accessed from a JSP with JSTL scripting, for
35  * example
36  * <pre>
37  * Selected Customer: &lt;c:out value="${paramPool.myParam.displayValue}"/&gt;
38  * </pre>
39  *
40  * <p/>
41  * Parameters may be accessed in a JSTL SQL tag like
42  * <pre>
43  * &lt;sql:query&gt;
44  * ...
45  * &lt;wcf:sqlParam name="..."/&gt;
46  * ...
47  * &lt;/sql:query&gt;
48  * </pre>
49  *
50  * <p/>
51  * Parameters may be used in MDX Queries like
52  * <pre>
53  * &lt;jp:mondrianQuery&gt;
54  * ...
55  * &lt;jp:mondrianParam name="..."/&gt;
56  * &lt;/jp:mondrianQuery&gt;
57  * </pre>
58  *
59  * <p/>
60  * A SessionParamPool creates two session variables
61  * <ul>
62  * <li><code>paramPool</code> the SessionParamPool instance that contains SessionParam's</li>
63  * <li><code>sqlValueMap</code> a map view to the SessionParamPool that contains the parameters name and their sql values</li>
64  * </ul>
65  *
66  * @author av
67  */

68
69 public class SessionParamPool implements Map JavaDoc {
70   private static final String JavaDoc SQL_VALUE_MAP = "sqlValueMap";
71   private static final String JavaDoc PARAM_POOL = "paramPool";
72
73   private static final Logger logger = Logger.getLogger(SessionParamPool.class);
74   private SqlValueMap sqlValueMap;
75   private Map JavaDoc map = new HashMap JavaDoc();
76
77   protected SessionParamPool() {
78     sqlValueMap = new SqlValueMap(this);
79   }
80
81   /**
82    * Package local (test only)
83    */

84   static SessionParamPool instance() {
85     return createInstance();
86   }
87
88   /**
89    * finds or creates the SessionParamPool
90    */

91   public static SessionParamPool instance(HttpSession JavaDoc session) {
92     // test environment?
93
if (session == null)
94       return createInstance();
95     // server!
96
SessionParamPool p = (SessionParamPool) session.getAttribute(PARAM_POOL);
97     if (p == null) {
98       p = createInstance();
99       session.setAttribute(PARAM_POOL, p);
100       session.setAttribute(SQL_VALUE_MAP, p.getSqlValueMap());
101     }
102     return p;
103   }
104
105   /**
106    * creates a new SessionParamPool instance
107    */

108   protected static SessionParamPool createInstance() {
109     String JavaDoc clazz = SessionParamPool.class.getName();
110     clazz = Resources.instance().getOptionalString(clazz, clazz);
111     try {
112       return (SessionParamPool) Class.forName(clazz).newInstance();
113     } catch (InstantiationException JavaDoc e) {
114       logger.error(null, e);
115       throw new IllegalArgumentException JavaDoc(clazz);
116     } catch (IllegalAccessException JavaDoc e) {
117       logger.error(null, e);
118       throw new IllegalArgumentException JavaDoc(clazz);
119     } catch (ClassNotFoundException JavaDoc e) {
120       logger.error(null, e);
121       throw new IllegalArgumentException JavaDoc(clazz);
122     }
123   }
124
125   /**
126    * finds or creates the SessionParamPool
127    */

128   public static SessionParamPool instance(PageContext JavaDoc pageContext) {
129     SessionParamPool p = (SessionParamPool) pageContext.findAttribute(PARAM_POOL);
130     if (p == null)
131       return instance(pageContext.getSession());
132     return p;
133   }
134
135   public SessionParam getParam(String JavaDoc name) {
136     return (SessionParam) map.get(name);
137   }
138
139   public SessionParam setParam(SessionParam p) {
140     return (SessionParam) map.put(p.getName(), p);
141   }
142
143   /**
144    * stores all SessionParam objects of c into the pool. Returns a Map
145    * that contains the previous value (or null) for the
146    * modified parameter names.
147    * @see #popParams(Map)
148    */

149   public Map JavaDoc pushParams(Collection JavaDoc c) {
150     Map JavaDoc memento = new HashMap JavaDoc();
151     for (Iterator JavaDoc it = c.iterator(); it.hasNext();) {
152       SessionParam param = (SessionParam) it.next();
153       SessionParam prev = setParam(param);
154       String JavaDoc name = param.getName();
155       if (!memento.containsKey(name))
156         memento.put(name, prev);
157     }
158     return memento;
159   }
160
161   /**
162    * restores the state of the pool that was modified
163    * by pushParams
164    *
165    * @see #pushParams(Collection)
166    */

167   public void popParams(Map JavaDoc memento) {
168     for (Iterator JavaDoc it = memento.entrySet().iterator(); it.hasNext();) {
169       Map.Entry JavaDoc e = (Entry) it.next();
170       SessionParam p = (SessionParam) e.getValue();
171       if (p == null)
172         removeParam((String JavaDoc) e.getKey());
173       else
174         setParam(p);
175     }
176   }
177
178   public void removeParam(SessionParam p) {
179     map.remove(p.getName());
180   }
181
182   public SessionParam removeParam(String JavaDoc name) {
183     return (SessionParam) map.remove(name);
184   }
185
186   /**
187    * returns a map that maps parameter names to their sql values.
188    */

189   public Map JavaDoc getSqlValueMap() {
190     return sqlValueMap;
191   }
192
193   public int size() {
194     return map.size();
195   }
196
197   public void clear() {
198     map.clear();
199   }
200
201   public boolean isEmpty() {
202     return map.isEmpty();
203   }
204
205   public boolean containsKey(Object JavaDoc key) {
206     return map.containsKey(key);
207   }
208
209   public boolean containsValue(Object JavaDoc value) {
210     return map.containsValue(value);
211   }
212
213   public Collection JavaDoc values() {
214     return map.values();
215   }
216
217   public void putAll(Map JavaDoc t) {
218     map.putAll(t);
219   }
220
221   public Set JavaDoc entrySet() {
222     return map.entrySet();
223   }
224
225   public Set JavaDoc keySet() {
226     return map.keySet();
227   }
228
229   public Object JavaDoc get(Object JavaDoc key) {
230     return map.get(key);
231   }
232
233   public Object JavaDoc remove(Object JavaDoc key) {
234     return map.remove(key);
235   }
236
237   public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
238     return map.put(key, value);
239   }
240
241 }
Popular Tags