001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.thrift;
019
020import org.slf4j.Logger;
021import org.slf4j.LoggerFactory;
022import java.io.Closeable;
023import java.io.IOException;
024
025/**
026 * Run ThriftServer with passed arguments. Access the exception thrown after we complete run -- if
027 * an exception thrown -- via {@link #getRunException()}}. Call close to shutdown this Runner
028 * and hosted {@link ThriftServer}.
029 */
030class ThriftServerRunner extends Thread implements Closeable {
031  private static final Logger LOG = LoggerFactory.getLogger(ThriftServerRunner.class);
032  Exception exception = null;
033  private final ThriftServer thriftServer;
034  private final String [] args;
035
036  ThriftServerRunner(ThriftServer thriftServer, String [] args) {
037    this.thriftServer = thriftServer;
038    this.args = args;
039    LOG.info("thriftServer={}, args={}", getThriftServer(), args);
040  }
041
042  ThriftServer getThriftServer() {
043    return this.thriftServer;
044  }
045
046  /**
047   * @return Empty unless {@link #run()} threw an exception; if it did, access it here.
048   */
049  Exception getRunException() {
050    return this.exception;
051  }
052
053  @Override public void run() {
054    try {
055      this.thriftServer.run(this.args);
056    } catch (Exception e) {
057      LOG.error("Run threw an exception", e);
058      this.exception = e;
059    }
060  }
061
062  @Override public void close() throws IOException {
063    LOG.info("Stopping {}", this);
064    this.thriftServer.stop();
065  }
066}