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