Source Code Cross Referenced for UserActivityLogger.java in  » JMX » jmanage » org » jmanage » core » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » JMX » jmanage » org.jmanage.core.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.