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 2010-2013 UnboundID Corp. 026 */ 027 package com.unboundid.directory.sdk.common.api; 028 029 030 031 import java.util.Collections; 032 import java.util.List; 033 import java.util.Map; 034 035 import com.unboundid.directory.sdk.common.config.ErrorLoggerConfig; 036 import com.unboundid.directory.sdk.common.internal.ExampleUsageProvider; 037 import com.unboundid.directory.sdk.common.internal.Reconfigurable; 038 import com.unboundid.directory.sdk.common.internal.UnboundIDExtension; 039 import com.unboundid.directory.sdk.common.types.LogCategory; 040 import com.unboundid.directory.sdk.common.types.LogSeverity; 041 import com.unboundid.directory.sdk.common.types.ServerContext; 042 import com.unboundid.directory.sdk.ds.internal.DirectoryServerExtension; 043 import com.unboundid.directory.sdk.metrics.internal.MetricsEngineExtension; 044 import com.unboundid.directory.sdk.proxy.internal.DirectoryProxyServerExtension; 045 import com.unboundid.directory.sdk.sync.internal.SynchronizationServerExtension; 046 import com.unboundid.ldap.sdk.LDAPException; 047 import com.unboundid.ldap.sdk.ResultCode; 048 import com.unboundid.util.Extensible; 049 import com.unboundid.util.ThreadSafety; 050 import com.unboundid.util.ThreadSafetyLevel; 051 import com.unboundid.util.args.ArgumentException; 052 import com.unboundid.util.args.ArgumentParser; 053 054 055 056 /** 057 * This class defines an API that must be implemented by extensions which 058 * record information about warnings, errors, and events which occur in the 059 * server. 060 * <BR><BR> 061 * Each error logger may configured to indicate whether whether to include or 062 * exclude log messages based on their category and/or severity. This is 063 * handled automatically by the server, so individual error logger 064 * implementations no not need to attempt to perform that filtering on their 065 * own. However, they may perform additional processing if desired to further 066 * narrow the set of messages that should be logged. 067 * <BR> 068 * <H2>Configuring Error Loggers</H2> 069 * In order to configure an error logger created using this API, use a command 070 * like: 071 * <PRE> 072 * dsconfig create-log-publisher \ 073 * --publisher-name "<I>{logger-name}</I>" \ 074 * --type third-party-error \ 075 * --set enabled:true \ 076 * --set "extension-class:<I>{class-name}</I>" \ 077 * --set "extension-argument:<I>{name=value}</I>" 078 * </PRE> 079 * where "<I>{logger-name}</I>" is the name to use for the error logger 080 * instance, "<I>{class-name}</I>" is the fully-qualified name of the Java class 081 * that extends {@code com.unboundid.directory.sdk.common.api.ErrorLogger}, 082 * and "<I>{name=value}</I>" represents name-value pairs for any arguments to 083 * provide to the logger. If multiple arguments should be provided to the 084 * logger, then the "<CODE>--set extension-argument:<I>{name=value}</I></CODE>" 085 * option should be provided multiple times. 086 * 087 * @see FileBasedErrorLogger 088 * @see com.unboundid.directory.sdk.common.scripting.ScriptedErrorLogger 089 * @see 090 * com.unboundid.directory.sdk.common.scripting.ScriptedFileBasedErrorLogger 091 */ 092 @Extensible() 093 @DirectoryServerExtension() 094 @DirectoryProxyServerExtension(appliesToLocalContent=true, 095 appliesToRemoteContent=true) 096 @SynchronizationServerExtension(appliesToLocalContent=true, 097 appliesToSynchronizedContent=true) 098 @MetricsEngineExtension() 099 @ThreadSafety(level=ThreadSafetyLevel.INTERFACE_THREADSAFE) 100 public abstract class ErrorLogger 101 implements UnboundIDExtension, Reconfigurable<ErrorLoggerConfig>, 102 ExampleUsageProvider 103 { 104 /** 105 * Creates a new instance of this error logger. All error logger 106 * implementations must include a default constructor, but any initialization 107 * should generally be done in the {@code initializeErrorLogger} method. 108 */ 109 public ErrorLogger() 110 { 111 // No implementation is required. 112 } 113 114 115 116 /** 117 * {@inheritDoc} 118 */ 119 public abstract String getExtensionName(); 120 121 122 123 /** 124 * {@inheritDoc} 125 */ 126 public abstract String[] getExtensionDescription(); 127 128 129 130 /** 131 * {@inheritDoc} 132 */ 133 public void defineConfigArguments(final ArgumentParser parser) 134 throws ArgumentException 135 { 136 // No arguments will be allowed by default. 137 } 138 139 140 141 /** 142 * Initializes this error logger. 143 * 144 * @param serverContext A handle to the server context for the server in 145 * which this extension is running. 146 * @param config The general configuration for this error logger. 147 * @param parser The argument parser which has been initialized from 148 * the configuration for this error logger. 149 * 150 * @throws LDAPException If a problem occurs while initializing this error 151 * logger. 152 */ 153 public void initializeErrorLogger(final ServerContext serverContext, 154 final ErrorLoggerConfig config, 155 final ArgumentParser parser) 156 throws LDAPException 157 { 158 // No initialization will be performed by default. 159 } 160 161 162 163 /** 164 * {@inheritDoc} 165 */ 166 public boolean isConfigurationAcceptable(final ErrorLoggerConfig config, 167 final ArgumentParser parser, 168 final List<String> unacceptableReasons) 169 { 170 // No extended validation will be performed by default. 171 return true; 172 } 173 174 175 176 /** 177 * {@inheritDoc} 178 */ 179 public ResultCode applyConfiguration(final ErrorLoggerConfig config, 180 final ArgumentParser parser, 181 final List<String> adminActionsRequired, 182 final List<String> messages) 183 { 184 // By default, no configuration changes will be applied. If there are any 185 // arguments, then add an admin action message indicating that the extension 186 // needs to be restarted for any changes to take effect. 187 if (! parser.getNamedArguments().isEmpty()) 188 { 189 adminActionsRequired.add( 190 "No configuration change has actually been applied. The new " + 191 "configuration will not take effect until this error logger " + 192 "is disabled and re-enabled or until the server is restarted."); 193 } 194 195 return ResultCode.SUCCESS; 196 } 197 198 199 200 /** 201 * Performs any cleanup which may be necessary when this error logger is to 202 * be taken out of service. 203 */ 204 public void finalizeErrorLogger() 205 { 206 // No implementation is required. 207 } 208 209 210 211 /** 212 * Records information about the provided message, if appropriate. 213 * 214 * @param category The category for the message to be logged. 215 * @param severity The severity for the message to be logged. 216 * @param messageID The unique identifier with which the message text is 217 * associated. 218 * @param message The message to be logged. 219 */ 220 public abstract void logError(final LogCategory category, 221 final LogSeverity severity, 222 final long messageID, 223 final String message); 224 225 226 227 /** 228 * {@inheritDoc} 229 */ 230 public Map<List<String>,String> getExamplesArgumentSets() 231 { 232 return Collections.emptyMap(); 233 } 234 }