PageRenderTime 25ms CodeModel.GetById 11ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/hwi/src/java/org/apache/hadoop/hive/hwi/HWIServer.java

#
Java | 146 lines | 73 code | 18 blank | 55 comment | 6 complexity | 39f5acd8894d0311fc807b9f59bcfceb MD5 | raw file
  1/**
  2 * Licensed to the Apache Software Foundation (ASF) under one
  3 * or more contributor license agreements.  See the NOTICE file
  4 * distributed with this work for additional information
  5 * regarding copyright ownership.  The ASF licenses this file
  6 * to you under the Apache License, Version 2.0 (the
  7 * "License"); you may not use this file except in compliance
  8 * with the License.  You may obtain a copy of the License at
  9 *
 10 *     http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19package org.apache.hadoop.hive.hwi;
 20
 21import java.io.File;
 22import java.io.IOException;
 23
 24import org.apache.commons.logging.Log;
 25import org.apache.commons.logging.LogFactory;
 26import org.apache.hadoop.hive.conf.HiveConf;
 27import org.apache.hadoop.hive.shims.JettyShims;
 28import org.apache.hadoop.hive.shims.ShimLoader;
 29
 30/**
 31 * This is the entry point for HWI. A web server is invoked in the same manner
 32 * as the hive CLI. Rather then opening a command line session a web server is
 33 * started and a web application to work with hive is started.
 34 */
 35public class HWIServer {
 36  protected static final Log l4j = LogFactory.getLog(HWIServer.class.getName());
 37
 38  private JettyShims.Server webServer;
 39  private final String[] args;
 40
 41  /**
 42   * 
 43   * @param args
 44   *          These are the command line arguments. Usually -hiveconf.
 45   * @throws java.io.IOException
 46   */
 47  public HWIServer(String[] args) throws IOException {
 48    this.args = args;
 49  }
 50
 51  /**
 52   * This method initialized the internal Jetty Servlet Engine. It adds the hwi
 53   * context path.
 54   * 
 55   * @throws java.io.IOException
 56   *           Port already in use, bad bind etc.
 57   */
 58  public void start() throws IOException {
 59
 60    HiveConf conf = new HiveConf(this.getClass());
 61
 62    String listen = null;
 63    int port = -1;
 64
 65    listen = conf.getVar(HiveConf.ConfVars.HIVEHWILISTENHOST);
 66    port = conf.getIntVar(HiveConf.ConfVars.HIVEHWILISTENPORT);
 67
 68    if (listen.equals("")) {
 69      l4j.warn("hive.hwi.listen.host was not specified defaulting to 0.0.0.0");
 70      listen = "0.0.0.0";
 71    }
 72    if (port == -1) {
 73      l4j.warn("hive.hwi.listen.port was not specified defaulting to 9999");
 74      port = 9999;
 75    }
 76
 77    String hwiWAR = conf.getVar(HiveConf.ConfVars.HIVEHWIWARFILE);
 78    String hivehome = System.getenv().get("HIVE_HOME");
 79    File hwiWARFile = new File(hivehome, hwiWAR);
 80    if (!hwiWARFile.exists()) {
 81      l4j.fatal("HWI WAR file not found at " + hwiWAR);
 82      System.exit(1);
 83    }
 84
 85    webServer = ShimLoader.getJettyShims().startServer(listen, port);
 86    webServer.addWar(hwiWARFile.toString(), "/hwi");
 87
 88    /*
 89     * The command line args may be used by multiple components. Rather by
 90     * setting these as a system property we avoid having to specifically pass
 91     * them
 92     */
 93    StringBuilder sb = new StringBuilder();
 94    for (String arg : args) {
 95      sb.append(arg + " ");
 96    }
 97    System.setProperty("hwi-args", sb.toString());
 98
 99    try {
100      while (true) {
101        try {
102          webServer.start();
103          webServer.join();
104          l4j.debug(" HWI Web Server is started.");
105          break;
106        } catch (org.mortbay.util.MultiException ex) {
107          throw ex;
108        }
109      }
110    } catch (IOException ie) {
111      throw ie;
112    } catch (Exception e) {
113      IOException ie = new IOException("Problem starting HWI server");
114      ie.initCause(e);
115      l4j.error("Parsing hwi.listen.port caused exception ", e);
116      throw ie;
117    }
118  }
119
120  /**
121   * 
122   * @param args
123   *          as of now no arguments are supported
124   * @throws java.lang.Exception
125   *           Could be thrown if due to issues with Jetty or bad configuration
126   *           options
127   * 
128   */
129  public static void main(String[] args) throws Exception {
130    HWIServer hwi = new HWIServer(args);
131    l4j.info("HWI is starting up");
132    hwi.start();
133  }
134
135  /**
136   * Shut down the running HWI Server.
137   * 
138   * @throws Exception
139   *           Running Thread.stop() can and probably will throw this
140   */
141  public void stop() throws Exception {
142    l4j.info("HWI is shutting down");
143    webServer.stop();
144  }
145
146}