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 */
018
019package org.apache.hadoop.fs.http.server;
020
021import org.apache.hadoop.classification.InterfaceAudience;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.fs.CommonConfigurationKeysPublic;
024import org.apache.hadoop.lib.server.ServerException;
025import org.apache.hadoop.lib.service.FileSystemAccess;
026import org.apache.hadoop.lib.servlet.ServerWebApp;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030import java.io.IOException;
031
032/**
033 * Bootstrap class that manages the initialization and destruction of the
034 * HttpFSServer server, it is a <code>javax.servlet.ServletContextListener
035 * </code> implementation that is wired in HttpFSServer's WAR
036 * <code>WEB-INF/web.xml</code>.
037 * <p/>
038 * It provides acces to the server context via the singleton {@link #get}.
039 * <p/>
040 * All the configuration is loaded from configuration properties prefixed
041 * with <code>httpfs.</code>.
042 */
043@InterfaceAudience.Private
044public class HttpFSServerWebApp extends ServerWebApp {
045  private static final Logger LOG =
046    LoggerFactory.getLogger(HttpFSServerWebApp.class);
047
048  /**
049   * Server name and prefix for all configuration properties.
050   */
051  public static final String NAME = "httpfs";
052
053  /**
054   * Configuration property that defines HttpFSServer admin group.
055   */
056  public static final String CONF_ADMIN_GROUP = "admin.group";
057
058  private static HttpFSServerWebApp SERVER;
059
060  private String adminGroup;
061
062  /**
063   * Default constructor.
064   *
065   * @throws IOException thrown if the home/conf/log/temp directory paths
066   * could not be resolved.
067   */
068  public HttpFSServerWebApp() throws IOException {
069    super(NAME);
070  }
071
072  /**
073   * Constructor used for testing purposes.
074   */
075  public HttpFSServerWebApp(String homeDir, String configDir, String logDir,
076                               String tempDir, Configuration config) {
077    super(NAME, homeDir, configDir, logDir, tempDir, config);
078  }
079
080  /**
081   * Constructor used for testing purposes.
082   */
083  public HttpFSServerWebApp(String homeDir, Configuration config) {
084    super(NAME, homeDir, config);
085  }
086
087  /**
088   * Initializes the HttpFSServer server, loads configuration and required
089   * services.
090   *
091   * @throws ServerException thrown if HttpFSServer server could not be
092   * initialized.
093   */
094  @Override
095  public void init() throws ServerException {
096    if (SERVER != null) {
097      throw new RuntimeException("HttpFSServer server already initialized");
098    }
099    SERVER = this;
100    super.init();
101    adminGroup = getConfig().get(getPrefixedName(CONF_ADMIN_GROUP), "admin");
102    LOG.info("Connects to Namenode [{}]",
103             get().get(FileSystemAccess.class).getFileSystemConfiguration().
104               get(CommonConfigurationKeysPublic.FS_DEFAULT_NAME_KEY));
105  }
106
107  /**
108   * Shutdowns all running services.
109   */
110  @Override
111  public void destroy() {
112    SERVER = null;
113    super.destroy();
114  }
115
116  /**
117   * Returns HttpFSServer server singleton, configuration and services are
118   * accessible through it.
119   *
120   * @return the HttpFSServer server singleton.
121   */
122  public static HttpFSServerWebApp get() {
123    return SERVER;
124  }
125
126  /**
127   * Returns HttpFSServer admin group.
128   *
129   * @return httpfs admin group.
130   */
131  public String getAdminGroup() {
132    return adminGroup;
133  }
134
135}