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.lang.ArrayUtils;
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.security.UserProvider;
26 import org.apache.hadoop.hbase.util.HttpServerUtil;
27 import org.apache.hadoop.util.StringUtils;
28 import org.mortbay.jetty.Server;
29 import org.mortbay.jetty.servlet.Context;
30 import org.mortbay.jetty.servlet.ServletHolder;
31 
32 import com.sun.jersey.spi.container.servlet.ServletContainer;
33 
34 public class HBaseRESTTestingUtility {
35 
36   private static final Log LOG = LogFactory.getLog(HBaseRESTTestingUtility.class);
37 
38   private int testServletPort;
39   private Server server;
40 
getServletPort()41   public int getServletPort() {
42     return testServletPort;
43   }
44 
startServletContainer(Configuration conf)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, UserProvider.instantiate(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     // Load filters specified from configuration.
72     String[] filterClasses = conf.getStrings(Constants.FILTER_CLASSES,
73       ArrayUtils.EMPTY_STRING_ARRAY);
74     for (String filter : filterClasses) {
75       filter = filter.trim();
76       context.addFilter(Class.forName(filter), "/*", 0);
77     }
78     HttpServerUtil.constrainHttpMethods(context);
79     LOG.info("Loaded filter classes :" + filterClasses);
80       // start the server
81     server.start();
82       // get the port
83     testServletPort = server.getConnectors()[0].getLocalPort();
84 
85     LOG.info("started " + server.getClass().getName() + " on port " +
86       testServletPort);
87   }
88 
shutdownServletContainer()89   public void shutdownServletContainer() {
90     if (server != null) try {
91       server.stop();
92       server = null;
93       RESTServlet.stop();
94     } catch (Exception e) {
95       LOG.warn(StringUtils.stringifyException(e));
96     }
97   }
98 }
99