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  package org.apache.hadoop.hbase.rest;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.rest.filter.GzipFilter;
25  import org.apache.hadoop.util.StringUtils;
26  import org.mortbay.jetty.Server;
27  import org.mortbay.jetty.servlet.Context;
28  import org.mortbay.jetty.servlet.ServletHolder;
29  
30  import com.sun.jersey.spi.container.servlet.ServletContainer;
31  
32  public class HBaseRESTTestingUtility {
33  
34    static final Log LOG = LogFactory.getLog(HBaseRESTTestingUtility.class);
35  
36    private int testServletPort;
37    private Server server;
38  
39    public int getServletPort() {
40      return testServletPort;
41    }
42  
43    public void startServletContainer(Configuration conf) throws Exception {
44      if (server != null) {
45        LOG.error("ServletContainer already running");
46        return;
47      }
48  
49      // Inject the conf for the test by being first to make singleton
50      RESTServlet.getInstance(conf);
51  
52      // set up the Jersey servlet container for Jetty
53      ServletHolder sh = new ServletHolder(ServletContainer.class);
54      sh.setInitParameter(
55        "com.sun.jersey.config.property.resourceConfigClass",
56        ResourceConfig.class.getCanonicalName());
57      sh.setInitParameter("com.sun.jersey.config.property.packages",
58        "jetty");
59  
60      LOG.info("configured " + ServletContainer.class.getName());
61      
62      // set up Jetty and run the embedded server
63      server = new Server(0);
64      server.setSendServerVersion(false);
65      server.setSendDateHeader(false);
66        // set up context
67      Context context = new Context(server, "/", Context.SESSIONS);
68      context.addServlet(sh, "/*");
69      context.addFilter(GzipFilter.class, "/*", 0);
70        // start the server
71      server.start();
72        // get the port
73      testServletPort = server.getConnectors()[0].getLocalPort();
74  
75      LOG.info("started " + server.getClass().getName() + " on port " + 
76        testServletPort);
77    }
78  
79    public void shutdownServletContainer() {
80      if (server != null) try {
81        server.stop();
82        server = null;
83        RESTServlet.stop();
84      } catch (Exception e) {
85        LOG.warn(StringUtils.stringifyException(e));
86      }
87    }
88  }