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.hbase.HBaseClusterTestCase;
25  import org.apache.hadoop.util.StringUtils;
26  import org.mortbay.jetty.Server;
27  import org.mortbay.jetty.servlet.Context;
28  import org.mortbay.jetty.servlet.ServletHolder;
29  
30  import com.sun.jersey.spi.container.servlet.ServletContainer;
31  
32  public class HBaseRESTClusterTestBase extends HBaseClusterTestCase 
33      implements Constants {
34  
35    static final Log LOG =
36      LogFactory.getLog(HBaseRESTClusterTestBase.class);
37  
38    protected int testServletPort;
39    Server server;
40  
41    protected void setUp() throws Exception {
42      super.setUp();
43      startServletContainer();
44    }
45  
46    protected void tearDown() throws Exception {
47      stopServletContainer();
48      super.tearDown();
49    }
50  
51    private void startServletContainer() throws Exception {
52      if (server != null) {
53        LOG.error("ServletContainer already running");
54        return;
55      }
56  
57      // set up the Jersey servlet container for Jetty
58      ServletHolder sh = new ServletHolder(ServletContainer.class);
59      sh.setInitParameter(
60        "com.sun.jersey.config.property.resourceConfigClass",
61        ResourceConfig.class.getCanonicalName());
62      sh.setInitParameter("com.sun.jersey.config.property.packages",
63        "jetty");
64  
65      LOG.info("configured " + ServletContainer.class.getName());
66      
67      // set up Jetty and run the embedded server
68      server = new Server(0);
69      server.setSendServerVersion(false);
70      server.setSendDateHeader(false);
71        // set up context
72      Context context = new Context(server, "/", Context.SESSIONS);
73      context.addServlet(sh, "/*");
74        // start the server
75      server.start();
76        // get the port
77      testServletPort = server.getConnectors()[0].getLocalPort();
78      
79      LOG.info("started " + server.getClass().getName() + " on port " + 
80        testServletPort);
81    }
82  
83    private void stopServletContainer() {
84      if (server != null) try {
85        server.stop();
86        server = null;
87      } catch (Exception e) {
88        LOG.warn(StringUtils.stringifyException(e));
89      }
90    }
91  }