View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, 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  /**
26   * @author Ceki Gülcü
27   */
28  public abstract class ConnectionSourceBase extends ContextAwareBase implements ConnectionSource {
29    
30    private boolean started;
31    
32    private String user = null;
33    private String password = null;
34  
35    // initially we have an unkonw dialect
36    private SQLDialectCode dialectCode = SQLDialectCode.UNKNOWN_DIALECT;
37    private boolean supportsGetGeneratedKeys = false;
38    private boolean supportsBatchUpdates = false;
39  
40  
41    /**
42     * Learn relevant information about this connection source.
43     *
44     */
45    public void discoverConnnectionProperties() {
46      try {
47        Connection connection = getConnection();
48        if (connection == null) {
49          addWarn("Could not get a connection");
50          return;
51        }
52        DatabaseMetaData meta = connection.getMetaData();
53        DBUtil util = new DBUtil();
54        util.setContext(getContext());
55        supportsGetGeneratedKeys = util.supportsGetGeneratedKeys(meta);
56        supportsBatchUpdates = util.supportsBatchUpdates(meta);
57        dialectCode = DBUtil.discoverSQLDialect(meta);
58        addInfo("Driver name="+meta.getDriverName());
59        addInfo("Driver version="+meta.getDriverVersion());
60        addInfo("supportsGetGeneratedKeys="+supportsGetGeneratedKeys);
61        
62      } catch (SQLException se) {
63        addWarn("Could not discover the dialect to use.", se);
64      }
65    }
66  
67    /**
68     * Does this connection support the JDBC Connection.getGeneratedKeys method?
69     */
70    public final boolean supportsGetGeneratedKeys() {
71      return supportsGetGeneratedKeys;
72    }
73  
74    public final SQLDialectCode getSQLDialectCode() {
75      return dialectCode;
76    }
77  
78    /**
79     * Get the password for this connection source.
80     */
81    public final String getPassword() {
82      return password;
83    }
84  
85    /**
86     * Sets the password.
87     * @param password The password to set
88     */
89    public final void setPassword(final String password) {
90      this.password = password;
91    }
92  
93    /**
94     * Get the user for this connection source.
95     */
96    public final String getUser() {
97      return user;
98    }
99  
100   /**
101    * Sets the username.
102    * @param username The username to set
103    */
104   public final void setUser(final String username) {
105     this.user = username;
106   }
107 
108   /**
109    * Does this connection support batch updates?
110    */
111   public final boolean supportsBatchUpdates() {
112     return supportsBatchUpdates;
113   }
114 
115   public boolean isStarted() {
116     return started;
117   }
118 
119   public void start() {
120     started = true;
121   }
122 
123   public void stop() {
124     started = false;
125   }
126   
127   
128 }