001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2006-2009 Sun Microsystems, Inc. 015 * Portions Copyright 2014-2016 ForgeRock AS. 016 */ 017package org.opends.server.protocols.internal; 018import org.forgerock.i18n.LocalizableMessage; 019 020 021 022 023import java.util.Collection; 024import java.util.LinkedList; 025 026import org.opends.server.admin.std.server.*; 027import org.opends.server.api.ClientConnection; 028import org.opends.server.api.ConnectionHandler; 029import org.opends.server.core.ServerContext; 030import org.forgerock.opendj.config.server.ConfigException; 031import org.forgerock.opendj.ldap.DN; 032import org.opends.server.types.InitializationException; 033import org.opends.server.types.HostPort; 034 035 036 037/** 038 * This class defines a Directory Server connection handler that will 039 * handle internal "connections". 040 */ 041@org.opends.server.types.PublicAPI( 042 stability=org.opends.server.types.StabilityLevel.PRIVATE, 043 mayInstantiate=false, 044 mayExtend=false, 045 mayInvoke=false) 046public final class InternalConnectionHandler 047 extends ConnectionHandler 048{ 049 /** The singleton instance of this internal connection handler. */ 050 private static InternalConnectionHandler handlerInstance = 051 new InternalConnectionHandler(); 052 053 /** The list of "connections" associated with this connection handler. */ 054 private LinkedList<ClientConnection> connectionList; 055 056 /** The list of listeners associated with this connection handler. */ 057 private LinkedList<HostPort> listeners; 058 059 /** The name of the protocol for this connection handler. */ 060 private String protocol; 061 062 /** Configuration object of the connection handler. */ 063 private ConnectionHandlerCfg configuration; 064 065 066 /** 067 * Creates a new instance of this connection handler. All 068 * initialization should be done in the 069 * <CODE>initializeConnectionHandler</CODE> method. 070 */ 071 private InternalConnectionHandler() 072 { 073 super("Internal Connection Handler Thread"); 074 075 076 // Since we can't guarantee that the initializeConnectionHandler 077 // method will always be called for this method, we'll do the 078 // necessary "initialization" here. 079 protocol = "internal"; 080 connectionList = new LinkedList<>(); 081 listeners = new LinkedList<>(); 082 } 083 084 085 086 /** 087 * Retrieves the static instance of this internal connection 088 * handler. 089 * 090 * @return The static instance of this internal connection handler. 091 */ 092 public static InternalConnectionHandler getInstance() 093 { 094 return handlerInstance; 095 } 096 097 098 099 /** 100 * Initializes this connection handler provider based on the 101 * information in the provided connection handler configuration. 102 * 103 * @param serverContext The server context. 104 * @param configuration The connection handler configuration that 105 * contains the information to use to 106 * initialize this connection handler. 107 * 108 * @throws ConfigException If an unrecoverable problem arises in 109 * the process of performing the 110 * initialization as a result of the 111 * server configuration. 112 * 113 * @throws InitializationException If a problem occurs during 114 * initialization that is not 115 * related to the server 116 * configuration. 117 */ 118 @Override 119 public void initializeConnectionHandler(ServerContext serverContext, ConnectionHandlerCfg configuration) 120 throws ConfigException, InitializationException 121 { 122 this.configuration = configuration; 123 } 124 125 126 127 /** {@inheritDoc} */ 128 @Override 129 public void finalizeConnectionHandler(LocalizableMessage finalizeReason) 130 { 131 // No implementation is required. 132 } 133 134 135 136 /** 137 * Retrieves a name that may be used to refer to this connection 138 * handler. Every connection handler instance (even handlers of the 139 * same type) must have a unique name. 140 * 141 * @return A unique name that may be used to refer to this 142 * connection handler. 143 */ 144 @Override 145 public String getConnectionHandlerName() 146 { 147 return "Internal Connection Handler"; 148 } 149 150 151 152 /** 153 * Retrieves the name of the protocol used to communicate with 154 * clients. It should take into account any special naming that may 155 * be needed to express any security mechanisms or other constraints 156 * in place (e.g., "LDAPS" for LDAP over SSL). 157 * 158 * @return The name of the protocol used to communicate with 159 * clients. 160 */ 161 @Override 162 public String getProtocol() 163 { 164 return protocol; 165 } 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 @Override 179 public Collection<HostPort> getListeners() 180 { 181 return listeners; 182 } 183 184 185 186 /** 187 * Retrieves the set of active client connections that have been 188 * established through this connection handler. 189 * 190 * @return The set of active client connections that have been 191 * established through this connection handler. 192 */ 193 @Override 194 public Collection<ClientConnection> getClientConnections() 195 { 196 return connectionList; 197 } 198 199 200 201 /** 202 * Operates in a loop, accepting new connections and ensuring that 203 * requests on those connections are handled properly. 204 */ 205 @Override 206 public void run() 207 { 208 // No implementation is required since this connection handler 209 // won't actually accept connections. 210 return; 211 } 212 213 214 215 /** 216 * Retrieves a string representation of this connection handler. 217 * 218 * @return A string representation of this connection handler. 219 */ 220 @Override 221 public String toString() 222 { 223 return "Internal Connection Handler"; 224 } 225 226 227 228 /** 229 * Appends a string representation of this connection handler to the 230 * provided buffer. 231 * 232 * @param buffer The buffer to which the information should be 233 * appended. 234 */ 235 @Override 236 public void toString(StringBuilder buffer) 237 { 238 buffer.append("Internal Connection Handler"); 239 } 240 241 /** 242 * Called near the end of server shutdown. This ensures that a new 243 * InternalClientConnection is created if the server is immediately 244 * restarted as part of an in-core restart. 245 */ 246 public static void clearRootClientConnectionAtShutdown() 247 { 248 InternalClientConnection.clearRootClientConnectionAtShutdown(); 249 } 250 251 /** 252 * Return the configuration dn of the object. 253 * @return DN of the entry. 254 */ 255 @Override 256 public DN getComponentEntryDN() { 257 return this.configuration.dn(); 258 } 259 260} 261