001/*
002 * Copyright 2008-2018 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright (C) 2015-2018 Ping Identity Corporation
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 */
021package com.unboundid.ldap.sdk.unboundidds.tasks;
022
023
024
025import java.util.Arrays;
026import java.util.Collections;
027import java.util.Date;
028import java.util.LinkedHashMap;
029import java.util.List;
030import java.util.Map;
031
032import com.unboundid.ldap.sdk.Attribute;
033import com.unboundid.ldap.sdk.Entry;
034import com.unboundid.util.NotMutable;
035import com.unboundid.util.ThreadSafety;
036import com.unboundid.util.ThreadSafetyLevel;
037import com.unboundid.util.Validator;
038
039import static com.unboundid.ldap.sdk.unboundidds.tasks.TaskMessages.*;
040
041
042
043/**
044 * This class defines a Directory Server task that can be used to add the
045 * contents of one or more files to the server schema.
046 * <BR>
047 * <BLOCKQUOTE>
048 *   <B>NOTE:</B>  This class, and other classes within the
049 *   {@code com.unboundid.ldap.sdk.unboundidds} package structure, are only
050 *   supported for use against Ping Identity, UnboundID, and
051 *   Nokia/Alcatel-Lucent 8661 server products.  These classes provide support
052 *   for proprietary functionality or for external specifications that are not
053 *   considered stable or mature enough to be guaranteed to work in an
054 *   interoperable way with other types of LDAP servers.
055 * </BLOCKQUOTE>
056 * <BR>
057 * The properties that are available for use with this type of task include:
058 * <UL>
059 *   <LI>The names of the files to add to the server schema.  The specified
060 *       files must exist within the server's schema configuration directory
061 *       with the appropriate schema elements defined.  They should be only the
062 *       base names for the file and should not include any path
063 *       information.  At least one name must be provided.</LI>
064 * </UL>
065 */
066@NotMutable()
067@ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
068public final class AddSchemaFileTask
069       extends Task
070{
071  /**
072   * The fully-qualified name of the Java class that is used for the add schema
073   * file task.
074   */
075  static final String ADD_SCHEMA_FILE_TASK_CLASS =
076       "com.unboundid.directory.server.tasks.AddSchemaFileTask";
077
078
079
080  /**
081   * The name of the attribute used to specify the name(s) of the schema file(s)
082   * to add.
083   */
084  private static final String ATTR_SCHEMA_FILE =
085       "ds-task-schema-file-name";
086
087
088
089  /**
090   * The name of the object class used in add schema file task entries.
091   */
092  private static final String OC_ADD_SCHEMA_FILE_TASK =
093       "ds-task-add-schema-file";
094
095
096
097  /**
098   * The task property that will be used for the schema file names.
099   */
100  private static final TaskProperty PROPERTY_SCHEMA_FILE =
101     new TaskProperty(ATTR_SCHEMA_FILE, INFO_DISPLAY_NAME_SCHEMA_FILE.get(),
102                      INFO_DESCRIPTION_SCHEMA_FILE.get(), String.class, true,
103                      true, false);
104
105
106
107  /**
108   * The serial version UID for this serializable class.
109   */
110  private static final long serialVersionUID = -5430392768265418966L;
111
112
113
114  // The names of the schema files to be added.
115  private final List<String> schemaFileNames;
116
117
118
119  /**
120   * Creates a new uninitialized add schema file task instance which should only
121   * be used for obtaining general information about this task, including the
122   * task name, description, and supported properties.  Attempts to use a task
123   * created with this constructor for any other reason will likely fail.
124   */
125  public AddSchemaFileTask()
126  {
127    schemaFileNames = null;
128  }
129
130
131
132  /**
133   * Creates a new add schema file task to add the specified file to the server
134   * schema.
135   *
136   * @param  taskID          The task ID to use for this task.  If it is
137   *                         {@code null} then a UUID will be generated for use
138   *                         as the task ID.
139   * @param  schemaFileName  The name (without path information) of the file to
140   *                         add to the server schema.  It must not be
141   *                         {@code null}.
142   */
143  public AddSchemaFileTask(final String taskID, final String schemaFileName)
144  {
145    this(taskID, Collections.singletonList(schemaFileName), null, null, null,
146         null, null);
147
148    Validator.ensureNotNull(schemaFileName);
149  }
150
151
152
153  /**
154   * Creates a new add schema file task to add the specified files to the server
155   * schema.
156   *
157   * @param  taskID           The task ID to use for this task.  If it is
158   *                          {@code null} then a UUID will be generated for use
159   *                          as the task ID.
160   * @param  schemaFileNames  The list of names (without path information) of
161   *                          the files to add to the server schema.  It must
162   *                          not be {@code null} or empty.
163   */
164  public AddSchemaFileTask(final String taskID,
165                           final List<String> schemaFileNames)
166  {
167    this(taskID, schemaFileNames, null, null, null, null, null);
168  }
169
170
171
172  /**
173   * Creates a new add schema file task to add the specified files to the server
174   * schema.
175   *
176   * @param  taskID                  The task ID to use for this task.  If it is
177   *                                 {@code null} then a UUID will be generated
178   *                                 for use as the task ID.
179   * @param  schemaFileNames         The list of names (without path
180   *                                 information) of the files to add to the
181   *                                 server schema.  It must not be {@code null}
182   *                                 or empty.
183   * @param  scheduledStartTime      The time that this task should start
184   *                                 running.
185   * @param  dependencyIDs           The list of task IDs that will be required
186   *                                 to complete before this task will be
187   *                                 eligible to start.
188   * @param  failedDependencyAction  Indicates what action should be taken if
189   *                                 any of the dependencies for this task do
190   *                                 not complete successfully.
191   * @param  notifyOnCompletion      The list of e-mail addresses of individuals
192   *                                 that should be notified when this task
193   *                                 completes.
194   * @param  notifyOnError           The list of e-mail addresses of individuals
195   *                                 that should be notified if this task does
196   *                                 not complete successfully.
197   */
198  public AddSchemaFileTask(final String taskID,
199                           final List<String> schemaFileNames,
200                           final Date scheduledStartTime,
201                           final List<String> dependencyIDs,
202                           final FailedDependencyAction failedDependencyAction,
203                           final List<String> notifyOnCompletion,
204                           final List<String> notifyOnError)
205  {
206    this(taskID, schemaFileNames, scheduledStartTime, dependencyIDs,
207         failedDependencyAction, null, notifyOnCompletion, null,
208         notifyOnError, null, null, null);
209  }
210
211
212
213  /**
214   * Creates a new add schema file task to add the specified files to the server
215   * schema.
216   *
217   * @param  taskID                  The task ID to use for this task.  If it is
218   *                                 {@code null} then a UUID will be generated
219   *                                 for use as the task ID.
220   * @param  schemaFileNames         The list of names (without path
221   *                                 information) of the files to add to the
222   *                                 server schema.  It must not be {@code null}
223   *                                 or empty.
224   * @param  scheduledStartTime      The time that this task should start
225   *                                 running.
226   * @param  dependencyIDs           The list of task IDs that will be required
227   *                                 to complete before this task will be
228   *                                 eligible to start.
229   * @param  failedDependencyAction  Indicates what action should be taken if
230   *                                 any of the dependencies for this task do
231   *                                 not complete successfully.
232   * @param  notifyOnStart           The list of e-mail addresses of individuals
233   *                                 that should be notified when this task
234   *                                 starts running.
235   * @param  notifyOnCompletion      The list of e-mail addresses of individuals
236   *                                 that should be notified when this task
237   *                                 completes.
238   * @param  notifyOnSuccess         The list of e-mail addresses of individuals
239   *                                 that should be notified if this task
240   *                                 completes successfully.
241   * @param  notifyOnError           The list of e-mail addresses of individuals
242   *                                 that should be notified if this task does
243   *                                 not complete successfully.
244   * @param  alertOnStart            Indicates whether the server should send an
245   *                                 alert notification when this task starts.
246   * @param  alertOnSuccess          Indicates whether the server should send an
247   *                                 alert notification if this task completes
248   *                                 successfully.
249   * @param  alertOnError            Indicates whether the server should send an
250   *                                 alert notification if this task fails to
251   *                                 complete successfully.
252   */
253  public AddSchemaFileTask(final String taskID,
254                           final List<String> schemaFileNames,
255                           final Date scheduledStartTime,
256                           final List<String> dependencyIDs,
257                           final FailedDependencyAction failedDependencyAction,
258                           final List<String> notifyOnStart,
259                           final List<String> notifyOnCompletion,
260                           final List<String> notifyOnSuccess,
261                           final List<String> notifyOnError,
262                           final Boolean alertOnStart,
263                           final Boolean alertOnSuccess,
264                           final Boolean alertOnError)
265  {
266    super(taskID, ADD_SCHEMA_FILE_TASK_CLASS, scheduledStartTime,
267          dependencyIDs, failedDependencyAction, notifyOnStart,
268         notifyOnCompletion, notifyOnSuccess, notifyOnError, alertOnStart,
269         alertOnSuccess, alertOnError);
270
271    Validator.ensureNotNull(schemaFileNames);
272    Validator.ensureFalse(schemaFileNames.isEmpty(),
273         "AddSchemaFileTask.schemaFileNames must not be empty.");
274
275    this.schemaFileNames = Collections.unmodifiableList(schemaFileNames);
276  }
277
278
279
280  /**
281   * Creates a new add schema file task from the provided entry.
282   *
283   * @param  entry  The entry to use to create this add schema file task.
284   *
285   * @throws  TaskException  If the provided entry cannot be parsed as a
286   *                         add schema file task entry.
287   */
288  public AddSchemaFileTask(final Entry entry)
289         throws TaskException
290  {
291    super(entry);
292
293    // Get the set of schema file names.  It must be present.
294    final String[] fileNames = entry.getAttributeValues(ATTR_SCHEMA_FILE);
295    if ((fileNames == null) || (fileNames.length == 0))
296    {
297      throw new TaskException(ERR_ADD_SCHEMA_FILE_TASK_NO_FILES.get(
298                                   getTaskEntryDN()));
299    }
300
301    schemaFileNames = Collections.unmodifiableList(Arrays.asList(fileNames));
302  }
303
304
305
306  /**
307   * Creates a new add schema file task from the provided set of task
308   * properties.
309   *
310   * @param  properties  The set of task properties and their corresponding
311   *                     values to use for the task.  It must not be
312   *                     {@code null}.
313   *
314   * @throws  TaskException  If the provided set of properties cannot be used to
315   *                         create a valid add schema file task.
316   */
317  public AddSchemaFileTask(final Map<TaskProperty,List<Object>> properties)
318         throws TaskException
319  {
320    super(ADD_SCHEMA_FILE_TASK_CLASS, properties);
321
322    String[] names = null;
323    for (final Map.Entry<TaskProperty,List<Object>> entry :
324         properties.entrySet())
325    {
326      final TaskProperty p = entry.getKey();
327      final String attrName = p.getAttributeName();
328      final List<Object> values = entry.getValue();
329
330      if (attrName.equalsIgnoreCase(ATTR_SCHEMA_FILE))
331      {
332        names = parseStrings(p, values, names);
333      }
334    }
335
336    if (names == null)
337    {
338      throw new TaskException(ERR_ADD_SCHEMA_FILE_TASK_NO_FILES.get(
339                                   getTaskEntryDN()));
340    }
341
342    schemaFileNames = Collections.unmodifiableList(Arrays.asList(names));
343  }
344
345
346
347  /**
348   * {@inheritDoc}
349   */
350  @Override()
351  public String getTaskName()
352  {
353    return INFO_TASK_NAME_ADD_SCHEMA_FILE.get();
354  }
355
356
357
358  /**
359   * {@inheritDoc}
360   */
361  @Override()
362  public String getTaskDescription()
363  {
364    return INFO_TASK_DESCRIPTION_ADD_SCHEMA_FILE.get();
365  }
366
367
368
369  /**
370   * Retrieves the names (without path information) of the schema files to be
371   * added to the server.
372   *
373   * @return  The names of the schema files to be added to the server.
374   */
375  public List<String> getSchemaFileNames()
376  {
377    return schemaFileNames;
378  }
379
380
381
382  /**
383   * {@inheritDoc}
384   */
385  @Override()
386  protected List<String> getAdditionalObjectClasses()
387  {
388    return Collections.singletonList(OC_ADD_SCHEMA_FILE_TASK);
389  }
390
391
392
393  /**
394   * {@inheritDoc}
395   */
396  @Override()
397  protected List<Attribute> getAdditionalAttributes()
398  {
399    return Collections.singletonList(
400         new Attribute(ATTR_SCHEMA_FILE, schemaFileNames));
401  }
402
403
404
405  /**
406   * {@inheritDoc}
407   */
408  @Override()
409  public List<TaskProperty> getTaskSpecificProperties()
410  {
411    return Collections.singletonList(PROPERTY_SCHEMA_FILE);
412  }
413
414
415
416  /**
417   * {@inheritDoc}
418   */
419  @Override()
420  public Map<TaskProperty,List<Object>> getTaskPropertyValues()
421  {
422    final LinkedHashMap<TaskProperty,List<Object>> props =
423         new LinkedHashMap<>(10);
424
425    props.put(PROPERTY_SCHEMA_FILE,
426              Collections.<Object>unmodifiableList(schemaFileNames));
427
428    props.putAll(super.getTaskPropertyValues());
429    return Collections.unmodifiableMap(props);
430  }
431}