1
|
/**
|
2
|
* '$RCSfile$'
|
3
|
* Copyright: 2004 Regents of the University of California and the
|
4
|
* National Center for Ecological Analysis and Synthesis
|
5
|
*
|
6
|
* '$Author: jones $'
|
7
|
* '$Date: 2004-04-02 18:02:21 -0800 (Fri, 02 Apr 2004) $'
|
8
|
* '$Revision: 2104 $'
|
9
|
*
|
10
|
* This program is free software; you can redistribute it and/or modify
|
11
|
* it under the terms of the GNU General Public License as published by
|
12
|
* the Free Software Foundation; either version 2 of the License, or
|
13
|
* (at your option) any later version.
|
14
|
*
|
15
|
* This program is distributed in the hope that it will be useful,
|
16
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
* GNU General Public License for more details.
|
19
|
*
|
20
|
* You should have received a copy of the GNU General Public License
|
21
|
* along with this program; if not, write to the Free Software
|
22
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23
|
*/
|
24
|
package edu.ucsb.nceas.metacat;
|
25
|
|
26
|
import java.sql.PreparedStatement;
|
27
|
import java.sql.SQLException;
|
28
|
import java.sql.Timestamp;
|
29
|
import java.util.Date;
|
30
|
|
31
|
/**
|
32
|
* EventLog is used to intialize and store a log of events that occur in an
|
33
|
* application. The events are registered with the logger as they occur, but
|
34
|
* EventLog writes them to permenant storage when it is most convenient or
|
35
|
* efficient. EventLog is a Singleton as there should always be only one object
|
36
|
* for these logging events.
|
37
|
*
|
38
|
* TODO: Logging to the database needn't be synchronous with the event.
|
39
|
* Instead, a separate thread can be launched that periodically sleeps and only
|
40
|
* wakes periodically to see if metacat is idle. The log event can be cached
|
41
|
* and inserted later when the thread wakes and finds metacat idle.
|
42
|
*
|
43
|
* TODO: Write a function that archives a part of the log table to an
|
44
|
* external text file so that the log table doesn't get to big. This
|
45
|
* function should be able to be called manually or on a schedule.
|
46
|
*
|
47
|
* TODO: Write an access function that returns an XML report for a
|
48
|
* specific subset of events. Users should be able to query on
|
49
|
* principal, docid/rev, date, event, and possibly other fields.
|
50
|
*
|
51
|
* @author jones
|
52
|
*/
|
53
|
public class EventLog
|
54
|
{
|
55
|
/**
|
56
|
* The single instance of the event log that is always returned.
|
57
|
*/
|
58
|
private static EventLog self = null;
|
59
|
|
60
|
/**
|
61
|
* A private constructor that initializes the class when getInstance() is
|
62
|
* called.
|
63
|
*/
|
64
|
private EventLog()
|
65
|
{
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
* Return the single instance of the event log after initializing it if it
|
70
|
* wasn't previously initialized.
|
71
|
*
|
72
|
* @return the single EventLog instance
|
73
|
*/
|
74
|
public static EventLog getInstance()
|
75
|
{
|
76
|
if (self == null) {
|
77
|
self = new EventLog();
|
78
|
}
|
79
|
return self;
|
80
|
}
|
81
|
|
82
|
/**
|
83
|
* Log an event of interest to the application. The information logged can
|
84
|
* include basic identification information about the principal or computer
|
85
|
* that initiated the event.
|
86
|
*
|
87
|
* @param ipAddress the internet protocol address for the event
|
88
|
* @param principal the principal for the event (a username, etc)
|
89
|
* @param docid the identifier of the document to which the event applies
|
90
|
* @param event the string code for the event
|
91
|
*/
|
92
|
public void log(String ipAddress, String principal, String docid,
|
93
|
String event)
|
94
|
{
|
95
|
EventLogData logData = new EventLogData(ipAddress, principal, docid,
|
96
|
event);
|
97
|
insertLogEntry(logData);
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* Insert a single log event record to the database.
|
102
|
*
|
103
|
* @param logData the data to be logged when an event occurs
|
104
|
*/
|
105
|
private void insertLogEntry(EventLogData logData)
|
106
|
{
|
107
|
String insertString = "insert into access_log"
|
108
|
+ "(ip_address, principal, docid, "
|
109
|
+ "event, date_logged) "
|
110
|
+ "values ("
|
111
|
+ "'" + logData.getIpAddress() + "', "
|
112
|
+ "'" + logData.getPrincipal() + "', "
|
113
|
+ "'" + logData.getDocid() + "', "
|
114
|
+ "'" + logData.getEvent() + "', "
|
115
|
+ " ? " + ")";
|
116
|
|
117
|
DBConnection dbConn = null;
|
118
|
int serialNumber = -1;
|
119
|
try {
|
120
|
// Get a database connection from the pool
|
121
|
dbConn = DBConnectionPool.getDBConnection("EventLog.writeLog");
|
122
|
serialNumber = dbConn.getCheckOutSerialNumber();
|
123
|
|
124
|
// Execute the insert statement
|
125
|
PreparedStatement stmt = dbConn.prepareStatement(insertString);
|
126
|
stmt.setTimestamp(1, new Timestamp(new Date().getTime()));
|
127
|
stmt.executeUpdate();
|
128
|
stmt.close();
|
129
|
} catch (SQLException e) {
|
130
|
MetaCatUtil.debugMessage("Error while logging event to database: "
|
131
|
+ e.getMessage(), 5);
|
132
|
} finally {
|
133
|
// Return database connection to the pool
|
134
|
DBConnectionPool.returnDBConnection(dbConn, serialNumber);
|
135
|
}
|
136
|
}
|
137
|
}
|