View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.log4j.db;
19  
20  import org.apache.log4j.db.dialect.Util;
21  import org.apache.log4j.spi.ComponentBase;
22  
23  import java.sql.Connection;
24  import java.sql.DatabaseMetaData;
25  import java.sql.SQLException;
26  
27  
28  /***
29   * @author Ceki Gülcü
30   */
31  public abstract class ConnectionSourceSkeleton extends ComponentBase implements ConnectionSource {
32    private String user = null;
33    private String password = null;
34  
35    // initially we have an unkonw dialect
36    private int dialectCode = 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          getLogger().warn("Could not get a conneciton");
50          return;
51        }
52        DatabaseMetaData meta = connection.getMetaData();
53        Util util = new Util();
54        util.setLoggerRepository(repository);
55        supportsGetGeneratedKeys = util.supportsGetGeneratedKeys(meta);
56        supportsBatchUpdates = util.supportsBatchUpdates(meta);
57        dialectCode = Util.discoverSQLDialect(meta);
58      } catch (SQLException se) {
59        getLogger().warn("Could not discover the dialect to use.", se);
60      }
61    }
62  
63    /***
64     * Does this connection support the JDBC Connection.getGeneratedKeys method?
65     */
66    public final boolean supportsGetGeneratedKeys() {
67      return supportsGetGeneratedKeys;
68    }
69  
70    public final int getSQLDialectCode() {
71      return dialectCode;
72    }
73  
74    /***
75     * Get the password for this connection source.
76     */
77    public final String getPassword() {
78      return password;
79    }
80  
81    /***
82     * Sets the password.
83     * @param password The password to set
84     */
85    public final void setPassword(final String password) {
86      this.password = password;
87    }
88  
89    /***
90     * Get the user for this connection source.
91     */
92    public final String getUser() {
93      return user;
94    }
95  
96    /***
97     * Sets the username.
98     * @param username The username to set
99     */
100   public final void setUser(final String username) {
101     this.user = username;
102   }
103 
104   /***
105    * Does this connection support batch updates?
106    */
107   public final boolean supportsBatchUpdates() {
108     return supportsBatchUpdates;
109   }
110 }