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