1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: ExecutionMonitorService.java,v 1.2 2004/02/02 03:49:20 tanderson Exp $
44 */
45 package org.exolab.jmscts.core.service;
46
47 import java.rmi.Naming;
48 import java.rmi.server.UID;
49 import java.util.ArrayList;
50 import java.util.HashMap;
51
52 import org.apache.log4j.Category;
53
54 import org.exolab.core.service.Service;
55 import org.exolab.core.service.ServiceException;
56
57 import org.exolab.jmscts.core.ExecutionListener;
58
59
60 /***
61 * This service enables the state of a running compliance suite to be captured.
62 * <p>
63 * This service is dependent on the
64 * {@link org.exolab.core.util.RmiRegistryService}. This must be initialised
65 * prior to starting this service
66 *
67 * @version $Revision: 1.2 $ $Date: 2004/02/02 03:49:20 $
68 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
69 */
70 public class ExecutionMonitorService extends Service {
71
72 /***
73 * The service name
74 */
75 public static final String NAME = "ExecutionMonitorService";
76
77 /***
78 * The singleton instance
79 */
80 private static ExecutionMonitorService _instance = null;
81
82 /***
83 * A map of application identifiers and their corresponding listeners
84 */
85 private HashMap _listeners = new HashMap();
86
87 /***
88 * The server to handle application notification
89 */
90 private ExecutionMonitorServer _server = null;
91
92 /***
93 * The name of the server bound in the RMI registry
94 */
95 private String _name = null;
96
97 /***
98 * The logger
99 */
100 private static final Category log =
101 Category.getInstance(ExecutionMonitorService.class);
102
103
104 /***
105 * Construct a new <code>ExecutionMonitorService</code>
106 *
107 * @param port the RMI registry port
108 */
109 protected ExecutionMonitorService(int port) {
110 super(NAME);
111 _name = "//localhost:" + port + "/ExecutionListener";
112 }
113
114 /***
115 * Initialise the ExecutionMonitorService
116 *
117 * @param port the RMI registry port
118 * @return the singleton instance of the service
119 */
120 public static synchronized ExecutionMonitorService initialise(int port) {
121 if (_instance != null) {
122 throw new IllegalStateException(
123 "ExecutionMonitorService has already been initialised");
124 }
125 _instance = new ExecutionMonitorService(port);
126 return _instance;
127 }
128
129 /***
130 * Returns the singleton instance of the ExecutionMonitorService
131 *
132 * @return the singleton instance of the ExecutionMonitorService, or null,
133 * if it hasn't been initialised
134 */
135 public static synchronized ExecutionMonitorService instance() {
136 return _instance;
137 }
138
139 /***
140 * Add a listener for an application
141 *
142 * @param id the application id
143 * @param listener the listener
144 */
145 public void addListener(String id, ExecutionListener listener) {
146 if (id == null) {
147 throw new IllegalArgumentException("Argument 'id' is null");
148 }
149 if (listener == null) {
150 throw new IllegalArgumentException("Argument 'listener' is null");
151 }
152 synchronized (_listeners) {
153 ArrayList listeners = (ArrayList) _listeners.get(id);
154 if (listeners == null) {
155 listeners = new ArrayList();
156 _listeners.put(id, listeners);
157 }
158 listeners.add(listener);
159 }
160 }
161
162 /***
163 * Remove a listener for an application
164 *
165 * @param id the application id
166 * @param listener the listener
167 */
168 public void removeListener(String id, ExecutionListener listener) {
169 if (id == null) {
170 throw new IllegalArgumentException("Argument 'id' is null");
171 }
172 if (listener == null) {
173 throw new IllegalArgumentException("Argument 'listener' is null");
174 }
175 synchronized (_listeners) {
176 ArrayList listeners = (ArrayList) _listeners.get(id);
177 if (listeners != null) {
178 listeners.remove(listener);
179 if (listeners.isEmpty()) {
180 _listeners.remove(id);
181 }
182 }
183 }
184 }
185
186 /***
187 * Notifies all registered listeners for an application that the
188 * application failed to start
189 *
190 * @param id the application id
191 * @param throwable the reason for the failure
192 */
193 public void failed(String id, Throwable throwable) {
194 ExecutionListener[] listeners = getListeners(id);
195 if (listeners != null) {
196 for (int i = 0; i < listeners.length; ++i) {
197 listeners[i].failed(throwable);
198 }
199 } else {
200 log.debug("Application=" + id + " failed to start but no "
201 + "listeners are registered", throwable);
202 }
203 }
204
205 /***
206 * Notifies all registered listeners for an application that the
207 * application has started
208
209 * @param id the application id
210 */
211 public void started(String id) {
212 ExecutionListener[] listeners = getListeners(id);
213 if (listeners != null) {
214 for (int i = 0; i < listeners.length; ++i) {
215 listeners[i].started();
216 }
217 } else {
218 log.debug("Application=" + id + " started but no "
219 + "listeners are registered");
220 }
221 }
222
223 /***
224 * Notifies all registered listeners for an application that the
225 * application has stopped
226 *
227 * @param id the application id
228 * @param status the exit status of the application
229 */
230 public void stopped(String id, int status) {
231 ExecutionListener[] listeners = getListeners(id);
232 if (listeners != null) {
233 for (int i = 0; i < listeners.length; ++i) {
234 listeners[i].stopped(status);
235 }
236 } else {
237 log.debug("Application=" + id + " stopped with status=" + status
238 + " but no listeners are registered");
239 }
240 }
241
242 /***
243 * Notifies all registered listeners for an application that the
244 * application has thrown an exception just prior to termination
245 *
246 * @param id the application id
247 * @param throwable the exception thrown by the application
248 */
249 public void error(String id, Throwable throwable) {
250 ExecutionListener[] listeners = getListeners(id);
251 if (listeners != null) {
252 for (int i = 0; i < listeners.length; ++i) {
253 listeners[i].failed(throwable);
254 }
255 } else {
256 log.debug("Application=" + id + " threw exception "
257 + " but no listeners are registered");
258 }
259 }
260
261 /***
262 * Allocate a unique identifier to an application
263 *
264 * @return a unique identifier for an application
265 */
266 public String allocateId() {
267 return new UID().toString();
268 }
269
270 /***
271 * Start the service
272 *
273 * @throws ServiceException if the service can't be started
274 */
275 public synchronized void start() throws ServiceException {
276 if (!getState().isRunning()) {
277 try {
278 _server = new ExecutionMonitorServer();
279 Naming.bind(_name, _server);
280 } catch (Exception exception) {
281 throw new ServiceException(exception.getMessage());
282 }
283 }
284 super.start();
285 }
286
287 /***
288 * Stop the service
289 *
290 * @throws ServiceException if the service can't be stopped
291 */
292 public synchronized void stop() throws ServiceException {
293 super.stop();
294 _listeners.clear();
295 try {
296 Naming.unbind(_name);
297 _server.unexportObject(_server, true);
298
299 } catch (Exception exception) {
300 throw new ServiceException(exception.getMessage());
301 }
302 }
303
304 /***
305 * Returns all listsners for the specified id
306 *
307 * @param id the application id
308 * @return the listsners, or <code>null</code> if none are registered
309 */
310 private ExecutionListener[] getListeners(String id) {
311 ExecutionListener[] result = null;
312 synchronized (_listeners) {
313 ArrayList listeners = (ArrayList) _listeners.get(id);
314 if (listeners != null) {
315 result = (ExecutionListener[]) listeners.toArray(
316 new ExecutionListener[]{});
317 }
318 }
319 return result;
320 }
321
322 }
This page was automatically generated by Maven