Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package it.inaf.oats.ia2.tapschemamanager.datalayer;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.postgresql.ds.PGPoolingDataSource;
/**
*
* @author Sonia Zorba <zorba at oats.inaf.it>
*/
public class DBWrapper implements Serializable {
private static final long serialVersionUID = 1721030677924066695L;
// Same credentials
private Credentials credentials;
// Separated credentials
private Credentials sourceCredentials;
private Credentials tapSchemaCredentials;
private final DataSourcesWrapper dataSources;
private DBWrapper() {
dataSources = new DataSourcesWrapper();
}
/**
* Constructor to use if the source schema credentials and the TAP_SCHEMA
* credentials are the same.
*/
public DBWrapper(Credentials credentials) {
this();
this.credentials = credentials;
}
/**
* Constructor to use if the source schema credentials are different from
* the TAP_SCHEMA credentials.
*/
public DBWrapper(Credentials sourceCredentials, Credentials tapSchemaCredentials) {
this();
this.sourceCredentials = sourceCredentials;
this.tapSchemaCredentials = tapSchemaCredentials;
}
public Connection getSourceConnection() throws SQLException {
return dataSources.getSourceDataSource().getConnection();
}
public Connection getTapSchemaConnection() throws SQLException {
return dataSources.getTapSchemaDataSource().getConnection();
}
public Credentials getSourceCredentials() {
if (credentials != null) {
return credentials;
}
return sourceCredentials;
}
public Credentials getTapSchemaCredentials() {
if (credentials != null) {
return credentials;
}
return tapSchemaCredentials;
}
public DatabaseType getSourceDatabaseType() {
return getSourceCredentials().getDatabaseType();
}
public DatabaseType getTapSchemaDatabaseType() {
return getTapSchemaCredentials().getDatabaseType();
}
public void testConnections() throws SQLException {
Connection connection;
if (credentials != null) {
connection = dataSources.getSourceDataSource().getConnection();
connection.close();
} else {
connection = dataSources.getSourceDataSource().getConnection();
connection.close();
connection = dataSources.getTapSchemaDataSource().getConnection();
connection.close();
}
}
private class DataSourcesWrapper implements Serializable {
private static final long serialVersionUID = -7025255003212206748L;
private transient DataSource dataSource;
private transient DataSource sourceDataSource;
private transient DataSource tapSchemaDataSource;
public DataSource getTapSchemaDataSource() {
if (credentials != null) {
if (dataSource == null) {
dataSource = createDataSource(credentials);
}
return dataSource;
}
if (tapSchemaDataSource == null) {
tapSchemaDataSource = createDataSource(tapSchemaCredentials);
}
return tapSchemaDataSource;
}
public DataSource getSourceDataSource() {
if (credentials != null) {
if (dataSource == null) {
dataSource = createDataSource(credentials);
}
return dataSource;
}
if (sourceDataSource == null) {
sourceDataSource = createDataSource(sourceCredentials);
}
return sourceDataSource;
}
private DataSource createDataSource(Credentials credentials) {
if (credentials.getDatabaseType() == DatabaseType.MYSQL) {
MysqlDataSource ds = new MysqlDataSource();
ds.setServerName(credentials.getHostname());
ds.setPortNumber(credentials.getPort());
ds.setUser(credentials.getUsername());
ds.setPassword(credentials.getPassword());
return ds;
} else if (credentials.getDatabaseType() == DatabaseType.POSTGRES) {
PGPoolingDataSource ds = new PGPoolingDataSource();
ds.setServerName(credentials.getHostname());
ds.setPortNumber(credentials.getPort());
ds.setUser(credentials.getUsername());
ds.setPassword(credentials.getPassword());
return ds;
}
throw new UnsupportedOperationException(credentials.getDatabaseType() + " not supported yet.");
}
}
}