View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.http;
21  
22  import java.io.IOException;
23  import java.net.URI;
24  
25  import javax.servlet.http.HttpServlet;
26  
27  import org.apache.hadoop.classification.InterfaceAudience;
28  import org.apache.hadoop.conf.Configuration;
29  
30  /**
31   * Create a Jetty embedded server to answer http requests. The primary goal
32   * is to serve up status information for the server.
33   * There are three contexts:
34   *   "/stacks/" -> points to stack trace
35   *   "/static/" -> points to common static files (src/hbase-webapps/static)
36   *   "/" -> the jsp server code from (src/hbase-webapps/<name>)
37   */
38  @InterfaceAudience.Private
39  public class InfoServer {
40    private static final String HBASE_APP_DIR = "hbase-webapps";
41    private final HttpServer httpServer;
42  
43    /**
44     * Create a status server on the given port.
45     * The jsp scripts are taken from src/hbase-webapps/<code>name<code>.
46     * @param name The name of the server
47     * @param bindAddress address to bind to
48     * @param port The port to use on the server
49     * @param findPort whether the server should start at the given port and
50     * increment by 1 until it finds a free port.
51     * @throws IOException e
52     */
53    public InfoServer(String name, String bindAddress, int port, boolean findPort,
54        final Configuration c)
55    throws IOException {
56      HttpServer.Builder builder = new HttpServer.Builder();
57      builder
58        .setName(name)
59        .addEndpoint(URI.create("http://" + bindAddress + ":" + port))
60        .setAppDir(HBASE_APP_DIR).setFindPort(findPort).setConf(c);
61      String logDir = System.getProperty("hbase.log.dir");
62      if (logDir != null) {
63        builder.setLogDir(logDir);
64      }
65      this.httpServer = builder.build();
66    }
67  
68    public void addServlet(String name, String pathSpec,
69            Class<? extends HttpServlet> clazz) {
70        this.httpServer.addServlet(name, pathSpec, clazz);
71    }
72  
73    public void setAttribute(String name, Object value) {
74      this.httpServer.setAttribute(name, value);
75    }
76  
77    public void start() throws IOException {
78      this.httpServer.start();
79    }
80  
81    @Deprecated
82    public int getPort() {
83      return this.httpServer.getPort();
84    }
85  
86    public void stop() throws Exception {
87      this.httpServer.stop();
88    }
89  
90  }