KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > getahead > dwrdemo > clock > Clock


1 /*
2  * Copyright 2005 Joe Walker
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.getahead.dwrdemo.clock;
17
18 import java.util.Collection JavaDoc;
19 import java.util.Date JavaDoc;
20
21 import javax.servlet.ServletContext JavaDoc;
22
23 import org.directwebremoting.ServerContext;
24 import org.directwebremoting.ServerContextFactory;
25 import org.directwebremoting.WebContextFactory;
26 import org.directwebremoting.proxy.dwr.Util;
27 import org.directwebremoting.util.Logger;
28
29 /**
30  * @author Joe Walker [joe at getahead dot ltd dot uk]
31  */

32 public class Clock implements Runnable JavaDoc
33 {
34     /**
35      *
36      */

37     public Clock()
38     {
39         ServletContext JavaDoc servletContext = WebContextFactory.get().getServletContext();
40         sctx = ServerContextFactory.get(servletContext);
41     }
42
43     /**
44      *
45      */

46     public synchronized void toggle()
47     {
48         active = !active;
49
50         if (active)
51         {
52             new Thread JavaDoc(this).start();
53         }
54     }
55
56     /* (non-Javadoc)
57      * @see java.lang.Runnable#run()
58      */

59     public void run()
60     {
61         try
62         {
63             log.debug("CLOCK: Starting server-side thread");
64
65             while (active)
66             {
67                 Collection JavaDoc sessions = sctx.getScriptSessionsByPage("/dwr/clock/index.html");
68                 Util pages = new Util(sessions);
69                 pages.setValue("clockDisplay", new Date JavaDoc().toString());
70
71                 log.debug("Sent message");
72                 Thread.sleep(1000);
73             }
74
75             Collection JavaDoc sessions = sctx.getScriptSessionsByPage("/dwr/clock/index.html");
76             Util pages = new Util(sessions);
77             pages.setValue("clockDisplay", "");
78
79             log.debug("CLOCK: Stopping server-side thread");
80         }
81         catch (InterruptedException JavaDoc ex)
82         {
83             ex.printStackTrace();
84         }
85     }
86
87     /**
88      * Our key to get hold of ServerContexts
89      */

90     private ServerContext sctx;
91
92     /**
93      * Are we updating the clocks on all the pages?
94      */

95     private transient boolean active = false;
96
97     /**
98      * The log stream
99      */

100     private static final Logger log = Logger.getLogger(Clock.class);
101 }
102
Popular Tags