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 org.apache.hadoop.hive.conf.HiveConf;
0022 import org.slf4j.Logger;
0023 import org.slf4j.LoggerFactory;
0024 
0025 /**
0026  * ServiceOperations.
0027  *
0028  */
0029 public final class ServiceOperations {
0030   private static final Logger LOG = LoggerFactory.getLogger(ServiceOperations.class);
0031 
0032   private ServiceOperations() {
0033   }
0034 
0035   /**
0036    * Verify that a service is in a given state.
0037    * @param state the actual state a service is in
0038    * @param expectedState the desired state
0039    * @throws IllegalStateException if the service state is different from
0040    * the desired state
0041    */
0042   public static void ensureCurrentState(Service.STATE state,
0043                                         Service.STATE expectedState) {
0044     if (state != expectedState) {
0045       throw new IllegalStateException("For this operation, the " +
0046                                           "current service state must be "
0047                                           + expectedState
0048                                           + " instead of " + state);
0049     }
0050   }
0051 
0052   /**
0053    * Initialize a service.
0054    *
0055    * The service state is checked <i>before</i> the operation begins.
0056    * This process is <i>not</i> thread safe.
0057    * @param service a service that must be in the state
0058    *   {@link Service.STATE#NOTINITED}
0059    * @param configuration the configuration to initialize the service with
0060    * @throws RuntimeException on a state change failure
0061    * @throws IllegalStateException if the service is in the wrong state
0062    */
0063 
0064   public static void init(Service service, HiveConf configuration) {
0065     Service.STATE state = service.getServiceState();
0066     ensureCurrentState(state, Service.STATE.NOTINITED);
0067     service.init(configuration);
0068   }
0069 
0070   /**
0071    * Start a service.
0072    *
0073    * The service state is checked <i>before</i> the operation begins.
0074    * This process is <i>not</i> thread safe.
0075    * @param service a service that must be in the state
0076    *   {@link Service.STATE#INITED}
0077    * @throws RuntimeException on a state change failure
0078    * @throws IllegalStateException if the service is in the wrong state
0079    */
0080 
0081   public static void start(Service service) {
0082     Service.STATE state = service.getServiceState();
0083     ensureCurrentState(state, Service.STATE.INITED);
0084     service.start();
0085   }
0086 
0087   /**
0088    * Initialize then start a service.
0089    *
0090    * The service state is checked <i>before</i> the operation begins.
0091    * This process is <i>not</i> thread safe.
0092    * @param service a service that must be in the state
0093    *   {@link Service.STATE#NOTINITED}
0094    * @param configuration the configuration to initialize the service with
0095    * @throws RuntimeException on a state change failure
0096    * @throws IllegalStateException if the service is in the wrong state
0097    */
0098   public static void deploy(Service service, HiveConf configuration) {
0099     init(service, configuration);
0100     start(service);
0101   }
0102 
0103   /**
0104    * Stop a service.
0105    *
0106    * Do nothing if the service is null or not in a state in which it can be/needs to be stopped.
0107    *
0108    * The service state is checked <i>before</i> the operation begins.
0109    * This process is <i>not</i> thread safe.
0110    * @param service a service or null
0111    */
0112   public static void stop(Service service) {
0113     if (service != null) {
0114       Service.STATE state = service.getServiceState();
0115       if (state == Service.STATE.STARTED) {
0116         service.stop();
0117       }
0118     }
0119   }
0120 
0121   /**
0122    * Stop a service; if it is null do nothing. Exceptions are caught and
0123    * logged at warn level. (but not Throwables). This operation is intended to
0124    * be used in cleanup operations
0125    *
0126    * @param service a service; may be null
0127    * @return any exception that was caught; null if none was.
0128    */
0129   public static Exception stopQuietly(Service service) {
0130     try {
0131       stop(service);
0132     } catch (Exception e) {
0133       LOG.warn("When stopping the service " + service.getName()
0134                    + " : " + e,
0135                e);
0136       return e;
0137     }
0138     return null;
0139   }
0140 
0141 }