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