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