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.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
0030
0031
0032 public abstract class AbstractService implements Service {
0033
0034 private static final Log LOG = LogFactory.getLog(AbstractService.class);
0035
0036
0037
0038
0039 private Service.STATE state = STATE.NOTINITED;
0040
0041
0042
0043
0044 private final String name;
0045
0046
0047
0048 private long startTime;
0049
0050
0051
0052
0053 private HiveConf hiveConf;
0054
0055
0056
0057
0058
0059 private final List<ServiceStateChangeListener> listeners =
0060 new ArrayList<ServiceStateChangeListener>();
0061
0062
0063
0064
0065
0066
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
0079
0080
0081
0082
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
0094
0095
0096
0097
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
0109
0110
0111
0112
0113
0114 @Override
0115 public synchronized void stop() {
0116 if (state == STATE.STOPPED ||
0117 state == STATE.INITED ||
0118 state == STATE.NOTINITED) {
0119
0120
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
0155
0156
0157
0158
0159
0160
0161
0162 private void ensureCurrentState(Service.STATE currentState) {
0163 ServiceOperations.ensureCurrentState(state, currentState);
0164 }
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176 private void changeState(Service.STATE newState) {
0177 state = newState;
0178
0179 for (ServiceStateChangeListener l : listeners) {
0180 l.stateChanged(this);
0181 }
0182 }
0183
0184 }