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 * Portions Copyright 2013-2015 ForgeRock AS.
015 */
016package org.opends.server.loggers;
017
018import static org.opends.messages.ConfigMessages.*;
019
020import java.util.Collection;
021
022import org.opends.server.admin.ClassPropertyDefinition;
023import org.opends.server.admin.std.meta.HTTPAccessLogPublisherCfgDefn;
024import org.opends.server.admin.std.server.HTTPAccessLogPublisherCfg;
025
026/**
027 * This class defines the wrapper that will invoke all registered HTTP access
028 * loggers for each type of request received or response sent.
029 */
030public class HTTPAccessLogger extends AbstractLogger
031<HTTPAccessLogPublisher<HTTPAccessLogPublisherCfg>, HTTPAccessLogPublisherCfg>
032{
033
034  private static LoggerStorage
035  <HTTPAccessLogPublisher<HTTPAccessLogPublisherCfg>, HTTPAccessLogPublisherCfg>
036  loggerStorage = new LoggerStorage<>();
037
038  /** The singleton instance of this class for configuration purposes. */
039  private static final HTTPAccessLogger instance = new HTTPAccessLogger();
040
041  /**
042   * The constructor for this class.
043   */
044  private HTTPAccessLogger()
045  {
046    super((Class) HTTPAccessLogPublisher.class,
047        ERR_CONFIG_LOGGER_INVALID_HTTP_ACCESS_LOGGER_CLASS);
048  }
049
050  /** {@inheritDoc} */
051  @Override
052  protected ClassPropertyDefinition getJavaClassPropertyDefinition()
053  {
054    return HTTPAccessLogPublisherCfgDefn.getInstance()
055        .getJavaClassPropertyDefinition();
056  }
057
058  /** {@inheritDoc} */
059  @Override
060  protected Collection<HTTPAccessLogPublisher<HTTPAccessLogPublisherCfg>> getLogPublishers()
061  {
062    return loggerStorage.getLogPublishers();
063  }
064
065  /**
066   * Retrieve the singleton instance of this class.
067   *
068   * @return The singleton instance of this logger.
069   */
070  public static HTTPAccessLogger getInstance()
071  {
072    return instance;
073  }
074
075  /**
076   * Returns all the registered HTTP access log publishers.
077   *
078   * @return a Collection of {@link HTTPAccessLogPublisher} objects
079   */
080  public static Collection<HTTPAccessLogPublisher<HTTPAccessLogPublisherCfg>>
081      getHTTPAccessLogPublishers()
082  {
083    return loggerStorage.getLogPublishers();
084  }
085
086  /**
087   * Logs the given HTTPRequestInfo.
088   *
089   * @param requestInfo
090   *          the HTTP request info to log
091   */
092  public static void logRequestInfo(HTTPRequestInfo requestInfo)
093  {
094    for (HTTPAccessLogPublisher<?> publisher : loggerStorage.getLogPublishers())
095    {
096      publisher.logRequestInfo(requestInfo);
097    }
098  }
099
100  /** {@inheritDoc} */
101  @Override
102  public final synchronized void addLogPublisher(
103      HTTPAccessLogPublisher<HTTPAccessLogPublisherCfg> publisher)
104  {
105    loggerStorage.addLogPublisher(publisher);
106  }
107
108  /** {@inheritDoc} */
109  @Override
110  public final synchronized boolean removeLogPublisher(
111      HTTPAccessLogPublisher<HTTPAccessLogPublisherCfg> publisher)
112  {
113    return loggerStorage.removeLogPublisher(publisher);
114  }
115
116  @Override
117  public final synchronized void removeAllLogPublishers()
118  {
119    loggerStorage.removeAllLogPublishers();
120    // Access logger may have not been fully initialized
121    if (getServerContext() != null && getServerContext().getCommonAudit() != null)
122    {
123      getServerContext().getCommonAudit().shutdown();
124    }
125  }
126
127}