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 2014-2015 ForgeRock AS
026 */
027package org.opends.server.protocols.internal;
028import org.forgerock.i18n.LocalizableMessage;
029
030
031
032import java.util.Collection;
033import java.util.LinkedList;
034
035import org.opends.server.admin.std.server.*;
036import org.opends.server.api.ClientConnection;
037import org.opends.server.api.ConnectionHandler;
038import org.forgerock.opendj.config.server.ConfigException;
039import org.opends.server.types.DN;
040import org.opends.server.types.InitializationException;
041import org.opends.server.types.HostPort;
042
043
044
045/**
046 * This class defines a Directory Server connection handler that will
047 * handle internal "connections".
048 */
049@org.opends.server.types.PublicAPI(
050     stability=org.opends.server.types.StabilityLevel.PRIVATE,
051     mayInstantiate=false,
052     mayExtend=false,
053     mayInvoke=false)
054public final class InternalConnectionHandler
055       extends ConnectionHandler
056{
057  /** The singleton instance of this internal connection handler. */
058  private static InternalConnectionHandler handlerInstance =
059       new InternalConnectionHandler();
060
061  /** The list of "connections" associated with this connection handler. */
062  private LinkedList<ClientConnection> connectionList;
063
064  /** The list of listeners associated with this connection handler. */
065  private LinkedList<HostPort> listeners;
066
067  /** The name of the protocol for this connection handler. */
068  private String protocol;
069
070  /** Configuration object of the connection handler. */
071  private ConnectionHandlerCfg configuration;
072
073
074  /**
075   * Creates a new instance of this connection handler.  All
076   * initialization should be done in the
077   * <CODE>initializeConnectionHandler</CODE> method.
078   */
079  private InternalConnectionHandler()
080  {
081    super("Internal Connection Handler Thread");
082
083
084    // Since we can't guarantee that the initializeConnectionHandler
085    // method will always be called for this method, we'll do the
086    // necessary "initialization" here.
087    protocol       = "internal";
088    connectionList = new LinkedList<>();
089    listeners      = new LinkedList<>();
090  }
091
092
093
094  /**
095   * Retrieves the static instance of this internal connection
096   * handler.
097   *
098   * @return  The static instance of this internal connection handler.
099   */
100  public static InternalConnectionHandler getInstance()
101  {
102    return handlerInstance;
103  }
104
105
106
107  /**
108   * Initializes this connection handler provider based on the
109   * information in the provided connection handler configuration.
110   *
111   * @param  configuration  The connection handler configuration that
112   *                        contains the information to use to
113   *                        initialize this connection handler.
114   *
115   * @throws  ConfigException  If an unrecoverable problem arises in
116   *                           the process of performing the
117   *                           initialization as a result of the
118   *                           server configuration.
119   *
120   * @throws  InitializationException  If a problem occurs during
121   *                                   initialization that is not
122   *                                   related to the server
123   *                                   configuration.
124   */
125  @Override
126  public void initializeConnectionHandler(
127                   ConnectionHandlerCfg configuration)
128      throws ConfigException, InitializationException
129  {
130    this.configuration = configuration;
131  }
132
133
134
135  /** {@inheritDoc} */
136  @Override
137  public void finalizeConnectionHandler(LocalizableMessage finalizeReason)
138  {
139    // No implementation is required.
140  }
141
142
143
144  /**
145   * Retrieves a name that may be used to refer to this connection
146   * handler.  Every connection handler instance (even handlers of the
147   * same type) must have a unique name.
148   *
149   * @return  A unique name that may be used to refer to this
150   *          connection handler.
151   */
152  @Override
153  public String getConnectionHandlerName()
154  {
155    return "Internal Connection Handler";
156  }
157
158
159
160  /**
161   * Retrieves the name of the protocol used to communicate with
162   * clients.  It should take into account any special naming that may
163   * be needed to express any security mechanisms or other constraints
164   * in place (e.g., "LDAPS" for LDAP over SSL).
165   *
166   * @return  The name of the protocol used to communicate with
167   *          clients.
168   */
169  @Override
170  public String getProtocol()
171  {
172    return protocol;
173  }
174
175
176
177  /**
178   * Retrieves information about the listener(s) that will be used to
179   * accept client connections.
180   *
181   * @return  Information about the listener(s) that will be used to
182   *          accept client connections, or an empty list if this
183   *          connection handler does not accept connections from
184   *          network clients.
185   */
186  @Override
187  public Collection<HostPort> getListeners()
188  {
189    return listeners;
190  }
191
192
193
194  /**
195   * Retrieves the set of active client connections that have been
196   * established through this connection handler.
197   *
198   * @return  The set of active client connections that have been
199   *          established through this connection handler.
200   */
201  @Override
202  public Collection<ClientConnection> getClientConnections()
203  {
204    return connectionList;
205  }
206
207
208
209  /**
210   * Operates in a loop, accepting new connections and ensuring that
211   * requests on those connections are handled properly.
212   */
213  @Override
214  public void run()
215  {
216    // No implementation is required since this connection handler
217    // won't actually accept connections.
218    return;
219  }
220
221
222
223  /**
224   * Retrieves a string representation of this connection handler.
225   *
226   * @return  A string representation of this connection handler.
227   */
228  @Override
229  public String toString()
230  {
231    return "Internal Connection Handler";
232  }
233
234
235
236  /**
237   * Appends a string representation of this connection handler to the
238   * provided buffer.
239   *
240   * @param  buffer  The buffer to which the information should be
241   *                 appended.
242   */
243  @Override
244  public void toString(StringBuilder buffer)
245  {
246    buffer.append("Internal Connection Handler");
247  }
248
249  /**
250   * Called near the end of server shutdown.  This ensures that a new
251   * InternalClientConnection is created if the server is immediately
252   * restarted as part of an in-core restart.
253   */
254  public static void clearRootClientConnectionAtShutdown()
255  {
256    InternalClientConnection.clearRootClientConnectionAtShutdown();
257  }
258
259  /**
260   * Return the configuration dn of the object.
261   * @return DN of the entry.
262   */
263  @Override
264  public DN getComponentEntryDN() {
265      return this.configuration.dn();
266  }
267
268}
269