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 2011-2016 ForgeRock AS.
016 */
017package org.opends.server.loggers;
018import java.util.Collection;
019
020import org.forgerock.i18n.LocalizableMessage;
021import org.forgerock.opendj.config.ClassPropertyDefinition;
022import org.forgerock.opendj.server.config.meta.AccessLogPublisherCfgDefn;
023import org.forgerock.opendj.server.config.server.AccessLogPublisherCfg;
024import org.opends.server.api.ClientConnection;
025import org.opends.server.core.*;
026import org.opends.server.types.DisconnectReason;
027import org.opends.server.types.SearchResultEntry;
028import org.opends.server.types.SearchResultReference;
029
030import static org.opends.messages.ConfigMessages.*;
031
032/**
033 * This class defines the wrapper that will invoke all registered access loggers
034 * for each type of request received or response sent.
035 */
036public class AccessLogger extends AbstractLogger
037    <AccessLogPublisher<AccessLogPublisherCfg>, AccessLogPublisherCfg>
038{
039
040  private static LoggerStorage
041      <AccessLogPublisher<AccessLogPublisherCfg>, AccessLogPublisherCfg>
042      loggerStorage = new LoggerStorage<>();
043
044  /** The singleton instance of this class. */
045  private static final AccessLogger instance = new AccessLogger();
046
047  /** The constructor for this class. */
048  private AccessLogger()
049  {
050    super((Class) AccessLogPublisher.class,
051        ERR_CONFIG_LOGGER_INVALID_ACCESS_LOGGER_CLASS);
052  }
053
054  @Override
055  protected ClassPropertyDefinition getJavaClassPropertyDefinition()
056  {
057    return AccessLogPublisherCfgDefn.getInstance()
058        .getJavaClassPropertyDefinition();
059  }
060
061  @Override
062  protected Collection<AccessLogPublisher<AccessLogPublisherCfg>> getLogPublishers()
063  {
064    return loggerStorage.getLogPublishers();
065  }
066
067  /**
068   * Retrieve the singleton instance of this class.
069   *
070   * @return The singleton instance of this logger.
071   */
072  public static AccessLogger getInstance()
073  {
074    return instance;
075  }
076
077  /**
078   * Returns all the registered access log publishers.
079   *
080   * @return a Collection of {@link AccessLogPublisher} objects
081   */
082  private static Collection
083      <AccessLogPublisher<AccessLogPublisherCfg>> getAccessLogPublishers()
084  {
085    return loggerStorage.getLogPublishers();
086  }
087
088
089  /**
090   * Writes a message to the access logger with information about a new client
091   * connection that has been established, regardless of whether it will be
092   * immediately terminated.
093   *
094   * @param  clientConnection  The client connection that has been established.
095   */
096  public static void logConnect(ClientConnection clientConnection)
097  {
098    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
099    {
100      publisher.logConnect(clientConnection);
101    }
102  }
103
104
105
106  /**
107   * Writes a message to the access logger with information about the
108   * termination of an existing client connection.
109   *
110   * @param  clientConnection  The client connection that has been terminated.
111   * @param  disconnectReason  A generic disconnect reason for the connection
112   *                           termination.
113   * @param  message           A human-readable message that can provide
114   *                           additional information about the disconnect.
115   */
116  public static void logDisconnect(ClientConnection clientConnection,
117                                   DisconnectReason disconnectReason,
118                                   LocalizableMessage message)
119  {
120    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
121    {
122      publisher.logDisconnect(clientConnection, disconnectReason, message);
123    }
124  }
125
126
127
128  /**
129   * Writes a message to the access logger with information about the abandon
130   * request associated with the provided abandon operation.
131   *
132   * @param  abandonOperation  The abandon operation containing the information
133   *                           to use to log the abandon request.
134   */
135  public static void logAbandonRequest(AbandonOperation abandonOperation)
136  {
137    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
138    {
139      publisher.logAbandonRequest(abandonOperation);
140    }
141  }
142
143
144
145  /**
146   * Writes a message to the access logger with information about the result of
147   * the provided abandon operation.
148   *
149   * @param  abandonOperation  The abandon operation containing the information
150   *                           to use to log the abandon result.
151   */
152  public static void logAbandonResult(AbandonOperation abandonOperation)
153  {
154    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
155    {
156      publisher.logAbandonResult(abandonOperation);
157    }
158  }
159
160
161
162  /**
163   * Writes a message to the access logger with information about the add
164   * request associated with the provided add operation.
165   *
166   * @param  addOperation  The add operation containing the information to use
167   *                       to log the add request.
168   */
169  public static void logAddRequest(AddOperation addOperation)
170  {
171    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
172    {
173      publisher.logAddRequest(addOperation);
174    }
175  }
176
177
178
179  /**
180   * Writes a message to the access logger with information about the add
181   * response associated with the provided add operation.
182   *
183   * @param  addOperation  The add operation containing the information to use
184   *                       to log the add response.
185   */
186  public static void logAddResponse(AddOperation addOperation)
187  {
188    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
189    {
190      publisher.logAddResponse(addOperation);
191    }
192  }
193
194
195
196  /**
197   * Writes a message to the access logger with information about the bind
198   * request associated with the provided bind operation.
199   *
200   * @param  bindOperation  The bind operation containing the information to use
201   *                        to log the bind request.
202   */
203  public static void logBindRequest(BindOperation bindOperation)
204  {
205    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
206    {
207      publisher.logBindRequest(bindOperation);
208    }
209  }
210
211
212
213  /**
214   * Writes a message to the access logger with information about the bind
215   * response associated with the provided bind operation.
216   *
217   * @param  bindOperation  The bind operation containing the information to use
218   *                        to log the bind response.
219   */
220  public static void logBindResponse(BindOperation bindOperation)
221  {
222    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
223    {
224      publisher.logBindResponse(bindOperation);
225    }
226  }
227
228
229
230  /**
231   * Writes a message to the access logger with information about the compare
232   * request associated with the provided compare operation.
233   *
234   * @param  compareOperation  The compare operation containing the information
235   *                           to use to log the compare request.
236   */
237  public static void logCompareRequest(CompareOperation compareOperation)
238  {
239    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
240    {
241      publisher.logCompareRequest(compareOperation);
242    }
243  }
244
245
246
247  /**
248   * Writes a message to the access logger with information about the compare
249   * response associated with the provided compare operation.
250   *
251   * @param  compareOperation  The compare operation containing the information
252   *                           to use to log the compare response.
253   */
254  public static void logCompareResponse(CompareOperation compareOperation)
255  {
256    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
257    {
258      publisher.logCompareResponse(compareOperation);
259    }
260  }
261
262
263
264  /**
265   * Writes a message to the access logger with information about the delete
266   * request associated with the provided delete operation.
267   *
268   * @param  deleteOperation  The delete operation containing the information to
269   *                          use to log the delete request.
270   */
271  public static void logDeleteRequest(DeleteOperation deleteOperation)
272  {
273    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
274    {
275      publisher.logDeleteRequest(deleteOperation);
276    }
277  }
278
279
280
281  /**
282   * Writes a message to the access logger with information about the delete
283   * response associated with the provided delete operation.
284   *
285   * @param  deleteOperation  The delete operation containing the information to
286   *                           use to log the delete response.
287   */
288  public static void logDeleteResponse(DeleteOperation deleteOperation)
289  {
290    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
291    {
292      publisher.logDeleteResponse(deleteOperation);
293    }
294  }
295
296
297
298  /**
299   * Writes a message to the access logger with information about the extended
300   * request associated with the provided extended operation.
301   *
302   * @param  extendedOperation  The extended operation containing the
303   *                            information to use to log the extended request.
304   */
305  public static void logExtendedRequest(ExtendedOperation extendedOperation)
306  {
307    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
308    {
309      publisher.logExtendedRequest(extendedOperation);
310    }
311  }
312
313
314
315  /**
316   * Writes a message to the access logger with information about the extended
317   * response associated with the provided extended operation.
318   *
319   * @param  extendedOperation  The extended operation containing the
320   *                            information to use to log the extended response.
321   */
322  public static void logExtendedResponse(ExtendedOperation extendedOperation)
323  {
324    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
325    {
326      publisher.logExtendedResponse(extendedOperation);
327    }
328  }
329
330
331
332  /**
333   * Writes a message to the access logger with information about the modify
334   * request associated with the provided modify operation.
335   *
336   * @param  modifyOperation  The modify operation containing the information to
337   *                          use to log the modify request.
338   */
339  public static void logModifyRequest(ModifyOperation modifyOperation)
340  {
341    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
342    {
343      publisher.logModifyRequest(modifyOperation);
344    }
345  }
346
347
348
349  /**
350   * Writes a message to the access logger with information about the modify
351   * response associated with the provided modify operation.
352   *
353   * @param  modifyOperation  The modify operation containing the information to
354   *                          use to log the modify response.
355   */
356  public static void logModifyResponse(ModifyOperation modifyOperation)
357  {
358    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
359    {
360      publisher.logModifyResponse(modifyOperation);
361    }
362  }
363
364
365
366  /**
367   * Writes a message to the access logger with information about the modify DN
368   * request associated with the provided modify DN operation.
369   *
370   * @param  modifyDNOperation  The modify DN operation containing the
371   *                            information to use to log the modify DN request.
372   */
373  public static void logModifyDNRequest(ModifyDNOperation modifyDNOperation)
374  {
375    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
376    {
377      publisher.logModifyDNRequest(modifyDNOperation);
378    }
379  }
380
381
382
383  /**
384   * Writes a message to the access logger with information about the modify DN
385   * response associated with the provided modify DN operation.
386   *
387   * @param  modifyDNOperation  The modify DN operation containing the
388   *                            information to use to log the modify DN
389   *                            response.
390   */
391  public static void logModifyDNResponse(ModifyDNOperation modifyDNOperation)
392  {
393    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
394    {
395      publisher.logModifyDNResponse(modifyDNOperation);
396    }
397  }
398
399
400
401  /**
402   * Writes a message to the access logger with information about the search
403   * request associated with the provided search operation.
404   *
405   * @param  searchOperation  The search operation containing the information to
406   *                          use to log the search request.
407   */
408  public static void logSearchRequest(SearchOperation searchOperation)
409  {
410    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
411    {
412      publisher.logSearchRequest(searchOperation);
413    }
414  }
415
416
417
418  /**
419   * Writes a message to the access logger with information about the search
420   * result entry that matches the criteria associated with the provided search
421   * operation.
422   *
423   * @param  searchOperation  The search operation with which the search result
424   *                          entry is associated.
425   * @param  searchEntry      The search result entry to be logged.
426   */
427  public static void logSearchResultEntry(SearchOperation searchOperation,
428                                          SearchResultEntry searchEntry)
429  {
430    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
431    {
432      publisher.logSearchResultEntry(searchOperation, searchEntry);
433    }
434  }
435
436
437
438  /**
439   * Writes a message to the access logger with information about the search
440   * result reference returned while processing the associated search operation.
441   *
442   * @param  searchOperation  The search operation with which the search result
443   *                          reference is associated.
444   * @param  searchReference  The search result reference to be logged.
445   */
446  public static void logSearchResultReference(SearchOperation searchOperation,
447                          SearchResultReference searchReference)
448  {
449    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
450    {
451      publisher.logSearchResultReference(searchOperation, searchReference);
452    }
453  }
454
455
456
457  /**
458   * Writes a message to the access logger with information about the completion
459   * of the provided search operation.
460   *
461   * @param  searchOperation  The search operation containing the information
462   *                          to use to log the search result done message.
463   */
464  public static void logSearchResultDone(SearchOperation searchOperation)
465  {
466    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
467    {
468      publisher.logSearchResultDone(searchOperation);
469    }
470  }
471
472
473
474  /**
475   * Writes a message to the access logger with information about the unbind
476   * request associated with the provided unbind operation.
477   *
478   * @param  unbindOperation  The unbind operation containing the information to
479   *                          use to log the unbind request.
480   */
481  public static void logUnbind(UnbindOperation unbindOperation)
482  {
483    for (AccessLogPublisher<?> publisher : getAccessLogPublishers())
484    {
485      publisher.logUnbind(unbindOperation);
486    }
487  }
488
489  @Override
490  public final synchronized void addLogPublisher(
491      AccessLogPublisher<AccessLogPublisherCfg> publisher)
492  {
493    loggerStorage.addLogPublisher(publisher);
494  }
495
496  @Override
497  public final synchronized boolean removeLogPublisher(
498      AccessLogPublisher<AccessLogPublisherCfg> publisher)
499  {
500    return loggerStorage.removeLogPublisher(publisher);
501  }
502
503  @Override
504  public final synchronized void removeAllLogPublishers()
505  {
506    loggerStorage.removeAllLogPublishers();
507    // Access logger may have not been fully initialized
508    if (getServerContext() != null && getServerContext().getCommonAudit() != null)
509    {
510      getServerContext().getCommonAudit().shutdown();
511    }
512  }
513}
514