1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
52 RESTServlet.getInstance(conf);
53
54
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
65 server = new Server(0);
66 server.setSendServerVersion(false);
67 server.setSendDateHeader(false);
68
69 Context context = new Context(server, "/", Context.SESSIONS);
70 context.addServlet(sh, "/*");
71 context.addFilter(GzipFilter.class, "/*", 0);
72 HttpServerUtil.constrainHttpMethods(context);
73
74 server.start();
75
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 }