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  private static final Logger LOG = LoggerFactory.getLogger(HBaseRESTTestingUtility.class);
029
030  private RESTServer server;
031
032  public int getServletPort() {
033    return server.getPort();
034  }
035
036  public void startServletContainer(Configuration conf) throws Exception {
037    if (server != null) {
038      LOG.error("RESTServer already running");
039      return;
040    }
041
042    conf.setInt("hbase.rest.port", 0);
043    conf.setInt("hbase.rest.info.port", -1);
044    conf.setBoolean(RESTServer.SKIP_LOGIN_KEY, true);
045
046    server = new RESTServer(conf);
047    server.run();
048
049    LOG.info("started " + server.getClass().getName() + " on port " +
050      server.getPort());
051  }
052
053  public void shutdownServletContainer() {
054    if (server != null) {
055      try {
056        server.stop();
057        server = null;
058        RESTServlet.stop();
059      } catch (Exception e) {
060        LOG.warn(StringUtils.stringifyException(e));
061      }
062    }
063  }
064}