KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > service > ServiceGroup


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 info@exolab.org.
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 1999-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42  */

43
44 package org.exolab.jms.service;
45
46 import java.util.ArrayList JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.Iterator JavaDoc;
49
50 import org.apache.commons.logging.Log;
51 import org.apache.commons.logging.LogFactory;
52
53
54 /**
55  * ServiceGroup is responsible for managing a collection of
56  * {@link Serviceable} objects.
57  * <br>
58  * All serviceable objects are named and no two sevices can have the same
59  * name.
60  *
61  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:01 $
62  * @author <a HREF="mailto:jima@comware.com.au">Jim Alateras</a>
63  * @see BasicService
64  * @see Service
65  * @see Serviceable
66  */

67 public class ServiceGroup {
68
69     /**
70      * A map of service names to the actual services
71      */

72     private HashMap JavaDoc _services = new HashMap JavaDoc();
73
74     /**
75      * Maintains the order in which services have been added to the
76      * service manager
77      */

78     private ArrayList JavaDoc _serviceOrder = new ArrayList JavaDoc();
79
80     /**
81      * The logger
82      */

83     private static final Log log = LogFactory.getLog(ServiceGroup.class);
84
85
86     /**
87      * Create a new service group
88      */

89     public ServiceGroup() {
90     }
91
92     /**
93      * Add the named service to the collection of managed services.
94      * The service must be in the STOPPED state before it can be added to the
95      * manager.
96      * <p>
97      * The {@link #startAll} method will start the services in the order that
98      * they were added to the ServiceGroup. Therefore if one service is
99      * dependent on another ensure that they are added in the correct order.
100      * Similarly {@link #stopAll} will stop the services in the reverse order
101      * to which they were started.
102      *
103      * @param name the name of the sevice
104      * @param service the service to add
105      * @throws ServiceAlreadyExistsException if a service exists with the same
106      * name
107      * @throws ServiceManagerException if the service is not in the stopped
108      * state
109      */

110     public synchronized void add(String JavaDoc name, Serviceable service)
111         throws ServiceAlreadyExistsException, ServiceManagerException {
112         if (_services.containsKey(name)) {
113             throw new ServiceAlreadyExistsException(
114                 "Service with name=" + name + " has already been registered");
115         }
116
117         if (!service.getState().isStopped()) {
118             throw new ServiceManagerException(
119                 "Service with name=" + name + " is in state=" +
120                 service.getState());
121         }
122
123         _services.put(name, service);
124         _serviceOrder.add(name);
125     }
126
127     /**
128      * Remove the named service from the ServiceGroup. The service must be
129      * in the 'stopped' state before it can be removed.
130      *
131      * @param name the name of the service to remove
132      * @throws ServiceDoesNotExistException if the named service doesn't exist
133      * @throws ServiceManagerException if a client tries to remove a service
134      * that is not stopped.
135      */

136     public synchronized void remove(String JavaDoc name)
137         throws ServiceDoesNotExistException, ServiceManagerException {
138         if (!_services.containsKey(name)) {
139             throw new ServiceDoesNotExistException("Service with name " +
140                 name + " is not registered");
141         }
142
143         Serviceable service = (Serviceable) _services.get(name);
144         if (!service.getState().isStopped()) {
145             throw new ServiceManagerException(
146                 "Cannot remove service=" + name + " while it is in state=" +
147                 service.getState());
148         } else {
149             _services.remove(name);
150             _serviceOrder.remove(name);
151         }
152     }
153
154     /**
155      * Remove the specified service from the ServiceGroup. It delegates
156      * the responsiblity to <i>remove(String)</i> method.
157      *
158      * @param service the service to remove
159      * @throws ServiceDoesNotExistException if the service isn't registered
160      * @throws ServiceManagerException if a client tries to remove a service
161      * that is not stopped.
162      */

163     public synchronized void remove(Serviceable service)
164         throws ServiceDoesNotExistException, ServiceManagerException
165     {
166         remove(service.getName());
167     }
168
169     /**
170      * Return an enumeration of the registered service names
171      *
172      * @return an enumeration of service names
173      */

174     public synchronized Iterator JavaDoc getServiceNames() {
175         return new ArrayList JavaDoc(_serviceOrder).iterator();
176     }
177
178     /**
179      * Return the service specified by name, or null if one does not exist
180      *
181      * @param name the service name
182      * @return the service, or null if non-existent
183      */

184     public synchronized Serviceable getServiceByName(String JavaDoc name) {
185         return (Serviceable) _services.get(name);
186     }
187
188     /**
189      * Start all the services in their registered order. If a service is
190      * already running then it is ignored. If one or more services could
191      * not be started then the ServiceManagerException is raised. The
192      * exception will only indicate the first service that failed to
193      * start.
194      *
195      * @throws ServiceManagerException
196      */

197     public synchronized void startAll() throws ServiceManagerException {
198         Iterator JavaDoc iter = _serviceOrder.iterator();
199         while (iter.hasNext()) {
200             String JavaDoc name = (String JavaDoc) iter.next();
201             Serviceable service = getServiceByName(name);
202             try {
203                 if (!service.getState().isRunning()) {
204                     service.start();
205                     log.info("Started service [" + service.getName() + "]");
206                 }
207             } catch(ServiceException exception) {
208                 // log the error and rethrow
209
log.error(exception);
210                 throw new ServiceManagerException(exception);
211             }
212         }
213     }
214
215     /**
216      * Stop all the services in their reverse registered order. If a sevice
217      * is already stopped then it is ignored. If one or more services could
218      * not be stopped an appropriate error will be logged
219      */

220     public synchronized void stopAll() {
221         Iterator JavaDoc iter = _serviceOrder.iterator();
222         while (iter.hasNext()) {
223             String JavaDoc name = (String JavaDoc) iter.next();
224             Serviceable service = getServiceByName(name);
225             if (!service.getState().isStopped()) {
226                 try {
227                     service.stop();
228                     log.info("Stopped service [" + service.getName() + "]");
229                 } catch(ServiceException exception) {
230                     log.error(exception);
231                 }
232             }
233         }
234     }
235
236     /**
237      * Remove all registered services. All services are stopped before removal
238      */

239     public synchronized void removeAll() {
240         stopAll();
241         _services.clear();
242         _serviceOrder.clear();
243     }
244
245 }
246
Popular Tags