1 16 package org.jmanage.core.util; 17 18 import java.util.*; 19 import java.io.*; 20 21 25 public class UserActivityLogger extends Thread { 26 27 private static final String USER_ACTIVITY_LOG_FILE_NAME = "userActivity.log"; 28 29 30 private static UserActivityLogger logger = new UserActivityLogger(); 31 32 34 private Vector activities, activityBuffer; 35 36 38 public static boolean INTERRUPT_THREAD = false; 39 40 42 private static final long DEFAULT_SLEEP_TIME = 30000; 43 44 45 private PrintWriter logWriter; 46 47 48 private Date date; 49 50 55 public static UserActivityLogger getInstance(){ 56 return logger; 57 } 58 59 62 static{ 63 logger.start(); 64 } 65 66 70 private UserActivityLogger(){ 71 super(); 72 activities = new Vector(); 73 activityBuffer = new Vector(); 74 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 getActivityLogFilePath(){ 87 return CoreUtils.getLogDir() + File.separatorChar + USER_ACTIVITY_LOG_FILE_NAME; 88 } 89 93 public void run() { 94 try{ 95 while(INTERRUPT_THREAD == false){ 96 sleep(DEFAULT_SLEEP_TIME); 97 writeToFile(); 98 } 99 }catch(java.lang.InterruptedException ie){ 100 ie.printStackTrace(); 101 } 102 } 103 104 115 public synchronized void logActivity(String user, String activity){ 116 date = Calendar.getInstance(Locale.getDefault()).getTime(); 117 activity = user.toUpperCase()+" "+activity+" on "+date.toString(); 118 activities.add(activity); 119 } 120 121 124 private void writeToFile() { 125 127 cloneActivities(); 128 129 for(Iterator iterator = activityBuffer.iterator(); iterator.hasNext();){ 130 String activity = (String )iterator.next(); 131 logWriter.println(activity); 132 logWriter.flush(); 133 } 134 } 135 136 140 private synchronized void cloneActivities() { 141 activityBuffer= (Vector) activities.clone(); 142 activities.clear(); 143 } 144 145 148 public void finalize() { 149 logWriter.close(); 150 activityBuffer.clear(); 151 activities.clear(); 152 } 153 } | Popular Tags |