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 @ 40435
History | View | Annotate | Download (7.29 KB)
1 | 40435 | jjdelcerro | /* 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 | /*
|
||
24 | * AUTHORS (In addition to CIT):
|
||
25 | * 2009 IVER T.I {{Task}}
|
||
26 | */
|
||
27 | |||
28 | package org.gvsig.fmap.dal.store.jdbc; |
||
29 | |||
30 | import java.sql.Connection; |
||
31 | import java.sql.SQLException; |
||
32 | import java.text.MessageFormat; |
||
33 | |||
34 | import javax.sql.DataSource; |
||
35 | |||
36 | import org.apache.commons.dbcp.BasicDataSource; |
||
37 | import org.gvsig.fmap.dal.exception.DataException; |
||
38 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
39 | import org.gvsig.fmap.dal.resource.ResourceParameters; |
||
40 | import org.gvsig.fmap.dal.resource.db.AbstractDBResourceNoBlocker; |
||
41 | import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
||
42 | import org.gvsig.fmap.dal.resource.exception.ResourceException; |
||
43 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCDriverClassNotFoundException; |
||
44 | import org.gvsig.fmap.dal.store.jdbc.exception.JDBCSQLException; |
||
45 | import org.slf4j.Logger; |
||
46 | import org.slf4j.LoggerFactory; |
||
47 | |||
48 | public class JDBCResource extends AbstractDBResourceNoBlocker { |
||
49 | |||
50 | final static private Logger logger = LoggerFactory |
||
51 | .getLogger(JDBCResource.class); |
||
52 | |||
53 | public final static String NAME = "JDBCResource"; |
||
54 | public static final String DESCRIPTION = "JDBC Connection"; |
||
55 | |||
56 | protected DataSource dataSource = null; |
||
57 | |||
58 | public JDBCResource(JDBCResourceParameters parameters)
|
||
59 | throws InitializeException {
|
||
60 | super(parameters);
|
||
61 | registerJDBCDriver(); |
||
62 | |||
63 | } |
||
64 | |||
65 | public String getName() throws AccessResourceException { |
||
66 | JDBCResourceParameters params = (JDBCResourceParameters) this
|
||
67 | .getParameters(); |
||
68 | return MessageFormat.format("JDBCResource({0},{1},{2},{3},{4})", |
||
69 | new Object[] { |
||
70 | params.getJDBCDriverClassName(), |
||
71 | params.getHost(), params.getPort(), |
||
72 | params.getUser(), params.getDBName() }); |
||
73 | } |
||
74 | |||
75 | @SuppressWarnings("unchecked") |
||
76 | protected void registerJDBCDriver() throws InitializeException { |
||
77 | String className = ((JDBCResourceParameters) getParameters())
|
||
78 | .getJDBCDriverClassName(); |
||
79 | if (className == null) { |
||
80 | return;
|
||
81 | } |
||
82 | |||
83 | Class theClass = null; |
||
84 | try {
|
||
85 | theClass = Class.forName(className);
|
||
86 | } catch (Exception e){ |
||
87 | throw new InitializeException(e); |
||
88 | } |
||
89 | if (theClass == null) { |
||
90 | try {
|
||
91 | throw new JDBCDriverClassNotFoundException(this.getName(), |
||
92 | className); |
||
93 | } catch (AccessResourceException e) {
|
||
94 | throw new InitializeException(e); |
||
95 | |||
96 | } |
||
97 | } |
||
98 | } |
||
99 | |||
100 | |||
101 | public Connection getJDBCConnection() throws AccessResourceException { |
||
102 | return (Connection) get(); |
||
103 | } |
||
104 | |||
105 | private void debugPoolStatus(String src) { |
||
106 | if (logger.isDebugEnabled() && dataSource instanceof BasicDataSource) { |
||
107 | BasicDataSource ds = (BasicDataSource) dataSource; |
||
108 | logger.debug(src + " actives:" + ds.getNumActive() + "(" |
||
109 | + ds.getMaxActive() + ") idle:" + ds.getNumIdle() + "(" |
||
110 | + ds.getMaxIdle() + ")");
|
||
111 | } |
||
112 | |||
113 | } |
||
114 | |||
115 | protected synchronized Object getTheConnection() throws DataException { |
||
116 | try {
|
||
117 | Object conn = this.dataSource.getConnection(); |
||
118 | debugPoolStatus("getTheConnection");
|
||
119 | return conn;
|
||
120 | } catch (SQLException e) { |
||
121 | throw new JDBCSQLException(e); |
||
122 | } |
||
123 | } |
||
124 | |||
125 | public boolean isThis(ResourceParameters parameters) |
||
126 | throws ResourceException {
|
||
127 | if (!super.isThis(parameters)) { |
||
128 | return false; |
||
129 | } |
||
130 | |||
131 | String dbName = ((JDBCResourceParameters) parameters).getDBName();
|
||
132 | String myDbName = ((JDBCResourceParameters) getParameters())
|
||
133 | .getDBName(); |
||
134 | if (dbName != myDbName) {
|
||
135 | if (!(dbName != null && dbName.equals(myDbName))) { |
||
136 | return false; |
||
137 | } |
||
138 | |||
139 | } |
||
140 | |||
141 | String driver = ((JDBCResourceParameters) parameters)
|
||
142 | .getJDBCDriverClassName(); |
||
143 | String myDriver = ((JDBCResourceParameters) getParameters())
|
||
144 | .getJDBCDriverClassName(); |
||
145 | if (driver != myDriver) {
|
||
146 | if (!(driver != null && driver.equals(myDriver))) { |
||
147 | return false; |
||
148 | } |
||
149 | } |
||
150 | return true; |
||
151 | |||
152 | } |
||
153 | |||
154 | public boolean isConnected() { |
||
155 | if (dataSource == null) { |
||
156 | return false; |
||
157 | } |
||
158 | if (dataSource instanceof BasicDataSource) { |
||
159 | return ((BasicDataSource) dataSource).getNumActive() > 0 |
||
160 | || ((BasicDataSource) dataSource).getNumIdle() > 0;
|
||
161 | } |
||
162 | return true; |
||
163 | } |
||
164 | |||
165 | protected DataSource createDataSource() { |
||
166 | JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this
|
||
167 | .getParameters(); |
||
168 | BasicDataSource dataSource = new BasicDataSource();
|
||
169 | dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName()); |
||
170 | dataSource.setUsername(jdbcParams.getUser()); |
||
171 | dataSource.setPassword(jdbcParams.getPassword()); |
||
172 | dataSource.setUrl(jdbcParams.getUrl()); |
||
173 | |||
174 | |||
175 | dataSource.setMaxWait(60L * 1000); // FIXME |
||
176 | |||
177 | // FIXME Set Pool parameters:
|
||
178 | /*
|
||
179 | dataSource.setMaxActive(maxActive);
|
||
180 | dataSource.setMaxIdle(maxActive);
|
||
181 | dataSource.setMaxOpenPreparedStatements(maxActive);
|
||
182 | dataSource.setMaxWait(maxActive);
|
||
183 | dataSource.setInitialSize(initialSize);
|
||
184 | dataSource.setDefaultReadOnly(defaultReadOnly);
|
||
185 | dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
|
||
186 | dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
||
187 | dataSource.setMinIdle(minIdle);
|
||
188 | dataSource.setTestOnBorrow(testOnBorrow);
|
||
189 | dataSource.setTestOnReturn(testOnReturn);
|
||
190 | dataSource.setTestWhileIdle(testOnReturn);
|
||
191 | dataSource
|
||
192 | .setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
||
193 | |||
194 | dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
||
195 | dataSource.setLoginTimeout(seconds);
|
||
196 | dataSource.setLogWriter(out);
|
||
197 | */
|
||
198 | return dataSource;
|
||
199 | } |
||
200 | |||
201 | protected void connectToDB() throws DataException { |
||
202 | if (this.dataSource != null) { |
||
203 | return;
|
||
204 | } |
||
205 | JDBCResourceParameters jdbcParams = (JDBCResourceParameters) this
|
||
206 | .getParameters(); |
||
207 | BasicDataSource dataSource = new BasicDataSource();
|
||
208 | dataSource.setDriverClassName(jdbcParams.getJDBCDriverClassName()); |
||
209 | dataSource.setUsername(jdbcParams.getUser()); |
||
210 | dataSource.setPassword(jdbcParams.getPassword()); |
||
211 | dataSource.setUrl(jdbcParams.getUrl()); |
||
212 | |||
213 | dataSource.setMaxWait(60L * 1000); // FIXME |
||
214 | |||
215 | // FIXME Set Pool parameters:
|
||
216 | /*
|
||
217 | dataSource.setMaxActive(maxActive);
|
||
218 | dataSource.setMaxIdle(maxActive);
|
||
219 | dataSource.setMaxOpenPreparedStatements(maxActive);
|
||
220 | dataSource.setMaxWait(maxActive);
|
||
221 | dataSource.setInitialSize(initialSize);
|
||
222 | dataSource.setDefaultReadOnly(defaultReadOnly);
|
||
223 | dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation);
|
||
224 | dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
|
||
225 | dataSource.setMinIdle(minIdle);
|
||
226 | dataSource.setTestOnBorrow(testOnBorrow);
|
||
227 | dataSource.setTestOnReturn(testOnReturn);
|
||
228 | dataSource.setTestWhileIdle(testOnReturn);
|
||
229 | dataSource
|
||
230 | .setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
||
231 | |||
232 | dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
||
233 | dataSource.setLoginTimeout(seconds);
|
||
234 | dataSource.setLogWriter(out);
|
||
235 | */
|
||
236 | |||
237 | this.dataSource = dataSource;
|
||
238 | } |
||
239 | |||
240 | |||
241 | } |