001: /**
002: * Copyright 2004-2005 jManage.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */package org.jmanage.core.util;
016:
017: import java.util.*;
018: import java.io.*;
019:
020: /**
021: * Date: Nov 7, 2004 6:31:22 PM
022: * @author Shashank Bellary
023: */
024: public class UserActivityLogger extends Thread {
025: /* Location of activity log file */
026: private static final String USER_ACTIVITY_LOG_FILE_NAME = "userActivity.log";
027:
028: /* Create the only instance of UserActivityLogger */
029: private static UserActivityLogger logger = new UserActivityLogger();
030:
031: /* Create a private store where all activities can be stored for
032: logging. */
033: private Vector activities, activityBuffer;
034:
035: /* This variable is used to control the running of the thread. Setting it
036: to true causes the logger to stop. */
037: public static boolean INTERRUPT_THREAD = false;
038:
039: /* This is the default length of "sleep" time for the logger thread
040: between writes to the log file */
041: private static final long DEFAULT_SLEEP_TIME = 30000;
042:
043: /* File writer that actually writes to the log file */
044: private PrintWriter logWriter;
045:
046: /* Represents the current logging date */
047: private Date date;
048:
049: /**
050: * The only access to the single instance.
051: *
052: * @return
053: */
054: public static UserActivityLogger getInstance() {
055: return logger;
056: }
057:
058: /**
059: * Now start the static thread running
060: */
061: static {
062: logger.start();
063: }
064:
065: /**
066: * The constuctor for UserActivityLogger is made private so that only the
067: * logger itself can create an instance of itself.
068: */
069: private UserActivityLogger() {
070: super ();
071: activities = new Vector();
072: activityBuffer = new Vector();
073: /* Open the file to write, and set it to append all new entries */
074: try {
075: File logDir = new File(CoreUtils.getLogDir());
076: logDir.mkdirs();
077: File logFile = new File(logDir.getPath()
078: + File.separatorChar + USER_ACTIVITY_LOG_FILE_NAME);
079: logWriter = new PrintWriter(new FileWriter(logFile, true));
080: } catch (IOException ioe) {
081: ioe.printStackTrace();
082: }
083: }
084:
085: public static String getActivityLogFilePath() {
086: return CoreUtils.getLogDir() + File.separatorChar
087: + USER_ACTIVITY_LOG_FILE_NAME;
088: }
089:
090: /**
091: * This ensures that all the activities stored in the local stored are
092: * written at regular intervals of time to the designated log file.
093: */
094: public void run() {
095: try {
096: while (INTERRUPT_THREAD == false) {
097: sleep(DEFAULT_SLEEP_TIME);
098: writeToFile();
099: }
100: } catch (java.lang.InterruptedException ie) {
101: ie.printStackTrace();
102: }
103: }
104:
105: /**
106: * This is the function that is accessed by programs or modules that
107: * actually want to write a log to the log file.
108: *
109: * Logs user activity by capturing user actions and writing the same to the
110: * userActivityLog file.
111: *
112: * @param user this is the user who performed the activity.
113: * @param activity this is the activity that needs to be written to the
114: * log file.
115: */
116: public synchronized void logActivity(String user, String activity) {
117: date = Calendar.getInstance(Locale.getDefault()).getTime();
118: activity = user.toUpperCase() + " " + activity + " on "
119: + date.toString();
120: activities.add(activity);
121: }
122:
123: /**
124: * This writes the accumulated activities to a file.
125: */
126: private void writeToFile() {
127: /* First clone the activities, so that people can still keep writing
128: to the log even while we're writing the log to the disk. */
129: cloneActivities();
130: /* Now write the cloned activities (buffer) to the log file */
131: for (Iterator iterator = activityBuffer.iterator(); iterator
132: .hasNext();) {
133: String activity = (String) iterator.next();
134: logWriter.println(activity);
135: logWriter.flush();
136: }
137: }
138:
139: /**
140: * This makes a copy of the activity Vector so that it can be written to a
141: * file in a non-blocking way.
142: */
143: private synchronized void cloneActivities() {
144: activityBuffer = (Vector) activities.clone();
145: activities.clear();
146: }
147:
148: /**
149: * Release resources used when class exits.
150: */
151: public void finalize() {
152: logWriter.close();
153: activityBuffer.clear();
154: activities.clear();
155: }
156: }
|