forked from openmrs/openmrs-module-patientmatching
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReaderProvider.java
136 lines (115 loc) · 3.91 KB
/
ReaderProvider.java
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
package org.regenstrief.linkage.io;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import java.util.Hashtable;
import javax.sql.DataSource;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.regenstrief.linkage.Record;
import org.regenstrief.linkage.util.LinkDataSource;
import org.regenstrief.linkage.util.MatchingConfig;
/**
* Class created to return Reader objects for a given
* data source description in a LinkDataSource object.
*
*
* For database readers, it uses Apache Commons DBCP to pool
* connections per datasource.
*
* @author jegg
*
*/
/*
* Use of apache-commons DBCP taken from sample code
* ManualPoolingDataSourceExample.java from the apache.org
* svn repository
*/
public class ReaderProvider {
Hashtable<LinkDataSource, DataSource> database_pools;
Hashtable<LinkDataSource,Hashtable<MatchingConfig,Long>> sort_times;
private static final ReaderProvider INSTANCE = new ReaderProvider();
private ReaderProvider(){
database_pools = new Hashtable<LinkDataSource, DataSource>();
sort_times = new Hashtable<LinkDataSource,Hashtable<MatchingConfig,Long>>();
}
public static ReaderProvider getInstance(){
return INSTANCE;
}
public DataSourceReader getReader(LinkDataSource lds){
if(lds.getType().equals("CharDelimFile")){
return new CharDelimFileReader(lds);
} else if(lds.getType().equals("DataBase")){
Connection db = getConnection(lds);
return new DataBaseReader(lds, db);
} else if(lds.getType().equals("Vector")){
return new VectorReader(lds);
}
return null;
}
public OrderedDataSourceReader getReader(LinkDataSource lds, MatchingConfig mc){
if(lds.getType().equals("CharDelimFile")){
return new OrderedCharDelimFileReader(lds, mc);
} else if(lds.getType().equals("DataBase")){
Connection db = getConnection(lds);
return new OrderedDataBaseReader(lds, db, mc);
} else if(lds.getType().equals("Vector")){
return new VectorReader(lds);
}
return null;
}
public SubsetDataSourceReader getReader(LinkDataSource lds, MatchingConfig mc, Record test){
if(lds.getType().equals("CharDelimFile")){
return null;
} else if(lds.getType().equals("DataBase")){
Connection db = getConnection(lds);
return new SubsetDataBaseReader(lds, db, mc, test);
} else if(lds.getType().equals("Vector")){
return null;
}
return null;
}
private Connection getConnection(LinkDataSource lds){
DataSource db = database_pools.get(lds);
// if link data source hasn't been used to get a database connection yet
if(db == null){
db = getDataSource(lds);
database_pools.put(lds, db);
}
try{
return db.getConnection();
}
catch(SQLException sqle){
return null;
}
catch(NullPointerException npe){
return null;
}
catch(Exception e){
return null;
}
}
private DataSource getDataSource(LinkDataSource lds){
try{
String driver, url, user, passwd;
String[] access = lds.getAccess().split(",",-1);
driver = access[0];
url = access[1];
user = access[2];
passwd = access[3];
Class.forName(driver);
ObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, user, passwd);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
PoolingDataSource data_source = new PoolingDataSource(connectionPool);
return data_source;
}
catch(Exception e){
}
return null;
}
}