26
26
27
27
import org .audit4j .core .dto .AuditEvent ;
28
28
import org .audit4j .core .dto .Field ;
29
+ import org .audit4j .core .exception .HandlerException ;
30
+ import org .audit4j .core .exception .InitializationException ;
29
31
30
32
/**
31
33
* The Class HSQLAuditLogDao.
35
37
final class AuditLogDaoImpl extends AuditBaseDao implements AuditLogDao {
36
38
37
39
public static boolean initialized = false ;
38
-
40
+
39
41
public static AuditLogDao auditDao ;
40
-
41
- private AuditLogDaoImpl (){}
42
+
43
+ private AuditLogDaoImpl () {
44
+ }
45
+
42
46
/*
43
47
* (non-Javadoc)
44
48
*
@@ -47,43 +51,60 @@ private AuditLogDaoImpl(){}
47
51
* dto.AuditEvent)
48
52
*/
49
53
@ Override
50
- public boolean writeEvent (AuditEvent event ) throws SQLException {
54
+ public boolean writeEvent (AuditEvent event ) throws HandlerException {
51
55
String uuid ;
52
56
String timestamp ;
53
57
StringBuffer elements = new StringBuffer ();
54
-
58
+
55
59
if (event .getUuid () == null ) {
56
- uuid = String .valueOf (UUID .randomUUID ().getMostSignificantBits ());
60
+ uuid = String .valueOf (UUID .randomUUID ().getMostSignificantBits ());
57
61
} else {
58
62
uuid = event .getUuid ().toString ();
59
63
}
60
-
64
+
61
65
if (event .getTimestamp () == null ) {
62
66
timestamp = new Date ().toString ();
63
67
} else {
64
68
timestamp = event .getTimestamp ().toString ();
65
69
}
66
70
67
-
68
71
for (Field element : event .getFields ()) {
69
72
elements .append (element .getName () + " " + element .getType () + ":" + element .getValue () + ", " );
70
73
}
71
74
StringBuffer query = new StringBuffer ();
72
75
query .append ("insert into audit(uuid, timestamp, actor, origin, action, elements) " ).append (
73
76
"values (?, ?, ?, ?, ?, ?)" );
74
77
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 ;
83
104
}
84
105
85
106
@ Override
86
- public boolean createAuditTableIFNotExist () throws SQLException {
107
+ public boolean createAuditTableIFNotExist () {
87
108
StringBuffer query = new StringBuffer ("create table if not exists audit (" );
88
109
query .append ("uuid varchar(200) NOT NULL," );
89
110
query .append ("timestamp varchar(100) NOT NULL," );
@@ -93,10 +114,20 @@ public boolean createAuditTableIFNotExist() throws SQLException {
93
114
query .append ("elements varchar(20000)" );
94
115
query .append (");" );
95
116
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
+ }
97
128
return true ;
98
129
}
99
-
130
+
100
131
public static AuditLogDao getInstance () {
101
132
if (!initialized ) {
102
133
synchronized (AuditLogDaoImpl .class ) {
0 commit comments