Project

General

Profile

« Previous | Next » 

Revision 2096

Added by Matt Jones about 20 years ago

Added classes for basic event logging support. These classes write log entries to the database connection. Still to do: query the event log, write oracle sql code (postgres is done), possibly allow the log to be archived to a text file, and possibly do the dtabase writes asynchronously via a caching mechanism.

View differences:

test/edu/ucsb/nceas/metacattest/EventLogTest.java
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$'
7
 *     '$Date$'
8
 * '$Revision$'
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.metacattest;
25

  
26
import java.io.File;
27
import java.io.FileNotFoundException;
28
import java.io.IOException;
29

  
30
import edu.ucsb.nceas.metacat.DBConnectionPool;
31
import edu.ucsb.nceas.metacat.EventLog;
32
import edu.ucsb.nceas.metacat.EventLogData;
33
import edu.ucsb.nceas.metacat.MetaCatUtil;
34
import edu.ucsb.nceas.utilities.Options;
35

  
36
import junit.framework.TestCase;
37

  
38
/**
39
 * Test the logging facility against the database connection.
40
 * 
41
 * @author jones
42
 */
43
public class EventLogTest extends TestCase
44
{
45

  
46
    protected void setUp() throws Exception
47
    {
48
        super.setUp();
49
        try {
50
            File propertyFile = new File("./lib/metacat.properties");
51
            Options options = Options.initialize(propertyFile);
52
            MetaCatUtil util = new MetaCatUtil();
53
            DBConnectionPool pool = DBConnectionPool.getInstance();
54
        } catch (FileNotFoundException e) {
55
            fail(e.getMessage());
56
        } catch (IOException e) {
57
            fail(e.getMessage());
58
        }
59
    }
60

  
61
    /**
62
     * Test whether a valid instance of the EventLog can be retrieved.
63
     *
64
     */
65
    public void testGetInstance()
66
    {
67
        EventLog logger = EventLog.getInstance();
68
        assertTrue(logger != null);
69
    }
70

  
71
    public void testLog()
72
    {
73
        
74
        EventLogData event = new EventLogData("192.168.1.103", "public", 
75
                "test.1", 1, "read");
76
        assertTrue(event != null);
77
        EventLog.getInstance().log(event);
78
    }
79

  
80
}
0 81

  
src/xmltables_postgres.sql
29 29
DROP SEQUENCE xml_replication_id_seq;
30 30
DROP SEQUENCE xml_documents_id_seq;
31 31
DROP SEQUENCE accession_number_id_seq;
32
DROP SEQUENCE access_log_id_seq;
32 33

  
33 34
DROP TABLE xml_index;
34 35
DROP TABLE xml_access;
......
40 41
DROP TABLE xml_replication;
41 42
DROP TABLE xml_catalog;
42 43
DROP TABLE accession_number;
44
DROP TABLE access_log;
43 45

  
44 46
/*
45 47
 *Replication -- table to store servers that metacat is replicated to
......
256 258
		FOREIGN KEY (docid) REFERENCES xml_documents
257 259
);
258 260

  
261
/*
262
 *Logging -- table to store metadata and data access log
263
 */
264
CREATE SEQUENCE access_log_id_seq;
265
CREATE TABLE access_log (
266
  entryid       INT8 default nextval ('access_log_id_seq'),
267
  ip_address    VARCHAR(512),
268
  principal     VARCHAR(512),
269
  docid         VARCHAR(250),	-- the document id #
270
  rev           INT8,           -- the revision number
271
  event         VARCHAR(512),
272
  date_logged   TIMESTAMP,
273
  CONSTRAINT access_log_pk PRIMARY KEY (entryid)
274
);
src/edu/ucsb/nceas/metacat/EventLogData.java
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$'
7
 *     '$Date$'
8
 * '$Revision$'
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
/**
27
 * @author jones
28
 *
29
 * TODO To change the template for this generated type comment go to
30
 * Window - Preferences - Java - Code Generation - Code and Comments
31
 */
