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