1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.rest;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.rest.filter.GzipFilter;
26  import org.apache.hadoop.util.StringUtils;
27  import org.mortbay.jetty.Server;
28  import org.mortbay.jetty.servlet.Context;
29  import org.mortbay.jetty.servlet.ServletHolder;
30  import org.apache.hadoop.hbase.util.HttpServerUtil;
31  
32  import com.sun.jersey.spi.container.servlet.ServletContainer;
33  
34  public class HBaseRESTTestingUtility {
35  
36    static final Log LOG = LogFactory.getLog(HBaseRESTTestingUtility.class);
37  
38    private int testServletPort;
39    private Server server;
40  
41    public int getServletPort() {
42      return testServletPort;
43    }
44  
45    public void startServletContainer(Configuration conf) throws Exception {
46      if (server != null) {
47        LOG.error("ServletContainer already running");
48        return;
49      }
50  
51      // Inject the conf for the test by being first to make singleton
52      RESTServlet.getInstance(conf);
53  
54      // set up the Jersey servlet container for Jetty
55      ServletHolder sh = new ServletHolder(ServletContainer.class);
56      sh.setInitParameter(
57        "com.sun.jersey.config.property.resourceConfigClass",
58        ResourceConfig.class.getCanonicalName());
59      sh.setInitParameter("com.sun.jersey.config.property.packages",
60        "jetty");
61  
62      LOG.info("configured " + ServletContainer.class.getName());
63      
64      // set up Jetty and run the embedded server
65      server = new Server(0);
66      server.setSendServerVersion(false);
67      server.setSendDateHeader(false);
68        // set up context
69      Context context = new Context(server, "/", Context.SESSIONS);
70      context.addServlet(sh, "/*");
71      context.addFilter(GzipFilter.class, "/*", 0);
72      HttpServerUtil.constrainHttpMethods(context);
73        // start the server
74      server.start();
75        // get the port
76      testServletPort = server.getConnectors()[0].getLocalPort();
77  
78      LOG.info("started " + server.getClass().getName() + " on port " + 
79        testServletPort);
80    }
81  
82    public void shutdownServletContainer() {
83      if (server != null) try {
84        server.stop();
85        server = null;
86        RESTServlet.stop();
87      } catch (Exception e) {
88        LOG.warn(StringUtils.stringifyException(e));
89      }
90    }
91  }