32
public class EventLogData {
33
	private String ipAddress;
34
	private String principal;
35
	private String docid;
36
    private long revision;
37
	private String event;
38
	
39
	
40
	/**
41
	 * @param ipAddress
42
	 * @param principal
43
	 * @param docid
44
	 * @param revision
45
	 * @param event
46
	 */
47
	public EventLogData(String ipAddress, String principal, String docid,
48
			long revision, String event) {
49
		this.ipAddress = ipAddress;
50
		this.principal = principal;
51
		this.docid = docid;
52
		this.revision = revision;
53
		this.event = event;
54
	}
55
	
56
	/**
57
	 * @return Returns the docid.
58
	 */
59
	public String getDocid() {
60
		return docid;
61
	}
62
	
63
	/**
64
	 * @param docid The docid to set.
65
	 */
66
	public void setDocid(String docid) {
67
		this.docid = docid;
68
	}
69
	
70
	/**
71
	 * @return Returns the event.
72
	 */
73
	public String getEvent() {
74
		return event;
75
	}
76
	
77
	/**
78
	 * @param event The event to set.
79
	 */
80
	public void setEvent(String event) {
81
		this.event = event;
82
	}
83
	
84
	/**
85
	 * @return Returns the ipAddress.
86
	 */
87
	public String getIpAddress() {
88
		return ipAddress;
89
	}
90
	
91
	/**
92
	 * @param ipAddress The ipAddress to set.
93
	 */
94
	public void setIpAddress(String ipAddress) {
95
		this.ipAddress = ipAddress;
96
	}
97
	
98
	/**
99
	 * @return Returns the principal.
100
	 */
101
	public String getPrincipal() {
102
		return principal;
103
	}
104
	
105
	/**
106
	 * @param principal The principal to set.
107
	 */
108
	public void setPrincipal(String principal) {
109
		this.principal = principal;
110
	}
111
	
112
	/**
113
	 * @return Returns the revision.
114
	 */
115
	public long getRevision() {
116
		return revision;
117
	}
118
	
119
	/**
120
	 * @param revision The revision to set.
121
	 */
122
	public void setRevision(long revision) {
123
		this.revision = revision;
124
	}
125
}
0 126

  
src/edu/ucsb/nceas/metacat/EventLog.java
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$'
7
 *     '$Date$'
8
 * '$Revision$'
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 logData the data to be logged when an event occurs
90
     */
91
    public void log(EventLogData logData)
92
    {
93
        insertLogEntry(logData);
94
    }
95

  
96
    /**
97
     * Insert a single log event record to the database.
98
     * 
99
     * @param logData the data to be logged when an event occurs
100
     */
101
    private void insertLogEntry(EventLogData logData)
102
    {
103
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
104
                "dd-MMM-yyyy HH:mm:ss");
105

  
106
        String insertString = "insert into access_log"
107
                + "(ip_address, principal, docid, rev, "
108
                + "event, date_logged) "
109
                + "values ("
110
                + "'" + logData.getIpAddress() + "', " 
111
                + "'" + logData.getPrincipal() + "', "
112
                + "'" + logData.getDocid() + "', "
113
                + logData.getRevision() + ", "
114
                + "'" + logData.getEvent() + "', "
115
                + "'" + simpleDateFormat.format(new Date()) + "')"; 
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
            Statement stmt = dbConn.createStatement();
126
            stmt.executeUpdate(insertString);
127
            stmt.close();
128
        } catch (SQLException e) {
129
            MetaCatUtil.debugMessage("Error while logging event to database: " 
130
                    + e.getMessage(), 5);
131
        } finally {
132
            // Return database connection to the pool
133
            DBConnectionPool.returnDBConnection(dbConn, serialNumber);
134
        }
135
    }
136
}
0 137

  
src/upgrade-db-to-1.4_postgres.sql
1
/*
2
 * logging.sql -- Create or replace tables for logging events
3
 */
4

  
5
/*
6
 *Logging -- table to store metadata and data access log
7
 */
8
CREATE SEQUENCE access_log_id_seq;
9
CREATE TABLE access_log (
10
  entryid       INT8 default nextval ('access_log_id_seq'),
11
  ip_address    VARCHAR(512),
12
  principal     VARCHAR(512),
13
  docid         VARCHAR(250),	-- the document id #
14
  rev           INT8,           -- the revision number
15
  event         VARCHAR(512),
16
  date_logged   TIMESTAMP,
17
  CONSTRAINT access_log_pk PRIMARY KEY (entryid)
18
);
0 19

  

Also available in: Unified diff