KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > event > datalist > dao > DataSourceSessionList


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/event/datalist/dao/DataSourceSessionList.java,v 1.1 2004/10/20 10:51:50 hkollmann Exp $
3  * $Revision: 1.1 $
4  * $Date: 2004/10/20 10:51:50 $
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 JavaDoc;
32
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Map JavaDoc;
36
37 import javax.servlet.http.HttpServletRequest JavaDoc;
38 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
39 import javax.servlet.http.HttpSessionBindingListener JavaDoc;
40
41
42
43 /**
44  * Holds a list of DataSourceFactory object in the session context. Needed by
45  * the navigation events to store the datasource by a per session mode. So it
46  * is possible to reuse the data between different calls and it's not
47  * neccessary to refetch again.
48  *
49  * @author hkk
50  */

51 public class DataSourceSessionList implements HttpSessionBindingListener JavaDoc {
52    /** Hashtable to hold all DataSource objects. Key is queryString. */
53
54    // logging category for this class;
55
private static Log logCat = LogFactory.getLog(DataSourceSessionList.class.getName());
56    private Map JavaDoc ht = new HashMap JavaDoc();
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 the request object
71     *
72     * @return DOCUMENT ME!
73     */

74    public static synchronized DataSourceSessionList getInstance(HttpServletRequest JavaDoc request) {
75       // try to retrieve an existant dataSourceList object from the session
76
// context;
77
DataSourceSessionList ds = (DataSourceSessionList) request.getSession()
78                                                   .getAttribute("org.dbforms.event.datalist.dao.DataSourceSessionList");
79
80       // if it does not exist, createn a new one and store
81
// its reference into the session;
82
if (ds == null) {
83          ds = new DataSourceSessionList();
84          request.getSession()
85                 .setAttribute("org.dbforms.event.datalist.dao.DataSourceSessionList",
86                               ds);
87       }
88
89       return ds;
90    }
91
92
93    /**
94     * Get a DataSourceFactory object.
95     *
96     * @param table the table object
97     * @param request the request object
98     *
99     * @return the DataSourceFactory object related to the input table
100     */

101    public DataSourceFactory get(Table table,
102                                 HttpServletRequest JavaDoc request) {
103       synchronized (ht) {
104          DataSourceFactory result = (DataSourceFactory) ht.get(getKey(table,
105                                                                       request));
106
107          return result;
108       }
109    }
110
111
112    /**
113     * Adds a DataSourceFactory object to the list. If object exists in the
114     * Hashtable close first!
115     *
116     * @param table the table object
117     * @param request the request object
118     * @param ds the DataSourceFactory object to store into the list
119     *
120     * @throws SQLException DOCUMENT ME!
121     */

122    public void put(Table table,
123                    HttpServletRequest JavaDoc request,
124                    DataSourceFactory ds) throws SQLException JavaDoc {
125       synchronized (ht) {
126          ht.put(getKey(table, request), ds);
127       }
128    }
129
130
131    /**
132     * Remove a DataSource object from the list.
133     *
134     * @param table the table object
135     * @param request the request object
136     *
137     * @return the DataSource object related to the input table. Note that the
138     * returned DataSource object has just been closed by this method.
139     */

140    public DataSourceFactory remove(Table table,
141                                    HttpServletRequest JavaDoc request) {
142       synchronized (ht) {
143          DataSourceFactory result = (DataSourceFactory) ht.remove(getKey(table,
144                                                                          request));
145
146          if (result != null) {
147             result.close();
148          }
149
150          return result;
151       }
152    }
153
154
155    /**
156     * Receive notification that this session will be passivated.
157     *
158     * @param event The session event that has occurred
159     */

160    public void valueBound(HttpSessionBindingEvent JavaDoc event) {
161    }
162
163
164    /**
165     * Receive notification that this session was activated.
166     *
167     * @param event The session event that has occurred
168     */

169    public void valueUnbound(HttpSessionBindingEvent JavaDoc event) {
170       synchronized (ht) {
171          logCat.info("valueUnbound called");
172
173          Iterator JavaDoc iter = ht.values()
174                            .iterator();
175
176          while (iter.hasNext()) {
177             Object JavaDoc obj = iter.next();
178             DataSourceFactory qry = (DataSourceFactory) obj;
179             qry.close();
180          }
181
182          ht.clear();
183       }
184    }
185
186
187    /**
188     * Get the key string used to retrieve the DataSource object from the
189     * internal hash table.
190     *
191     * @param table the table object
192     * @param request the request object
193     *
194     * @return the key string (a.k.a. the queryString) used to retrieve the
195     * DataSource object from the internal hash table
196     */

197    private String JavaDoc getKey(Table table,
198                          HttpServletRequest JavaDoc request) {
199       String JavaDoc refSource = request.getRequestURI();
200       refSource = refSource + "?" + table.getName();
201
202       return refSource;
203    }
204 }
205
Popular Tags