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.AlertHandlerConfig;
034 import com.unboundid.directory.sdk.common.internal.Reconfigurable;
035 import com.unboundid.directory.sdk.common.types.AlertNotification;
036 import com.unboundid.directory.sdk.common.types.ServerContext;
037 import com.unboundid.directory.sdk.metrics.internal.MetricsEngineExtension;
038 import com.unboundid.directory.sdk.proxy.internal.DirectoryProxyServerExtension;
039 import com.unboundid.directory.sdk.ds.internal.DirectoryServerExtension;
040 import com.unboundid.directory.sdk.sync.internal.SynchronizationServerExtension;
041 import com.unboundid.ldap.sdk.LDAPException;
042 import com.unboundid.ldap.sdk.ResultCode;
043 import com.unboundid.util.Extensible;
044 import com.unboundid.util.ThreadSafety;
045 import com.unboundid.util.ThreadSafetyLevel;
046 import com.unboundid.util.args.ArgumentException;
047 import com.unboundid.util.args.ArgumentParser;
048
049
050
051 /**
052 * This class defines an API that must be implemented by scripted extensions
053 * which will be invoked whenever an administrative alert is generated within
054 * the server. Administrative alerts may be used to notify administrators
055 * whenever a significant error, warning, or other type of event occurs within
056 * the server. Alert handlers may be used to help ensure that those
057 * notifications are made available to administrators in an appropriate manner.
058 * <BR><BR>
059 * Each alert handler may be configured so that it will only be invoked for
060 * certain types of alerts, either based on the specific alert type or based on
061 * the alert severity. This is handled automatically by the server, so
062 * individual alert handler implementations do not need to attempt to perform
063 * that filtering on their own. However, they may perform additional processing
064 * if desired to further narrow the set of alerts that should be made available
065 * to administrators.
066 * <BR>
067 * <H2>Configuring Groovy-Scripted Alert Handlers</H2>
068 * In order to configure a scripted alert handler based on this API and written
069 * in the Groovy scripting language, use a command like:
070 * <PRE>
071 * dsconfig create-alert-handler \
072 * --handler-name "<I>{handler-name}</I>" \
073 * --type groovy-scripted \
074 * --set enabled:true \
075 * --set "script-class:<I>{class-name}</I>" \
076 * --set "script-argument:<I>{name=value}</I>"
077 * </PRE>
078 * where "<I>{handler-name}</I>" is the name to use for the alert handler
079 * instance, "<I>{class-name}</I>" is the fully-qualified name of the Groovy
080 * class written using this API, and "<I>{name=value}</I>" represents name-value
081 * pairs for any arguments to provide to the alert handler. If multiple
082 * arguments should be provided to the alert handler, then the
083 * "<CODE>--set script-argument:<I>{name=value}</I></CODE>" option should be
084 * provided multiple times.
085 *
086 * @see com.unboundid.directory.sdk.common.api.AlertHandler
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 ScriptedAlertHandler
097 implements Reconfigurable<AlertHandlerConfig>
098 {
099 /**
100 * Creates a new instance of this alert handler. All alert handler
101 * implementations must include a default constructor, but any initialization
102 * should generally be done in the {@code initializeAlertHandler} method.
103 */
104 public ScriptedAlertHandler()
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 alert handler.
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 alert handler.
128 * @param parser The argument parser which has been initialized from
129 * the configuration for this alert handler.
130 *
131 * @throws LDAPException If a problem occurs while initializing this alert
132 * handler.
133 */
134 public void initializeAlertHandler(final ServerContext serverContext,
135 final AlertHandlerConfig 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 alert handler is to
146 * be taken out of service.
147 */
148 public void finalizeAlertHandler()
149 {
150 // No implementation is required.
151 }
152
153
154
155 /**
156 * {@inheritDoc}
157 */
158 public boolean isConfigurationAcceptable(final AlertHandlerConfig 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 AlertHandlerConfig 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 alert handler " +
184 "is disabled and re-enabled or until the server is restarted.");
185 }
186
187 return ResultCode.SUCCESS;
188 }
189
190
191
192 /**
193 * Performs any processing which may be necessary to handle the provided alert
194 * notification.
195 *
196 * @param alert The alert notification generated within the server.
197 */
198 public abstract void handleAlert(final AlertNotification alert);
199 }