0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 package org.apache.hive.service;
0020
0021 import java.util.ArrayList;
0022 import java.util.Collection;
0023 import java.util.Collections;
0024 import java.util.List;
0025
0026 import org.apache.hadoop.hive.conf.HiveConf;
0027 import org.slf4j.Logger;
0028 import org.slf4j.LoggerFactory;
0029
0030
0031
0032
0033
0034 public class CompositeService extends AbstractService {
0035
0036 private static final Logger LOG = LoggerFactory.getLogger(CompositeService.class);
0037
0038 private final List<Service> serviceList = new ArrayList<Service>();
0039
0040 public CompositeService(String name) {
0041 super(name);
0042 }
0043
0044 public Collection<Service> getServices() {
0045 return Collections.unmodifiableList(serviceList);
0046 }
0047
0048 protected synchronized void addService(Service service) {
0049 serviceList.add(service);
0050 }
0051
0052 protected synchronized boolean removeService(Service service) {
0053 return serviceList.remove(service);
0054 }
0055
0056 @Override
0057 public synchronized void init(HiveConf hiveConf) {
0058 for (Service service : serviceList) {
0059 service.init(hiveConf);
0060 }
0061 super.init(hiveConf);
0062 }
0063
0064 @Override
0065 public synchronized void start() {
0066 int i = 0;
0067 try {
0068 for (int n = serviceList.size(); i < n; i++) {
0069 Service service = serviceList.get(i);
0070 service.start();
0071 }
0072 super.start();
0073 } catch (Throwable e) {
0074 LOG.error("Error starting services " + getName(), e);
0075
0076
0077
0078
0079 stop(i);
0080 throw new ServiceException("Failed to Start " + getName(), e);
0081 }
0082
0083 }
0084
0085 @Override
0086 public synchronized void stop() {
0087 if (this.getServiceState() == STATE.STOPPED) {
0088
0089 return;
0090 }
0091 if (serviceList.size() > 0) {
0092 stop(serviceList.size() - 1);
0093 }
0094 super.stop();
0095 }
0096
0097 private synchronized void stop(int numOfServicesStarted) {
0098
0099 for (int i = numOfServicesStarted; i >= 0; i--) {
0100 Service service = serviceList.get(i);
0101 try {
0102 service.stop();
0103 } catch (Throwable t) {
0104 LOG.info("Error stopping " + service.getName(), t);
0105 }
0106 }
0107 }
0108
0109
0110
0111
0112
0113 public static class CompositeServiceShutdownHook implements Runnable {
0114
0115 private final CompositeService compositeService;
0116
0117 public CompositeServiceShutdownHook(CompositeService compositeService) {
0118 this.compositeService = compositeService;
0119 }
0120
0121 @Override
0122 public void run() {
0123 try {
0124
0125 compositeService.stop();
0126 } catch (Throwable t) {
0127 LOG.info("Error stopping " + compositeService.getName(), t);
0128 }
0129 }
0130 }
0131
0132
0133 }