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
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
51 RESTServlet.getInstance(conf);
52
53
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
64 server = new Server(0);
65 server.setSendServerVersion(false);
66 server.setSendDateHeader(false);
67
68 Context context = new Context(server, "/", Context.SESSIONS);
69 context.addServlet(sh, "/*");
70 context.addFilter(GzipFilter.class, "/*", 0);
71
72 server.start();
73
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 }