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