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