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