Back to home page

OSCL-LXR

 
 

    


0001 /**
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements.  See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership.  The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License.  You may obtain a copy of the License at
0009  *
0010  *     http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing, software
0013  * distributed under the License is distributed on an "AS IS" BASIS,
0014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015  * See the License for the specific language governing permissions and
0016  * limitations under the License.
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  * CompositeService.
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       // Note that the state of the failed service is still INITED and not
0076       // STARTED. Even though the last service is not started completely, still
0077       // call stop() on all services including failed service to make sure cleanup
0078       // happens.
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       // The base composite-service is already stopped, don't do anything again.
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     // stop in reserve order of start
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    * JVM Shutdown hook for CompositeService which will stop the given
0111    * CompositeService gracefully in case of JVM shutdown.
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         // Stop the Composite Service
0125         compositeService.stop();
0126       } catch (Throwable t) {
0127         LOG.info("Error stopping " + compositeService.getName(), t);
0128       }
0129     }
0130   }
0131 
0132 
0133 }