root / trunk / org.gvsig.postgresql / org.gvsig.postgresql.provider / src / main / java / org / gvsig / fmap / dal / store / postgresql / PostgreSQLResource.java @ 294
History | View | Annotate | Download (9.53 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
package org.gvsig.fmap.dal.store.postgresql; |
24 |
|
25 |
import javax.sql.DataSource; |
26 |
import org.apache.commons.dbcp.BasicDataSource; |
27 |
import org.gvsig.fmap.dal.exception.InitializeException; |
28 |
import org.gvsig.fmap.dal.store.jdbc.JDBCResource; |
29 |
import org.gvsig.fmap.dal.store.jdbc.JDBCResourceParameters; |
30 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
31 |
import org.slf4j.Logger; |
32 |
import org.slf4j.LoggerFactory; |
33 |
|
34 |
public class PostgreSQLResource extends JDBCResource { |
35 |
|
36 |
final static private Logger logger = LoggerFactory.getLogger(PostgreSQLResource.class); |
37 |
|
38 |
public final static String NAME = "PostgreSQLResource"; |
39 |
public static final String DESCRIPTION = "PostgreSQL Connection"; |
40 |
|
41 |
public PostgreSQLResource(
|
42 |
PostgreSQLResourceParameters parameters |
43 |
) throws InitializeException {
|
44 |
super(parameters);
|
45 |
} |
46 |
|
47 |
|
48 |
/**
|
49 |
* Registra/Carga el driver de JDBC.
|
50 |
*
|
51 |
* Debe ser sobreescrita en cada proveedor de datos para asegurar que se
|
52 |
* tegna acceso al driver.
|
53 |
*
|
54 |
* @throws InitializeException
|
55 |
*/
|
56 |
@Override
|
57 |
protected void registerJDBCDriver() throws InitializeException { |
58 |
String className = this.getParameters().getJDBCDriverClassName(); |
59 |
if (className == null) { |
60 |
return;
|
61 |
} |
62 |
try {
|
63 |
Class theClass = Class.forName(className); |
64 |
if (theClass == null) { |
65 |
throw new JDBCDriverClassNotFoundException(this.getName(), className); |
66 |
} |
67 |
} catch (Exception e) { |
68 |
throw new InitializeException(e); |
69 |
} |
70 |
} |
71 |
|
72 |
/**
|
73 |
* Crea el JDBC DataSource a partir de la informacion de los parametros
|
74 |
* del recurso.
|
75 |
*
|
76 |
* Debe ser sobreescrita en cada proveedor de datos para asegurar que se
|
77 |
* tenga acceso al driver.
|
78 |
*
|
79 |
* @return
|
80 |
*/
|
81 |
@Override
|
82 |
protected DataSource createDataSource() { |
83 |
JDBCResourceParameters jdbcParams = this.getParameters();
|
84 |
BasicDataSource dataSource = new BasicDataSource();
|
85 |
dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName()); |
86 |
dataSource.setUsername(jdbcParams.getUser()); |
87 |
dataSource.setPassword(jdbcParams.getPassword()); |
88 |
dataSource.setUrl(jdbcParams.getUrl()); |
89 |
|
90 |
dataSource.setMaxWait(60L * 1000); // FIXME |
91 |
|
92 |
// FIXME Set Pool parameters:
|
93 |
// dataSource.setMaxActive(maxActive);
|
94 |
// dataSource.setMaxIdle(maxActive);
|
95 |
// dataSource.setMaxOpenPreparedStatements(maxActive);
|
96 |
// dataSource.setMaxWait(maxActive);
|
97 |
// dataSource.setInitialSize(initialSize);
|
98 |
// dataSource.setDefaultReadOnly(defaultReadOnly);
|
99 |
// dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
|
100 |
// dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
101 |
// dataSource.setMinIdle(minIdle);
|
102 |
// dataSource.setTestOnBorrow(testOnBorrow);
|
103 |
// dataSource.setTestOnReturn(testOnReturn);
|
104 |
// dataSource.setTestWhileIdle(testOnReturn);
|
105 |
// dataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
106 |
//
|
107 |
// dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
108 |
// dataSource.setLoginTimeout(seconds);
|
109 |
// dataSource.setLogWriter(out);
|
110 |
return dataSource;
|
111 |
} |
112 |
|
113 |
//
|
114 |
// public String getName() throws AccessResourceException {
|
115 |
// PostgreSQLResourceParameters params = (PostgreSQLResourceParameters) this.getParameters();
|
116 |
// return MessageFormat.format("PostgreSQLResource({0},{1})",
|
117 |
// new Object[] { params.getUrl(),params.getUser() });
|
118 |
// }
|
119 |
//
|
120 |
// public String toString() {
|
121 |
// try {
|
122 |
// return this.getName();
|
123 |
// } catch(Exception ex) {
|
124 |
// return super.toString();
|
125 |
// }
|
126 |
// }
|
127 |
//
|
128 |
// protected void connectToDB() throws DataException {
|
129 |
// if (this.dataSource != null) {
|
130 |
// return;
|
131 |
// }
|
132 |
// JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this
|
133 |
// .getParameters();
|
134 |
// BasicDataSource dataSource = new BasicDataSource();
|
135 |
// dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName());
|
136 |
// dataSource.setUsername(jdbcParams.getUser());
|
137 |
// dataSource.setPassword(jdbcParams.getPassword());
|
138 |
// dataSource.setUrl(jdbcParams.getUrl());
|
139 |
//
|
140 |
// dataSource.setMaxWait(60L * 1000); // FIXME
|
141 |
//
|
142 |
// // FIXME Set Pool parameters:
|
143 |
// /*
|
144 |
// dataSource.setMaxActive(maxActive);
|
145 |
// dataSource.setMaxIdle(maxActive);
|
146 |
// dataSource.setMaxOpenPreparedStatements(maxActive);
|
147 |
// dataSource.setMaxWait(maxActive);
|
148 |
// dataSource.setInitialSize(initialSize);
|
149 |
// dataSource.setDefaultReadOnly(defaultReadOnly);
|
150 |
// dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
|
151 |
// dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
152 |
// dataSource.setMinIdle(minIdle);
|
153 |
// dataSource.setTestOnBorrow(testOnBorrow);
|
154 |
// dataSource.setTestOnReturn(testOnReturn);
|
155 |
// dataSource.setTestWhileIdle(testOnReturn);
|
156 |
// dataSource
|
157 |
// .setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
158 |
//
|
159 |
// dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
160 |
// dataSource.setLoginTimeout(seconds);
|
161 |
// dataSource.setLogWriter(out);
|
162 |
// */
|
163 |
//
|
164 |
// this.dataSource = dataSource;
|
165 |
// }
|
166 |
// protected void registerJDBCDriver() throws InitializeException {
|
167 |
// String className = ((JDBCResourceParameters) getParameters())
|
168 |
// .getJDBCDriverClassName();
|
169 |
// if (className == null) {
|
170 |
// return;
|
171 |
// }
|
172 |
//
|
173 |
// Class theClass = null;
|
174 |
// try {
|
175 |
// theClass = Class.forName(className);
|
176 |
// } catch (Exception e){
|
177 |
// throw new InitializeException(e);
|
178 |
// }
|
179 |
// if (theClass == null) {
|
180 |
// try {
|
181 |
// throw new JDBCDriverClassNotFoundException(this.getName(),
|
182 |
// className);
|
183 |
// } catch (AccessResourceException e) {
|
184 |
// throw new InitializeException(e);
|
185 |
//
|
186 |
// }
|
187 |
// }
|
188 |
// }
|
189 |
//
|
190 |
// protected DataSource createDataSource() {
|
191 |
// PostgreSQLResourceParameters jdbcParams = (PostgreSQLResourceParameters) this
|
192 |
// .getParameters();
|
193 |
// BasicDataSource dataSource = new BasicDataSource();
|
194 |
// dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName());
|
195 |
// dataSource.setUsername(jdbcParams.getUser());
|
196 |
// dataSource.setPassword(jdbcParams.getPassword());
|
197 |
// dataSource.setUrl(jdbcParams.getUrl());
|
198 |
//
|
199 |
// dataSource.setMaxWait(60L * 1000); // FIXME
|
200 |
//
|
201 |
// // FIXME Set Pool parameters:
|
202 |
// /*
|
203 |
// dataSource.setMaxActive(maxActive);
|
204 |
// dataSource.setMaxIdle(maxActive);
|
205 |
// dataSource.setMaxOpenPreparedStatements(maxActive);
|
206 |
// dataSource.setMaxWait(maxActive);
|
207 |
// dataSource.setInitialSize(initialSize);
|
208 |
// dataSource.setDefaultReadOnly(defaultReadOnly);
|
209 |
// dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
|
210 |
// dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
211 |
// dataSource.setMinIdle(minIdle);
|
212 |
// dataSource.setTestOnBorrow(testOnBorrow);
|
213 |
// dataSource.setTestOnReturn(testOnReturn);
|
214 |
// dataSource.setTestWhileIdle(testOnReturn);
|
215 |
// dataSource
|
216 |
// .setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
217 |
//
|
218 |
// dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
219 |
// dataSource.setLoginTimeout(seconds);
|
220 |
// dataSource.setLogWriter(out);
|
221 |
// */
|
222 |
// return dataSource;
|
223 |
// }
|
224 |
//
|
225 |
//
|
226 |
// public boolean isConnected() {
|
227 |
// if (dataSource == null) {
|
228 |
// return false;
|
229 |
// }
|
230 |
// if (dataSource instanceof BasicDataSource) {
|
231 |
// return ((BasicDataSource) dataSource).getNumActive() > 0
|
232 |
// || ((BasicDataSource) dataSource).getNumIdle() > 0;
|
233 |
// }
|
234 |
// return true;
|
235 |
// }
|
236 |
//
|
237 |
// private void logPoolStatus(String src) {
|
238 |
// if (logger.isDebugEnabled() ) {
|
239 |
// logger.debug(src + " " + this.getStatusInformation());
|
240 |
// }
|
241 |
//
|
242 |
// }
|
243 |
//
|
244 |
// public String getStatusInformation() {
|
245 |
// if ( !(dataSource instanceof BasicDataSource) ) {
|
246 |
// return "Connected: " + this.isConnected();
|
247 |
// }
|
248 |
// BasicDataSource ds = (BasicDataSource) dataSource;
|
249 |
// String s = "Connected: " + this.isConnected() + ", " +
|
250 |
// "actives: "+ ds.getNumActive() + "/"+ ds.getMaxActive() + ", " +
|
251 |
// "idle: " + ds.getNumIdle() + "/"+ ds.getMaxIdle() ;
|
252 |
// return s;
|
253 |
// }
|
254 |
//
|
255 |
//
|
256 |
//
|
257 |
// private static class CanGetConnectionException extends JDBCSQLException {
|
258 |
// public CanGetConnectionException(String datasource, SQLException cause) {
|
259 |
// super("Can't get a connection to the data source (%(datasource))", cause, "_CanGetConnectionException", 0);
|
260 |
// setValue("datasource", datasource);
|
261 |
// }
|
262 |
// }
|
263 |
//
|
264 |
// protected synchronized Object getTheConnection() throws DataException {
|
265 |
// try {
|
266 |
// Object conn = this.dataSource.getConnection();
|
267 |
// logPoolStatus("getTheConnection");
|
268 |
// return conn;
|
269 |
// } catch (SQLException e) {
|
270 |
// throw new CanGetConnectionException(this.toString(),e);
|
271 |
// }
|
272 |
// }
|
273 |
|
274 |
} |