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-2016 ForgeRock AS. 015 */ 016package org.opends.server.loggers; 017 018import static org.opends.messages.ConfigMessages.*; 019 020import java.util.Collection; 021 022import org.forgerock.opendj.config.ClassPropertyDefinition; 023import org.forgerock.opendj.server.config.meta.HTTPAccessLogPublisherCfgDefn; 024import org.forgerock.opendj.server.config.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 /** The constructor for this class. */ 042 private HTTPAccessLogger() 043 { 044 super((Class) HTTPAccessLogPublisher.class, 045 ERR_CONFIG_LOGGER_INVALID_HTTP_ACCESS_LOGGER_CLASS); 046 } 047 048 @Override 049 protected ClassPropertyDefinition getJavaClassPropertyDefinition() 050 { 051 return HTTPAccessLogPublisherCfgDefn.getInstance() 052 .getJavaClassPropertyDefinition(); 053 } 054 055 @Override 056 protected Collection<HTTPAccessLogPublisher<HTTPAccessLogPublisherCfg>> getLogPublishers() 057 { 058 return loggerStorage.getLogPublishers(); 059 } 060 061 /** 062 * Retrieve the singleton instance of this class. 063 * 064 * @return The singleton instance of this logger. 065 */ 066 public static HTTPAccessLogger getInstance() 067 { 068 return instance; 069 } 070 071 /** 072 * Returns all the registered HTTP access log publishers. 073 * 074 * @return a Collection of {@link HTTPAccessLogPublisher} objects 075 */ 076 public static Collection<HTTPAccessLogPublisher<HTTPAccessLogPublisherCfg>> 077 getHTTPAccessLogPublishers() 078 { 079 return loggerStorage.getLogPublishers(); 080 } 081 082 /** 083 * Logs the given HTTPRequestInfo. 084 * 085 * @param requestInfo 086 * the HTTP request info to log 087 */ 088 public static void logRequestInfo(HTTPRequestInfo requestInfo) 089 { 090 for (HTTPAccessLogPublisher<?> publisher : loggerStorage.getLogPublishers()) 091 { 092 publisher.logRequestInfo(requestInfo); 093 } 094 } 095 096 @Override 097 public final synchronized void addLogPublisher( 098 HTTPAccessLogPublisher<HTTPAccessLogPublisherCfg> publisher) 099 { 100 loggerStorage.addLogPublisher(publisher); 101 } 102 103 @Override 104 public final synchronized boolean removeLogPublisher( 105 HTTPAccessLogPublisher<HTTPAccessLogPublisherCfg> publisher) 106 { 107 return loggerStorage.removeLogPublisher(publisher); 108 } 109 110 @Override 111 public final synchronized void removeAllLogPublishers() 112 { 113 loggerStorage.removeAllLogPublishers(); 114 // Access logger may have not been fully initialized 115 if (getServerContext() != null && getServerContext().getCommonAudit() != null) 116 { 117 getServerContext().getCommonAudit().shutdown(); 118 } 119 } 120 121}