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.List;
0023 
0024 import org.apache.commons.logging.Log;
0025 import org.apache.commons.logging.LogFactory;
0026 import org.apache.hadoop.hive.conf.HiveConf;
0027 
0028 /**
0029  * AbstractService.
0030  *
0031  */
0032 public abstract class AbstractService implements Service {
0033 
0034   private static final Log LOG = LogFactory.getLog(AbstractService.class);
0035 
0036   /**
0037    * Service state: initially {@link STATE#NOTINITED}.
0038    */
0039   private Service.STATE state = STATE.NOTINITED;
0040 
0041   /**
0042    * Service name.
0043    */
0044   private final String name;
0045   /**
0046    * Service start time. Will be zero until the service is started.
0047    */
0048   private long startTime;
0049 
0050   /**
0051    * The configuration. Will be null until the service is initialized.
0052    */
0053   private HiveConf hiveConf;
0054 
0055   /**
0056    * List of state change listeners; it is final to ensure
0057    * that it will never be null.
0058    */
0059   private final List<ServiceStateChangeListener> listeners =
0060       new ArrayList<ServiceStateChangeListener>();
0061 
0062   /**
0063    * Construct the service.
0064    *
0065    * @param name
0066    *          service name
0067    */
0068   public AbstractService(String name) {
0069     this.name = name;
0070   }
0071 
0072   @Override
0073   public synchronized Service.STATE getServiceState() {
0074     return state;
0075   }
0076 
0077   /**
0078    * {@inheritDoc}
0079    *
0080    * @throws IllegalStateException
0081    *           if the current service state does not permit
0082    *           this action
0083    */
0084   @Override
0085   public synchronized void init(HiveConf hiveConf) {
0086     ensureCurrentState(STATE.NOTINITED);
0087     this.hiveConf = hiveConf;
0088     changeState(STATE.INITED);
0089     LOG.info("Service:" + getName() + " is inited.");
0090   }
0091 
0092   /**
0093    * {@inheritDoc}
0094    *
0095    * @throws IllegalStateException
0096    *           if the current service state does not permit
0097    *           this action
0098    */
0099   @Override
0100   public synchronized void start() {
0101     startTime = System.currentTimeMillis();
0102     ensureCurrentState(STATE.INITED);
0103     changeState(STATE.STARTED);
0104     LOG.info("Service:" + getName() + " is started.");
0105   }
0106 
0107   /**
0108    * {@inheritDoc}
0109    *
0110    * @throws IllegalStateException
0111    *           if the current service state does not permit
0112    *           this action
0113    */
0114   @Override
0115   public synchronized void stop() {
0116     if (state == STATE.STOPPED ||
0117         state == STATE.INITED ||
0118         state == STATE.NOTINITED) {
0119       // already stopped, or else it was never
0120       // started (eg another service failing canceled startup)
0121       return;
0122     }
0123     ensureCurrentState(STATE.STARTED);
0124     changeState(STATE.STOPPED);
0125     LOG.info("Service:" + getName() + " is stopped.");
0126   }
0127 
0128   @Override
0129   public synchronized void register(ServiceStateChangeListener l) {
0130     listeners.add(l);
0131   }
0132 
0133   @Override
0134   public synchronized void unregister(ServiceStateChangeListener l) {
0135     listeners.remove(l);
0136   }
0137 
0138   @Override
0139   public String getName() {
0140     return name;
0141   }
0142 
0143   @Override
0144   public synchronized HiveConf getHiveConf() {
0145     return hiveConf;
0146   }
0147 
0148   @Override
0149   public long getStartTime() {
0150     return startTime;
0151   }
0152 
0153   /**
0154    * Verify that a service is in a given state.
0155    *
0156    * @param currentState
0157    *          the desired state
0158    * @throws IllegalStateException
0159    *           if the service state is different from
0160    *           the desired state
0161    */
0162   private void ensureCurrentState(Service.STATE currentState) {
0163     ServiceOperations.ensureCurrentState(state, currentState);
0164   }
0165 
0166   /**
0167    * Change to a new state and notify all listeners.
0168    * This is a private method that is only invoked from synchronized methods,
0169    * which avoid having to clone the listener list. It does imply that
0170    * the state change listener methods should be short lived, as they
0171    * will delay the state transition.
0172    *
0173    * @param newState
0174    *          new service state
0175    */
0176   private void changeState(Service.STATE newState) {
0177     state = newState;
0178     // notify listeners
0179     for (ServiceStateChangeListener l : listeners) {
0180       l.stateChanged(this);
0181     }
0182   }
0183 
0184 }