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 com.google.common.net.HostAndPort;
23  
24  import java.io.IOException;
25  import java.net.URI;
26  
27  import javax.servlet.http.HttpServlet;
28  
29  import org.apache.hadoop.hbase.HBaseConfiguration;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
31  import org.apache.hadoop.conf.Configuration;
32  
33  
34  /**
35   * Create a Jetty embedded server to answer http requests. The primary goal
36   * is to serve up status information for the server.
37   * There are three contexts:
38   *   "/stacks/" -> points to stack trace
39   *   "/static/" -> points to common static files (src/hbase-webapps/static)
40   *   "/" -> the jsp server code from (src/hbase-webapps/<name>)
41   */
42  @InterfaceAudience.Private
43  public class InfoServer {
44    
45    private static final String HBASE_APP_DIR = "hbase-webapps";
46    private final org.apache.hadoop.hbase.http.HttpServer httpServer;
47  
48    /**
49     * Create a status server on the given port.
50     * The jsp scripts are taken from src/hbase-webapps/<code>name</code>.
51     * @param name The name of the server
52     * @param bindAddress address to bind to
53     * @param port The port to use on the server
54     * @param findPort whether the server should start at the given port and
55     * increment by 1 until it finds a free port.
56     * @throws IOException e
57     */
58    public InfoServer(String name, String bindAddress, int port, boolean findPort,
59        final Configuration c)
60    throws IOException {
61      HttpConfig httpConfig = new HttpConfig(c);
62      HttpServer.Builder builder =
63        new org.apache.hadoop.hbase.http.HttpServer.Builder();
64  
65        builder.setName(name).addEndpoint(URI.create(httpConfig.getSchemePrefix() +
66          HostAndPort.fromParts(bindAddress, port).toString()))
67            .setAppDir(HBASE_APP_DIR).setFindPort(findPort).setConf(c);
68        String logDir = System.getProperty("hbase.log.dir");
69        if (logDir != null) {
70          builder.setLogDir(logDir);
71        }
72      if (httpConfig.isSecure()) {
73      builder.keyPassword(HBaseConfiguration.getPassword(c, "ssl.server.keystore.keypassword", null))
74        .keyStore(c.get("ssl.server.keystore.location"),
75          HBaseConfiguration.getPassword(c,"ssl.server.keystore.password", null),
76          c.get("ssl.server.keystore.type", "jks"))
77        .trustStore(c.get("ssl.server.truststore.location"),
78          HBaseConfiguration.getPassword(c, "ssl.server.truststore.password", null),
79          c.get("ssl.server.truststore.type", "jks"));
80      }
81      // Enable SPNEGO authentication
82      if ("kerberos".equalsIgnoreCase(c.get(HttpServer.HTTP_UI_AUTHENTICATION, null))) {
83        builder.setUsernameConfKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_PRINCIPAL_KEY)
84          .setKeytabConfKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_KEYTAB_KEY)
85          .setKerberosNameRulesKey(HttpServer.HTTP_SPNEGO_AUTHENTICATION_KRB_NAME_KEY)
86          .setSignatureSecretFileKey(
87              HttpServer.HTTP_AUTHENTICATION_SIGNATURE_SECRET_FILE_KEY)
88          .setSecurityEnabled(true);
89      }
90      this.httpServer = builder.build();
91    }
92  
93    public void addServlet(String name, String pathSpec,
94            Class<? extends HttpServlet> clazz) {
95        this.httpServer.addServlet(name, pathSpec, clazz);
96    }
97  
98    public void setAttribute(String name, Object value) {
99      this.httpServer.setAttribute(name, value);
100   }
101 
102   public void start() throws IOException {
103     this.httpServer.start();
104   }
105 
106   @Deprecated
107   public int getPort() {
108     return this.httpServer.getPort();
109   }
110 
111   public void stop() throws Exception {
112     this.httpServer.stop();
113   }
114 
115 }