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