1 package org.opensync.engine.server.connector;
2
3 import java.sql.Connection;
4 import java.sql.SQLException;
5
6 import org.apache.commons.dbcp.BasicDataSource;
7 import org.opensync.engine.server.DefaultConnector;
8 import org.opensync.engine.server.OpenSync;
9 import org.opensync.engine.server.OpenSyncException;
10 import org.opensync.tools.ConnectionPool;
11 import org.opensync.tools.PoolManager;
12 import org.opensync.tools.Utils;
13
14 /***
15 * The connector used to connect to a database server
16 */
17
18 public class BDConnector extends DefaultConnector{
19
20 BasicDataSource dataSource;
21
22 /***
23 * The user login
24 *
25 */
26 protected String user;
27 /***
28 * The jdbc driver
29 *
30 */
31 protected String driver;
32 /****/
33 /***
34 * The source
35 *
36 */
37 protected String source;
38 /***
39 * The user password
40 *
41 *
42 */
43 protected String password;
44 /***
45 * The maximum of the opened connection
46 *
47 */
48 protected int max;
49 /***
50 * The minimum of the opened connection
51 *
52 */
53 protected int init;
54 /***
55 * The time out
56 *
57 */
58 protected int timeout;
59 /***
60 * The operator for concatenation
61 *
62 */
63 protected String operatorConcat;
64
65 /***
66 * The operator for conversion
67 *
68 */
69 protected String conversion_function;
70 private int windowSize;
71
72 public BDConnector() {
73 }
74
75 /***
76 * Construct a FileConnetor object
77 *
78 * @param source the source name
79 */
80 public BDConnector(String source) {
81 this.source = source;
82 this.type = BD;
83 }
84 /***
85 * Set the user login used to connect to the database server
86 *
87 * @param user
88 */
89 public void setUser(String user) {
90 this.user = user;
91 }
92 /***
93 * Get the user login used to connect to the database server
94 *
95 */
96 public String getUser() {
97 return user;
98 }
99 /***
100 * @param pwd
101 */
102 /****/
103 /***
104 * Set the jdbc driver
105 *
106 * @param driver
107 */
108 public void setDriver(String driver) {
109 this.driver = driver;
110 }
111 /***
112 * Get the jdbc driver
113 *
114 */
115 public String getDriver() {
116 return driver;
117 }
118 /***
119 * @param url
120 */
121 /****/
122 /***
123 * Get the connection pool
124 *
125 * @exception SQLException
126 */
127 public ConnectionPool getConnectionPool()throws SQLException{
128 try {
129 if (Utils.debug) System.out.println("BDConnector::getConnectionPool - operatorConcat="+operatorConcat);
130
131 PoolManager poolManager = OpenSync.getInstance().getPoolManager();
132 ConnectionPool pool = poolManager.getConnectionPool(source);
133 if(pool == null){
134
135
136 pool = poolManager.createPool(
137 source,driver,url,user,password,max,init,timeout,null,operatorConcat, conversion_function
138 );
139 }
140
141
142 pool.setAutocommit(false);
143
144
145 pool.setBatchmode(false);
146 return pool;
147 }
148 catch (Exception ex) {
149 throw new SQLException(ex.getMessage());
150 }
151 }
152 /***
153 * Set the user password used to connect to the file server
154 *
155 * @param password the password
156 */
157 public void setPassword(String password) {
158 this.password = password;
159 }
160 /***
161 * Get the user password used to connect to the database server
162 *
163 */
164 public String getPassword() {
165 return password;
166 }
167 /***
168 * Set the maximum of the opened connection
169 *
170 * @param max the maximum
171 */
172 public void setMax(int max) {
173 this.max = max;
174 }
175 /***
176 * Get the maximum of the opened connection
177 *
178 */
179 public int getMax() {
180 return max;
181 }
182 /***
183 * Set the minimum of the opened connection
184 *
185 * @param init
186 */
187 public void setInit(int init) {
188 this.init = init;
189 }
190 /***
191 * Get the minimum of the opened connection
192 *
193 */
194 public int getInit() {
195 return init;
196 }
197 /***
198 * Set time-out
199 *
200 * @param timeout the timeout
201 */
202 public void setTimeout(int timeout) {
203 this.timeout = timeout;
204 }
205 /***
206 * Get the time out
207 *
208 */
209 public int getTimeout() {
210 return timeout;
211 }
212
213 public void validate() throws OpenSyncException {
214 try {
215 Connection conn = getConnectionPool().getConnection();
216 getConnectionPool().freeConnection(conn);
217 } catch (SQLException sqlex) {
218 throw new OpenSyncException(sqlex.getMessage()+":\n\r"+this.toString());
219 }
220 }
221
222 /***
223 *
224 * @return
225 */
226 public void release() throws SQLException {
227 getConnectionPool().release();
228 this.getAdapter().release();
229 }
230
231 /***
232 *
233 * @return
234 */
235 public void releaseWithException() throws SQLException {
236 getConnectionPool().releaseWithException();
237 this.getAdapter().releaseWithException();
238 }
239
240 /***
241 * Use for debug only
242 *
243 */
244 public String toString(){
245 StringBuffer buffer = new StringBuffer();
246 buffer.append("BDConnector{\n");
247 buffer.append("\ttype : ").append(type).append("\n");
248 buffer.append("\tsource : ").append(source).append("\n");
249 buffer.append("\tuser : ").append(user).append("\n");
250 buffer.append("\tpassword : ").append(password).append("\n");
251 buffer.append("\tdriver : ").append(driver).append("\n");
252 buffer.append("\turl : ").append(url).append("\n");
253 buffer.append("\tmax : ").append(max).append("\n");
254 buffer.append("\tinit : ").append(init).append("\n");
255 buffer.append("\ttimeout : ").append(timeout).append("\n");
256 buffer.append("\toperatorConcat : ").append(operatorConcat).append("\n");
257 buffer.append("}\n");
258 return buffer.toString();
259 }
260
261 /***
262 * Set the operator for concatenation
263 *
264 * @param operatorConcat the operator
265 */
266 public void setOperatorConcat(String operatorConcat) {
267 this.operatorConcat = operatorConcat;
268 }
269 /***
270 * Get the operator for concatenation
271 *
272 */
273 public String getOperatorConcat() {
274 return operatorConcat;
275 }
276
277 /***
278 * Set the conversion function
279 *
280 * @param operatorConcat conversion function
281 */
282 public void setConversionFunction(String sConversionFunction) {
283 this.conversion_function = sConversionFunction;
284 }
285
286 /***
287 * Get the conversion function
288 *
289 */
290 public String getConversionFunction() {
291 return conversion_function;
292 }
293 public int getWindowSize() {
294 return windowSize;
295 }
296 public void setWindowSize(int windowSize) {
297 this.windowSize = windowSize;
298 }
299
300 /***
301 * @param datasource The datasource to set.
302 */
303 public void setDataSource(BasicDataSource dataSource) {
304 this.dataSource = dataSource;
305 }
306 }