Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down Expand Up @@ -153,6 +155,40 @@
<scope>test</scope>
</dependency>

<!-- Arguments given for element JDBC are invalid: field 'connectionSource' has invalid value 'null' &lt;!&ndash; https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-jdbc-dbcp2 &ndash;&gt;-->
<!-- <dependency>-->
<!-- <groupId>org.apache.logging.log4j</groupId>-->
<!-- <artifactId>log4j-jdbc-dbcp2</artifactId>-->
<!-- <version>2.12.0</version>-->
<!-- </dependency>-->

<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4.1212</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 -->
<!-- <dependency>-->
<!-- <groupId>org.apache.commons</groupId>-->
<!-- <artifactId>commons-dbcp2</artifactId>-->
<!-- <version>2.6.0</version>-->
<!-- </dependency>-->
<!-- -->



</dependencies>

Expand Down
49 changes: 49 additions & 0 deletions src/main/java/io/jenkins/plugins/audit/db/ConnectionFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.jenkins.plugins.audit.db;


import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.DriverManagerConnectionFactory;
import org.apache.commons.dbcp.PoolableConnection;
import org.apache.commons.dbcp.PoolableConnectionFactory;
import org.apache.commons.dbcp.PoolingDataSource;
import org.apache.commons.pool.impl.GenericObjectPool;


public class ConnectionFactory {

public static interface Singleton {
final ConnectionFactory INSTANCE = new ConnectionFactory();
}

private final DataSource dataSource;

private ConnectionFactory() {

try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
System.exit(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely don't want this anywhere.

}

Properties properties = new Properties();
properties.setProperty("user", "aarthi");
properties.setProperty("password", "postgres");

GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<PoolableConnection>();
DriverManagerConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
"jdbc:postgresql://localhost:5432/audit_logs", properties
);
new PoolableConnectionFactory(connectionFactory, pool, null, "SELECT 1", 100, false, false, Connection.TRANSACTION_READ_COMMITTED);
this.dataSource = new PoolingDataSource(pool);
}

public static Connection getDatabaseConnection() throws SQLException {
return Singleton.INSTANCE.dataSource.getConnection();
}
}
10 changes: 10 additions & 0 deletions src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@
<SizeBasedTriggeringPolicy size="20 MB"/>
</Policies>
</RollingRandomAccessFile>
<JDBC name="Db_appender" tableName="jenkins_log">
<ConnectionFactory
class="io.jenkins.plugins.audit.db.ConnectionFactory" method="getDatabaseConnection" />
<Column name="LOG_ID" pattern="%u" isUnicode="false"/>
<Column name="ENTRY_DATE" isEventTimestamp="true" />
<Column name="LOGGER" pattern="%logger" isUnicode="false"/>
<Column name="LOG_LEVEL" pattern="%level" isUnicode="false"/>
<Column name="MESSAGE" pattern="%m" isUnicode="false" />
</JDBC>
</Appenders>
<Loggers>
<Logger name="AuditLogger" level="trace" additivity="false">
<AppenderRef ref="audit"/>
<AppenderRef ref="Db_appender"/>
</Logger>
</Loggers>
</Configuration>