1 /*
2 * $Id: EventTimerTask.java 8077 2007-08-27 20:15:25Z aperepel $
3 * --------------------------------------------------------------------------------------
4 * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5 *
6 * The software in this package is published under the terms of the CPAL v1.0
7 * license, a copy of which has been included with this distribution in the
8 * LICENSE.txt file.
9 */
10
11 package org.mule.util.timer;
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.TimerTask;
16
17 /**
18 * <code>EventTimerTask</code> is a task that causes TimeEvent to be fired to
19 * listening objects when a specific number of milliseconds have passed. This
20 * implementation is based on the java.util.TimerTask.
21 */
22 public class EventTimerTask extends TimerTask
23 {
24 /**
25 * A list of listeners on this task
26 */
27 private List listeners = null;
28
29 /**
30 * The name of the task
31 */
32 private String name = null;
33
34 /**
35 * Determines if the task has been started
36 */
37 private boolean started = true;
38
39 /**
40 * Constructs a EventTimeTask and registers a listener with it
41 *
42 * @param listener the listener to register
43 */
44 public EventTimerTask(TimeEventListener listener)
45 {
46 super();
47 addListener(listener);
48 this.name = "EventTimerTask." + hashCode();
49 }
50
51 /**
52 * Constructs a EventTimeTask and registers a listener with it
53 *
54 * @param listener the listener to register
55 * @param name the name for the task
56 */
57 public EventTimerTask(TimeEventListener listener, String name)
58 {
59 super();
60 addListener(listener);
61 this.name = name;
62 }
63
64 /**
65 * The action to be performed by this timer task. The fireTime event method is
66 * called.
67 */
68 public void run()
69 {
70
71 TimeEvent event = new TimeEvent(this, getName(), scheduledExecutionTime());
72 fireTimerEvent(event);
73 }
74
75 /**
76 * Gets the task name (this is also the timer thread name)
77 *
78 * @return the task name
79 */
80 public String getName()
81 {
82 return name;
83 }
84
85 public void removeListener(TimeEventListener listener)
86 {
87 if (listeners != null && listeners.contains(listener))
88 {
89 listeners.remove(listener);
90 }
91 }
92
93 public void removeAllListeners()
94 {
95 listeners = new ArrayList();
96 }
97
98 public void addListener(TimeEventListener listener)
99 {
100 if (listeners == null)
101 {
102 listeners = new ArrayList();
103 listeners.add(listener);
104 }
105 else if (!listeners.contains(listener))
106 {
107 listeners.add(listener);
108 }
109 }
110
111 protected void fireTimerEvent(TimeEvent event)
112 {
113 if (listeners != null && started)
114 {
115 int count = listeners.size();
116 for (int i = 0; i < count; i++)
117 {
118 ((TimeEventListener) listeners.get(i)).timeExpired(event);
119 }
120 }
121 }
122
123 public void stop()
124 {
125 started = false;
126 }
127
128 public void start()
129 {
130 started = true;
131 }
132
133 public boolean isStarted()
134 {
135 return started;
136 }
137 }