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