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