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  
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      public void discoverConnnectionProperties() {
48          Connection connection = null;
49          try {
50              connection = getConnection();
51              if (connection == null) {
52                  getLogger().warn("Could not get a conneciton");
53                  return;
54              }
55              DatabaseMetaData meta = connection.getMetaData();
56              Util util = new Util();
57              util.setLoggerRepository(repository);
58              if (overriddenSupportsGetGeneratedKeys != null) {
59                  supportsGetGeneratedKeys = overriddenSupportsGetGeneratedKeys;
60              } else {
61                  supportsGetGeneratedKeys = util.supportsGetGeneratedKeys(meta);
62              }
63              supportsBatchUpdates = util.supportsBatchUpdates(meta);
64              dialectCode = Util.discoverSQLDialect(meta);
65          } catch (SQLException se) {
66              getLogger().warn("Could not discover the dialect to use.", se);
67          } finally {
68              DBHelper.closeConnection(connection);
69          }
70      }
71  
72      /**
73       * Does this connection support the JDBC Connection.getGeneratedKeys method?
74       */
75      public final boolean supportsGetGeneratedKeys() {
76          return supportsGetGeneratedKeys;
77      }
78  
79      public final int getSQLDialectCode() {
80          return dialectCode;
81      }
82  
83      /**
84       * Get the password for this connection source.
85       */
86      public final String getPassword() {
87          return password;
88      }
89  
90      /**
91       * Sets the password.
92       *
93       * @param password The password to set
94       */
95      public final void setPassword(final String password) {
96          this.password = password;
97      }
98  
99      /**
100      * Get the user for this connection source.
101      */
102     public final String getUser() {
103         return user;
104     }
105 
106     /**
107      * Sets the username.
108      *
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 A non null string, with "true" or "false" value, if overridden,
135      *                                           <code>null</code> if not overridden.
136      */
137     public void setOverriddenSupportsGetGeneratedKeys(
138         String overriddenSupportsGetGeneratedKeys) {
139         this.overriddenSupportsGetGeneratedKeys = Boolean
140             .valueOf(overriddenSupportsGetGeneratedKeys);
141     }
142 
143     /**
144      * Does this connection support batch updates?
145      */
146     public final boolean supportsBatchUpdates() {
147         return supportsBatchUpdates;
148     }
149 }