Skip to content

Commit 09014b3

Browse files
committed
added fixes.
1 parent 9846ae0 commit 09014b3

11 files changed

+574
-209
lines changed

src/main/java/org/audit4j/handler/db/AuditLogDao.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.sql.SQLException;
2323

2424
import org.audit4j.core.dto.AuditEvent;
25+
import org.audit4j.core.exception.HandlerException;
2526

2627
/**
2728
* The Interface AuditLogDao.
@@ -36,8 +37,9 @@ interface AuditLogDao {
3637
* @param event the event
3738
* @return true, if successful
3839
* @throws SQLException the sQL exception
40+
* @throws HandlerException
3941
*/
40-
boolean writeEvent(AuditEvent event) throws SQLException;
42+
boolean writeEvent(AuditEvent event) throws SQLException, HandlerException;
4143

4244
/**
4345
* Creates the audit table if not exist.

src/main/java/org/audit4j/handler/db/AuditLogDaoImpl.java

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import org.audit4j.core.dto.AuditEvent;
2828
import org.audit4j.core.dto.Field;
29+
import org.audit4j.core.exception.HandlerException;
30+
import org.audit4j.core.exception.InitializationException;
2931

3032
/**
3133
* The Class HSQLAuditLogDao.
@@ -35,10 +37,12 @@
3537
final class AuditLogDaoImpl extends AuditBaseDao implements AuditLogDao {
3638

3739
public static boolean initialized = false;
38-
40+
3941
public static AuditLogDao auditDao;
40-
41-
private AuditLogDaoImpl(){}
42+
43+
private AuditLogDaoImpl() {
44+
}
45+
4246
/*
4347
* (non-Javadoc)
4448
*
@@ -47,43 +51,60 @@ private AuditLogDaoImpl(){}
4751
* dto.AuditEvent)
4852
*/
4953
@Override
50-
public boolean writeEvent(AuditEvent event) throws SQLException {
54+
public boolean writeEvent(AuditEvent event) throws HandlerException {
5155
String uuid;
5256
String timestamp;
5357
StringBuffer elements = new StringBuffer();
54-
58+
5559
if (event.getUuid() == null) {
56-
uuid=String.valueOf(UUID.randomUUID().getMostSignificantBits());
60+
uuid = String.valueOf(UUID.randomUUID().getMostSignificantBits());
5761
} else {
5862
uuid = event.getUuid().toString();
5963
}
60-
64+
6165
if (event.getTimestamp() == null) {
6266
timestamp = new Date().toString();
6367
} else {
6468
timestamp = event.getTimestamp().toString();
6569
}
6670

67-
6871
for (Field element : event.getFields()) {
6972
elements.append(element.getName() + " " + element.getType() + ":" + element.getValue() + ", ");
7073
}
7174
StringBuffer query = new StringBuffer();
7275
query.append("insert into audit(uuid, timestamp, actor, origin, action, elements) ").append(
7376
"values (?, ?, ?, ?, ?, ?)");
7477

75-
PreparedStatement statement = getConnection().prepareStatement(query.toString());
76-
statement.setString(1, uuid);
77-
statement.setString(2, timestamp);
78-
statement.setString(3, event.getActor());
79-
statement.setString(4, event.getOrigin());
80-
statement.setString(5, event.getAction());
81-
statement.setString(6, elements.toString());
82-
return statement.execute();
78+
PreparedStatement statement = null;
79+
try {
80+
statement = getConnection().prepareStatement(query.toString());
81+
statement.setString(1, uuid);
82+
statement.setString(2, timestamp);
83+
statement.setString(3, event.getActor());
84+
statement.setString(4, event.getOrigin());
85+
statement.setString(5, event.getAction());
86+
statement.setString(6, elements.toString());
87+
statement.execute();
88+
} catch (SQLException e) {
89+
throw new HandlerException("SQL Exception", DatabaseAuditHandler.class, e);
90+
} finally {
91+
try {
92+
super.getConnection().close();
93+
} catch (SQLException e) {
94+
throw new HandlerException("SQL Exception", DatabaseAuditHandler.class, e);
95+
} finally {
96+
try {
97+
statement.close();
98+
} catch (SQLException e) {
99+
throw new HandlerException("SQL Exception", DatabaseAuditHandler.class, e);
100+
}
101+
}
102+
}
103+
return true;
83104
}
84105

85106
@Override
86-
public boolean createAuditTableIFNotExist() throws SQLException {
107+
public boolean createAuditTableIFNotExist() {
87108
StringBuffer query = new StringBuffer("create table if not exists audit (");
88109
query.append("uuid varchar(200) NOT NULL,");
89110
query.append("timestamp varchar(100) NOT NULL,");
@@ -93,10 +114,20 @@ public boolean createAuditTableIFNotExist() throws SQLException {
93114
query.append("elements varchar(20000)");
94115
query.append(");");
95116

96-
getConnection().prepareStatement(query.toString()).execute();
117+
try {
118+
getConnection().prepareStatement(query.toString()).execute();
119+
} catch (SQLException e) {
120+
throw new InitializationException("SQL Exception: ", e);
121+
} finally {
122+
try {
123+
super.getConnection().close();
124+
} catch (SQLException e) {
125+
throw new InitializationException("SQL Exception: ", e);
126+
}
127+
}
97128
return true;
98129
}
99-
130+
100131
public static AuditLogDao getInstance() {
101132
if (!initialized) {
102133
synchronized (AuditLogDaoImpl.class) {

0 commit comments

Comments
 (0)