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  
21  import org.apache.log4j.xml.DOMConfigurator;
22  import org.apache.log4j.xml.UnrecognizedElementHandler;
23  import org.w3c.dom.Element;
24  
25  import javax.sql.DataSource;
26  import java.sql.Connection;
27  import java.sql.SQLException;
28  import java.util.Properties;
29  
30  
31  /**
32   *  The DataSourceConnectionSource is an implementation of {@link ConnectionSource}
33   *  that obtains the Connection in the recommended JDBC manner based on
34   *  a {@link javax.sql.DataSource DataSource}.
35   *  <p>
36   *
37   *  @author Ray DeCampo
38   *  @author Ceki G&uuml;lc&uuml;
39   */
40  public class DataSourceConnectionSource extends ConnectionSourceSkeleton
41          implements UnrecognizedElementHandler {
42  
43    private DataSource dataSource;
44  
45    
46    public void activateOptions() {
47      //LogLog.debug("**********DataSourceConnectionSource.activateOptions called");
48      if (dataSource == null) {
49        getLogger().warn("WARNING: No data source specified");
50      } else {
51        Connection connection = null;
52        try {
53          connection = getConnection();
54        } catch(SQLException se) {
55          getLogger().warn("Could not get a connection to discover the dialect to use.", se);
56        }
57        if(connection != null) {
58          discoverConnnectionProperties();
59        } 
60        if(!supportsGetGeneratedKeys() && getSQLDialectCode() == ConnectionSource.UNKNOWN_DIALECT) {
61          getLogger().warn("Connection does not support GetGeneratedKey method and could not discover the dialect.");
62        }
63      }
64    }
65  
66    /**
67     * @see org.apache.log4j.db.ConnectionSource#getConnection()
68     */
69    public Connection getConnection() throws SQLException {
70      if (dataSource == null) {
71        getLogger().error("WARNING: No data source specified");
72        return null;
73      }
74  
75      if (getUser() == null) {
76        return dataSource.getConnection();
77      } else {
78        return dataSource.getConnection(getUser(), getPassword());
79      }
80    }
81  
82    public DataSource getDataSource() {
83      return dataSource;
84    }
85  
86    public void setDataSource(DataSource dataSource) {
87      this.dataSource = dataSource;
88    }
89  
90      /**
91       * {@inheritDoc}
92       */
93    public boolean parseUnrecognizedElement(Element element, Properties props) throws Exception {
94          if ("dataSource".equals(element.getNodeName())) {
95              Object instance =
96                      DOMConfigurator.parseElement(element, props, DataSource.class);
97              if (instance instanceof DataSource) {
98                 setDataSource((DataSource) instance);
99              }
100             return true;
101         }
102         return false;
103   }
104 
105 }