KickJava   Java API By Example, From Geeks To Geeks.

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


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.faces.async.render.IntervalRenderer;
37 import com.icesoft.faces.async.render.RenderManager;
38 import com.icesoft.faces.async.render.Renderable;
39 import com.icesoft.faces.webapp.xmlhttp.PersistentFacesState;
40 import com.icesoft.faces.webapp.xmlhttp.RenderingException;
41 import com.icesoft.faces.context.ViewListener;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44
45 /**
46  * Class used to control the background clock of the entire auction monitor By
47  * queueing a render call every pollInterval (default 1000) milliseconds, this
48  * class allows the auction monitor UI to have ticking clocks In addition this
49  * class will help AuctionBean maintain a list of the number of users online
50  * through incrementUsers and decrementUsers
51  */

52 public class ClockBean implements Renderable, ViewListener {
53     private static Log log = LogFactory.getLog(ClockBean.class);
54     private boolean isRunning = false;
55     private IntervalRenderer clock;
56     private int pollInterval = 1000;
57     private String JavaDoc autoLoad = " ";
58     private PersistentFacesState state = null;
59
60     private static final String JavaDoc AUTO_LOAD = "ClockBean-Loaded";
61     private static final String JavaDoc INTERVAL_RENDERER_GROUP = "clock";
62
63     public ClockBean() {
64         state = PersistentFacesState.getInstance();
65         state.addViewListener(this);
66         AuctionBean.incrementUsers();
67     }
68
69     public String JavaDoc getAutoLoad() {
70         if (" ".equals(autoLoad)) {
71             autoLoad = AUTO_LOAD;
72         }
73         return autoLoad;
74     }
75
76     public void setPollInterval(int interval) {
77         pollInterval = interval;
78     }
79
80     public int getPollInterval() {
81         return pollInterval;
82     }
83
84     public boolean isRunning() {
85         return isRunning;
86     }
87
88     public void setRenderManager(RenderManager manager) {
89         if (manager != null) {
90             clock = manager.getIntervalRenderer(INTERVAL_RENDERER_GROUP + "-" + System.currentTimeMillis() + "-" + this);
91             if (clock.getInterval() != pollInterval) {
92                 clock.setInterval(pollInterval);
93             }
94             clock.add(this);
95             clock.requestRender();
96             isRunning = true;
97         }
98     }
99
100     /**
101      * Method to get the render manager, just return null to satisfy WAS
102      */

103     public RenderManager getRenderManager() {
104         return null;
105     }
106
107     public PersistentFacesState getState() {
108         return state;
109     }
110
111     public void renderingException(RenderingException renderingException) {
112         if (log.isDebugEnabled()) {
113             log.debug("Rendering exception called because of " +
114                       renderingException);
115         }
116
117         performCleanup();
118     }
119     
120     protected boolean performCleanup() {
121         try{
122             if (clock != null) {
123                 clock.requestStop();
124                 clock.remove(this);
125                 clock.dispose();
126                 clock = null;
127             }
128             
129             isRunning = false;
130             AuctionBean.decrementUsers();
131             
132             return true;
133         }catch (Exception JavaDoc failedCleanup) {
134             if (log.isErrorEnabled()) {
135                 log.error("Failed to cleanup a clock bean", failedCleanup);
136             }
137         }
138         
139         return false;
140     }
141     
142     public void viewCreated() {
143     }
144     
145     public void viewDisposed() {
146         if (log.isInfoEnabled()) {
147             log.info("ViewListener of ClockBean fired for a user - cleaning up");
148         }
149         
150         performCleanup();
151     }
152 }
153
Popular Tags