svn-gvsig-desktop / tags / v10_RC2c / libraries / libIverUtiles / src / com / iver / utiles / connections / ConnectionDB.java @ 8745
History | View | Annotate | Download (13.9 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.utiles.connections; |
42 |
|
43 |
import org.apache.commons.dbcp.ConnectionFactory; |
44 |
import org.apache.commons.dbcp.DriverManagerConnectionFactory; |
45 |
import org.apache.commons.dbcp.PoolableConnectionFactory; |
46 |
import org.apache.commons.dbcp.PoolingDriver; |
47 |
import org.apache.commons.pool.ObjectPool; |
48 |
import org.apache.commons.pool.impl.GenericObjectPool; |
49 |
|
50 |
import java.io.File; |
51 |
import java.io.FileInputStream; |
52 |
import java.io.FileOutputStream; |
53 |
import java.io.IOException; |
54 |
|
55 |
import java.sql.Connection; |
56 |
import java.sql.DatabaseMetaData; |
57 |
import java.sql.DriverManager; |
58 |
import java.sql.ResultSet; |
59 |
import java.sql.SQLException; |
60 |
import java.sql.Statement; |
61 |
|
62 |
import java.util.ArrayList; |
63 |
import java.util.Properties; |
64 |
|
65 |
|
66 |
/**
|
67 |
* Class responsible of the operations on the database.
|
68 |
*
|
69 |
* @author Vicente Caballero Navarro
|
70 |
*/
|
71 |
public class ConnectionDB { |
72 |
public static ConnectionDB instance = new ConnectionDB(); |
73 |
private ConnectionTrans[] connTrans; |
74 |
/**
|
75 |
* Creates a new one ConnectionDB.
|
76 |
*/
|
77 |
private ConnectionDB() {
|
78 |
} |
79 |
public void setConnTrans(ConnectionTrans[] ct){ |
80 |
connTrans=ct; |
81 |
} |
82 |
/**
|
83 |
* It returns a static instance of the class.
|
84 |
*
|
85 |
* @return instance
|
86 |
*/
|
87 |
public static ConnectionDB getInstance() { |
88 |
return instance;
|
89 |
} |
90 |
|
91 |
/**
|
92 |
* It verifies that the connection is correct
|
93 |
*
|
94 |
* @param ct Data of the connection
|
95 |
* @param driver Driver
|
96 |
*
|
97 |
* @throws ConnectionException
|
98 |
*/
|
99 |
public boolean testDB(ConnectionTrans ct) |
100 |
throws ConnectionException {
|
101 |
/*try {
|
102 |
Class.forName(driver.getDriverString());
|
103 |
} catch (java.lang.ClassNotFoundException e) {
|
104 |
throw new ConnectionException("Driver no encontrado", e);
|
105 |
}
|
106 |
*/
|
107 |
String url = ct.getConnBeginning() + "//" + |
108 |
ct.getHost() + ":" + ct.getPort() + "/" + ct.getDb(); |
109 |
String user = ct.getUser();
|
110 |
String password = ct.getPassword();
|
111 |
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url,
|
112 |
user, password); |
113 |
Connection connection = null; |
114 |
|
115 |
try {
|
116 |
connection = connectionFactory.createConnection(); |
117 |
} catch (SQLException e) { |
118 |
throw new ConnectionException(JDBCManager.getTranslation("fallo_crear_conexion"), e); |
119 |
} catch (Exception e) { |
120 |
throw new ConnectionException(JDBCManager.getTranslation("fallo_crear_conexion"), e); |
121 |
} |
122 |
|
123 |
try {
|
124 |
connection.close(); |
125 |
} catch (SQLException e) { |
126 |
throw new ConnectionException(JDBCManager.getTranslation("fallo_crear_conexion"), e); |
127 |
} |
128 |
return true; |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Returns a connection by Name.
|
133 |
*
|
134 |
* @param url_name Name of connection
|
135 |
*
|
136 |
* @return Connection
|
137 |
*
|
138 |
* @throws ConnectionException
|
139 |
*/
|
140 |
public Connection getConnectionByName(String url_name) |
141 |
throws ConnectionException {
|
142 |
Connection conn = null; |
143 |
|
144 |
try {
|
145 |
conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:" + |
146 |
url_name); |
147 |
} catch (SQLException e) { |
148 |
throw new ConnectionException(JDBCManager.getTranslation("fallo_obtener_conexion_existente"), |
149 |
e); |
150 |
} |
151 |
|
152 |
return conn;
|
153 |
} |
154 |
|
155 |
/**
|
156 |
* Returns a connection from the data
|
157 |
*
|
158 |
* @param ct Data connection
|
159 |
*
|
160 |
* @return Connection
|
161 |
*
|
162 |
* @throws ConnectionException
|
163 |
*/
|
164 |
public Connection getConnection(ConnectionTrans ct) |
165 |
throws ConnectionException {
|
166 |
//VectorialJDBCDriver driver = getDriver(ct.getDriver());
|
167 |
|
168 |
String name = ct.getHost() + "_" + ct.getName(); |
169 |
Connection conn = null; |
170 |
|
171 |
/* try {
|
172 |
Class.forName(driver.getDriverString());
|
173 |
} catch (ClassNotFoundException e) {
|
174 |
throw new ConnectionException("Driver no encontrado", e);
|
175 |
}
|
176 |
*/
|
177 |
setupDriver(ct); |
178 |
conn = getConnectionByName(name); |
179 |
|
180 |
return conn;
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* Returns a Driver by name.
|
185 |
*
|
186 |
* @param name Driver?s name
|
187 |
*
|
188 |
* @return Driver
|
189 |
*
|
190 |
* @throws ConnectionException
|
191 |
*/
|
192 |
/* public VectorialJDBCDriver getDriver(String name)
|
193 |
throws ConnectionException {
|
194 |
VectorialJDBCDriver driver = null;
|
195 |
|
196 |
String[] drivers = LayerFactory.getDM().getDriverNames();
|
197 |
|
198 |
for (int i = 0; i < drivers.length; i++) {
|
199 |
try {
|
200 |
if (LayerFactory.getDM().getDriver(drivers[i]) instanceof VectorialJDBCDriver) {
|
201 |
VectorialJDBCDriver d = (VectorialJDBCDriver) LayerFactory.getDM()
|
202 |
.getDriver(drivers[i]);
|
203 |
|
204 |
if (d.getName().equals(name)) {
|
205 |
driver = d;
|
206 |
}
|
207 |
}
|
208 |
} catch (DriverLoadException e) {
|
209 |
throw new ConnectionException("Driver no encontrado", e);
|
210 |
}
|
211 |
}
|
212 |
|
213 |
return driver;
|
214 |
}
|
215 |
*/
|
216 |
/**
|
217 |
* Registers in the driverpool a new connection
|
218 |
*
|
219 |
* @param ct Data connection
|
220 |
* @param driver Driver
|
221 |
*
|
222 |
* @throws ConnectionException
|
223 |
*/
|
224 |
public void setupDriver(ConnectionTrans ct) |
225 |
throws ConnectionException {
|
226 |
String url = ct.getConnBeginning() + "//" + |
227 |
ct.getHost() + ":" + ct.getPort() + "/" + ct.getDb(); |
228 |
String user = ct.getUser();
|
229 |
String password = ct.getPassword();
|
230 |
String name = ct.getHost() + "_" + ct.getName(); |
231 |
ObjectPool connectionPool = new GenericObjectPool();
|
232 |
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url,
|
233 |
user, password); |
234 |
|
235 |
try {
|
236 |
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
|
237 |
connectionPool, null, null, false, true); |
238 |
} catch (Exception e) { |
239 |
throw new ConnectionException(JDBCManager.getTranslation("fallo_crear_pool"), |
240 |
e); |
241 |
} |
242 |
|
243 |
try {
|
244 |
Class.forName("org.apache.commons.dbcp.PoolingDriver"); |
245 |
} catch (ClassNotFoundException e) { |
246 |
throw new ConnectionException("Clase : " + |
247 |
"org.apache.commons.dbcp.PoolingDriver", e);
|
248 |
} |
249 |
|
250 |
PoolingDriver driverPool; |
251 |
|
252 |
try {
|
253 |
driverPool = (PoolingDriver) DriverManager.getDriver(
|
254 |
"jdbc:apache:commons:dbcp:");
|
255 |
} catch (SQLException e) { |
256 |
throw new ConnectionException(JDBCManager.getTranslation("fallo_registrar_conexion"), e); |
257 |
} |
258 |
|
259 |
driverPool.registerPool(name, connectionPool); |
260 |
ct.setConnected(true);
|
261 |
} |
262 |
|
263 |
/**
|
264 |
* Executes a query
|
265 |
*
|
266 |
* @param SQL query
|
267 |
* @param name Driver?s name
|
268 |
*
|
269 |
* @throws ConnectionException
|
270 |
*/
|
271 |
public void ejecutaSQLnors(String SQL, String name) |
272 |
throws ConnectionException {
|
273 |
Connection conn = null; |
274 |
Statement s = null; |
275 |
|
276 |
try {
|
277 |
try {
|
278 |
conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:" + |
279 |
name); |
280 |
|
281 |
s = conn.createStatement(); |
282 |
s.execute(SQL); |
283 |
} catch (SQLException e) { |
284 |
throw new ConnectionException(JDBCManager.getTranslation("fallo_realizar_consulta"), |
285 |
e); |
286 |
} |
287 |
} finally {
|
288 |
try {
|
289 |
s.close(); |
290 |
} catch (Exception e) { |
291 |
} |
292 |
|
293 |
try {
|
294 |
conn.close(); |
295 |
} catch (Exception e) { |
296 |
} |
297 |
} |
298 |
} |
299 |
|
300 |
/**
|
301 |
* Returns the names of the tables
|
302 |
*
|
303 |
* @param conn Connection
|
304 |
*
|
305 |
* @return Array of string with the names of the tables.
|
306 |
*
|
307 |
* @throws ConnectionException
|
308 |
*/
|
309 |
public String[] getTableNames(Connection conn) throws ConnectionException { |
310 |
//Connection conn=getConnectionByName(name);
|
311 |
ArrayList tableNames = new ArrayList(); |
312 |
String nombreTablas = "%"; // Listamos todas las tablas |
313 |
String[] tipos = new String[1]; // Listamos s?lo tablas |
314 |
tipos[0] = "TABLE"; |
315 |
|
316 |
try {
|
317 |
DatabaseMetaData dbmd = conn.getMetaData();
|
318 |
ResultSet tablas = dbmd.getTables(null, null, nombreTablas, tipos); |
319 |
|
320 |
boolean seguir = tablas.next();
|
321 |
|
322 |
while (seguir) {
|
323 |
// Mostramos s?lo el nombre de las tablas, guardado
|
324 |
// en la columna "TABLE_NAME"
|
325 |
System.out.println(tablas.getString(tablas.findColumn(
|
326 |
"TABLE_NAME")));
|
327 |
tableNames.add(tablas.getString(tablas.findColumn("TABLE_NAME")));
|
328 |
seguir = tablas.next(); |
329 |
} |
330 |
} catch (SQLException e) { |
331 |
throw new ConnectionException(JDBCManager.getTranslation("fallo_obtener_tablas"), |
332 |
e); |
333 |
} |
334 |
|
335 |
return (String[]) tableNames.toArray(new String[0]); |
336 |
} |
337 |
|
338 |
/**
|
339 |
* Keeps in disk the data of the connection
|
340 |
*
|
341 |
* @param ct Data connection
|
342 |
*
|
343 |
* @throws IOException
|
344 |
*/
|
345 |
public void setPersistence(ConnectionTrans ct) throws IOException { |
346 |
String name = ct.getHost() + "_" + ct.getName(); |
347 |
Properties properties = new Properties(); |
348 |
properties.put("jdbc.drivers", ct.getDriver());
|
349 |
properties.put("jdbc.name", ct.getName());
|
350 |
properties.put("jdbc.host", ct.getHost());
|
351 |
properties.put("jdbc.port", ct.getPort());
|
352 |
properties.put("jdbc.username", ct.getUser());
|
353 |
properties.put("jdbc.savepassword", String.valueOf(ct.isSavePassword())); |
354 |
|
355 |
if (ct.isSavePassword()) {
|
356 |
properties.put("jdbc.password", ct.getPassword());
|
357 |
} |
358 |
|
359 |
properties.put("jdbc.database", ct.getDb());
|
360 |
properties.put("jdbc.connBeginning",ct.getConnBeginning());
|
361 |
boolean success = true; |
362 |
File file = null; |
363 |
/*
|
364 |
* TODO LWS: esto tiene pinta de no ser portable (case, en linux). Ahora
|
365 |
* en el Launcher de Andami hay un appHome, pero si lo pongo aqui ya estamos metiendo
|
366 |
* dependencias circulares ...
|
367 |
*/
|
368 |
String directory = System.getProperty("user.home") + "\\" + "gvsig" + |
369 |
"\\" + "connections"; |
370 |
|
371 |
if (!new File(directory).exists()) { |
372 |
file = new File(directory); |
373 |
success = file.mkdirs(); |
374 |
} |
375 |
|
376 |
if (success) {
|
377 |
File f = new File(directory + "/" + name + ".properties"); |
378 |
f.createNewFile(); |
379 |
|
380 |
FileOutputStream out = new FileOutputStream(f); |
381 |
properties.store(out, name); |
382 |
out.close(); |
383 |
} |
384 |
} |
385 |
public void delPersistence(String name){ |
386 |
String directory = System.getProperty("user.home") + "\\" + "gvsig" + |
387 |
"\\" + "connections"; |
388 |
File dir = new File(directory); |
389 |
File[] files = dir.listFiles(); |
390 |
for (int i = 0; i < files.length; i++) { |
391 |
if (files[i].getName().substring(0,files[i].getName().length()-11).equals(name)){ |
392 |
files[i].delete(); |
393 |
} |
394 |
} |
395 |
} |
396 |
/**
|
397 |
* Returns the data of the connection
|
398 |
*
|
399 |
* @return Array of data connections
|
400 |
*
|
401 |
* @throws IOException
|
402 |
*/
|
403 |
public ConnectionTrans[] getPersistence() throws IOException { |
404 |
ArrayList conns = new ArrayList(); |
405 |
String directory = System.getProperty("user.home") + "\\" + "gvsig" + |
406 |
"\\" + "connections"; |
407 |
File dir = new File(directory); |
408 |
File[] files = dir.listFiles(); |
409 |
if (files == null) |
410 |
return null; |
411 |
for (int i = 0; i < files.length; i++) { |
412 |
Properties properties = new Properties(); |
413 |
FileInputStream in = new FileInputStream(files[i]); |
414 |
properties.load(in); |
415 |
in.close(); |
416 |
|
417 |
ConnectionTrans ct = new ConnectionTrans();
|
418 |
ct.setDriver(properties.get("jdbc.drivers").toString());
|
419 |
ct.setName(properties.get("jdbc.name").toString());
|
420 |
ct.setHost(properties.get("jdbc.host").toString());
|
421 |
ct.setPort(properties.get("jdbc.port").toString());
|
422 |
ct.setUser(properties.get("jdbc.username").toString());
|
423 |
|
424 |
boolean isSave = Boolean.valueOf(properties.get("jdbc.savepassword") |
425 |
.toString()) |
426 |
.booleanValue(); |
427 |
ct.setSavePassword(isSave); |
428 |
|
429 |
if (isSave) {
|
430 |
ct.setPassword(properties.get("jdbc.password").toString());
|
431 |
} |
432 |
|
433 |
ct.setDb(properties.get("jdbc.database").toString());
|
434 |
ct.setConnBegining(properties.get("jdbc.connBeginning").toString());
|
435 |
conns.add(ct); |
436 |
} |
437 |
|
438 |
return (ConnectionTrans[]) conns.toArray(new ConnectionTrans[0]); |
439 |
} |
440 |
public ConnectionTrans[] getDefaultTrans() { |
441 |
return connTrans;
|
442 |
} |
443 |
|
444 |
} |