svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.personaldb / org.gvsig.personaldb.lib / org.gvsig.personaldb.lib.impl / src / main / java / org / gvsig / personaldb / impl / DefaultPersonalDBManager.java @ 40560
History | View | Annotate | Download (7.18 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 |
package org.gvsig.personaldb.impl; |
25 |
|
26 |
import java.sql.Connection; |
27 |
import java.sql.SQLException; |
28 |
|
29 |
import javax.sql.DataSource; |
30 |
|
31 |
import org.apache.commons.dbcp.BasicDataSource; |
32 |
import org.slf4j.Logger; |
33 |
import org.slf4j.LoggerFactory; |
34 |
|
35 |
import org.gvsig.fmap.dal.DALLocator; |
36 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
37 |
import org.gvsig.fmap.dal.DataStoreParameters; |
38 |
import org.gvsig.fmap.dal.exception.InitializeException; |
39 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
40 |
import org.gvsig.fmap.dal.store.jdbc.JDBCResourceParameters; |
41 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorer; |
42 |
import org.gvsig.fmap.dal.store.jdbc.JDBCServerExplorerParameters; |
43 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreParameters; |
44 |
import org.gvsig.fmap.dal.store.jdbc.JDBCStoreProvider; |
45 |
import org.gvsig.personaldb.DBAccessException; |
46 |
import org.gvsig.personaldb.DataServerExplorerParametersException; |
47 |
import org.gvsig.personaldb.PersonalDBException; |
48 |
import org.gvsig.personaldb.PersonalDBManager; |
49 |
import org.gvsig.tools.dispose.impl.AbstractDisposable; |
50 |
import org.gvsig.tools.exception.BaseException; |
51 |
|
52 |
/**
|
53 |
* Default {@link PersonalDBManager} implementation.
|
54 |
*
|
55 |
* @author gvSIG Team
|
56 |
* @version $Id$
|
57 |
*/
|
58 |
public class DefaultPersonalDBManager extends AbstractDisposable implements |
59 |
PersonalDBManager { |
60 |
|
61 |
private static final Logger LOG = LoggerFactory |
62 |
.getLogger(DefaultPersonalDBManager.class); |
63 |
|
64 |
private BasicDataSource dataSource;
|
65 |
private JDBCResourceParameters resourceParameters;
|
66 |
|
67 |
public Connection getConnection() throws PersonalDBException { |
68 |
|
69 |
try {
|
70 |
return getDataSource().getConnection();
|
71 |
} catch (SQLException e) { |
72 |
throw new DBAccessException(resourceParameters, e); |
73 |
} |
74 |
} |
75 |
|
76 |
private synchronized DataSource getDataSource() { |
77 |
|
78 |
if (dataSource == null) { |
79 |
dataSource = new BasicDataSource();
|
80 |
dataSource.setDriverClassName(resourceParameters |
81 |
.getJDBCDriverClassName()); |
82 |
dataSource.setUsername(resourceParameters.getUser()); |
83 |
dataSource.setPassword(resourceParameters.getPassword()); |
84 |
dataSource.setUrl(resourceParameters.getUrl()); |
85 |
|
86 |
dataSource.setMaxWait(60L * 1000); // FIXME |
87 |
|
88 |
dataSource.setMaxActive(20);
|
89 |
dataSource.setMaxIdle(4);
|
90 |
dataSource.setMinIdle(1);
|
91 |
dataSource.setInitialSize(1);
|
92 |
|
93 |
// FIXME Set Pool parameters:
|
94 |
/*
|
95 |
* dataSource.setMaxOpenPreparedStatements(maxActive);
|
96 |
* dataSource.setMaxWait(maxActive);
|
97 |
* dataSource.setDefaultReadOnly(defaultReadOnly);
|
98 |
* dataSource.setDefaultTransactionIsolation(defaultTransactionIsolation
|
99 |
* )
|
100 |
* ;
|
101 |
* dataSource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis
|
102 |
* );
|
103 |
* dataSource.setTestOnBorrow(testOnBorrow);
|
104 |
* dataSource.setTestOnReturn(testOnReturn);
|
105 |
* dataSource.setTestWhileIdle(testOnReturn);
|
106 |
* dataSource
|
107 |
* .setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
|
108 |
*
|
109 |
* dataSource.setAccessToUnderlyingConnectionAllowed(allow);
|
110 |
* dataSource.setLoginTimeout(seconds);
|
111 |
* dataSource.setLogWriter(out);
|
112 |
*/
|
113 |
} |
114 |
|
115 |
return dataSource;
|
116 |
} |
117 |
|
118 |
public DataStoreParameters getDataStoreParameters()
|
119 |
throws PersonalDBException {
|
120 |
|
121 |
return getJDBCStoreParameters();
|
122 |
} |
123 |
|
124 |
public DataStoreParameters getDataStoreParameters(String schema, |
125 |
String table) throws PersonalDBException { |
126 |
|
127 |
JDBCStoreParameters params = getJDBCStoreParameters(); |
128 |
params.setSchema(schema); |
129 |
params.setTable(table); |
130 |
return params;
|
131 |
} |
132 |
|
133 |
private JDBCStoreParameters getJDBCStoreParameters()
|
134 |
throws PersonalDBException {
|
135 |
try {
|
136 |
JDBCStoreParameters params = |
137 |
(JDBCStoreParameters) DALLocator.getDataManager() |
138 |
.createStoreParameters(JDBCStoreProvider.NAME); |
139 |
|
140 |
params.setUrl(resourceParameters.getUrl()); |
141 |
params.setJDBCDriverClassName(resourceParameters |
142 |
.getJDBCDriverClassName()); |
143 |
|
144 |
return params;
|
145 |
} catch (InitializeException e) {
|
146 |
throw new DataServerExplorerParametersException( |
147 |
JDBCServerExplorer.NAME, e); |
148 |
} catch (ProviderNotRegisteredException e) {
|
149 |
throw new DataServerExplorerParametersException( |
150 |
JDBCServerExplorer.NAME, e); |
151 |
} |
152 |
} |
153 |
|
154 |
public DataServerExplorerParameters getDataServerExplorerParameters()
|
155 |
throws PersonalDBException {
|
156 |
return getJDBCServerExplorerParameters();
|
157 |
} |
158 |
|
159 |
public DataServerExplorerParameters getDataServerExplorerParameters(
|
160 |
String schema) throws PersonalDBException { |
161 |
JDBCServerExplorerParameters params = getJDBCServerExplorerParameters(); |
162 |
params.setSchema(schema); |
163 |
return params;
|
164 |
} |
165 |
|
166 |
private JDBCServerExplorerParameters getJDBCServerExplorerParameters()
|
167 |
throws PersonalDBException {
|
168 |
try {
|
169 |
JDBCServerExplorerParameters params = |
170 |
(JDBCServerExplorerParameters) DALLocator.getDataManager() |
171 |
.createServerExplorerParameters(JDBCServerExplorer.NAME); |
172 |
|
173 |
params.setUrl(resourceParameters.getUrl()); |
174 |
params.setJDBCDriverClassName(resourceParameters |
175 |
.getJDBCDriverClassName()); |
176 |
|
177 |
return params;
|
178 |
} catch (InitializeException e) {
|
179 |
throw new DataServerExplorerParametersException( |
180 |
JDBCServerExplorer.NAME, e); |
181 |
} catch (ProviderNotRegisteredException e) {
|
182 |
throw new DataServerExplorerParametersException( |
183 |
JDBCServerExplorer.NAME, e); |
184 |
} |
185 |
} |
186 |
|
187 |
public void open(JDBCResourceParameters resourceParameters) { |
188 |
LOG.debug( |
189 |
"Creating connection to personal database with parameters: {}",
|
190 |
resourceParameters); |
191 |
|
192 |
this.resourceParameters = resourceParameters;
|
193 |
} |
194 |
|
195 |
@Override
|
196 |
protected void doDispose() throws BaseException { |
197 |
try {
|
198 |
dataSource.close(); |
199 |
} catch (SQLException e) { |
200 |
throw new DBAccessException(resourceParameters, e); |
201 |
} |
202 |
} |
203 |
} |