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      HttpConfig httpConfig = new HttpConfig(c);
58      HttpServer.Builder builder =
59        new org.apache.hadoop.hbase.http.HttpServer.Builder();
60  
61        builder.setName(name).addEndpoint(URI.create(httpConfig.getSchemePrefix() +
62          bindAddress + ":" +
63          port)).setAppDir(HBASE_APP_DIR).setFindPort(findPort).setConf(c);
64        String logDir = System.getProperty("hbase.log.dir");
65        if (logDir != null) {
66          builder.setLogDir(logDir);
67        }
68      if (httpConfig.isSecure()) {
69      builder.keyPassword(c.get("ssl.server.keystore.keypassword"))
70        .keyStore(c.get("ssl.server.keystore.location"),
71          c.get("ssl.server.keystore.password"),
72          c.get("ssl.server.keystore.type", "jks"))
73        .trustStore(c.get("ssl.server.truststore.location"),
74          c.get("ssl.server.truststore.password"),
75          c.get("ssl.server.truststore.type", "jks"));
76      }
77      this.httpServer = builder.build();
78    }
79  
80    public void addServlet(String name, String pathSpec,
81            Class<? extends HttpServlet> clazz) {
82        this.httpServer.addServlet(name, pathSpec, clazz);
83    }
84  
85    public void setAttribute(String name, Object value) {
86      this.httpServer.setAttribute(name, value);
87    }
88  
89    public void start() throws IOException {
90      this.httpServer.start();
91    }
92  
93    @Deprecated
94    public int getPort() {
95      return this.httpServer.getPort();
96    }
97  
98    public void stop() throws Exception {
99      this.httpServer.stop();
100   }
101 
102 }