KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > scalagent > scheduler > SchedulerAlarm


1 /*
2  * Copyright (C) 2001 - 2006 ScalAgent Distributed Technologies
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA.
18  *
19  * Initial developer(s): ScalAgent Distributed Technologies
20  * Contributor(s):
21  */

22 package com.scalagent.scheduler;
23
24 import fr.dyade.aaa.agent.*;
25
26 import java.io.*;
27 import java.util.Date JavaDoc;
28
29 import org.objectweb.util.monolog.api.BasicLevel;
30 import org.objectweb.util.monolog.api.Logger;
31
32 /**
33  * This object is the input stream of a <code>Scheduler</code> agent.
34  * It produces a <code>ScheduleNotification</code> notification when its timer
35  * falls.
36  *
37  * @see Scheduler
38  * @see ScheduleNotification
39  */

40 public class SchedulerAlarm implements NotificationInputStream {
41   public static Logger logger = Debug.getLogger(SchedulerAlarm.class.getName());
42   
43   /** time to sleep in milliseconds */
44   long time;
45
46   /**
47    * Constructor.
48    */

49   public SchedulerAlarm() {
50     time = 0;
51   }
52
53   /**
54    * Gets a <code>Notification</code> from the stream.
55    * <p>
56    * This function is executed by the driver thread.
57    *
58    * @return a <code>ScheduleNotification</code> notification when the
59    * timer falls
60    */

61   public Notification readNotification() throws ClassNotFoundException JavaDoc, IOException {
62     synchronized (this) {
63       waitLoop: while (true) {
64         while (time == 0) {
65           try {
66             if (logger.isLoggable(BasicLevel.DEBUG))
67               logger.log(BasicLevel.DEBUG, "SchedulerAlarm indefinitely waits");
68             wait();
69           } catch (InterruptedException JavaDoc exc) {
70             // ignores interrupts
71
}
72         }
73
74         if (time < 0) {
75           // stops this thread, see close function
76
throw new EOFException();
77         }
78
79         // waits for time
80
long localTime = time;
81         time = 0;
82         try {
83           if (logger.isLoggable(BasicLevel.DEBUG))
84             logger.log(BasicLevel.DEBUG, "SchedulerAlarm waits " + localTime);
85           wait(localTime);
86         } catch (InterruptedException JavaDoc exc) {
87           // ignores interrupts
88
if (logger.isLoggable(BasicLevel.DEBUG))
89             logger.log(BasicLevel.DEBUG, "SchedulerAlarm interrupted", exc);
90         }
91         if (time == 0) {
92           // timer has fallen
93
if (logger.isLoggable(BasicLevel.DEBUG))
94             logger.log(BasicLevel.DEBUG, "SchedulerAlarm break loop");
95           break waitLoop;
96         } else {
97           if (logger.isLoggable(BasicLevel.DEBUG))
98             logger.log(BasicLevel.DEBUG, "SchedulerAlarm loops");
99         }
100       }
101     }
102
103     // sends notification
104
if (logger.isLoggable(BasicLevel.DEBUG))
105       logger.log(BasicLevel.DEBUG, "SchedulerAlarm send notif");
106     return new ScheduleNotification();
107   }
108
109   /**
110    * Sets the timer.
111    * <p>
112    * This function is executed by the main scheduler thread.
113    *
114    * @param time time to sleep in milliseconds
115    */

116   synchronized void setTime(long time) {
117     if (logger.isLoggable(BasicLevel.DEBUG))
118       logger.log(BasicLevel.DEBUG, "SchedulerAlarm.setTime(" +
119             time + ')');
120     this.time = time;
121     notify();
122   }
123
124   /**
125    * Closes the stream.
126    * <p>
127    * This function is called from main scheduler thread.
128    */

129   public void close() throws IOException {
130     if (logger.isLoggable(BasicLevel.DEBUG))
131       logger.log(BasicLevel.DEBUG, "SchedulerAlarm.close()");
132     synchronized(this) {
133       this.time = -1;
134       notify();
135     }
136   }
137   
138 }
139
Popular Tags