View Javadoc

1   package cz.cuni.amis.pogamut.base.component.bus.event;
2   
3   import java.util.logging.Level;
4   import java.util.logging.Logger;
5   
6   import cz.cuni.amis.pogamut.base.component.IComponent;
7   import cz.cuni.amis.pogamut.base.component.bus.IComponentBus;
8   import cz.cuni.amis.pogamut.base.component.bus.IComponentEvent;
9   import cz.cuni.amis.pogamut.base.component.bus.event.impl.FatalErrorEvent;
10  import cz.cuni.amis.pogamut.base.component.bus.event.impl.PausedEvent;
11  import cz.cuni.amis.pogamut.base.component.bus.event.impl.PausingEvent;
12  import cz.cuni.amis.pogamut.base.component.bus.event.impl.ResumedEvent;
13  import cz.cuni.amis.pogamut.base.component.bus.event.impl.ResumingEvent;
14  import cz.cuni.amis.pogamut.base.component.bus.event.impl.StartedEvent;
15  import cz.cuni.amis.pogamut.base.component.bus.event.impl.StartingEvent;
16  import cz.cuni.amis.pogamut.base.component.bus.event.impl.StartingPausedEvent;
17  import cz.cuni.amis.pogamut.base.component.bus.event.impl.StoppedEvent;
18  import cz.cuni.amis.pogamut.base.component.bus.event.impl.StoppingEvent;
19  
20  /**
21   * This class provides simple methods for propagation of events that happened in some component.
22   * <p><p>
23   * Every component should instantiate this class for itself as it provides a convenient way to send new events into 
24   * the bus (but it is not mandatory).
25   * <p><p>
26   * There is a possibility to disable sending of all events by setting false via {@link ComponentBusEvents#setBroadcasting(boolean)}.
27   * Think of it as applying aspect to all event-broadcasting used by your class which allows you to disable the cross-cutting concern
28   * of sending events.
29   * 
30   * @author Jimmy
31   */
32  public class ComponentBusEvents  {
33  
34  	protected IComponentBus bus;
35  	protected IComponent component;
36  	protected Logger log;
37  	
38  	/**
39  	 * Whether the event broadcasting is enabled.
40  	 */
41  	protected boolean broadcasting = true;
42  
43  	public ComponentBusEvents(IComponentBus bus, IComponent component, Logger log) {
44  		this.bus = bus;
45  		this.component = component;
46  		this.log = log;
47  	}
48  
49  	/**
50  	 * When some method is called to broadcast some event then it will go through ONLY IFF isBroadcasting().
51  	 * @return
52  	 */
53  	public boolean isBroadcasting() {
54  		return broadcasting;
55  	}
56  
57  	/**
58  	 * This may enable (== true) / disable (== false) whether this object will actually be sending any events or not.
59  	 * @param broadcasting
60  	 */
61  	public void setBroadcasting(boolean broadcasting) {
62  		this.broadcasting = broadcasting;
63  	}
64  	
65  	private boolean event(IComponentEvent event) {
66  		if (!isBroadcasting()) {
67  			if (log != null && log.isLoggable(Level.FINEST)) log.finest(component.getComponentId().getToken() + " WON'T SEND " + event + " to " + bus + " as broadcasting is DISABLED");
68  			return false;
69  		}
70  		if (log != null && log.isLoggable(Level.FINER)) log.finer(component.getComponentId().getToken() + " is sending " + event + " to " + bus); 
71  		if (bus.event(event)) {
72  			if (log != null && log.isLoggable(Level.FINEST)) log.finest(component.getComponentId().getToken() + " sent " + event + " to " + bus + " and was processed");
73  			return true;
74  		} else {
75  			if (log != null && log.isLoggable(Level.FINER)) log.warning(component.getComponentId().getToken() + " sent StartingEvent to " + bus + " and its processing was postponed.");
76  			return false;
77  		}
78  	}
79  	
80  	private boolean eventTransactional(IComponentEvent event) {
81  		if (!isBroadcasting()) {
82  			if (log != null && log.isLoggable(Level.FINEST)) log.finest(component.getComponentId().getToken() + " WON'T SEND TRANSACTIONAL " + event + " to " + bus + " as broadcasting is DISABLED");
83  			return false;
84  		}
85  		if (log != null && log.isLoggable(Level.FINER)) log.finer(component.getComponentId().getToken() + " is sending transactional " + event + " to " + bus); 
86  		bus.eventTransactional(event);
87  		if (log != null && log.isLoggable(Level.FINEST)) log.finest(component.getComponentId().getToken() + " sent transactional " + event + " to " + bus + " and was processed");
88  		return true;
89  	}
90  
91  	public boolean starting() {
92  		return event(new StartingEvent(component));
93  	}
94  	
95  	public boolean starting(String message) {
96  		return event(new StartingEvent(component, message));
97  	}
98  	
99  	public boolean startingPaused() {
100 		return event(new StartingPausedEvent(component));
101 	}
102 	
103 	public boolean startingPaused(String message) {
104 		return event(new StartingPausedEvent(component, message));
105 	}
106 	
107 	public boolean started() {
108 		return event(new StartedEvent(component));
109 	}
110 	
111 	public boolean started(String message) {
112 		return event(new StartedEvent(component, message));
113 	}
114 	
115 	public boolean pausing() {
116 		return event(new PausingEvent(component));
117 	}
118 	
119 	public boolean pausing(String message) {
120 		return event(new PausingEvent(component, message));
121 	}
122 	
123 	public boolean paused() {
124 		return event(new PausedEvent(component));
125 	}
126 	
127 	public boolean paused(String message) {
128 		return event(new PausedEvent(component, message));
129 	}
130 	
131 	public boolean resuming() {
132 		return event(new ResumingEvent(component));
133 	}
134 	
135 	public boolean resuming(String message) {
136 		return event(new ResumingEvent(component, message));
137 	}
138 	
139 	public boolean resumed() {
140 		return event(new ResumedEvent(component));
141 	}
142 	
143 	public boolean resumed(String message) {
144 		return event(new ResumedEvent(component, message));
145 	}
146 	
147 	public boolean stopping() {
148 		return event(new StoppingEvent(component));
149 	}
150 	
151 	public boolean stopping(String message) {
152 		return event(new StoppingEvent(component, message));
153 	}
154 	
155 	public boolean stopped() {
156 		return event(new StoppedEvent(component));
157 	}
158 	
159 	public boolean stopped(String message) {
160 		return event(new StoppedEvent(component, message));
161 	}
162 	
163 	public boolean fatalError(String message) {
164 		return event(new FatalErrorEvent(component, message));
165 	}
166 	
167 	public boolean fatalError(String message, Throwable cause) {
168 		return event(new FatalErrorEvent(component, message, cause));
169 	}
170 	
171 	public boolean fatalError(Throwable cause) {
172 		return event(new FatalErrorEvent(component, cause));
173 	}
174 	
175 	public boolean startingTransactional() {
176 		return eventTransactional(new StartingEvent(component));
177 	}
178 	
179 	public boolean startingTransactional(String message) {
180 		return eventTransactional(new StartingEvent(component, message));
181 	}
182 	
183 	public boolean startingPausedTransactional() {
184 		return eventTransactional(new StartingPausedEvent(component));
185 	}
186 	
187 	public boolean startingPausedTransactional(String message) {
188 		return eventTransactional(new StartingPausedEvent(component, message));		
189 	}
190 	
191 	public boolean startedTransactional() {
192 		return eventTransactional(new StartedEvent(component));
193 	}
194 	
195 	public boolean startedTransactional(String message) {
196 		return eventTransactional(new StartedEvent(component, message));
197 	}
198 	
199 	public boolean pausingTransactional() {
200 		return eventTransactional(new PausingEvent(component));
201 	}
202 	
203 	public boolean pausingTransactional(String message) {
204 		return eventTransactional(new PausingEvent(component, message));
205 	}
206 	
207 	public boolean pausedTransactional() {
208 		return eventTransactional(new PausedEvent(component));
209 	}
210 	
211 	public boolean pausedTransactional(String message) {
212 		return eventTransactional(new PausedEvent(component, message));
213 	}
214 	
215 	public boolean resumingTransactional() {
216 		return eventTransactional(new ResumingEvent(component));
217 	}
218 	
219 	public boolean resumingTransactional(String message) {
220 		return eventTransactional(new ResumingEvent(component, message));
221 	}
222 	
223 	public boolean resumedTransactional() {
224 		return eventTransactional(new ResumedEvent(component));
225 	}
226 	
227 	public boolean resumedTransactional(String message) {
228 		return eventTransactional(new ResumedEvent(component, message));
229 	}
230 	
231 	public boolean stoppingTransactional() {
232 		return eventTransactional(new StoppingEvent(component));
233 	}
234 	
235 	public boolean stoppingTransactional(String message) {
236 		return eventTransactional(new StoppingEvent(component, message));
237 	}
238 	
239 	public boolean stoppedTransactional() {
240 		return eventTransactional(new StoppedEvent(component));
241 	}
242 	
243 	public boolean stoppedTransactional(String message) {
244 		return eventTransactional(new StoppedEvent(component, message));
245 	}
246 	
247 }