001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.rest; 019 020import org.apache.hadoop.conf.Configuration; 021import org.apache.hadoop.util.StringUtils; 022import org.slf4j.Logger; 023import org.slf4j.LoggerFactory; 024 025public class HBaseRESTTestingUtility { 026 private static final Logger LOG = LoggerFactory.getLogger(HBaseRESTTestingUtility.class); 027 028 private RESTServer server; 029 030 public int getServletPort() { 031 return server.getPort(); 032 } 033 034 public void startServletContainer(Configuration conf) throws Exception { 035 if (server != null) { 036 LOG.error("RESTServer already running"); 037 return; 038 } 039 040 conf.setInt("hbase.rest.port", 0); 041 conf.setInt("hbase.rest.info.port", -1); 042 conf.setBoolean(RESTServer.SKIP_LOGIN_KEY, true); 043 044 server = new RESTServer(conf); 045 server.run(); 046 047 LOG.info("started " + server.getClass().getName() + " on port " + server.getPort()); 048 } 049 050 public void shutdownServletContainer() { 051 if (server != null) { 052 try { 053 server.stop(); 054 server = null; 055 RESTServlet.stop(); 056 } catch (Exception e) { 057 LOG.warn(StringUtils.stringifyException(e)); 058 } 059 } 060 } 061}