PageRenderTime 49ms CodeModel.GetById 22ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.hwi;
  19. import java.io.File;
  20. import java.io.IOException;
  21. import org.apache.commons.logging.Log;
  22. import org.apache.commons.logging.LogFactory;
  23. import org.apache.hadoop.hive.conf.HiveConf;
  24. import org.apache.hadoop.hive.shims.JettyShims;
  25. import org.apache.hadoop.hive.shims.ShimLoader;
  26. /**
  27. * This is the entry point for HWI. A web server is invoked in the same manner
  28. * as the hive CLI. Rather then opening a command line session a web server is
  29. * started and a web application to work with hive is started.
  30. */
  31. public class HWIServer {
  32. protected static final Log l4j = LogFactory.getLog(HWIServer.class.getName());
  33. private JettyShims.Server webServer;
  34. private final String[] args;
  35. /**
  36. *
  37. * @param args
  38. * These are the command line arguments. Usually -hiveconf.
  39. * @throws java.io.IOException
  40. */
  41. public HWIServer(String[] args) throws IOException {
  42. this.args = args;
  43. }
  44. /**
  45. * This method initialized the internal Jetty Servlet Engine. It adds the hwi
  46. * context path.
  47. *
  48. * @throws java.io.IOException
  49. * Port already in use, bad bind etc.
  50. */
  51. public void start() throws IOException {
  52. HiveConf conf = new HiveConf(this.getClass());
  53. String listen = null;
  54. int port = -1;
  55. listen = conf.getVar(HiveConf.ConfVars.HIVEHWILISTENHOST);
  56. port = conf.getIntVar(HiveConf.ConfVars.HIVEHWILISTENPORT);
  57. if (listen.equals("")) {
  58. l4j.warn("hive.hwi.listen.host was not specified defaulting to 0.0.0.0");
  59. listen = "0.0.0.0";
  60. }
  61. if (port == -1) {
  62. l4j.warn("hive.hwi.listen.port was not specified defaulting to 9999");
  63. port = 9999;
  64. }
  65. String hwiWAR = conf.getVar(HiveConf.ConfVars.HIVEHWIWARFILE);
  66. String hivehome = System.getenv().get("HIVE_HOME");
  67. File hwiWARFile = new File(hivehome, hwiWAR);
  68. if (!hwiWARFile.exists()) {
  69. l4j.fatal("HWI WAR file not found at " + hwiWAR);
  70. System.exit(1);
  71. }
  72. webServer = ShimLoader.getJettyShims().startServer(listen, port);
  73. webServer.addWar(hwiWARFile.toString(), "/hwi");
  74. /*
  75. * The command line args may be used by multiple components. Rather by
  76. * setting these as a system property we avoid having to specifically pass
  77. * them
  78. */
  79. StringBuilder sb = new StringBuilder();
  80. for (String arg : args) {
  81. sb.append(arg + " ");
  82. }
  83. System.setProperty("hwi-args", sb.toString());
  84. try {
  85. while (true) {
  86. try {
  87. webServer.start();
  88. webServer.join();
  89. l4j.debug(" HWI Web Server is started.");
  90. break;
  91. } catch (org.mortbay.util.MultiException ex) {
  92. throw ex;
  93. }
  94. }
  95. } catch (IOException ie) {
  96. throw ie;
  97. } catch (Exception e) {
  98. IOException ie = new IOException("Problem starting HWI server");
  99. ie.initCause(e);
  100. l4j.error("Parsing hwi.listen.port caused exception ", e);
  101. throw ie;
  102. }
  103. }
  104. /**
  105. *
  106. * @param args
  107. * as of now no arguments are supported
  108. * @throws java.lang.Exception
  109. * Could be thrown if due to issues with Jetty or bad configuration
  110. * options
  111. *
  112. */
  113. public static void main(String[] args) throws Exception {
  114. HWIServer hwi = new HWIServer(args);
  115. l4j.info("HWI is starting up");
  116. hwi.start();
  117. }
  118. /**
  119. * Shut down the running HWI Server.
  120. *
  121. * @throws Exception
  122. * Running Thread.stop() can and probably will throw this
  123. */
  124. public void stop() throws Exception {
  125. l4j.info("HWI is shutting down");
  126. webServer.stop();
  127. }
  128. }