001/* 002 * CDDL HEADER START 003 * 004 * The contents of this file are subject to the terms of the 005 * Common Development and Distribution License, Version 1.0 only 006 * (the "License"). You may not use this file except in compliance 007 * with the License. 008 * 009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt 010 * or http://forgerock.org/license/CDDLv1.0.html. 011 * See the License for the specific language governing permissions 012 * and limitations under the License. 013 * 014 * When distributing Covered Code, include this CDDL HEADER in each 015 * file and include the License file at legal-notices/CDDLv1_0.txt. 016 * If applicable, add the following below this CDDL HEADER, with the 017 * fields enclosed by brackets "[]" replaced with your own identifying 018 * information: 019 * Portions Copyright [yyyy] [name of copyright owner] 020 * 021 * CDDL HEADER END 022 * 023 * 024 * Copyright 2006-2009 Sun Microsystems, Inc. 025 * Portions Copyright 2012-2015 ForgeRock AS. 026 */ 027package org.opends.server.api; 028 029import static org.opends.messages.ProtocolMessages.*; 030 031import org.forgerock.i18n.slf4j.LocalizedLogger; 032 033import java.util.Collection; 034import java.util.Collections; 035import java.util.List; 036 037import org.forgerock.i18n.LocalizableMessage; 038import org.opends.server.admin.std.server.ConnectionHandlerCfg; 039import org.opends.server.core.ServerContext; 040import org.forgerock.opendj.config.server.ConfigException; 041import org.opends.server.monitors.ConnectionHandlerMonitor; 042import org.opends.server.types.DN; 043import org.opends.server.types.HostPort; 044import org.opends.server.types.InitializationException; 045 046/** 047 * This class defines the set of methods and structures that must be 048 * implemented by a Directory Server connection handler. 049 * 050 * @param <T> 051 * The type of connection handler configuration handled by 052 * this connection handler implementation. 053 */ 054@org.opends.server.types.PublicAPI( 055 stability=org.opends.server.types.StabilityLevel.VOLATILE, 056 mayInstantiate=false, 057 mayExtend=true, 058 mayInvoke=false) 059public abstract class ConnectionHandler 060 <T extends ConnectionHandlerCfg> 061 extends DirectoryThread 062{ 063 064 private static final LocalizedLogger logger = LocalizedLogger.getLoggerForThisClass(); 065 066 /** The monitor associated with this connection handler. */ 067 private ConnectionHandlerMonitor monitor; 068 069 /** Is this handler the admin connection handler. */ 070 private boolean isAdminConnectionHandler; 071 072 073 074 /** 075 * Creates a new instance of this connection handler. This must be 076 * called by all connection handlers, and all connection handlers 077 * must provide default constructors (i.e., those that do not take 078 * any arguments) that invoke this constructor. 079 * 080 * @param threadName 081 * The name to use for this thread. 082 */ 083 protected ConnectionHandler(String threadName) { 084 super(threadName); 085 086 monitor = null; 087 } 088 089 090 091 /** 092 * Closes this connection handler so that it will no longer accept 093 * new client connections. Implementations should disconnect any 094 * existing connections. 095 * 096 * @param finalizeReason 097 * The reason that this connection handler should be 098 * finalized. 099 */ 100 public abstract void finalizeConnectionHandler( 101 LocalizableMessage finalizeReason); 102 103 104 105 /** 106 * Retrieves a name that may be used to refer to this connection 107 * handler. Every connection handler instance (even handlers of the 108 * same type) must have a unique name. 109 * 110 * @return A unique name that may be used to refer to this 111 * connection handler. 112 */ 113 public abstract String getConnectionHandlerName(); 114 115 116 117 /** 118 * Retrieves an unmodifiable set of enabled SSL cipher suites configured for 119 * this connection handler, if applicable. Implementations must return an 120 * empty set if use of SSL/TLS is not possible. 121 * 122 * @return The set of enabled SSL cipher suites configured for this connection 123 * handler. 124 */ 125 public Collection<String> getEnabledSSLCipherSuites() 126 { 127 return Collections.emptyList(); 128 } 129 130 131 132 /** 133 * Retrieves the set of enabled SSL protocols configured for this connection 134 * handler. Implementations must return an empty set if use of SSL/TLS is not 135 * possible. 136 * 137 * @return The set of enabled SSL protocols configured for this connection 138 * handler. 139 */ 140 public Collection<String> getEnabledSSLProtocols() 141 { 142 return Collections.emptyList(); 143 } 144 145 146 147 /** 148 * Retrieves the DN of the configuration entry with which this alert 149 * generator is associated. 150 * 151 * @return The DN of the configuration entry with which this alert 152 * generator is associated. 153 */ 154 public abstract DN getComponentEntryDN(); 155 156 /** 157 * Retrieves the name of the protocol used to communicate with 158 * clients. It should take into account any special naming that may 159 * be needed to express any security mechanisms or other constraints 160 * in place (e.g., "LDAPS" for LDAP over SSL). 161 * 162 * @return The name of the protocol used to communicate with 163 * clients. 164 */ 165 public abstract String getProtocol(); 166 167 168 169 /** 170 * Retrieves information about the listener(s) that will be used to 171 * accept client connections. 172 * 173 * @return Information about the listener(s) that will be used to 174 * accept client connections, or an empty list if this 175 * connection handler does not accept connections from 176 * network clients. 177 */ 178 public abstract Collection<HostPort> getListeners(); 179 180 181 182 /** 183 * Retrieves the set of active client connections that have been 184 * established through this connection handler. 185 * 186 * @return The set of active client connections that have been 187 * established through this connection handler. 188 */ 189 public abstract Collection<ClientConnection> getClientConnections(); 190 191 192 193 /** 194 * Initializes this connection handler provider based on the 195 * information in the provided connection handler configuration. 196 * 197 * @param serverContext 198 * The server context. 199 * @param configuration 200 * The connection handler configuration that contains the 201 * information to use to initialize this connection 202 * handler. 203 * @throws ConfigException 204 * If an unrecoverable problem arises in the process of 205 * performing the initialization as a result of the server 206 * configuration. 207 * @throws InitializationException 208 * If a problem occurs during initialization that is not 209 * related to the server configuration. 210 */ 211 public abstract void initializeConnectionHandler(ServerContext serverContext, T configuration) 212 throws ConfigException, InitializationException; 213 214 215 216 /** 217 * Indicates whether the provided configuration is acceptable for 218 * this connection handler. It should be possible to call this 219 * method on an uninitialized connection handler instance in order 220 * to determine whether the connection handler would be able to use 221 * the provided configuration. 222 * <BR><BR> 223 * Note that implementations which use a subclass of the provided 224 * configuration class will likely need to cast the configuration 225 * to the appropriate subclass type. 226 * 227 * @param configuration The connection handler configuration 228 * for which to make the determination. 229 * @param unacceptableReasons A list that may be used to hold the 230 * reasons that the provided 231 * configuration is not acceptable. 232 * 233 * @return {@code true} if the provided configuration is acceptable 234 * for this connection handler, or {@code false} if not. 235 */ 236 public boolean isConfigurationAcceptable( 237 ConnectionHandlerCfg configuration, 238 List<LocalizableMessage> unacceptableReasons) 239 { 240 // This default implementation does not perform any special 241 // validation. It should be overridden by connection handler 242 // implementations that wish to perform more detailed validation. 243 return true; 244 } 245 246 247 248 /** 249 * Operates in a loop, accepting new connections and ensuring that 250 * requests on those connections are handled properly. 251 */ 252 @Override 253 public abstract void run(); 254 255 256 257 /** 258 * Retrieves the monitor instance for this connection handler. 259 * 260 * @return The monitor instance for this connection handler, or 261 * {@code null} if none has been provided. 262 */ 263 public final ConnectionHandlerMonitor getConnectionHandlerMonitor() 264 { 265 return monitor; 266 } 267 268 269 270 /** 271 * Sets the monitor instance for this connection handler. 272 * 273 * @param monitor The monitor instance for this connection 274 * handler. 275 */ 276 public final void setConnectionHandlerMonitor( 277 ConnectionHandlerMonitor monitor) 278 { 279 this.monitor = monitor; 280 } 281 282 283 284 /** 285 * Sets this connection handler as the admin connection handler. 286 */ 287 public void setAdminConnectionHandler() { 288 isAdminConnectionHandler = true; 289 } 290 291 292 /** 293 * Returns whether this connection handler is the admin 294 * connection handler. 295 * @return boolean True if this connection handler is the admin 296 * connection handler, false otherwise 297 */ 298 public boolean isAdminConnectionHandler() { 299 return isAdminConnectionHandler; 300 } 301 302 303 /** 304 * Determine the number of request handlers. 305 * 306 * @param numRequestHandlers 307 * the number of request handlers from the configuration. 308 * @param friendlyName 309 * the friendly name of this connection handler 310 * @return the number of request handlers from the configuration determined 311 * from the configuration or from the number of available processors 312 * on the current machine 313 */ 314 public int getNumRequestHandlers(Integer numRequestHandlers, 315 String friendlyName) 316 { 317 if (numRequestHandlers == null) 318 { 319 // Automatically choose based on the number of processors. 320 int cpus = Runtime.getRuntime().availableProcessors(); 321 int value = Math.max(2, cpus / 2); 322 323 logger.debug(INFO_ERGONOMIC_SIZING_OF_REQUEST_HANDLER_THREADS, friendlyName, 324 value); 325 326 return value; 327 } 328 else 329 { 330 return numRequestHandlers; 331 } 332 } 333 334 /** 335 * Retrieves a string representation of this connection handler. 336 * 337 * @return A string representation of this connection handler. 338 */ 339 @Override 340 public String toString() { 341 StringBuilder buffer = new StringBuilder(); 342 toString(buffer); 343 return buffer.toString(); 344 } 345 346 347 348 /** 349 * Appends a string representation of this connection handler to the 350 * provided buffer. 351 * 352 * @param buffer 353 * The buffer to which the information should be appended. 354 */ 355 public abstract void toString(StringBuilder buffer); 356}