View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.db;
15  
16  import java.sql.Connection;
17  import java.sql.DatabaseMetaData;
18  import java.sql.SQLException;
19  
20  import ch.qos.logback.core.db.dialect.DBUtil;
21  import ch.qos.logback.core.db.dialect.SQLDialectCode;
22  import ch.qos.logback.core.spi.ContextAwareBase;
23  
24  /**
25   * @author Ceki Gülcü
26   */
27  public abstract class ConnectionSourceBase extends ContextAwareBase implements ConnectionSource {
28  
29      private boolean started;
30  
31      private String user = null;
32      private String password = null;
33  
34      // initially we have an unknown dialect
35      private SQLDialectCode dialectCode = SQLDialectCode.UNKNOWN_DIALECT;
36      private boolean supportsGetGeneratedKeys = false;
37      private boolean supportsBatchUpdates = false;
38  
39      /**
40       * Learn relevant information about this connection source.
41       *
42       */
43      public void discoverConnectionProperties() {
44          Connection connection = null;
45          try {
46              connection = getConnection();
47              if (connection == null) {
48                  addWarn("Could not get a connection");
49                  return;
50              }
51              DatabaseMetaData meta = connection.getMetaData();
52              DBUtil util = new DBUtil();
53              util.setContext(getContext());
54              supportsGetGeneratedKeys = util.supportsGetGeneratedKeys(meta);
55              supportsBatchUpdates = util.supportsBatchUpdates(meta);
56              dialectCode = DBUtil.discoverSQLDialect(meta);
57              addInfo("Driver name=" + meta.getDriverName());
58              addInfo("Driver version=" + meta.getDriverVersion());
59              addInfo("supportsGetGeneratedKeys=" + supportsGetGeneratedKeys);
60  
61          } catch (SQLException se) {
62              addWarn("Could not discover the dialect to use.", se);
63          } finally {
64              DBHelper.closeConnection(connection);
65          }
66      }
67  
68      /**
69       * Does this connection support the JDBC Connection.getGeneratedKeys method?
70       */
71      public final boolean supportsGetGeneratedKeys() {
72          return supportsGetGeneratedKeys;
73      }
74  
75      public final SQLDialectCode getSQLDialectCode() {
76          return dialectCode;
77      }
78  
79      /**
80       * Get the password for this connection source.
81       */
82      public final String getPassword() {
83          return password;
84      }
85  
86      /**
87       * Sets the password.
88       * @param password The password to set
89       */
90      public final void setPassword(final String password) {
91          this.password = password;
92      }
93  
94      /**
95       * Get the user for this connection source.
96       */
97      public final String getUser() {
98          return user;
99      }
100 
101     /**
102      * Sets the username.
103      * @param username The username to set
104      */
105     public final void setUser(final String username) {
106         this.user = username;
107     }
108 
109     /**
110      * Does this connection support batch updates?
111      */
112     public final boolean supportsBatchUpdates() {
113         return supportsBatchUpdates;
114     }
115 
116     public boolean isStarted() {
117         return started;
118     }
119 
120     public void start() {
121         started = true;
122     }
123 
124     public void stop() {
125         started = false;
126     }
127 
128 }