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;
037
038import static com.unboundid.ldap.sdk.unboundidds.tasks.TaskMessages.*;
039import static com.unboundid.util.Validator.*;
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 Alcatel-Lucent 8661
051 *   server products.  These classes provide support for proprietary
052 *   functionality or for external specifications that are not considered stable
053 *   or mature enough to be guaranteed to work in an interoperable way with
054 *   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  /**
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  /**
135   * Creates a new add schema file task to add the specified file to the server
136   * schema.
137   *
138   * @param  taskID          The task ID to use for this task.  If it is
139   *                         {@code null} then a UUID will be generated for use
140   *                         as the task ID.
141   * @param  schemaFileName  The name (without path information) of the file to
142   *                         add to the server schema.  It must not be
143   *                         {@code null}.
144   */
145  public AddSchemaFileTask(final String taskID, final String schemaFileName)
146  {
147    this(taskID, Arrays.asList(schemaFileName), null, null, null, null, null);
148
149    ensureNotNull(schemaFileName);
150  }
151
152
153
154
155  /**
156   * Creates a new add schema file task to add the specified files to the server
157   * schema.
158   *
159   * @param  taskID           The task ID to use for this task.  If it is
160   *                          {@code null} then a UUID will be generated for use
161   *                          as the task ID.
162   * @param  schemaFileNames  The list of names (without path information) of
163   *                          the files to add to the server schema.  It must
164   *                          not be {@code null} or empty.
165   */
166  public AddSchemaFileTask(final String taskID,
167                           final List<String> schemaFileNames)
168  {
169    this(taskID, schemaFileNames, null, null, null, null, null);
170  }
171
172
173
174  /**
175   * Creates a new add schema file task to add the specified files to the server
176   * schema.
177   *
178   * @param  taskID                  The task ID to use for this task.  If it is
179   *                                 {@code null} then a UUID will be generated
180   *                                 for use as the task ID.
181   * @param  schemaFileNames         The list of names (without path
182   *                                 information) of the files to add to the
183   *                                 server schema.  It must not be {@code null}
184   *                                 or empty.
185   * @param  scheduledStartTime      The time that this task should start
186   *                                 running.
187   * @param  dependencyIDs           The list of task IDs that will be required
188   *                                 to complete before this task will be
189   *                                 eligible to start.
190   * @param  failedDependencyAction  Indicates what action should be taken if
191   *                                 any of the dependencies for this task do
192   *                                 not complete successfully.
193   * @param  notifyOnCompletion      The list of e-mail addresses of individuals
194   *                                 that should be notified when this task
195   *                                 completes.
196   * @param  notifyOnError           The list of e-mail addresses of individuals
197   *                                 that should be notified if this task does
198   *                                 not complete successfully.
199   */
200  public AddSchemaFileTask(final String taskID,
201                           final List<String> schemaFileNames,
202                           final Date scheduledStartTime,
203                           final List<String> dependencyIDs,
204                           final FailedDependencyAction failedDependencyAction,
205                           final List<String> notifyOnCompletion,
206                           final List<String> notifyOnError)
207  {
208    super(taskID, ADD_SCHEMA_FILE_TASK_CLASS, scheduledStartTime,
209          dependencyIDs, failedDependencyAction, notifyOnCompletion,
210          notifyOnError);
211
212    ensureNotNull(schemaFileNames);
213    ensureFalse(schemaFileNames.isEmpty(),
214                "AddSchemaFileTask.schemaFileNames must not be empty.");
215
216    this.schemaFileNames = Collections.unmodifiableList(schemaFileNames);
217  }
218
219
220
221  /**
222   * Creates a new add schema file task from the provided entry.
223   *
224   * @param  entry  The entry to use to create this add schema file task.
225   *
226   * @throws  TaskException  If the provided entry cannot be parsed as a
227   *                         add schema file task entry.
228   */
229  public AddSchemaFileTask(final Entry entry)
230         throws TaskException
231  {
232    super(entry);
233
234    // Get the set of schema file names.  It must be present.
235    final String[] fileNames = entry.getAttributeValues(ATTR_SCHEMA_FILE);
236    if ((fileNames == null) || (fileNames.length == 0))
237    {
238      throw new TaskException(ERR_ADD_SCHEMA_FILE_TASK_NO_FILES.get(
239                                   getTaskEntryDN()));
240    }
241
242    schemaFileNames = Collections.unmodifiableList(Arrays.asList(fileNames));
243  }
244
245
246
247  /**
248   * Creates a new add schema file task from the provided set of task
249   * properties.
250   *
251   * @param  properties  The set of task properties and their corresponding
252   *                     values to use for the task.  It must not be
253   *                     {@code null}.
254   *
255   * @throws  TaskException  If the provided set of properties cannot be used to
256   *                         create a valid add schema file task.
257   */
258  public AddSchemaFileTask(final Map<TaskProperty,List<Object>> properties)
259         throws TaskException
260  {
261    super(ADD_SCHEMA_FILE_TASK_CLASS, properties);
262
263    String[] names = null;
264    for (final Map.Entry<TaskProperty,List<Object>> entry :
265         properties.entrySet())
266    {
267      final TaskProperty p = entry.getKey();
268      final String attrName = p.getAttributeName();
269      final List<Object> values = entry.getValue();
270
271      if (attrName.equalsIgnoreCase(ATTR_SCHEMA_FILE))
272      {
273        names = parseStrings(p, values, names);
274      }
275    }
276
277    if (names == null)
278    {
279      throw new TaskException(ERR_ADD_SCHEMA_FILE_TASK_NO_FILES.get(
280                                   getTaskEntryDN()));
281    }
282
283    schemaFileNames = Collections.unmodifiableList(Arrays.asList(names));
284  }
285
286
287
288  /**
289   * {@inheritDoc}
290   */
291  @Override()
292  public String getTaskName()
293  {
294    return INFO_TASK_NAME_ADD_SCHEMA_FILE.get();
295  }
296
297
298
299  /**
300   * {@inheritDoc}
301   */
302  @Override()
303  public String getTaskDescription()
304  {
305    return INFO_TASK_DESCRIPTION_ADD_SCHEMA_FILE.get();
306  }
307
308
309
310  /**
311   * Retrieves the names (without path information) of the schema files to be
312   * added to the server.
313   *
314   * @return  The names of the schema files to be added to the server.
315   */
316  public List<String> getSchemaFileNames()
317  {
318    return schemaFileNames;
319  }
320
321
322
323  /**
324   * {@inheritDoc}
325   */
326  @Override()
327  protected List<String> getAdditionalObjectClasses()
328  {
329    return Arrays.asList(OC_ADD_SCHEMA_FILE_TASK);
330  }
331
332
333
334  /**
335   * {@inheritDoc}
336   */
337  @Override()
338  protected List<Attribute> getAdditionalAttributes()
339  {
340    return Arrays.asList(new Attribute(ATTR_SCHEMA_FILE, schemaFileNames));
341  }
342
343
344
345  /**
346   * {@inheritDoc}
347   */
348  @Override()
349  public List<TaskProperty> getTaskSpecificProperties()
350  {
351    return Collections.unmodifiableList(Arrays.asList(PROPERTY_SCHEMA_FILE));
352  }
353
354
355
356  /**
357   * {@inheritDoc}
358   */
359  @Override()
360  public Map<TaskProperty,List<Object>> getTaskPropertyValues()
361  {
362    final LinkedHashMap<TaskProperty,List<Object>> props =
363         new LinkedHashMap<TaskProperty,List<Object>>();
364
365    props.put(PROPERTY_SCHEMA_FILE,
366              Collections.<Object>unmodifiableList(schemaFileNames));
367
368    props.putAll(super.getTaskPropertyValues());
369    return Collections.unmodifiableMap(props);
370  }
371}