View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/datalist/dao/DataSourceSessionList.java,v 1.2 2005/09/01 19:30:46 hkollmann Exp $
3    * $Revision: 1.2 $
4    * $Date: 2005/09/01 19:30:46 $
5    *
6    * DbForms - a Rapid Application Development Framework
7    * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8    *
9    * This library is free software; you can redistribute it and/or
10   * modify it under the terms of the GNU Lesser General Public
11   * License as published by the Free Software Foundation; either
12   * version 2.1 of the License, or (at your option) any later version.
13   *
14   * This library is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this library; if not, write to the Free Software
21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
22   */
23  
24  package org.dbforms.event.datalist.dao;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  import org.dbforms.config.Table;
30  
31  import java.sql.SQLException;
32  
33  import java.util.HashMap;
34  import java.util.Iterator;
35  import java.util.Map;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpSessionBindingEvent;
39  import javax.servlet.http.HttpSessionBindingListener;
40  
41  /***
42   * Holds a list of DataSourceFactory object in the session context. Needed by
43   * the navigation events to store the datasource by a per session mode. So it is
44   * possible to reuse the data between different calls and it's not neccessary to
45   * refetch again.
46   * 
47   * @author hkk
48   */
49  public class DataSourceSessionList implements HttpSessionBindingListener {
50  	/*** Hashtable to hold all DataSource objects. Key is queryString. */
51  
52  	// logging category for this class;
53  	private static Log logCat = LogFactory.getLog(DataSourceSessionList.class
54  			.getName());
55  
56  	private Map ht = new HashMap();
57  
58  	/***
59  	 * Private constructor. <br>
60  	 * Use <code>getInstance</code> to get an instance of the DataSourceList
61  	 * object.
62  	 */
63  	private DataSourceSessionList() {
64  		super();
65  	}
66  
67  	/***
68  	 * Returns an unique instance of this class for each session
69  	 * 
70  	 * @param request
71  	 *            the request object
72  	 * 
73  	 * @return DOCUMENT ME!
74  	 */
75  	public static synchronized DataSourceSessionList getInstance(
76  			HttpServletRequest request) {
77  		// try to retrieve an existant dataSourceList object from the session
78  		// context;
79  		DataSourceSessionList ds = (DataSourceSessionList) request.getSession()
80  				.getAttribute(
81  						"org.dbforms.event.datalist.dao.DataSourceSessionList");
82  
83  		// if it does not exist, createn a new one and store
84  		// its reference into the session;
85  		if (ds == null) {
86  			ds = new DataSourceSessionList();
87  			request.getSession().setAttribute(
88  					"org.dbforms.event.datalist.dao.DataSourceSessionList", ds);
89  		}
90  
91  		return ds;
92  	}
93  
94  	/***
95  	 * Get a DataSourceFactory object.
96  	 * 
97  	 * @param table
98  	 *            the table object
99  	 * @param request
100 	 *            the request object
101 	 * 
102 	 * @return the DataSourceFactory object related to the input table
103 	 */
104 	public DataSourceFactory get(Table table, HttpServletRequest request) {
105 		synchronized (ht) {
106 			DataSourceFactory result = (DataSourceFactory) ht.get(getKey(table,
107 					request));
108 
109 			return result;
110 		}
111 	}
112 
113 	/***
114 	 * Adds a DataSourceFactory object to the list. If object exists in the
115 	 * Hashtable close first!
116 	 * 
117 	 * @param table
118 	 *            the table object
119 	 * @param request
120 	 *            the request object
121 	 * @param ds
122 	 *            the DataSourceFactory object to store into the list
123 	 * 
124 	 * @throws SQLException
125 	 *             DOCUMENT ME!
126 	 */
127 	public void put(Table table, HttpServletRequest request,
128 			DataSourceFactory ds) throws SQLException {
129 		synchronized (ht) {
130 			ht.put(getKey(table, request), ds);
131 		}
132 	}
133 
134 	/***
135 	 * Remove a DataSource object from the list.
136 	 * 
137 	 * @param table
138 	 *            the table object
139 	 * @param request
140 	 *            the request object
141 	 * 
142 	 * @return the DataSource object related to the input table. Note that the
143 	 *         returned DataSource object has just been closed by this method.
144 	 */
145 	public DataSourceFactory remove(Table table, HttpServletRequest request) {
146 		synchronized (ht) {
147 			DataSourceFactory result = (DataSourceFactory) ht.remove(getKey(
148 					table, request));
149 
150 			if (result != null) {
151 				result.close();
152 			}
153 
154 			return result;
155 		}
156 	}
157 
158 	/***
159 	 * Receive notification that this session will be passivated.
160 	 * 
161 	 * @param event
162 	 *            The session event that has occurred
163 	 */
164 	public void valueBound(HttpSessionBindingEvent event) {
165 	}
166 
167 	/***
168 	 * Receive notification that this session was activated.
169 	 * 
170 	 * @param event
171 	 *            The session event that has occurred
172 	 */
173 	public void valueUnbound(HttpSessionBindingEvent event) {
174 		synchronized (ht) {
175 			logCat.info("valueUnbound called");
176 
177 			Iterator iter = ht.values().iterator();
178 
179 			while (iter.hasNext()) {
180 				Object obj = iter.next();
181 				DataSourceFactory qry = (DataSourceFactory) obj;
182 				qry.close();
183 			}
184 
185 			ht.clear();
186 		}
187 	}
188 
189 	/***
190 	 * Get the key string used to retrieve the DataSource object from the
191 	 * internal hash table.
192 	 * 
193 	 * @param table
194 	 *            the table object
195 	 * @param request
196 	 *            the request object
197 	 * 
198 	 * @return the key string (a.k.a. the queryString) used to retrieve the
199 	 *         DataSource object from the internal hash table
200 	 */
201 	private String getKey(Table table, HttpServletRequest request) {
202 		String refSource = request.getRequestURI();
203 		refSource = refSource + "?" + table.getName();
204 
205 		return refSource;
206 	}
207 
208 }