?? servicegroup.java
字號:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 1999-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
*/
package org.exolab.jms.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* ServiceGroup is responsible for managing a collection of
* {@link Serviceable} objects.
* <br>
* All serviceable objects are named and no two sevices can have the same
* name.
*
* @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:01 $
* @author <a href="mailto:jima@comware.com.au">Jim Alateras</a>
* @see BasicService
* @see Service
* @see Serviceable
*/
public class ServiceGroup {
/**
* A map of service names to the actual services
*/
private HashMap _services = new HashMap();
/**
* Maintains the order in which services have been added to the
* service manager
*/
private ArrayList _serviceOrder = new ArrayList();
/**
* The logger
*/
private static final Log log = LogFactory.getLog(ServiceGroup.class);
/**
* Create a new service group
*/
public ServiceGroup() {
}
/**
* Add the named service to the collection of managed services.
* The service must be in the STOPPED state before it can be added to the
* manager.
* <p>
* The {@link #startAll} method will start the services in the order that
* they were added to the ServiceGroup. Therefore if one service is
* dependent on another ensure that they are added in the correct order.
* Similarly {@link #stopAll} will stop the services in the reverse order
* to which they were started.
*
* @param name the name of the sevice
* @param service the service to add
* @throws ServiceAlreadyExistsException if a service exists with the same
* name
* @throws ServiceManagerException if the service is not in the stopped
* state
*/
public synchronized void add(String name, Serviceable service)
throws ServiceAlreadyExistsException, ServiceManagerException {
if (_services.containsKey(name)) {
throw new ServiceAlreadyExistsException(
"Service with name=" + name + " has already been registered");
}
if (!service.getState().isStopped()) {
throw new ServiceManagerException(
"Service with name=" + name + " is in state=" +
service.getState());
}
_services.put(name, service);
_serviceOrder.add(name);
}
/**
* Remove the named service from the ServiceGroup. The service must be
* in the 'stopped' state before it can be removed.
*
* @param name the name of the service to remove
* @throws ServiceDoesNotExistException if the named service doesn't exist
* @throws ServiceManagerException if a client tries to remove a service
* that is not stopped.
*/
public synchronized void remove(String name)
throws ServiceDoesNotExistException, ServiceManagerException {
if (!_services.containsKey(name)) {
throw new ServiceDoesNotExistException("Service with name " +
name + " is not registered");
}
Serviceable service = (Serviceable) _services.get(name);
if (!service.getState().isStopped()) {
throw new ServiceManagerException(
"Cannot remove service=" + name + " while it is in state=" +
service.getState());
} else {
_services.remove(name);
_serviceOrder.remove(name);
}
}
/**
* Remove the specified service from the ServiceGroup. It delegates
* the responsiblity to <i>remove(String)</i> method.
*
* @param service the service to remove
* @throws ServiceDoesNotExistException if the service isn't registered
* @throws ServiceManagerException if a client tries to remove a service
* that is not stopped.
*/
public synchronized void remove(Serviceable service)
throws ServiceDoesNotExistException, ServiceManagerException
{
remove(service.getName());
}
/**
* Return an enumeration of the registered service names
*
* @return an enumeration of service names
*/
public synchronized Iterator getServiceNames() {
return new ArrayList(_serviceOrder).iterator();
}
/**
* Return the service specified by name, or null if one does not exist
*
* @param name the service name
* @return the service, or null if non-existent
*/
public synchronized Serviceable getServiceByName(String name) {
return (Serviceable) _services.get(name);
}
/**
* Start all the services in their registered order. If a service is
* already running then it is ignored. If one or more services could
* not be started then the ServiceManagerException is raised. The
* exception will only indicate the first service that failed to
* start.
*
* @throws ServiceManagerException
*/
public synchronized void startAll() throws ServiceManagerException {
Iterator iter = _serviceOrder.iterator();
while (iter.hasNext()) {
String name = (String) iter.next();
Serviceable service = getServiceByName(name);
try {
if (!service.getState().isRunning()) {
service.start();
log.info("Started service [" + service.getName() + "]");
}
} catch(ServiceException exception) {
// log the error and rethrow
log.error(exception);
throw new ServiceManagerException(exception);
}
}
}
/**
* Stop all the services in their reverse registered order. If a sevice
* is already stopped then it is ignored. If one or more services could
* not be stopped an appropriate error will be logged
*/
public synchronized void stopAll() {
Iterator iter = _serviceOrder.iterator();
while (iter.hasNext()) {
String name = (String) iter.next();
Serviceable service = getServiceByName(name);
if (!service.getState().isStopped()) {
try {
service.stop();
log.info("Stopped service [" + service.getName() + "]");
} catch(ServiceException exception) {
log.error(exception);
}
}
}
}
/**
* Remove all registered services. All services are stopped before removal
*/
public synchronized void removeAll() {
stopAll();
_services.clear();
_serviceOrder.clear();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -