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.hbase.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    
41    private static final String HBASE_APP_DIR = "hbase-webapps";
42    private final org.apache.hadoop.hbase.http.HttpServer httpServer;
43  
44    /**
45     * Create a status server on the given port.
46     * The jsp scripts are taken from src/hbase-webapps/<code>name<code>.
47     * @param name The name of the server
48     * @param bindAddress address to bind to
49     * @param port The port to use on the server
50     * @param findPort whether the server should start at the given port and
51     * increment by 1 until it finds a free port.
52     * @throws IOException e
53     */
54    public InfoServer(String name, String bindAddress, int port, boolean findPort,
55        final Configuration c)
56    throws IOException {
57      HttpServer.Builder builder =
58        new org.apache.hadoop.hbase.http.HttpServer.Builder();
59      builder
60        .setName(name)
61        .addEndpoint(URI.create("http://" + bindAddress + ":" + port))
62        .setAppDir(HBASE_APP_DIR).setFindPort(findPort).setConf(c);
63      String logDir = System.getProperty("hbase.log.dir");
64      if (logDir != null) {
65        builder.setLogDir(logDir);
66      }
67      this.httpServer = builder.build();
68    }
69  
70    public void addServlet(String name, String pathSpec,
71            Class<? extends HttpServlet> clazz) {
72        this.httpServer.addServlet(name, pathSpec, clazz);
73    }
74  
75    public void setAttribute(String name, Object value) {
76      this.httpServer.setAttribute(name, value);
77    }
78  
79    public void start() throws IOException {
80      this.httpServer.start();
81    }
82  
83    @Deprecated
84    public int getPort() {
85      return this.httpServer.getPort();
86    }
87  
88    public void stop() throws Exception {
89      this.httpServer.stop();
90    }
91  
92  }