1 43 44 package org.exolab.jms.service; 45 46 import java.util.ArrayList ; 47 import java.util.HashMap ; 48 import java.util.Iterator ; 49 50 import org.apache.commons.logging.Log; 51 import org.apache.commons.logging.LogFactory; 52 53 54 67 public class ServiceGroup { 68 69 72 private HashMap _services = new HashMap (); 73 74 78 private ArrayList _serviceOrder = new ArrayList (); 79 80 83 private static final Log log = LogFactory.getLog(ServiceGroup.class); 84 85 86 89 public ServiceGroup() { 90 } 91 92 110 public synchronized void add(String 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 136 public synchronized void remove(String 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 163 public synchronized void remove(Serviceable service) 164 throws ServiceDoesNotExistException, ServiceManagerException 165 { 166 remove(service.getName()); 167 } 168 169 174 public synchronized Iterator getServiceNames() { 175 return new ArrayList (_serviceOrder).iterator(); 176 } 177 178 184 public synchronized Serviceable getServiceByName(String name) { 185 return (Serviceable) _services.get(name); 186 } 187 188 197 public synchronized void startAll() throws ServiceManagerException { 198 Iterator iter = _serviceOrder.iterator(); 199 while (iter.hasNext()) { 200 String name = (String ) 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.error(exception); 210 throw new ServiceManagerException(exception); 211 } 212 } 213 } 214 215 220 public synchronized void stopAll() { 221 Iterator iter = _serviceOrder.iterator(); 222 while (iter.hasNext()) { 223 String name = (String ) 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 239 public synchronized void removeAll() { 240 stopAll(); 241 _services.clear(); 242 _serviceOrder.clear(); 243 } 244 245 } 246 | Popular Tags |