1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
33
34
35
36
37
38
39
40 public class DataSourceConnectionSource extends ConnectionSourceSkeleton
41 implements UnrecognizedElementHandler {
42
43 private DataSource dataSource;
44
45
46 public void activateOptions() {
47
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
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
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 }