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.lib.servlet; 020 021import com.google.common.annotations.VisibleForTesting; 022import org.apache.hadoop.classification.InterfaceAudience; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.lib.server.Server; 025import org.apache.hadoop.lib.server.ServerException; 026 027import javax.servlet.ServletContextEvent; 028import javax.servlet.ServletContextListener; 029import java.net.InetAddress; 030import java.net.InetSocketAddress; 031import java.net.UnknownHostException; 032import java.text.MessageFormat; 033 034/** 035 * {@link Server} subclass that implements <code>ServletContextListener</code> 036 * and uses its lifecycle to start and stop the server. 037 */ 038@InterfaceAudience.Private 039public abstract class ServerWebApp 040 extends Server 041 implements ServletContextListener { 042 043 private static final String HOME_DIR = ".home.dir"; 044 private static final String CONFIG_DIR = ".config.dir"; 045 private static final String LOG_DIR = ".log.dir"; 046 private static final String TEMP_DIR = ".temp.dir"; 047 private static final String HTTP_HOSTNAME = ".http.hostname"; 048 private static final String HTTP_PORT = ".http.port"; 049 public static final String SSL_ENABLED = ".ssl.enabled"; 050 051 private static final ThreadLocal<String> HOME_DIR_TL = 052 new ThreadLocal<String>(); 053 054 private InetSocketAddress authority; 055 056 /** 057 * Method for testing purposes. 058 */ 059 public static void setHomeDirForCurrentThread(String homeDir) { 060 HOME_DIR_TL.set(homeDir); 061 } 062 063 /** 064 * Constructor for testing purposes. 065 */ 066 protected ServerWebApp(String name, String homeDir, String configDir, 067 String logDir, String tempDir, Configuration config) { 068 super(name, homeDir, configDir, logDir, tempDir, config); 069 } 070 071 /** 072 * Constructor for testing purposes. 073 */ 074 protected ServerWebApp(String name, String homeDir, Configuration config) { 075 super(name, homeDir, config); 076 } 077 078 /** 079 * Constructor. Subclasses must have a default constructor specifying 080 * the server name. 081 * <p> 082 * The server name is used to resolve the Java System properties that define 083 * the server home, config, log and temp directories. 084 * <p> 085 * The home directory is looked in the Java System property 086 * <code>#SERVER_NAME#.home.dir</code>. 087 * <p> 088 * The config directory is looked in the Java System property 089 * <code>#SERVER_NAME#.config.dir</code>, if not defined it resolves to 090 * the <code>#SERVER_HOME_DIR#/conf</code> directory. 091 * <p> 092 * The log directory is looked in the Java System property 093 * <code>#SERVER_NAME#.log.dir</code>, if not defined it resolves to 094 * the <code>#SERVER_HOME_DIR#/log</code> directory. 095 * <p> 096 * The temp directory is looked in the Java System property 097 * <code>#SERVER_NAME#.temp.dir</code>, if not defined it resolves to 098 * the <code>#SERVER_HOME_DIR#/temp</code> directory. 099 * 100 * @param name server name. 101 */ 102 public ServerWebApp(String name) { 103 super(name, getHomeDir(name), 104 getDir(name, CONFIG_DIR, getHomeDir(name) + "/conf"), 105 getDir(name, LOG_DIR, getHomeDir(name) + "/log"), 106 getDir(name, TEMP_DIR, getHomeDir(name) + "/temp"), null); 107 } 108 109 /** 110 * Returns the server home directory. 111 * <p> 112 * It is looked up in the Java System property 113 * <code>#SERVER_NAME#.home.dir</code>. 114 * 115 * @param name the server home directory. 116 * 117 * @return the server home directory. 118 */ 119 static String getHomeDir(String name) { 120 String homeDir = HOME_DIR_TL.get(); 121 if (homeDir == null) { 122 String sysProp = name + HOME_DIR; 123 homeDir = System.getProperty(sysProp); 124 if (homeDir == null) { 125 throw new IllegalArgumentException(MessageFormat.format( 126 "System property [{0}] not defined", sysProp)); 127 } 128 } 129 return homeDir; 130 } 131 132 /** 133 * Convenience method that looks for Java System property defining a 134 * diretory and if not present defaults to the specified directory. 135 * 136 * @param name server name, used as prefix of the Java System property. 137 * @param dirType dir type, use as postfix of the Java System property. 138 * @param defaultDir the default directory to return if the Java System 139 * property <code>name + dirType</code> is not defined. 140 * 141 * @return the directory defined in the Java System property or the 142 * the default directory if the Java System property is not defined. 143 */ 144 static String getDir(String name, String dirType, String defaultDir) { 145 String sysProp = name + dirType; 146 return System.getProperty(sysProp, defaultDir); 147 } 148 149 /** 150 * Initializes the <code>ServletContextListener</code> which initializes 151 * the Server. 152 * 153 * @param event servelt context event. 154 */ 155 @Override 156 public void contextInitialized(ServletContextEvent event) { 157 try { 158 init(); 159 } catch (ServerException ex) { 160 event.getServletContext().log("ERROR: " + ex.getMessage()); 161 throw new RuntimeException(ex); 162 } 163 } 164 165 /** 166 * Resolves the host and port InetSocketAddress the 167 * web server is listening to. 168 * <p> 169 * This implementation looks for the following 2 properties: 170 * <ul> 171 * <li>#SERVER_NAME#.http.hostname</li> 172 * <li>#SERVER_NAME#.http.port</li> 173 * </ul> 174 * 175 * @return the host and port InetSocketAddress the 176 * web server is listening to. 177 * @throws ServerException thrown 178 * if any of the above 2 properties is not defined. 179 */ 180 protected InetSocketAddress resolveAuthority() throws ServerException { 181 String hostnameKey = getName() + HTTP_HOSTNAME; 182 String portKey = getName() + HTTP_PORT; 183 String host = System.getProperty(hostnameKey); 184 String port = System.getProperty(portKey); 185 if (host == null) { 186 throw new ServerException(ServerException.ERROR.S13, hostnameKey); 187 } 188 if (port == null) { 189 throw new ServerException(ServerException.ERROR.S13, portKey); 190 } 191 try { 192 InetAddress add = InetAddress.getByName(host); 193 int portNum = Integer.parseInt(port); 194 return new InetSocketAddress(add, portNum); 195 } catch (UnknownHostException ex) { 196 throw new ServerException(ServerException.ERROR.S14, ex.toString(), ex); 197 } 198 } 199 200 /** 201 * Destroys the <code>ServletContextListener</code> which destroys 202 * the Server. 203 * 204 * @param event servelt context event. 205 */ 206 @Override 207 public void contextDestroyed(ServletContextEvent event) { 208 destroy(); 209 } 210 211 /** 212 * Returns the hostname:port InetSocketAddress the webserver is listening to. 213 * 214 * @return the hostname:port InetSocketAddress the webserver is listening to. 215 */ 216 public InetSocketAddress getAuthority() throws ServerException { 217 synchronized (this) { 218 if (authority == null) { 219 authority = resolveAuthority(); 220 } 221 } 222 return authority; 223 } 224 225 /** 226 * Sets an alternate hostname:port InetSocketAddress to use. 227 * <p> 228 * For testing purposes. 229 * 230 * @param authority alterante authority. 231 */ 232 @VisibleForTesting 233 public void setAuthority(InetSocketAddress authority) { 234 this.authority = authority; 235 } 236 237 238 /** 239 * 240 */ 241 public boolean isSslEnabled() { 242 return Boolean.parseBoolean( 243 System.getProperty(getName() + SSL_ENABLED, "false")); 244 } 245}