1 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 43 51 public class DataSourceSessionList implements HttpSessionBindingListener { 52 53 54 private static Log logCat = LogFactory.getLog(DataSourceSessionList.class.getName()); 56 private Map ht = new HashMap (); 57 58 63 private DataSourceSessionList() { 64 super(); 65 } 66 67 74 public static synchronized DataSourceSessionList getInstance(HttpServletRequest request) { 75 DataSourceSessionList ds = (DataSourceSessionList) request.getSession() 78 .getAttribute("org.dbforms.event.datalist.dao.DataSourceSessionList"); 79 80 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 101 public DataSourceFactory get(Table table, 102 HttpServletRequest request) { 103 synchronized (ht) { 104 DataSourceFactory result = (DataSourceFactory) ht.get(getKey(table, 105 request)); 106 107 return result; 108 } 109 } 110 111 112 122 public void put(Table table, 123 HttpServletRequest request, 124 DataSourceFactory ds) throws SQLException { 125 synchronized (ht) { 126 ht.put(getKey(table, request), ds); 127 } 128 } 129 130 131 140 public DataSourceFactory remove(Table table, 141 HttpServletRequest 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 160 public void valueBound(HttpSessionBindingEvent event) { 161 } 162 163 164 169 public void valueUnbound(HttpSessionBindingEvent event) { 170 synchronized (ht) { 171 logCat.info("valueUnbound called"); 172 173 Iterator iter = ht.values() 174 .iterator(); 175 176 while (iter.hasNext()) { 177 Object obj = iter.next(); 178 DataSourceFactory qry = (DataSourceFactory) obj; 179 qry.close(); 180 } 181 182 ht.clear(); 183 } 184 } 185 186 187 197 private String getKey(Table table, 198 HttpServletRequest request) { 199 String refSource = request.getRequestURI(); 200 refSource = refSource + "?" + table.getName(); 201 202 return refSource; 203 } 204 } 205
| Popular Tags
|