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 java.sql.Connection;
21  import java.sql.DatabaseMetaData;
22  import java.sql.SQLException;
23  
24  import org.apache.log4j.db.dialect.Util;
25  import org.apache.log4j.spi.ComponentBase;
26  
27  
28  /**
29   * @author Ceki Gülcü
30   */
31  public abstract class ConnectionSourceSkeleton extends ComponentBase implements ConnectionSource {
32    
33    private Boolean overriddenSupportsGetGeneratedKeys = null;
34    
35    private String user = null;
36    private String password = null;
37  
38    // initially we have an unkonw dialect
39    private int dialectCode = UNKNOWN_DIALECT;
40    private boolean supportsGetGeneratedKeys = false;
41    private boolean supportsBatchUpdates = false;
42  
43  
44    /**
45     * Learn relevant information about this connection source.
46     *
47     */
48    public void discoverConnnectionProperties() {
49      Connection connection = null;
50      try {
51        connection = getConnection();
52        if (connection == null) {
53          getLogger().warn("Could not get a conneciton");
54          return;
55        }
56        DatabaseMetaData meta = connection.getMetaData();
57        Util util = new Util();
58        util.setLoggerRepository(repository);
59        if (overriddenSupportsGetGeneratedKeys != null) {
60          supportsGetGeneratedKeys = overriddenSupportsGetGeneratedKeys
61              .booleanValue();
62        } else {
63          supportsGetGeneratedKeys = util.supportsGetGeneratedKeys(meta);
64        }
65        supportsBatchUpdates = util.supportsBatchUpdates(meta);
66        dialectCode = Util.discoverSQLDialect(meta);
67      } catch (SQLException se) {
68        getLogger().warn("Could not discover the dialect to use.", se);
69      } finally {
70        DBHelper.closeConnection(connection);
71      }
72    }
73  
74    /**
75     * Does this connection support the JDBC Connection.getGeneratedKeys method?
76     */
77    public final boolean supportsGetGeneratedKeys() {
78      return supportsGetGeneratedKeys;
79    }
80  
81    public final int getSQLDialectCode() {
82      return dialectCode;
83    }
84  
85    /**
86     * Get the password for this connection source.
87     */
88    public final String getPassword() {
89      return password;
90    }
91  
92    /**
93     * Sets the password.
94     * @param password The password to set
95     */
96    public final void setPassword(final String password) {
97      this.password = password;
98    }
99  
100   /**
101    * Get the user for this connection source.
102    */
103   public final String getUser() {
104     return user;
105   }
106 
107   /**
108    * Sets the username.
109    * @param username The username to set
110    */
111   public final void setUser(final String username) {
112     this.user = username;
113   }
114 
115   /**
116    * Returns the "overridden" value of "supportsGetGeneratedKeys" property of
117    * the JDBC driver. In certain cases, getting (e.g. Oracle 10g) generated keys
118    * does not work because it returns the ROWID, not the value of the sequence.
119    * 
120    * @return A non null string, with "true" or "false" value, if overridden,
121    *         <code>null</code> if not overridden.
122    */
123   public String getOverriddenSupportsGetGeneratedKeys() {
124     return overriddenSupportsGetGeneratedKeys != null ? overriddenSupportsGetGeneratedKeys
125         .toString()
126         : null;
127   }
128 
129   /**
130    * Sets the "overridden" value of "supportsGetGeneratedKeys" property of the
131    * JDBC driver. In certain cases, getting (e.g. Oracle 10g) generated keys
132    * does not work because it returns the ROWID, not the value of the sequence.
133    * 
134    * @param overriddenSupportsGetGeneratedKeys
135    *          A non null string, with "true" or "false" value, if overridden,
136    *          <code>null</code> if not overridden.
137    */
138   public void setOverriddenSupportsGetGeneratedKeys(
139       String overriddenSupportsGetGeneratedKeys) {
140     this.overriddenSupportsGetGeneratedKeys = Boolean
141         .valueOf(overriddenSupportsGetGeneratedKeys);
142   }
143   
144   /**
145    * Does this connection support batch updates?
146    */
147   public final boolean supportsBatchUpdates() {
148     return supportsBatchUpdates;
149   }
150 }