KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icesoft > applications > faces > auctionMonitor > beans > LogBean


1 /*
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * "The contents of this file are subject to the Mozilla Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
11  * License for the specific language governing rights and limitations under
12  * the License.
13  *
14  * The Original Code is ICEfaces 1.5 open source software code, released
15  * November 5, 2006. The Initial Developer of the Original Code is ICEsoft
16  * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
17  * 2004-2006 ICEsoft Technologies Canada, Corp. All Rights Reserved.
18  *
19  * Contributor(s): _____________________.
20  *
21  * Alternatively, the contents of this file may be used under the terms of
22  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
23  * License), in which case the provisions of the LGPL License are
24  * applicable instead of those above. If you wish to allow use of your
25  * version of this file only under the terms of the LGPL License and not to
26  * allow others to use your version of this file under the MPL, indicate
27  * your decision by deleting the provisions above and replace them with
28  * the notice and other provisions required by the LGPL License. If you do
29  * not delete the provisions above, a recipient may use your version of
30  * this file under either the MPL or the LGPL License."
31  *
32  */

33
34 package com.icesoft.applications.faces.auctionMonitor.beans;
35
36 import com.icesoft.applications.faces.auctionMonitor.ChatState;
37 import com.icesoft.applications.faces.auctionMonitor.MessageLog;
38 import com.icesoft.applications.faces.auctionMonitor.stubs.StubServer;
39 import org.apache.commons.logging.Log;
40 import org.apache.commons.logging.LogFactory;
41
42 import javax.faces.context.ExternalContext;
43 import javax.faces.context.FacesContext;
44 import java.sql.Time JavaDoc;
45 import java.util.ArrayList JavaDoc;
46 import java.util.Calendar JavaDoc;
47 import java.util.Date JavaDoc;
48 import java.util.GregorianCalendar JavaDoc;
49 import java.util.Map JavaDoc;
50 import java.util.Timer JavaDoc;
51 import java.util.TimerTask JavaDoc;
52
53 /**
54  * Singleton bean that should be in the application scope as a holder for the
55  * message log The LogBean is not a true singleton class as the constructor is
56  * still public This is necessary as the bean is created from Tomcat and needs a
57  * constructor
58  */

