Skip to content

Commit 2237bf8

Browse files
committed
Added audit4j Database Handler
1 parent 64896af commit 2237bf8

File tree

8 files changed

+784
-0
lines changed

8 files changed

+784
-0
lines changed

pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.audit4j.handler-db</groupId>
5+
<artifactId>audit4j-handler-db</artifactId>
6+
<packaging>jar</packaging>
7+
<version>2.0-SNAPSHOT</version>
8+
<name>Audit4j - Database Handler</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<properties>
12+
<slf4j.version>1.7.5</slf4j.version>
13+
</properties>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>4.11</version>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.slf4j</groupId>
23+
<artifactId>slf4j-api</artifactId>
24+
<version>1.7.6</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>c3p0</groupId>
28+
<artifactId>c3p0</artifactId>
29+
<version>0.9.1</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.hsqldb</groupId>
33+
<artifactId>hsqldb</artifactId>
34+
<version>2.3.2</version>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-compiler-plugin</artifactId>
43+
</plugin>
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-eclipse-plugin</artifactId>
47+
</plugin>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-jar-plugin</artifactId>
51+
</plugin>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-surefire-plugin</artifactId>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
</project>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright 2014 Janith Bandara, This source is a part of Audit4j -
3+
* An open-source audit platform for Enterprise java platform.
4+
* http://mechanizedspace.com/audit4j
5+
* http://audit4j.org
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package org.audit4j.handler.db;
21+
22+
import java.sql.Connection;
23+
24+
/**
25+
* The Class AuditBaseDao.
26+
*
27+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
28+
*/
29+
class AuditBaseDao {
30+
31+
/**
32+
* Gets the connection.
33+
*
34+
* @return the connection
35+
*/
36+
protected Connection getConnection() {
37+
ConnectionFactory factory = ConnectionFactory.getInstance();
38+
return factory.getConnection();
39+
}
40+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2014 Janith Bandara, This source is a part of Audit4j -
3+
* An open-source audit platform for Enterprise java platform.
4+
* http://mechanizedspace.com/audit4j
5+
* http://audit4j.org
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package org.audit4j.handler.db;
21+
22+
import java.sql.SQLException;
23+
24+
import org.audit4j.core.dto.AuditEvent;
25+
26+
/**
27+
* The Interface AuditLogDao.
28+
*
29+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
30+
*/
31+
interface AuditLogDao {
32+
33+
/**
34+
* Write event.
35+
*
36+
* @param event the event
37+
* @return true, if successful
38+
* @throws SQLException the sQL exception
39+
*/
40+
boolean writeEvent(AuditEvent event) throws SQLException;
41+
42+
/**
43+
* Creates the audit table if not exist.
44+
*
45+
* @return true, if successful
46+
* @throws SQLException the sQL exception
47+
*/
48+
boolean createAuditTableIFNotExist() throws SQLException;
49+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2014 Janith Bandara, This source is a part of Audit4j -
3+
* An open-source audit platform for Enterprise java platform.
4+
* http://mechanizedspace.com/audit4j
5+
* http://audit4j.org
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package org.audit4j.handler.db;
21+
22+
import java.sql.PreparedStatement;
23+
import java.sql.SQLException;
24+
25+
import org.audit4j.core.dto.AuditEvent;
26+
import org.audit4j.core.dto.Element;
27+
28+
/**
29+
* The Class HSQLAuditLogDao.
30+
*
31+
* @author <a href="mailto:[email protected]">Janith Bandara</a>
32+
*/
33+
class AuditLogDaoImpl extends AuditBaseDao implements AuditLogDao {
34+
35+
/*
36+
* (non-Javadoc)
37+
*
38+
* @see
39+
* org.audit4j.core.handler.db.AuditLogDao#createEvent(org.audit4j.core.
40+
* dto.AuditEvent)
41+
*/
42+
@Override
43+
public boolean writeEvent(AuditEvent event) throws SQLException {
44+
45+
StringBuffer elements = new StringBuffer();
46+
47+
for (Element element : event.getEventElements()) {
48+
elements.append(element.getName() + " " + element.getType() + ":" + element.getValue() + ", ");
49+
}
50+
StringBuffer query = new StringBuffer();
51+
query.append("insert into audit(auditId, uuid, timestamp, actor, origin, action, elements) ").append(
52+
"values (?, ?, ?, ?, ?, ?, ?)");
53+
54+
PreparedStatement statement = getConnection().prepareStatement(query.toString());
55+
statement.setInt(1, event.getAuditId());
56+
statement.setString(2, event.getUuid().toString());
57+
statement.setString(3, event.getTimestamp().toString());
58+
statement.setString(4, event.getActor());
59+
statement.setString(5, event.getOrigin());
60+
statement.setString(6, event.getAction());
61+
statement.setString(7, elements.toString());
62+
return statement.execute();
63+
}
64+
65+
@Override
66+
public boolean createAuditTableIFNotExist() throws SQLException {
67+
StringBuffer query = new StringBuffer("create table if not exists audit (");
68+
query.append("auditId INT NOT NULL,");
69+
query.append("uuid char(60) NOT NULL,");
70+
query.append("timestamp varchar(100) NOT NULL,");
71+
query.append("actor varchar(200) NOT NULL,");
72+
query.append("origin varchar(200),");
73+
query.append("action varchar(200) NOT NULL,");
74+
query.append("elements varchar(20000)");
75+
query.append(");");
76+
77+
getConnection().prepareStatement(query.toString()).execute();
78+
return true;
79+
}
80+
81+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright 2014 Janith Bandara, This source is a part of Audit4j -
3+
* An open-source audit platform for Enterprise java platform.
4+
* http://mechanizedspace.com/audit4j
5+
* http://audit4j.org
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package org.audit4j.handler.db;
21+
22+
import java.beans.PropertyVetoException;
23+
import java.sql.Connection;
24+
import java.sql.DriverManager;
25+
import java.sql.SQLException;
26+
27+
import org.audit4j.core.exception.InitializationException;
28+
29+
import com.mchange.v2.c3p0.ComboPooledDataSource;
30+
31+
/**
32+
* A factory for creating Connection objects.
33+
*/
34+
final class ConnectionFactory {
35+
36+
static final String SINGLE_CONNECTION = "single";
37+
38+
static final String POOLED_CONNECTION = "pooled";
39+
40+
/** The db_driver. */
41+
private String driver = "org.hsqldb.jdbcDriver";
42+
43+
/** The db_url. */
44+
private String url = "jdbc:hsqldb:hsql://localhost/audit4j";
45+
46+
/** The db_user. */
47+
private String user = "audit4juser";
48+
49+
/** The db_password. */
50+
private String password = "audit4jpassword";
51+
52+
53+
private String connectionType;
54+
55+
56+
/**
57+
* Instantiates a new connection factory.
58+
*/
59+
private ConnectionFactory() {
60+
}
61+
62+
/** The instance. */
63+
private static ConnectionFactory instance;
64+
65+
private static ComboPooledDataSource cpds;
66+
67+
/**
68+
* Gets the connection.
69+
*
70+
* @return the connection
71+
*
72+
* @deprecated please use getPooledConnection() instead.
73+
*/
74+
@Deprecated
75+
Connection getSingleConnection() {
76+
Connection connection = null;
77+
try {
78+
Class.forName(driver);
79+
connection = DriverManager.getConnection(url, user, password);
80+
} catch (SQLException e) {
81+
throw new InitializationException("Could not initialize the connection", e);
82+
} catch (ClassNotFoundException e) {
83+
throw new InitializationException("Could not find the driver class", e);
84+
}
85+
return connection;
86+
}
87+
88+
Connection getConnection() {
89+
if (SINGLE_CONNECTION.equals(connectionType)) {
90+
return getSingleConnection();
91+
} else {
92+
try {
93+
return cpds.getConnection();
94+
} catch (SQLException e) {
95+
throw new InitializationException("Could not obtain the db connection", e);
96+
}
97+
}
98+
}
99+
100+
101+
102+
/**
103+
* Inits the.
104+
*
105+
* @return true, if successful
106+
* @throws SQLException
107+
*/
108+
boolean init() {
109+
try {
110+
cpds = new ComboPooledDataSource();
111+
cpds.setDriverClass(this.driver);
112+
113+
cpds.setJdbcUrl(this.url);
114+
cpds.setUser(this.user);
115+
cpds.setPassword(this.password);
116+
117+
} catch (PropertyVetoException e) {
118+
throw new InitializationException("Couldn't initialize c3p0 object pool", e);
119+
}
120+
return Boolean.TRUE;
121+
}
122+
123+
void setDriver(String driver) {
124+
this.driver = driver;
125+
}
126+
127+
void setUrl(String url) {
128+
this.url = url;
129+
}
130+
131+
void setUser(String user) {
132+
this.user = user;
133+
}
134+
135+
void setPassword(String password) {
136+
this.password = password;
137+
}
138+
139+
void setConnectionType(String connectionType) {
140+
this.connectionType = connectionType;
141+
}
142+
143+
/**
144+
* Gets the single instance of ConnectionFactory.
145+
*
146+
* @return single instance of ConnectionFactory
147+
*/
148+
static ConnectionFactory getInstance() {
149+
if (instance == null) {
150+
instance = new ConnectionFactory();
151+
}
152+
return instance;
153+
}
154+
}

0 commit comments

Comments
 (0)