001    /*
002     * Copyright 2008-2015 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2015 UnboundID Corp.
007     *
008     * This program is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (GPLv2 only)
010     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011     * as published by the Free Software Foundation.
012     *
013     * This program is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program; if not, see <http://www.gnu.org/licenses>.
020     */
021    package com.unboundid.ldap.sdk.unboundidds.tasks;
022    
023    
024    
025    import java.util.ArrayList;
026    import java.util.Arrays;
027    import java.util.Collections;
028    import java.util.Date;
029    import java.util.LinkedHashMap;
030    import java.util.List;
031    import java.util.Map;
032    
033    import com.unboundid.ldap.sdk.Attribute;
034    import com.unboundid.ldap.sdk.Entry;
035    import com.unboundid.util.NotMutable;
036    import com.unboundid.util.ThreadSafety;
037    import com.unboundid.util.ThreadSafetyLevel;
038    
039    import static com.unboundid.ldap.sdk.unboundidds.tasks.TaskMessages.*;
040    
041    
042    
043    /**
044     * <BLOCKQUOTE>
045     *   <B>NOTE:</B>  This class is part of the Commercial Edition of the UnboundID
046     *   LDAP SDK for Java.  It is not available for use in applications that
047     *   include only the Standard Edition of the LDAP SDK, and is not supported for
048     *   use in conjunction with non-UnboundID products.
049     * </BLOCKQUOTE>
050     * This class defines a Directory Server task that can be used to cause the
051     * server to enter lockdown mode, in which case it will only allow requests
052     * from users with the lockdown-mode privilege.  Lockdown mode is intended to
053     * allow administrators to perform operations with the server online but without
054     * worrying about the impact that those operations may have on other users.  In
055     * In some special cases, the server may place itself in lockdown mode as a
056     * defense mechanism rather than risking the exposure of sensitive information.
057     * For example, if the server detects any malformed access control rules at
058     * startup, then it will place itself in lockdown mode rather than attempt to
059     * run without that rule in effect since it could have been intended to prevent
060     * unauthorized users from accessing certain data.
061     * <BR><BR>
062     * The enter lockdown mode task does not have any task-specific properties.  See
063     * the {@link LeaveLockdownModeTask} class for the corresponding mechanism to
064     * bring the server out of lockdown mode.
065     */
066    @NotMutable()
067    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
068    public final class EnterLockdownModeTask
069           extends Task
070    {
071      /**
072       * The fully-qualified name of the Java class that is used for the enter
073       * lockdown mode task.
074       */
075      static final String ENTER_LOCKDOWN_MODE_TASK_CLASS =
076           "com.unboundid.directory.server.tasks.EnterLockdownModeTask";
077    
078    
079    
080      /**
081       * The name of the attribute used to specify the reason for putting the server
082       * into lockdown mode.
083       */
084      private static final String ATTR_ENTER_LOCKDOWN_REASON =
085           "ds-task-enter-lockdown-reason";
086    
087    
088    
089      /**
090       * The task property for the enter-lockdown reason.
091       */
092      private static final TaskProperty PROPERTY_ENTER_LOCKDOWN_REASON =
093           new TaskProperty(ATTR_ENTER_LOCKDOWN_REASON,
094                            INFO_DISPLAY_NAME_ENTER_LOCKDOWN_REASON.get(),
095                            INFO_DESCRIPTION_ENTER_LOCKDOWN_REASON.get(),
096                            String.class, false, false, false);
097    
098    
099    
100      /**
101       * The name of the object class used in enter-lockdown-mode task entries.
102       */
103      private static final String OC_ENTER_LOCKDOWN_MODE_TASK =
104          "ds-task-enter-lockdown-mode";
105    
106    
107    
108      /**
109       * The serial version UID for this serializable class.
110       */
111      private static final long serialVersionUID = -4104020769734351458L;
112    
113    
114    
115      // The reason for entering lockdown mode.
116      private final String reason;
117    
118    
119    
120      /**
121       * Creates a new uninitialized enter lockdown mode task instance which should
122       * only be used for obtaining general information about this task, including
123       * the task name, description, and supported properties.  Attempts to use a
124       * task created with this constructor for any other reason will likely fail.
125       */
126      public EnterLockdownModeTask()
127      {
128        reason = null;
129      }
130    
131    
132    
133      /**
134       * Creates a new enter lockdown mode task with the specified task ID.
135       *
136       * @param  taskID  The task ID to use for this task.  If it is {@code null}
137       *                 then a UUID will be generated for use as the task ID.
138       */
139      public EnterLockdownModeTask(final String taskID)
140      {
141        this(taskID, null);
142      }
143    
144    
145    
146      /**
147       * Creates a new enter lockdown mode task with the specified task ID.
148       *
149       * @param  taskID  The task ID to use for this task.  If it is {@code null}
150       *                 then a UUID will be generated for use as the task ID.
151       * @param  reason  The user-specified reason for entering lockdown mode. This
152       *                 may be {@code null}.
153       */
154      public EnterLockdownModeTask(final String taskID, final String reason)
155      {
156        this(taskID, reason, null, null, null, null, null);
157      }
158    
159    
160    
161      /**
162       * Creates a new enter lockdown mode task with the provided information.
163       *
164       * @param  taskID                  The task ID to use for this task.  If it is
165       *                                 {@code null} then a UUID will be generated
166       *                                 for use as the task ID.
167       * @param  scheduledStartTime      The time that this task should start
168       *                                 running.
169       * @param  dependencyIDs           The list of task IDs that will be required
170       *                                 to complete before this task will be
171       *                                 eligible to start.
172       * @param  failedDependencyAction  Indicates what action should be taken if
173       *                                 any of the dependencies for this task do
174       *                                 not complete successfully.
175       * @param  notifyOnCompletion      The list of e-mail addresses of individuals
176       *                                 that should be notified when this task
177       *                                 completes.
178       * @param  notifyOnError           The list of e-mail addresses of individuals
179       *                                 that should be notified if this task does
180       *                                 not complete successfully.
181       */
182      public EnterLockdownModeTask(final String taskID,
183                  final Date scheduledStartTime, final List<String> dependencyIDs,
184                  final FailedDependencyAction failedDependencyAction,
185                  final List<String> notifyOnCompletion,
186                  final List<String> notifyOnError)
187      {
188        this(taskID, null, scheduledStartTime, dependencyIDs,
189             failedDependencyAction, notifyOnCompletion, notifyOnError);
190      }
191    
192    
193    
194      /**
195       * Creates a new enter lockdown mode task with the provided information.
196       *
197       * @param  taskID                  The task ID to use for this task.  If it is
198       *                                 {@code null} then a UUID will be generated
199       *                                 for use as the task ID.
200       * @param  reason                  The user-specified reason for entering
201       *                                 lockdown mode. This may be {@code null}.
202       * @param  scheduledStartTime      The time that this task should start
203       *                                 running.
204       * @param  dependencyIDs           The list of task IDs that will be required
205       *                                 to complete before this task will be
206       *                                 eligible to start.
207       * @param  failedDependencyAction  Indicates what action should be taken if
208       *                                 any of the dependencies for this task do
209       *                                 not complete successfully.
210       * @param  notifyOnCompletion      The list of e-mail addresses of individuals
211       *                                 that should be notified when this task
212       *                                 completes.
213       * @param  notifyOnError           The list of e-mail addresses of individuals
214       *                                 that should be notified if this task does
215       *                                 not complete successfully.
216       */
217      public EnterLockdownModeTask(final String taskID, final String reason,
218                  final Date scheduledStartTime, final List<String> dependencyIDs,
219                  final FailedDependencyAction failedDependencyAction,
220                  final List<String> notifyOnCompletion,
221                  final List<String> notifyOnError)
222      {
223        super(taskID, ENTER_LOCKDOWN_MODE_TASK_CLASS, scheduledStartTime,
224              dependencyIDs, failedDependencyAction, notifyOnCompletion,
225              notifyOnError);
226    
227        this.reason = reason;
228      }
229    
230    
231    
232      /**
233       * Creates a new enter lockdown mode task from the provided entry.
234       *
235       * @param  entry  The entry to use to create this enter lockdown mode task.
236       *
237       * @throws  TaskException  If the provided entry cannot be parsed as an enter
238       *                         lockdown mode task entry.
239       */
240      public EnterLockdownModeTask(final Entry entry)
241             throws TaskException
242      {
243        super(entry);
244    
245        // Get the "reason" string if it is present.
246        reason = entry.getAttributeValue(ATTR_ENTER_LOCKDOWN_REASON);
247      }
248    
249    
250    
251      /**
252       * Creates a new enter lockdown mode task from the provided set of task
253       * properties.
254       *
255       * @param  properties  The set of task properties and their corresponding
256       *                     values to use for the task.  It must not be
257       *                     {@code null}.
258       *
259       * @throws  TaskException  If the provided set of properties cannot be used to
260       *                         create a valid enter lockdown mode task.
261       */
262      public EnterLockdownModeTask(final Map<TaskProperty,List<Object>> properties)
263             throws TaskException
264      {
265        super(ENTER_LOCKDOWN_MODE_TASK_CLASS, properties);
266    
267        String r = null;
268        for (final Map.Entry<TaskProperty,List<Object>> entry :
269                properties.entrySet())
270        {
271          final TaskProperty p = entry.getKey();
272          final String attrName = p.getAttributeName();
273          final List<Object> values = entry.getValue();
274    
275          if (attrName.equalsIgnoreCase(ATTR_ENTER_LOCKDOWN_REASON))
276          {
277            r = parseString(p, values, null);
278            break;
279          }
280        }
281    
282        reason = r;
283      }
284    
285    
286    
287      /**
288       * Retrieves the user-specified reason why the server is entering lockdown
289       * mode.
290       *
291       * @return  The reason the server is entering lockdown mode, or {@code null}
292       *          if none was specified.
293       */
294      public String getReason()
295      {
296        return reason;
297      }
298    
299    
300    
301      /**
302       * {@inheritDoc}
303       */
304      @Override()
305      public String getTaskName()
306      {
307        return INFO_TASK_NAME_ENTER_LOCKDOWN_MODE.get();
308      }
309    
310    
311    
312      /**
313       * {@inheritDoc}
314       */
315      @Override()
316      public String getTaskDescription()
317      {
318        return INFO_TASK_DESCRIPTION_ENTER_LOCKDOWN_MODE.get();
319      }
320    
321    
322    
323      /**
324       * {@inheritDoc}
325       */
326      @Override()
327      protected List<String> getAdditionalObjectClasses()
328      {
329        return Arrays.asList(OC_ENTER_LOCKDOWN_MODE_TASK);
330      }
331    
332    
333    
334      /**
335       * {@inheritDoc}
336       */
337      @Override()
338      protected List<Attribute> getAdditionalAttributes()
339      {
340        final ArrayList<Attribute> attrs = new ArrayList<Attribute>(1);
341        if(reason != null)
342        {
343          attrs.add(new Attribute(ATTR_ENTER_LOCKDOWN_REASON, reason));
344        }
345        return attrs;
346      }
347    
348    
349    
350      /**
351       * {@inheritDoc}
352       */
353      @Override()
354      public List<TaskProperty> getTaskSpecificProperties()
355      {
356        final List<TaskProperty> propList =
357                  Arrays.asList(PROPERTY_ENTER_LOCKDOWN_REASON);
358    
359        return Collections.unmodifiableList(propList);
360      }
361    
362    
363    
364      /**
365       * {@inheritDoc}
366       */
367      @Override()
368      public Map<TaskProperty,List<Object>> getTaskPropertyValues()
369      {
370        final LinkedHashMap<TaskProperty,List<Object>> props =
371             new LinkedHashMap<TaskProperty,List<Object>>();
372    
373        if (reason != null)
374        {
375          props.put(PROPERTY_ENTER_LOCKDOWN_REASON,
376                  Collections.<Object>unmodifiableList(Arrays.asList(reason)));
377        }
378    
379        props.putAll(super.getTaskPropertyValues());
380        return Collections.unmodifiableMap(props);
381      }
382    }