Skip to content

Commit 0540752

Browse files
committed
importing DBAppender from logback version 1.2.7
Signed-off-by: Ceki Gulcu <[email protected]>
0 parents  commit 0540752

24 files changed

+1664
-0
lines changed

.gitignore

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.settings
2+
target
3+
.classpath
4+
.factorypath
5+
.project
6+
.idea
7+
*~
8+
*.iml
9+
*.ipr
10+
*.iws
11+
cobertura.ser
12+
# ignore files with pound characters
13+
\#*\#

logback-core-db/pom.xml

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
8+
<parent>
9+
<groupId>ch.qos.logback.db</groupId>
10+
<artifactId>logback-db-parent</artifactId>
11+
<version>1.2.7</version>
12+
</parent>
13+
14+
<artifactId>logback-db-core</artifactId>
15+
<packaging>jar</packaging>
16+
<name>Logback DBAppender Core Module</name>
17+
<description>logback-db-core module</description>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>ch.qos.logback</groupId>
22+
<artifactId>logback-core</artifactId>
23+
</dependency>
24+
25+
</dependencies>
26+
27+
<build>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-surefire-plugin</artifactId>
32+
<configuration>
33+
<forkCount>1C</forkCount>
34+
<reuseForks>true</reuseForks>
35+
<parallel>classes</parallel>
36+
<threadCount>20</threadCount>
37+
<reportFormat>plain</reportFormat>
38+
<trimStackTrace>false</trimStackTrace>
39+
<excludes>
40+
<exclude>**/All*Test.java</exclude>
41+
<exclude>**/PackageTest.java</exclude>
42+
<!-- ConsoleAppenderTest redirects System.out which is not well tolerated by Maven -->
43+
<exclude>**/ConsoleAppenderTest.java</exclude>
44+
<!--<exclude>**/TimeBasedRollingTest.java</exclude>-->
45+
</excludes>
46+
</configuration>
47+
</plugin>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-jar-plugin</artifactId>
51+
<configuration>
52+
<archive>
53+
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
54+
</archive>
55+
</configuration>
56+
<executions>
57+
<execution>
58+
<id>bundle-test-jar</id>
59+
<phase>package</phase>
60+
<goals>
61+
<goal>jar</goal>
62+
<goal>test-jar</goal>
63+
</goals>
64+
</execution>
65+
</executions>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
70+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4+
*
5+
* This program and the accompanying materials are dual-licensed under
6+
* either the terms of the Eclipse Public License v1.0 as published by
7+
* the Eclipse Foundation
8+
*
9+
* or (per the licensee's choosing)
10+
*
11+
* under the terms of the GNU Lesser General Public License version 2.1
12+
* as published by the Free Software Foundation.
13+
*/
14+
package ch.qos.logback.core.db;
15+
16+
import javax.naming.Context;
17+
import javax.naming.InitialContext;
18+
import javax.sql.DataSource;
19+
20+
import org.xml.sax.Attributes;
21+
22+
import ch.qos.logback.core.joran.action.Action;
23+
import ch.qos.logback.core.joran.spi.InterpretationContext;
24+
import ch.qos.logback.core.joran.util.PropertySetter;
25+
import ch.qos.logback.core.joran.util.beans.BeanDescriptionCache;
26+
import ch.qos.logback.core.util.OptionHelper;
27+
28+
/**
29+
*
30+
* @author Ceki Gulcu
31+
*
32+
*/
33+
public class BindDataSourceToJNDIAction extends Action {
34+
35+
static final String DATA_SOURCE_CLASS = "dataSourceClass";
36+
static final String URL = "url";
37+
static final String USER = "user";
38+
static final String PASSWORD = "password";
39+
private final BeanDescriptionCache beanDescriptionCache;
40+
41+
public BindDataSourceToJNDIAction(BeanDescriptionCache beanDescriptionCache) {
42+
this.beanDescriptionCache = beanDescriptionCache;
43+
}
44+
45+
/**
46+
* Instantiates an a data source and bind it to JNDI
47+
* Most of the required parameters are placed in the ec.substitutionProperties
48+
*/
49+
public void begin(InterpretationContext ec, String localName, Attributes attributes) {
50+
String dsClassName = ec.getProperty(DATA_SOURCE_CLASS);
51+
52+
if (OptionHelper.isEmpty(dsClassName)) {
53+
addWarn("dsClassName is a required parameter");
54+
ec.addError("dsClassName is a required parameter");
55+
56+
return;
57+
}
58+
59+
String urlStr = ec.getProperty(URL);
60+
String userStr = ec.getProperty(USER);
61+
String passwordStr = ec.getProperty(PASSWORD);
62+
63+
try {
64+
DataSource ds = (DataSource) OptionHelper.instantiateByClassName(dsClassName, DataSource.class, context);
65+
66+
PropertySetter setter = new PropertySetter(beanDescriptionCache,ds);
67+
setter.setContext(context);
68+
69+
if (!OptionHelper.isEmpty(urlStr)) {
70+
setter.setProperty("url", urlStr);
71+
}
72+
73+
if (!OptionHelper.isEmpty(userStr)) {
74+
setter.setProperty("user", userStr);
75+
}
76+
77+
if (!OptionHelper.isEmpty(passwordStr)) {
78+
setter.setProperty("password", passwordStr);
79+
}
80+
81+
Context ctx = new InitialContext();
82+
ctx.rebind("dataSource", ds);
83+
} catch (Exception oops) {
84+
addError("Could not bind datasource. Reported error follows.", oops);
85+
ec.addError("Could not not bind datasource of type [" + dsClassName + "].");
86+
}
87+
}
88+
89+
public void end(InterpretationContext ec, String name) {
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4+
*
5+
* This program and the accompanying materials are dual-licensed under
6+
* either the terms of the Eclipse Public License v1.0 as published by
7+
* the Eclipse Foundation
8+
*
9+
* or (per the licensee's choosing)
10+
*
11+
* under the terms of the GNU Lesser General Public License version 2.1
12+
* as published by the Free Software Foundation.
13+
*/
14+
package ch.qos.logback.core.db;
15+
16+
import java.sql.Connection;
17+
import java.sql.SQLException;
18+
19+
import ch.qos.logback.core.db.dialect.SQLDialectCode;
20+
import ch.qos.logback.core.spi.LifeCycle;
21+
22+
/**
23+
* The <id>ConnectionSource</id> interface provides a pluggable means of
24+
* transparently obtaining JDBC {@link java.sql.Connection}s for logback classes
25+
* that require the use of a {@link java.sql.Connection}.
26+
*
27+
* For more information about this component, please refer to the online manual at
28+
* http://logback.qos.ch/manual/appenders.html#DBAppender
29+
*
30+
* @author <a href="mailto:[email protected]">Ray DeCampo</a>
31+
*/
32+
public interface ConnectionSource extends LifeCycle {
33+
34+
/**
35+
* Obtain a {@link java.sql.Connection} for use. The client is
36+
* responsible for closing the {@link java.sql.Connection} when it is no
37+
* longer required.
38+
*
39+
* @throws SQLException if a {@link java.sql.Connection} could not be
40+
* obtained
41+
*/
42+
Connection getConnection() throws SQLException;
43+
44+
/**
45+
* Get the SQL dialect that should be used for this connection. Note that the
46+
* dialect is not needed if the JDBC driver supports the getGeneratedKeys
47+
* method.
48+
*/
49+
SQLDialectCode getSQLDialectCode();
50+
51+
/**
52+
* If the connection supports the JDBC 3.0 getGeneratedKeys method, then
53+
* we do not need any specific dialect support.
54+
*/
55+
boolean supportsGetGeneratedKeys();
56+
57+
/**
58+
* If the connection does not support batch updates, we will avoid using them.
59+
*/
60+
boolean supportsBatchUpdates();
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/**
2+
* Logback: the reliable, generic, fast and flexible logging framework.
3+
* Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4+
*
5+
* This program and the accompanying materials are dual-licensed under
6+
* either the terms of the Eclipse Public License v1.0 as published by
7+
* the Eclipse Foundation
8+
*
9+
* or (per the licensee's choosing)
10+
*
11+
* under the terms of the GNU Lesser General Public License version 2.1
12+
* as published by the Free Software Foundation.
13+
*/
14+
package ch.qos.logback.core.db;
15+
16+
import java.sql.Connection;
17+
import java.sql.DatabaseMetaData;
18+
import java.sql.SQLException;
19+
20+
import ch.qos.logback.core.db.dialect.DBUtil;
21+
import ch.qos.logback.core.db.dialect.SQLDialectCode;
22+
import ch.qos.logback.core.spi.ContextAwareBase;
23+
24+
/**
25+
* @author Ceki G&uuml;lc&uuml;
26+
*/
27+
public abstract class ConnectionSourceBase extends ContextAwareBase implements ConnectionSource {
28+
29+
private boolean started;
30+
31+
private String user = null;
32+
private String password = null;
33+
34+
// initially we have an unknown dialect
35+
private SQLDialectCode dialectCode = SQLDialectCode.UNKNOWN_DIALECT;
36+
private boolean supportsGetGeneratedKeys = false;
37+
private boolean supportsBatchUpdates = false;
38+
39+
/**
40+
* Learn relevant information about this connection source.
41+
*
42+
*/
43+
public void discoverConnectionProperties() {
44+
Connection connection = null;
45+
try {
46+
connection = getConnection();
47+
if (connection == null) {
48+
addWarn("Could not get a connection");
49+
return;
50+
}
51+
DatabaseMetaData meta = connection.getMetaData();
52+
DBUtil util = new DBUtil();
53+
util.setContext(getContext());
54+
supportsGetGeneratedKeys = util.supportsGetGeneratedKeys(meta);
55+
supportsBatchUpdates = util.supportsBatchUpdates(meta);
56+
dialectCode = DBUtil.discoverSQLDialect(meta);
57+
addInfo("Driver name=" + meta.getDriverName());
58+
addInfo("Driver version=" + meta.getDriverVersion());
59+
addInfo("supportsGetGeneratedKeys=" + supportsGetGeneratedKeys);
60+
61+
} catch (SQLException se) {
62+
addWarn("Could not discover the dialect to use.", se);
63+
} finally {
64+
DBHelper.closeConnection(connection);
65+
}
66+
}
67+
68+
/**
69+
* Does this connection support the JDBC Connection.getGeneratedKeys method?
70+
*/
71+
public final boolean supportsGetGeneratedKeys() {
72+
return supportsGetGeneratedKeys;
73+
}
74+
75+
public final SQLDialectCode getSQLDialectCode() {
76+
return dialectCode;
77+
}
78+
79+
/**
80+
* Get the password for this connection source.
81+
*/
82+
public final String getPassword() {
83+
return password;
84+
}
85+
86+
/**
87+
* Sets the password.
88+
* @param password The password to set
89+
*/
90+
public final void setPassword(final String password) {
91+
this.password = password;
92+
}
93+
94+
/**
95+
* Get the user for this connection source.
96+
*/
97+
public final String getUser() {
98+
return user;
99+
}
100+
101+
/**
102+
* Sets the username.
103+
* @param username The username to set
104+
*/
105+
public final void setUser(final String username) {
106+
this.user = username;
107+
}
108+
109+
/**
110+
* Does this connection support batch updates?
111+
*/
112+
public final boolean supportsBatchUpdates() {
113+
return supportsBatchUpdates;
114+
}
115+
116+
public boolean isStarted() {
117+
return started;
118+
}
119+
120+
public void start() {
121+
started = true;
122+
}
123+
124+
public void stop() {
125+
started = false;
126+
}
127+
128+
}

0 commit comments

Comments
 (0)