59 public class LogBean {
60     public static final String JavaDoc LOG_PATH =
61             "com.icesoft.applications.faces.auctionMonitor.messageLog";
62     private static final int CONTROL_LOG_CAP = 20;
63     private static final Runtime JavaDoc core = Runtime.getRuntime();
64     private static Log log = LogFactory.getLog(LogBean.class);
65     private static LogBean singleton;
66     private ChatState state = ChatState.getInstance();
67     private ExternalContext externalContext;
68     private ArrayList JavaDoc controlLog = new ArrayList JavaDoc(0);
69     private MessageLog messageLog = new MessageLog(0);
70     private String JavaDoc autoLoad = " ";
71
72     private static final int TIME_DAYS = 24 * 60 * 60 * 1000;
73
74     public LogBean() {
75         externalContext =
76                 FacesContext.getCurrentInstance().getExternalContext();
77         //Disabling the reset functionality as it causes a PermGen memory
78
//leak through the TimerThread ContextClassLoader keeping references
79
//to all ICEfaces classes
80
//timedReset();
81
}
82
83     public long getFreeMemory() {
84         return (core.freeMemory());
85     }
86
87     public long getTotalMemory() {
88         return (core.totalMemory());
89     }
90
91     public long getMaxMemory() {
92         return (core.maxMemory());
93     }
94
95     public ArrayList JavaDoc getControlLog() {
96         return (controlLog);
97     }
98
99     public MessageLog getMessageLog() {
100         return (storeMessageLog());
101     }
102
103     public String JavaDoc getAutoLoad() {
104         getInstance();
105         if (this.autoLoad.equals(" ")) {
106             this.autoLoad = "LogBean-Loaded";
107         }
108         return (this.autoLoad);
109     }
110
111     public String JavaDoc getTimeStampState() {
112         if (state.getTimeStampEnabled()) {
113             return ("Disable");
114         }
115
116         return ("Enable");
117     }
118
119     public String JavaDoc getTimeStampMessage() {
120         if (state.getTimeStampEnabled()) {
121             return ("enabled");
122         }
123
124         return ("disabled");
125     }
126
127     private MessageLog getContextMessageLog() {
128         // Get and check the applicationMap from the context
129
Map JavaDoc applicationMap = externalContext.getApplicationMap();
130         if (applicationMap == null) {
131             return (new MessageLog(0));
132         }
133
134         // Get and check the message log from the applicationMap
135
messageLog = (MessageLog) applicationMap.get(LogBean.LOG_PATH);
136         if (messageLog == null) {
137             messageLog = new MessageLog(0);
138         }
139
140         return (messageLog);
141     }
142
143     /**
144      * Method to return a singleton instance of this class
145      *
146      * @return this LogBean object
147      */

148     public static synchronized LogBean getInstance() {
149         if (singleton == null) {
150             singleton = new LogBean();
151         }
152         return (singleton);
153     }
154
155     /**
156      * Method to silently reset the auction every night at midnight
157      */

158     private void timedReset() {
159         // Generate the date for tomorrow at midnight
160
Calendar JavaDoc tomorrow = new GregorianCalendar JavaDoc();
161         tomorrow.add(Calendar.DATE, 1);
162         Date JavaDoc midnight = new GregorianCalendar JavaDoc(tomorrow.get(Calendar.YEAR),
163                                               tomorrow.get(Calendar.MONTH),
164                                               tomorrow.get(Calendar.DATE))
165                 .getTime();
166
167         // Schedule a task to reset at midnight, then every 24 hours past that
168
new Timer JavaDoc().scheduleAtFixedRate(new TimerTask JavaDoc() {
169             public void run() {
170                 resetAuction(true);
171             }
172         }, midnight, TIME_DAYS);
173     }
174
175
176     /**
177      * Method to update and return the message log stored in the faces context
178      *
179      * @return MessageLog this message log, to save having to go through the
180      * context
181      */

182     public MessageLog storeMessageLog() {
183         externalContext.getApplicationMap().put(LOG_PATH, messageLog);
184         return (messageLog);
185     }
186
187
188     /**
189      * Action method wrapper to be called from the page
190      *
191      * @return String "resetAuction" to be used by JSF action if needed
192      */

193     public String JavaDoc resetAuction() {
194         return resetAuction(false);
195     }
196
197     /**
198      * Method to clear outstanding bids and restore them to their initial state
199      *
200      * @param silent true if no notification should be given
201      * @return String "resetAuction" to be used by JSF action if needed
202      */

203     public String JavaDoc resetAuction(boolean silent) {
204         // Get the latest message log
205
getContextMessageLog();
206
207         // Call the reset method on the stub server
208
StubServer.resetAuction();
209         appendControl("Auction has been reset");
210
211         // Notify of the bid reset, store the updated log, and force a reRender
212
if (!silent) {
213             messageLog.addMessage("Notice",
214                                   "Auction has been reset by the administrator.",
215                                   null);
216         }
217         storeMessageLog();
218         state.updateAll();
219
220         return ("resetAuction");
221     }
222
223     /**
224      * Convience method to clear all items from the log
225      *
226      * @return String "clearMessageLog" to be used by JSF action if needed
227      */

228     public String JavaDoc clearMessageLog() {
229         // Get the latest message log
230
getContextMessageLog();
231
232         // Add an admin message then clear the log
233
appendControl("Message log (size " + messageLog.size() + ") cleared");
234         messageLog = (MessageLog) clear(messageLog);
235
236         // Notify of clearing the message log, store the updated log, and force a reRender
237
messageLog.addMessage("Notice",
238                               "Message log was cleared by the administrator.",
239                               null);
240         storeMessageLog();
241         state.updateAll();
242
243         return ("clearMessageLog");
244     }
245
246     /**
247      * Method to dump the latest message log to the terminal
248      *
249      * @return String "printMessageLog" to be used by JSF action if needed
250      */

251     public String JavaDoc printMessageLog() {
252         // Get the latest message log
253
getContextMessageLog();
254
255         if (log.isInfoEnabled()) {
256             log.info("Message log dump started");
257         }
258         int size = messageLog.size();
259         for (int i = 0; i < size; i++) {
260             if (log.isInfoEnabled()) {
261                 log.info("> " + messageLog.getMessageAt(i));
262             }
263         }
264         if (log.isInfoEnabled()) {
265             log.info("Message log dump end");
266         }
267         appendControl("Message log (size " + size + ") printed");
268
269         return ("printMessageLog");
270     }
271
272     /**
273      * Method to toggle timestamps to on or off
274      *
275      * @return String "toggleTimeStamp" to be used by JSF action if needed
276      */

277     public String JavaDoc toggleTimeStamp() {
278         // Toggle timestamps and notify administrator
279
state.toggleTimeStamp();
280         appendControl("Timestamps set to " + state.getTimeStampEnabled());
281
282         // Notify of the current timestamp state, store the updated log, and force a reRender
283
messageLog.addMessage("Notice", "Timestamps have been " +
284                                         getTimeStampMessage() +
285                                         " by the administrator.", null);
286         storeMessageLog();
287         state.updateAll();
288
289         return ("toggleTimeStamp");
290     }
291
292     /**
293      * Convience method to pad the passed message with the time then append it
294      * to the control log
295      *
296      * @param controlMessage message to append
297      */

298     private void appendControl(String JavaDoc controlMessage) {
299         if (controlLog.size() > CONTROL_LOG_CAP) {
300             controlLog = clear(controlLog);
301             appendControl("Control log cleared (max size of " +
302                           CONTROL_LOG_CAP + ")");
303         }
304
305         controlLog.add((new Time JavaDoc(System.currentTimeMillis()) + ": " +
306                         controlMessage));
307     }
308
309     /**
310      * Convience method to clear a passed ArrayList Basically clones the
311      * functionality of a Vector.removeAll
312      *
313      * @param toClear to remove all elements from
314      * @return ArrayList cleared (size = 0)
315      */

316     private ArrayList JavaDoc clear(ArrayList JavaDoc toClear) {
317         // Loop through the ArrayList and remove each element
318
for (int i = 0, len = toClear.size(); i < len; i++) {
319             toClear.remove(0);
320         }
321
322         return (toClear);
323     }
324 }
325
Popular Tags