svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.jdbc / src / main / java / org / gvsig / fmap / dal / store / jdbc / JDBCResource.java @ 40559
History | View | Annotate | Download (8.23 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
/* gvSIG. Geographic Information System of the Valencian Government
|
25 |
*
|
26 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
27 |
* of the Valencian Government (CIT)
|
28 |
*
|
29 |
* This program is free software; you can redistribute it and/or
|
30 |
* modify it under the terms of the GNU General Public License
|
31 |
* as published by the Free Software Foundation; either version 2
|
32 |
* of the License, or (at your option) any later version.
|
33 |
*
|
34 |
* This program is distributed in the hope that it will be useful,
|
35 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
36 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
37 |
* GNU General Public License for more details.
|
38 |
*
|
39 |
* You should have received a copy of the GNU General Public License
|
40 |
* along with this program; if not, write to the Free Software
|
41 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
42 |
* MA 02110-1301, USA.
|
43 |
*
|
44 |
*/
|
45 |
|
46 |
/*
|
47 |
* AUTHORS (In addition to CIT):
|
48 |
* 2009 IVER T.I {{Task}}
|
49 |
*/
|
50 |
|
51 |
package org.gvsig.fmap.dal.store.jdbc; |
52 |
|
53 |
import java.sql.Connection; |
54 |
import java.sql.SQLException; |
55 |
import java.text.MessageFormat; |
56 |
|
57 |
import javax.sql.DataSource; |
58 |
|
59 |
import org.apache.commons.dbcp.BasicDataSource; |
60 |
import org.gvsig.fmap.dal.exception.DataException; |
61 |
import org.gvsig.fmap.dal.exception.InitializeException; |
62 |
import org.gvsig.fmap.dal.resource.ResourceParameters; |
63 |
import org.gvsig.fmap.dal.resource.db.AbstractDBResourceNoBlocker; |
64 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
65 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
66 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
67 |
import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
68 |
import org.slf4j.Logger; |
69 |
import org.slf4j.LoggerFactory; |
70 |
|
71 |
public class JDBCResource extends AbstractDBResourceNoBlocker { |
72 |
|
73 |
final static private Logger logger = LoggerFactory |
74 |
.getLogger(JDBCResource.class); |
75 |
|
76 |
public final static String NAME = "JDBCResource"; |
77 |
public static final String DESCRIPTION = "JDBC Connection"; |
78 |
|
79 |
protected DataSource dataSource = null; |
80 |
|
81 |
public JDBCResource(JDBCResourceParameters parameters)
|
82 |
throws InitializeException {
|
83 |
super(parameters);
|
84 |
registerJDBCDriver(); |
85 |
|
86 |
} |
87 |
|
88 |
public String getName() throws AccessResourceException { |
89 |
JDBCResourceParameters params = (JDBCResourceParameters) this
|
90 |
.getParameters(); |
91 |
return MessageFormat.format("JDBCResource({0},{1},{2},{3},{4})", |
92 |
new Object[] { |
93 |
params.getJDBCDriverClassName(), |
94 |
params.getHost(), params.getPort(), |
95 |
params.getUser(), params.getDBName() }); |
96 |
} |
97 |
|
98 |
@SuppressWarnings("unchecked") |
99 |
protected void registerJDBCDriver() throws InitializeException { |
100 |
String className = ((JDBCResourceParameters) getParameters())
|
101 |
.getJDBCDriverClassName(); |
102 |
if (className == null) { |
103 |
return;
|
104 |
} |
105 |
|
106 |
Class theClass = null; |
107 |
try {
|
108 |
theClass = Class.forName(className);
|
109 |
} catch (Exception e){ |
110 |
throw new InitializeException(e); |
111 |
} |
112 |
if (theClass == null) { |
113 |
try {
|
114 |
throw new JDBCDriverClassNotFoundException(this.getName(), |
115 |
className); |
116 |
} catch (AccessResourceException e) {
|
117 |
throw new InitializeException(e); |
118 |
|
119 |
} |
120 |
} |
121 |
} |
122 |
|
123 |
|
124 |
public Connection getJDBCConnection() throws AccessResourceException { |
125 |
return (Connection) get(); |
126 |
} |
127 |
|
128 |
private void debugPoolStatus(String src) { |
129 |
if (logger.isDebugEnabled() && dataSource instanceof BasicDataSource) { |
130 |
BasicDataSource ds = (BasicDataSource) dataSource; |
131 |
logger.debug(src + " actives:" + ds.getNumActive() + "(" |
132 |
+ ds.getMaxActive() + ") idle:" + ds.getNumIdle() + "(" |
133 |
+ ds.getMaxIdle() + ")");
|
134 |
} |
135 |
|
136 |
} |
137 |
|
138 |
protected synchronized Object getTheConnection() throws DataException { |
139 |
try {
|
140 |
Object conn = this.dataSource.getConnection(); |
141 |
debugPoolStatus("getTheConnection");
|
142 |
return conn;
|
143 |
} catch (SQLException e) { |
144 |
throw new JDBCSQLException(e); |
145 |
} |
146 |
} |
147 |
|
148 |
public boolean isThis(ResourceParameters parameters) |
149 |
throws ResourceException {
|
150 |
if (!super.isThis(parameters)) { |
151 |
return false; |
152 |
} |
153 |
|
154 |
String dbName = ((JDBCResourceParameters) parameters).getDBName();
|
155 |
String myDbName = ((JDBCResourceParameters) getParameters())
|
156 |
.getDBName(); |
157 |
if (dbName != myDbName) {
|
158 |
if (!(dbName != null && dbName.equals(myDbName))) { |
159 |
return false; |
160 |
} |
161 |
|
162 |
} |
163 |
|
164 |
String driver = ((JDBCResourceParameters) parameters)
|
165 |
.getJDBCDriverClassName(); |
166 |
String myDriver = ((JDBCResourceParameters) getParameters())
|
167 |
.getJDBCDriverClassName(); |
168 |
if (driver != myDriver) {
|
169 |
if (!(driver != null && driver.equals(myDriver))) { |
170 |
return false; |
171 |
} |
172 |
} |
173 |
return true; |
174 |
|
175 |
} |
176 |
|
177 |
public boolean isConnected() { |
178 |
if (dataSource == null) { |
179 |
return false; |
180 |
} |
181 |
if (dataSource instanceof BasicDataSource) { |
182 |
return ((BasicDataSource) dataSource).getNumActive() > 0 |
183 |
|| ((BasicDataSource) dataSource).getNumIdle() > 0;
|
184 |
} |
185 |
return true; |
186 |
} |
187 |
|
188 |
protected DataSource createDataSource() { |
189 |
JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this
|
190 |
.getParameters(); |
191 |
BasicDataSource dataSource = new BasicDataSource();
|
192 |
dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName()); |
193 |
dataSource.setUsername(jdbcParams.getUser()); |
194 |
dataSource.setPassword(jdbcParams.getPassword()); |
195 |
dataSource.setUrl(jdbcParams.getUrl()); |
196 |
|
197 |
|
198 |
dataSource.setMaxWait(60L * 1000); // FIXME |
199 |
|
200 |
// FIXME Set Pool parameters:
|
201 |
/*
|
202 |
dataSource.setMaxActive(maxActive);
|
203 |
dataSource.setMaxIdle(maxActive);
|
204 |
dataSource.setMaxOpenPreparedStatements(maxActive);
|
205 |
dataSource.setMaxWait(maxActive);
|
206 |
dataSource.setInitialSize(initialSize);
|
207 |
dataSource.setDefaultReadOnly(defaultReadOnly);
|
208 |
dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
|
209 |
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
210 |
dataSource.setMinIdle(minIdle);
|
211 |
dataSource.setTestOnBorrow(testOnBorrow);
|
212 |
dataSource.setTestOnReturn(testOnReturn);
|
213 |
dataSource.setTestWhileIdle(testOnReturn);
|
214 |
dataSource
|
215 |
.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
216 |
|
217 |
dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
218 |
dataSource.setLoginTimeout(seconds);
|
219 |
dataSource.setLogWriter(out);
|
220 |
*/
|
221 |
return dataSource;
|
222 |
} |
223 |
|
224 |
protected void connectToDB() throws DataException { |
225 |
if (this.dataSource != null) { |
226 |
return;
|
227 |
} |
228 |
JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this
|
229 |
.getParameters(); |
230 |
BasicDataSource dataSource = new BasicDataSource();
|
231 |
dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName()); |
232 |
dataSource.setUsername(jdbcParams.getUser()); |
233 |
dataSource.setPassword(jdbcParams.getPassword()); |
234 |
dataSource.setUrl(jdbcParams.getUrl()); |
235 |
|
236 |
dataSource.setMaxWait(60L * 1000); // FIXME |
237 |
|
238 |
// FIXME Set Pool parameters:
|
239 |
/*
|
240 |
dataSource.setMaxActive(maxActive);
|
241 |
dataSource.setMaxIdle(maxActive);
|
242 |
dataSource.setMaxOpenPreparedStatements(maxActive);
|
243 |
dataSource.setMaxWait(maxActive);
|
244 |
dataSource.setInitialSize(initialSize);
|
245 |
dataSource.setDefaultReadOnly(defaultReadOnly);
|
246 |
dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
|
247 |
dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
248 |
dataSource.setMinIdle(minIdle);
|
249 |
dataSource.setTestOnBorrow(testOnBorrow);
|
250 |
dataSource.setTestOnReturn(testOnReturn);
|
251 |
dataSource.setTestWhileIdle(testOnReturn);
|
252 |
dataSource
|
253 |
.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
254 |
|
255 |
dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
256 |
dataSource.setLoginTimeout(seconds);
|
257 |
dataSource.setLogWriter(out);
|
258 |
*/
|
259 |
|
260 |
this.dataSource = dataSource;
|
261 |
} |
262 |
|
263 |
|
264 |
} |