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.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.FileBasedErrorLoggerConfig;
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 may be used to create a specific type of
058     * error logger which is intended to write log messages to text files.  This is
059     * a convenience for developers which wish to create custom error loggers that
060     * write to text files and provides support for a wide range of functionality
061     * including high-performance and highly-concurrent logging.  All of the options
062     * available to {@link ErrorLogger} implementations are available for
063     * file-based error loggers, as well as options for indicating the log file
064     * path, the rotation and retention policies, whether to buffer the output, etc.
065     * <BR><BR>
066     * Note that file-based error loggers will automatically be registered within
067     * the server as disk space consumers, so there is no need to implement the
068     * {@link DiskSpaceConsumer} interface.  Also note that configuration change
069     * related to the log file (e.g., the log file path, buffer size, queue size,
070     * etc.) will also automatically be handled by the server, so subclasses only
071     * need to be concerned about changes to the custom arguments they define.
072     * <BR>
073     * <H2>Configuring File-BasedError Loggers</H2>
074     * In order to configure a file-based error logger created using this API, use a
075     * command like:
076     * <PRE>
077     *      dsconfig create-log-publisher \
078     *           --publisher-name "<I>{logger-name}</I>" \
079     *           --type third-party-file-based-error \
080     *           --set enabled:true \
081     *           --set "log-file:<I>{path}</I>" \
082     *           --set "rotation-policy:<I>{rotation-policy-name}</I>" \
083     *           --set "retention-policy:<I>{retention-policy-name}</I>" \
084     *           --set "extension-class:<I>{class-name}</I>" \
085     *           --set "extension-argument:<I>{name=value}</I>"
086     * </PRE>
087     * where "<I>{logger-name}</I>" is the name to use for the error logger
088     * instance, "<I>{path}</I>" is the path to the log file to be written,
089     * "<I>{rotation-policy-name}</I>" is the name of the log rotation policy to use
090     * for the log file, "<I>{retention-policy-name}</I>" is the name of the log
091     * retention policy to use for the log file, "<I>{class-name}</I>" is the
092     * fully-qualified name of the Java class that extends
093     * {@code com.unboundid.directory.sdk.common.api.FileBasedErrorLogger}, and
094     * "<I>{name=value}</I>" represents name-value pairs for any arguments to
095     * provide to the logger.  If multiple arguments should be provided to the
096     * logger, then the "<CODE>--set extension-argument:<I>{name=value}</I></CODE>"
097     * option should be provided multiple times.  It is also possible to specify
098     * multiple log rotation and/or retention policies if desired.
099     *
100     * @see  ErrorLogger
101     * @see  com.unboundid.directory.sdk.common.scripting.ScriptedErrorLogger
102     * @see
103     *    com.unboundid.directory.sdk.common.scripting.ScriptedFileBasedErrorLogger
104     */
105    @Extensible()
106    @DirectoryServerExtension()
107    @DirectoryProxyServerExtension(appliesToLocalContent=true,
108         appliesToRemoteContent=true)
109    @SynchronizationServerExtension(appliesToLocalContent=true,
110         appliesToSynchronizedContent=true)
111    @MetricsEngineExtension()
112    @ThreadSafety(level=ThreadSafetyLevel.INTERFACE_THREADSAFE)
113    public abstract class FileBasedErrorLogger
114           implements UnboundIDExtension,
115                      Reconfigurable<FileBasedErrorLoggerConfig>,
116                      ExampleUsageProvider
117    {
118      /**
119       * Creates a new instance of this file-based error logger.  All file-based
120       * error logger implementations must include a default constructor, but any
121       * initialization should generally be done in the
122       * {@code initializeErrorLogger} method.
123       */
124      public FileBasedErrorLogger()
125      {
126        // No implementation is required.
127      }
128    
129    
130    
131      /**
132       * {@inheritDoc}
133       */
134      public abstract String getExtensionName();
135    
136    
137    
138      /**
139       * {@inheritDoc}
140       */
141      public abstract String[] getExtensionDescription();
142    
143    
144    
145      /**
146       * {@inheritDoc}
147       */
148      public void defineConfigArguments(final ArgumentParser parser)
149             throws ArgumentException
150      {
151        // No arguments will be allowed by default.
152      }
153    
154    
155    
156      /**
157       * Initializes this file-based error logger.
158       *
159       * @param  serverContext  A handle to the server context for the server in
160       *                        which this extension is running.
161       * @param  config         The general configuration for this file-based error
162       *                        logger.
163       * @param  parser         The argument parser which has been initialized from
164       *                        the configuration for this file-based error logger.
165       *
166       * @throws  LDAPException  If a problem occurs while initializing this
167       *                         file-based error logger.
168       */
169      public void initializeErrorLogger(final ServerContext serverContext,
170                                        final FileBasedErrorLoggerConfig config,
171                                        final ArgumentParser parser)
172             throws LDAPException
173      {
174        // No initialization will be performed by default.
175      }
176    
177    
178    
179      /**
180       * {@inheritDoc}
181       */
182      public boolean isConfigurationAcceptable(
183                          final FileBasedErrorLoggerConfig config,
184                          final ArgumentParser parser,
185                          final List<String> unacceptableReasons)
186      {
187        // No extended validation will be performed by default.
188        return true;
189      }
190    
191    
192    
193      /**
194       * {@inheritDoc}
195       */
196      public ResultCode applyConfiguration(final FileBasedErrorLoggerConfig config,
197                                           final ArgumentParser parser,
198                                           final List<String> adminActionsRequired,
199                                           final List<String> messages)
200      {
201        // If there are any custom arguments, then add an admin action message
202        // indicating that the extension needs to be restarted for any changes to
203        // take effect.
204        if (! parser.getNamedArguments().isEmpty())
205        {
206          adminActionsRequired.add(
207               "If any extension-argument values have been altered, then " +
208                    "those new values have not actually been applied.  The new " +
209                    "configuration for those arguments will not take effect " +
210                    "until this file-based error logger is disabled and " +
211                    "re-enabled, or until the server is restarted.");
212        }
213    
214        return ResultCode.SUCCESS;
215      }
216    
217    
218    
219      /**
220       * Performs any cleanup which may be necessary when this file-based error
221       * logger is to be taken out of service.
222       */
223      public void finalizeErrorLogger()
224      {
225        // No implementation is required.
226      }
227    
228    
229    
230      /**
231       * Records information about the provided message, if appropriate.
232       *
233       * @param  category   The category for the message to be logged.
234       * @param  severity   The severity for the message to be logged.
235       * @param  messageID  The unique identifier with which the message text is
236       *                    associated.
237       * @param  message    The message to be logged.
238       *
239       * @return  The content of the log message that should be written.  It may be
240       *          {@code null} or empty if no message should be written.  It may
241       *          optionally include line breaks if the log message should span
242       *          multiple lines.
243       */
244      public abstract CharSequence logError(final LogCategory category,
245                                            final LogSeverity severity,
246                                            final long messageID,
247                                            final String message);
248    
249    
250    
251      /**
252       * {@inheritDoc}
253       */
254      public Map<List<String>,String> getExamplesArgumentSets()
255      {
256        return Collections.emptyMap();
257      }
258    }