KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > util > UserActivityLogger


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.core.util;
17
18 import java.util.*;
19 import java.io.*;
20
21 /**
22  * Date: Nov 7, 2004 6:31:22 PM
23  * @author Shashank Bellary
24  */

25 public class UserActivityLogger extends Thread JavaDoc{
26     /* Location of activity log file */
27     private static final String JavaDoc USER_ACTIVITY_LOG_FILE_NAME = "userActivity.log";
28
29     /* Create the only instance of UserActivityLogger */
30     private static UserActivityLogger logger = new UserActivityLogger();
31
32     /* Create a private store where all activities can be stored for
33         logging. */

34     private Vector activities, activityBuffer;
35
36     /* This variable is used to control the running of the thread. Setting it
37         to true causes the logger to stop. */

38     public static boolean INTERRUPT_THREAD = false;
39
40     /* This is the default length of "sleep" time for the logger thread
41         between writes to the log file */

42     private static final long DEFAULT_SLEEP_TIME = 30000;
43
44     /* File writer that actually writes to the log file */
45     private PrintWriter logWriter;
46
47     /* Represents the current logging date */
48     private Date date;
49
50     /**
51      * The only access to the single instance.
52      *
53      * @return
54      */

55     public static UserActivityLogger getInstance(){
56         return logger;
57     }
58
59     /**
60      * Now start the static thread running
61      */

62     static{
63         logger.start();
64     }
65
66     /**
67      * The constuctor for UserActivityLogger is made private so that only the
68      * logger itself can create an instance of itself.
69      */

70     private UserActivityLogger(){
71         super();
72         activities = new Vector();
73         activityBuffer = new Vector();
74         /* Open the file to write, and set it to append all new entries */
75         try{
76             File logDir = new File(CoreUtils.getLogDir());
77             logDir.mkdirs();
78             File logFile = new File(logDir.getPath() + File.separatorChar +
79                     USER_ACTIVITY_LOG_FILE_NAME);
80             logWriter = new PrintWriter(new FileWriter(logFile, true));
81         }catch (IOException ioe){
82             ioe.printStackTrace();
83         }
84     }
85
86     public static String JavaDoc getActivityLogFilePath(){
87         return CoreUtils.getLogDir() + File.separatorChar + USER_ACTIVITY_LOG_FILE_NAME;
88     }
89     /**
90      * This ensures that all the activities stored in the local stored are
91      * written at regular intervals of time to the designated log file.
92      */

93     public void run() {
94         try{
95             while(INTERRUPT_THREAD == false){
96                 sleep(DEFAULT_SLEEP_TIME);
97                 writeToFile();
98             }
99         }catch(java.lang.InterruptedException JavaDoc ie){
100             ie.printStackTrace();
101         }
102     }
103
104     /**
105      * This is the function that is accessed by programs or modules that
106      * actually want to write a log to the log file.
107      *
108      * Logs user activity by capturing user actions and writing the same to the
109      * userActivityLog file.
110      *
111      * @param user this is the user who performed the activity.
112      * @param activity this is the activity that needs to be written to the
113      * log file.
114      */

115     public synchronized void logActivity(String JavaDoc user, String JavaDoc activity){
116         date = Calendar.getInstance(Locale.getDefault()).getTime();
117         activity = user.toUpperCase()+" "+activity+" on "+date.toString();
118         activities.add(activity);
119     }
120
121     /**
122      * This writes the accumulated activities to a file.
123      */

124     private void writeToFile() {
125         /* First clone the activities, so that people can still keep writing
126             to the log even while we're writing the log to the disk. */

127         cloneActivities();
128         /* Now write the cloned activities (buffer) to the log file */
129         for(Iterator iterator = activityBuffer.iterator(); iterator.hasNext();){
130             String JavaDoc activity = (String JavaDoc)iterator.next();
131             logWriter.println(activity);
132             logWriter.flush();
133         }
134     }
135
136     /**
137      * This makes a copy of the activity Vector so that it can be written to a
138      * file in a non-blocking way.
139      */

140     private synchronized void cloneActivities() {
141         activityBuffer= (Vector) activities.clone();
142         activities.clear();
143     }
144
145     /**
146      * Release resources used when class exits.
147      */

148     public void finalize() {
149         logWriter.close();
150         activityBuffer.clear();
151         activities.clear();
152     }
153 }
Popular Tags