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 010 * docs/licenses/cddl.txt 011 * or http://www.opensource.org/licenses/cddl1.php. 012 * See the License for the specific language governing permissions 013 * and limitations under the License. 014 * 015 * When distributing Covered Code, include this CDDL HEADER in each 016 * file and include the License file at 017 * docs/licenses/cddl.txt. If applicable, 018 * add the following below this CDDL HEADER, with the fields enclosed 019 * by brackets "[]" replaced with your own identifying information: 020 * Portions Copyright [yyyy] [name of copyright owner] 021 * 022 * CDDL HEADER END 023 * 024 * 025 * Copyright 2011-2013 UnboundID Corp. 026 */ 027 package com.unboundid.directory.sdk.common.scripting; 028 029 030 031 import java.util.List; 032 033 import com.unboundid.directory.sdk.common.config.FileBasedErrorLoggerConfig; 034 import com.unboundid.directory.sdk.common.internal.Reconfigurable; 035 import com.unboundid.directory.sdk.common.types.LogCategory; 036 import com.unboundid.directory.sdk.common.types.LogSeverity; 037 import com.unboundid.directory.sdk.common.types.ServerContext; 038 import com.unboundid.directory.sdk.metrics.internal.MetricsEngineExtension; 039 import com.unboundid.directory.sdk.proxy.internal.DirectoryProxyServerExtension; 040 import com.unboundid.directory.sdk.ds.internal.DirectoryServerExtension; 041 import com.unboundid.directory.sdk.sync.internal.SynchronizationServerExtension; 042 import com.unboundid.ldap.sdk.LDAPException; 043 import com.unboundid.ldap.sdk.ResultCode; 044 import com.unboundid.util.Extensible; 045 import com.unboundid.util.ThreadSafety; 046 import com.unboundid.util.ThreadSafetyLevel; 047 import com.unboundid.util.args.ArgumentException; 048 import com.unboundid.util.args.ArgumentParser; 049 050 051 052 /** 053 * This class defines an API that may be used to create a specific type of 054 * scripted error logger which is intended to write log messages to text files. 055 * This is a convenience for developers which wish to create custom error 056 * loggers that write to text files and provides support for a wide range of 057 * functionality including high-performance and highly-concurrent logging. All 058 * of the options available to {@link ScriptedErrorLogger} implementations are 059 * available for file-based error loggers, as well as options for indicating 060 * the log file path, the rotation and retention policies, whether to buffer the 061 * output, etc. 062 * <BR><BR> 063 * Note that scripted file-based error loggers will automatically be registered 064 * within the server as disk space consumers, so there is no need to implement 065 * the {@link com.unboundid.directory.sdk.common.api.DiskSpaceConsumer} 066 * interface. Also note that configuration change related to the log file 067 * (e.g., the log file path, buffer size, queue size, etc.) will also 068 * automatically be handled by the server, so subclasses only need to be 069 * concerned about changes to the custom arguments they define. 070 * <BR> 071 * <H2>Configuring File-Based Error Loggers</H2> 072 * In order to configure an error logger created using this API, use a command 073 * like: 074 * <PRE> 075 * dsconfig create-log-publisher \ 076 * --publisher-name "<I>{logger-name}</I>" \ 077 * --type groovy-scripted-file-based-error \ 078 * --set enabled:true \ 079 * --set "log-file:<I>{path}</I>" \ 080 * --set "rotation-policy:<I>{rotation-policy-name}</I>" \ 081 * --set "retention-policy:<I>{retention-policy-name}</I>" \ 082 * --set "script-class:<I>{class-name}</I>" \ 083 * --set "script-argument:<I>{name=value}</I>" 084 * </PRE> 085 * where "<I>{logger-name}</I>" is the name to use for the error logger 086 * instance, "<I>{path}</I>" is the path to the log file to be written, 087 * "<I>{rotation-policy-name}</I>" is the name of the log rotation policy to use 088 * for the log file, "<I>{retention-policy-name}</I>" is the name of the log 089 * retention policy to use for the log file, "<I>{class-name}</I>" is the 090 * fully-qualified name of the Groovy class written using this API, and 091 * "<I>{name=value}</I>" represents name-value pairs for any arguments to 092 * provide to the logger. If multiple arguments should be provided to the 093 * logger, then the "<CODE>--set script-argument:<I>{name=value}</I></CODE>" 094 * option should be provided multiple times. It is also possible to specify 095 * multiple log rotation and/or retention policies if desired. 096 * 097 * @see com.unboundid.directory.sdk.common.api.ErrorLogger 098 * @see com.unboundid.directory.sdk.common.api.FileBasedErrorLogger 099 * @see ScriptedErrorLogger 100 */ 101 @Extensible() 102 @DirectoryServerExtension() 103 @DirectoryProxyServerExtension(appliesToLocalContent=true, 104 appliesToRemoteContent=false) 105 @SynchronizationServerExtension(appliesToLocalContent=true, 106 appliesToSynchronizedContent=false) 107 @MetricsEngineExtension() 108 @ThreadSafety(level=ThreadSafetyLevel.INTERFACE_THREADSAFE) 109 public abstract class ScriptedFileBasedErrorLogger 110 implements Reconfigurable<FileBasedErrorLoggerConfig> 111 { 112 /** 113 * Creates a new instance of this file-based error logger. All file-based 114 * error logger implementations must include a default constructor, but any 115 * initialization should generally be done in the 116 * {@code initializeErrorLogger} method. 117 */ 118 public ScriptedFileBasedErrorLogger() 119 { 120 // No implementation is required. 121 } 122 123 124 125 /** 126 * {@inheritDoc} 127 */ 128 public void defineConfigArguments(final ArgumentParser parser) 129 throws ArgumentException 130 { 131 // No arguments will be allowed by default. 132 } 133 134 135 136 /** 137 * Initializes this file-based error logger. 138 * 139 * @param serverContext A handle to the server context for the server in 140 * which this extension is running. 141 * @param config The general configuration for this file-based error 142 * logger. 143 * @param parser The argument parser which has been initialized from 144 * the configuration for this file-based error logger. 145 * 146 * @throws LDAPException If a problem occurs while initializing this 147 * file-based error logger. 148 */ 149 public void initializeErrorLogger(final ServerContext serverContext, 150 final FileBasedErrorLoggerConfig config, 151 final ArgumentParser parser) 152 throws LDAPException 153 { 154 // No initialization will be performed by default. 155 } 156 157 158 159 /** 160 * Performs any cleanup which may be necessary when this file-based error 161 * logger is to be taken out of service. 162 */ 163 public void finalizeErrorLogger() 164 { 165 // No implementation is required. 166 } 167 168 169 170 /** 171 * {@inheritDoc} 172 */ 173 public boolean isConfigurationAcceptable( 174 final FileBasedErrorLoggerConfig config, 175 final ArgumentParser parser, 176 final List<String> unacceptableReasons) 177 { 178 // No extended validation will be performed. 179 return true; 180 } 181 182 183 184 /** 185 * {@inheritDoc} 186 */ 187 public ResultCode applyConfiguration(final FileBasedErrorLoggerConfig config, 188 final ArgumentParser parser, 189 final List<String> adminActionsRequired, 190 final List<String> messages) 191 { 192 // If there are any custom arguments, then add an admin action message 193 // indicating that the extension needs to be restarted for any changes to 194 // take effect. 195 if (! parser.getNamedArguments().isEmpty()) 196 { 197 adminActionsRequired.add( 198 "If any extension-argument values have been altered, then " + 199 "those new values have not actually been applied. The new " + 200 "configuration for those arguments will not take effect " + 201 "until this file-based error logger is disabled and " + 202 "re-enabled, or until the server is restarted."); 203 } 204 205 return ResultCode.SUCCESS; 206 } 207 208 209 210 /** 211 * Records information about the provided message, if appropriate. 212 * 213 * @param category The category for the message to be logged. 214 * @param severity The severity for the message to be logged. 215 * @param messageID The unique identifier with which the message text is 216 * associated. 217 * @param message The message to be logged. 218 * 219 * @return The content of the log message that should be written. It may be 220 * {@code null} or empty if no message should be written. It may 221 * optionally include line breaks if the log message should span 222 * multiple lines. 223 */ 224 public abstract CharSequence logError(final LogCategory category, 225 final LogSeverity severity, 226 final long messageID, 227 final String message); 228 }