Project

General

Profile

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 10:15:04 -0800 (Fri, 02 Apr 2004) $'
8
 * '$Revision: 2099 $'
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.SQLException;
27
import java.sql.Statement;
28
import java.text.SimpleDateFormat;
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 oracle sql code for creating logging table
48
 * 
49
 * TODO: Write an access function that returns an XML report for a
50
 * specific subset of events.  Users should be able to query on
51
 * principal, docid/rev, date, event, and possibly other fields.
52
 * 
53
 * @author jones
54
 */
55
public class EventLog
56
{
57
    /**
58
     * The single instance of the event log that is always returned.
59
     */
60
    private static EventLog self = null;
61

    
62
    /**
63
     * A private constructor that initializes the class when getInstance() is
64
     * called.
65
     */
66
    private EventLog()
67
    {
68
    }
69

    
70
    /**
71
     * Return the single instance of the event log after initializing it if it
72
     * wasn't previously initialized.
73
     * 
74
     * @return the single EventLog instance
75
     */
76
    public static EventLog getInstance()
77
    {
78
        if (self == null) {
79
            self = new EventLog();
80
        }
81
        return self;
82
    }
83

    
84
    /**
85
     * Log an event of interest to the application. The information logged can
86
     * include basic identification information about the principal or computer
87
     * that initiated the event.
88
     * 
89
     * @param ipAddress the internet protocol address for the event
90
	 * @param principal the principal for the event (a username, etc)
91
	 * @param docid the identifier of the document to which the event applies
92
	 * @param revision the revision of the document to which the event applies
93
	 * @param event the string code for the event
94
     */
95
    public void log(String ipAddress, String principal, String docid,
96
			long revision, String event)
97
    {
98
        EventLogData logData = new EventLogData(ipAddress, principal, docid,
99
                revision, event);
100
        insertLogEntry(logData);
101
    }
102
    
103
    /**
104
     * Insert a single log event record to the database.
105
     * 
106
     * @param logData the data to be logged when an event occurs
107
     */
108
    private void insertLogEntry(EventLogData logData)
109
    {
110
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
111
                "dd-MMM-yyyy HH:mm:ss");
112

    
113
        String insertString = "insert into access_log"
114
                + "(ip_address, principal, docid, rev, "
115
                + "event, date_logged) "
116
                + "values ("
117
                + "'" + logData.getIpAddress() + "', " 
118
                + "'" + logData.getPrincipal() + "', "
119
                + "'" + logData.getDocid() + "', "
120
                + logData.getRevision() + ", "
121
                + "'" + logData.getEvent() + "', "
122
                + "'" + simpleDateFormat.format(new Date()) + "')"; 
123

    
124
        DBConnection dbConn = null;
125
        int serialNumber = -1;
126
        try {
127
            // Get a database connection from the pool
128
            dbConn = DBConnectionPool.getDBConnection("EventLog.writeLog");
129
            serialNumber = dbConn.getCheckOutSerialNumber();
130
            
131
            // Execute the insert statement
132
            Statement stmt = dbConn.createStatement();
133
            stmt.executeUpdate(insertString);
134
            stmt.close();
135
        } catch (SQLException e) {
136
            MetaCatUtil.debugMessage("Error while logging event to database: " 
137
                    + e.getMessage(), 5);
138
        } finally {
139
            // Return database connection to the pool
140
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
141
        }
142
    }
143
}
(35-35/61)