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