KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > controller > backend > DriverCompliance


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.controller.backend;
26
27 import java.net.ConnectException JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.DatabaseMetaData JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.Statement JavaDoc;
32
33 import org.objectweb.cjdbc.common.i18n.Translate;
34 import org.objectweb.cjdbc.common.log.Trace;
35 import org.objectweb.cjdbc.controller.connection.DriverManager;
36
37 /**
38  * This class checks if a given driver provides the mandatory features necessary
39  * for C-JDBC.
40  *
41  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
42  * @version 1.0
43  */

44 public class DriverCompliance
45 {
46   private boolean isCompliant = false;
47   private boolean hasBeenTested = false;
48   private boolean supportSetQueryTimeout = false;
49   private boolean supportGetGeneratedKeys = false;
50   private boolean supportGetColumnCount = false;
51   private boolean supportGetColumnClassName = false;
52   private boolean supportGetColumnTypeName = false;
53   private boolean supportGetColumnType = false;
54   private boolean supportGetColumnDisplaySize = false;
55   private boolean supportGetTableName = false;
56   private boolean supportSetCursorName = false;
57   private boolean supportSetFetchSize = false;
58   private boolean supportSetMaxRows = false;
59
60   private Trace logger;
61
62   private static final int TIMEOUT_VALUE = 1000;
63   private static final String JavaDoc DEFAULT_TEST_STATEMENT = "select 1";
64   private String JavaDoc databaseProductName = "C-JDBC";
65
66   /**
67    * Builds a new DriverCompliance object.
68    *
69    * @param logger the logger to use
70    */

71   public DriverCompliance(Trace logger)
72   {
73     this.logger = logger;
74   }
75
76   /**
77    * Check the driver compliance.
78    *
79    * @param backendUrl the JDBC URL to connect to
80    * @param login the user login
81    * @param password the user password
82    * @param driverPath path for driver
83    * @param driverClassName class name for driver
84    * @param connectionTestStatement SQL statement used to check if a connection
85    * is still valid
86    * @return true if the driver is C-JDBC compliant
87    * @throws ConnectException if it is not possible to connect to the backend
88    */

89   public boolean complianceTest(String JavaDoc backendUrl, String JavaDoc login,
90       String JavaDoc password, String JavaDoc driverPath, String JavaDoc driverClassName,
91       String JavaDoc connectionTestStatement) throws ConnectException JavaDoc
92   {
93     if (hasBeenTested)
94       return isCompliant;
95
96     isCompliant = false;
97
98     Connection JavaDoc c = null;
99     try
100     {
101       c = DriverManager.getConnection(backendUrl, login, password, driverPath,
102           driverClassName);
103     }
104     catch (Exception JavaDoc e)
105     {
106       if (logger.isDebugEnabled())
107         logger.debug(Translate.get("backend.driver.test.connection.failed", e),
108             e);
109       else
110         logger.error(Translate.get("backend.driver.test.connection.failed", e));
111       throw new ConnectException JavaDoc(e.getMessage());
112     }
113     if (logger.isDebugEnabled())
114       logger.debug(Translate.get("backend.driver.test.connection.ok"));
115
116     DatabaseMetaData JavaDoc connectionMetaData;
117     try
118     {
119       connectionMetaData = c.getMetaData();
120     }
121     catch (Exception JavaDoc e)
122     {
123       if (logger.isDebugEnabled())
124         logger
125             .debug(Translate.get("backend.driver.test.metadata.failed", e), e);
126       else
127         logger.error(Translate.get("backend.driver.test.metadata.failed", e));
128       return isCompliant;
129     }
130     if (logger.isDebugEnabled())
131       logger.debug(Translate.get("backend.driver.test.metadata.ok"));
132
133     try
134     {
135       this.databaseProductName = connectionMetaData.getDatabaseProductName();
136       logger.info(Translate
137           .get("backend.detected.as", this.databaseProductName));
138     }
139     catch (Exception JavaDoc e)
140     {
141       logger.warn(Translate
142           .get("backend.driver.test.database.productname.failed"));
143     }
144
145     Statement JavaDoc s;
146     try
147     {
148       s = c.createStatement();
149     }
150     catch (Exception JavaDoc e)
151     {
152       if (logger.isDebugEnabled())
153         logger.debug(Translate.get("backend.driver.test.statement.failed", e),
154             e);
155       else
156         logger.error(Translate.get("backend.driver.test.statement.failed", e));
157       return isCompliant;
158     }
159
160     try
161     {
162       if (connectionTestStatement == null)
163       {
164         if (logger.isDebugEnabled())
165           logger.debug(Translate.get("backend.driver.using.default.statement",
166               DEFAULT_TEST_STATEMENT));
167         connectionTestStatement = DEFAULT_TEST_STATEMENT;
168       }
169       s.execute(connectionTestStatement);
170     }
171     catch (Exception JavaDoc e)
172     {
173       if (logger.isDebugEnabled())
174         logger.debug(Translate.get("backend.driver.test.statement.invalid",
175             new String JavaDoc[]{connectionTestStatement, e.getMessage()}), e);
176       else
177         logger.error(Translate.get("backend.driver.test.statement.invalid",
178             new String JavaDoc[]{connectionTestStatement, e.getMessage()}));
179       return isCompliant;
180     }
181     if (logger.isDebugEnabled())
182       logger.debug(Translate.get("backend.driver.test.statement.ok"));
183
184     try
185     {
186       s.setCursorName("testcursor");
187       supportSetCursorName = true;
188       if (logger.isDebugEnabled())
189         logger
190             .debug(Translate.get("backend.driver.statement.setCursorName.ok"));
191     }
192     catch (Exception JavaDoc e1)
193     {
194       logger.warn(Translate
195           .get("backend.driver.statement.setCursorName.failed"));
196       supportSetMaxRows = false;
197     }
198
199     try
200     {
201       s.setFetchSize(25);
202       supportSetFetchSize = true;
203       if (logger.isDebugEnabled())
204         logger.debug(Translate.get("backend.driver.statement.setFetchSize.ok"));
205     }
206     catch (Exception JavaDoc e1)
207     {
208       logger
209           .warn(Translate.get("backend.driver.statement.setFetchSize.failed"));
210       supportSetMaxRows = false;
211     }
212
213     try
214     {
215       s.setMaxRows(5);
216       supportSetMaxRows = true;
217       if (logger.isDebugEnabled())
218         logger.debug(Translate.get("backend.driver.statement.setMaxRows.ok"));
219     }
220     catch (Exception JavaDoc e1)
221     {
222       logger.warn(Translate.get("backend.driver.statement.setMaxRows.failed"));
223       supportSetMaxRows = false;
224     }
225
226     try
227     {
228       s.getGeneratedKeys();
229       supportGetGeneratedKeys = true;
230       if (logger.isDebugEnabled())
231         logger.debug(Translate
232             .get("backend.driver.statement.getGeneratedKeys.ok"));
233     }
234     catch (Exception JavaDoc e1)
235     {
236       logger.warn(Translate
237           .get("backend.driver.statement.getGeneratedKeys.failed"));
238       supportGetGeneratedKeys = false;
239     }
240     catch (AbstractMethodError JavaDoc e1)
241     {
242       logger.warn(Translate
243           .get("backend.driver.statement.getGeneratedKeys.failed"));
244       supportGetGeneratedKeys = false;
245     }
246     catch (java.lang.NoSuchMethodError JavaDoc e1)
247     {
248       logger.warn(Translate
249           .get("backend.driver.statement.getGeneratedKeys.failed"));
250       supportGetGeneratedKeys = false;
251     }
252
253     // Commented out:
254
// A prepared statement can be sent to the DBMS right away to be compiled
255
// Should fine a work around for this test.
256

257     // PreparedStatement ps;
258
// try
259
// {
260
// ps = c.prepareStatement("INSERT INTO versions VALUES (?,?)");
261
// ps.setInt(1, 10);
262
// ps.setString(2, "just a test");
263
// }
264
// catch (Exception e)
265
// {
266
// logger.warn(Translate.get("backend.driver.prepared.statement.failed"),
267
// e);
268
// }
269
// if (logger.isDebugEnabled())
270
// logger.debug(Translate.get("backend.driver.prepared.statement.ok"));
271

272     try
273     {
274       s.setQueryTimeout(TIMEOUT_VALUE);
275       supportSetQueryTimeout = true;
276     }
277     catch (Exception JavaDoc e)
278     {
279       logger.warn(Translate.get("backend.driver.setQueryTimeout.failed", e));
280     }
281     if (supportSetQueryTimeout && logger.isDebugEnabled())
282       logger.debug(Translate.get("backend.driver.setQueryTimeout.ok"));
283
284     ResultSet JavaDoc rs;
285     try
286     {
287       String JavaDoc[] types = {"TABLE", "VIEW"};
288       rs = connectionMetaData.getTables(null, null, "%", types);
289     }
290     catch (Exception JavaDoc e)
291     {
292       if (logger.isDebugEnabled())
293         logger.debug(Translate.get("backend.driver.metadata.getTables.failed",
294             e), e);
295       else
296         logger.error(Translate.get("backend.driver.metadata.getTables.failed",
297             e));
298       return isCompliant;
299     }
300     if (logger.isDebugEnabled())
301       logger.debug(Translate.get("backend.driver.metadata.getTables.ok"));
302
303     java.sql.ResultSetMetaData JavaDoc rsMetaData;
304     try
305     {
306       rsMetaData = rs.getMetaData();
307     }
308     catch (Exception JavaDoc e)
309     {
310       if (logger.isDebugEnabled())
311         logger.debug(Translate.get(
312             "backend.driver.resultset.getMetaData.failed", e), e);
313       else
314         logger.error(Translate.get(
315             "backend.driver.resultset.getMetaData.failed", e));
316       return isCompliant;
317     }
318     if (logger.isDebugEnabled())
319       logger.debug(Translate.get("backend.driver.resultset.getMetaData.ok"));
320
321     try
322     {
323       if (rs.next() && (rsMetaData.getColumnCount() > 0))
324       {
325         rs.getObject(1);
326       }
327       else
328         logger.warn(Translate.get("backend.driver.resultset.getObject.unable"));
329     }
330     catch (Exception JavaDoc e)
331     {
332       if (logger.isDebugEnabled())
333         logger.debug(Translate.get("backend.driver.resultset.getObject.failed",
334             e), e);
335       else
336         logger.error(Translate.get("backend.driver.resultset.getObject.failed",
337             e));
338       return isCompliant;
339     }
340     if (logger.isDebugEnabled())
341       logger.debug(Translate.get("backend.driver.resultset.getObject.ok"));
342
343     try
344     {
345       rsMetaData.getColumnCount();
346       supportGetColumnCount = true;
347     }
348     catch (Exception JavaDoc e)
349     {
350       if (logger.isDebugEnabled())
351         logger.debug(Translate.get(
352             "backend.driver.metadata.getColumnCount.failed", e), e);
353       else
354         logger.error(Translate.get(
355             "backend.driver.metadata.getColumnCount.failed", e));
356       return isCompliant;
357     }
358     if (supportGetColumnCount && logger.isDebugEnabled())
359       logger.debug(Translate.get("backend.driver.metadata.getColumnCount.ok"));
360
361     try
362     {
363       rsMetaData.getColumnName(1);
364     }
365     catch (Exception JavaDoc e)
366     {
367       if (logger.isDebugEnabled())
368         logger.debug(Translate.get(
369             "backend.driver.metadata.getColumnName.failed", e), e);
370       else
371         logger.error(Translate.get(
372             "backend.driver.metadata.getColumnName.failed", e));
373       return isCompliant;
374     }
375     if (logger.isDebugEnabled())
376       logger.debug(Translate.get("backend.driver.metadata.getColumnName.ok"));
377
378     try
379     {
380       rsMetaData.getTableName(1);
381       supportGetTableName = true;
382     }
383     catch (Exception JavaDoc e)
384     {
385       logger.warn(Translate.get("backend.driver.metadata.getTableName.failed",
386           e));
387     }
388
389     if (supportGetTableName && logger.isDebugEnabled())
390       logger.debug(Translate.get("backend.driver.metadata.getTableName.ok"));
391
392     try
393     {
394       rsMetaData.getColumnDisplaySize(1);
395       supportGetColumnDisplaySize = true;
396     }
397     catch (Exception JavaDoc e)
398     {
399       logger.warn(Translate.get(
400           "backend.driver.metadata.getColumnDisplaySize.failed", e));
401     }
402     if (supportGetColumnDisplaySize && logger.isDebugEnabled())
403       logger.debug(Translate
404           .get("backend.driver.metadata.getColumnDisplaySize.ok"));
405
406     try
407     {
408       rsMetaData.getColumnType(1);
409       supportGetColumnType = true;
410     }
411     catch (Exception JavaDoc e)
412     {
413       logger.warn(Translate.get("backend.driver.metadata.getColumnType.failed",
414           e));
415     }
416     if (supportGetColumnType && logger.isDebugEnabled())
417       logger.debug(Translate.get("backend.driver.metadata.getColumnType.ok"));
418
419     try
420     {
421       rsMetaData.getColumnTypeName(1);
422       supportGetColumnTypeName = true;
423     }
424     catch (Exception JavaDoc e)
425     {
426       logger.warn(Translate.get(
427           "backend.driver.metadata.getColumnTypeName.failed", e));
428     }
429     if (supportGetColumnTypeName && logger.isDebugEnabled())
430       logger.debug(Translate
431           .get("backend.driver.metadata.getColumnTypeName.ok"));
432
433     try
434     {
435       rsMetaData.getColumnClassName(1);
436       supportGetColumnClassName = true;
437     }
438     catch (Exception JavaDoc e)
439     {
440       logger.warn(Translate.get(
441           "backend.driver.metadata.getColumnClassName.failed", e));
442     }
443     if (supportGetColumnClassName && logger.isDebugEnabled())
444       logger.debug(Translate
445           .get("backend.driver.metadata.getColumnClassName.ok"));
446
447     isCompliant = true;
448     hasBeenTested = true;
449     return isCompliant;
450   }
451
452   /**
453    * @return true if the driver is compliant to the C-JDBC requirements
454    */

455   public boolean isCompliant()
456   {
457     return isCompliant;
458   }
459
460   /**
461    * @return true if the driver supports getGeneratedKeys
462    */

463   public boolean supportGetGeneratedKeys()
464   {
465     return supportGetGeneratedKeys;
466   }
467
468   /**
469    * @return true if the driver supports getColumnClassName
470    */

471   public boolean supportGetColumnClassName()
472   {
473     return supportGetColumnClassName;
474   }
475
476   /**
477    * @return true if the driver supports getColumnCount
478    */

479   public boolean supportGetColumnCount()
480   {
481     return supportGetColumnCount;
482   }
483
484   /**
485    * @return true if the driver supports getColumnDisplaySize
486    */

487   public boolean supportGetColumnDisplaySize()
488   {
489     return supportGetColumnDisplaySize;
490   }
491
492   /**
493    * @return true if the driver supports getColumnType
494    */

495   public boolean supportGetColumnType()
496   {
497     return supportGetColumnType;
498   }
499
500   /**
501    * @return true if the driver supports getColumnTypeName
502    */

503   public boolean supportGetColumnTypeName()
504   {
505     return supportGetColumnTypeName;
506   }
507
508   /**
509    * @return true if the driver supports getTableName
510    */

511   public boolean supportGetTableName()
512   {
513     return supportGetTableName;
514   }
515
516   /**
517    * @return true if the driver supports setQueryTimeout
518    */

519   public boolean supportSetQueryTimeout()
520   {
521     return supportSetQueryTimeout;
522   }
523
524   /**
525    * @return true if the driver supports Statement.setMaxRows
526    */

527   public boolean supportSetMaxRows()
528   {
529     return supportSetMaxRows;
530   }
531
532   /**
533    * Returns the supportSetCursorName value.
534    *
535    * @return Returns the supportSetCursorName.
536    */

537   public boolean supportSetCursorName()
538   {
539     return supportSetCursorName;
540   }
541
542   /**
543    * Returns the supportSetFetchSize value.
544    *
545    * @return Returns the supportSetFetchSize.
546    */

547   public boolean supportSetFetchSize()
548   {
549     return supportSetFetchSize;
550   }
551
552   /**
553    * Returns the databaseProductName value.
554    *
555    * @return Returns the databaseProductName.
556    */

557   public String JavaDoc getDatabaseProductName()
558   {
559     return this.databaseProductName;
560   }
561 }
Popular Tags