mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Nicolas Capponi
14.22.2016 5360b07bd36e6b3479c1bb3f911bb34f8d661b1f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2008-2009 Sun Microsystems, Inc.
 * Portions Copyright 2014-2016 ForgeRock AS.
 */
package org.opends.server.tools.tasks;
 
import static org.opends.server.util.CollectionUtils.*;
import static org.opends.server.util.ServerConstants.*;
 
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;
 
import org.forgerock.i18n.LocalizableMessage;
import org.forgerock.opendj.ldap.ByteString;
import org.forgerock.opendj.ldap.DN;
import org.forgerock.opendj.ldap.schema.AttributeType;
import org.opends.server.backends.task.FailedDependencyAction;
import org.opends.server.backends.task.Task;
import org.opends.server.backends.task.TaskState;
import org.opends.server.types.Attribute;
import org.opends.server.types.Entry;
import org.opends.server.util.StaticUtils;
 
/**
 * Processes information from a task entry from the directory and
 * provides accessors for attribute information.  In some cases the
 * data is formatted into more human-friendly formats.
 */
public class TaskEntry {
 
  private static Map<String, LocalizableMessage> mapClassToTypeName = new HashMap<>();
  private static Map<String, LocalizableMessage> mapAttrToDisplayName = new HashMap<>();
 
  private int hashCode;
 
  /**
   * These attributes associated with the ds-task object
   * class are all handled explicitly below in the constructor.
   */
  private static final Set<String> supAttrNames = newHashSet(
    // @formatter:off
    "ds-task-id",
    "ds-task-class-name",
    "ds-task-state",
    "ds-task-scheduled-start-time",
    "ds-task-actual-start-time",
    "ds-task-completion-time",
    "ds-task-dependency-id",
    "ds-task-failed-dependency-action",
    "ds-task-log-message",
    "ds-task-notify-on-completion",
    "ds-task-notify-on-error",
    "ds-recurring-task-id",
    "ds-recurring-task-schedule");
    // @formatter:on
 
  private String id;
  private String className;
  private String state;
  private String schedStart;
  private String actStart;
  private String compTime;
  private String schedTab;
  private List<String> depends;
  private String depFailAct;
  private List<String> logs;
  private List<String> notifyComp;
  private List<String> notifyErr;
  private DN dn;
 
  /**
   * Task of the same type that implements.  Used for obtaining
   * task name and attribute display information.
   */
  private Task task;
 
  private Map<LocalizableMessage, List<String>> taskSpecificAttrValues = new HashMap<>();
 
  /**
   * Creates a parameterized instance.
   *
   * @param entry to wrap
   */
  public TaskEntry(Entry entry) {
    dn = entry.getName();
 
    String p = "ds-task-";
    id =         getSingleStringValue(entry, p + "id");
    className =  getSingleStringValue(entry, p + "class-name");
    state =      getSingleStringValue(entry, p + "state");
    schedStart = getSingleStringValue(entry, p + "scheduled-start-time");
    actStart =   getSingleStringValue(entry, p + "actual-start-time");
    compTime =   getSingleStringValue(entry, p + "completion-time");
    depends =    getMultiStringValue(entry,  p + "dependency-id");
    depFailAct = getSingleStringValue(entry, p + "failed-dependency-action");
    logs =       getMultiStringValue(entry,  p + "log-message");
    notifyErr =  getMultiStringValue(entry,  p + "notify-on-error");
    notifyComp = getMultiStringValue(entry,  p + "notify-on-completion");
    schedTab =   getSingleStringValue(entry, "ds-recurring-task-schedule");
 
    // Build a map of non-superior attribute value pairs for display
    for (AttributeType attrType : entry.getUserAttributes().keySet()) {
      // See if we've handled it already above
      if (!hasAnyNameOrOID(attrType, supAttrNames)) {
        LocalizableMessage attrTypeName = getAttributeDisplayName(attrType);
        for (Attribute attr : entry.getUserAttribute(attrType)) {
          for (ByteString av : attr) {
            List<String> valueList = taskSpecificAttrValues.get(attrTypeName);
            if (valueList == null) {
              valueList = new ArrayList<>();
              taskSpecificAttrValues.put(attrTypeName, valueList);
            }
            valueList.add(av.toString());
          }
        }
      }
    }
 
    hashCode += id.hashCode();
    hashCode += className.hashCode();
    hashCode += state.hashCode();
    hashCode += schedStart.hashCode();
    hashCode += actStart.hashCode();
    hashCode += compTime.hashCode();
    hashCode += depends.hashCode();
    hashCode += depFailAct.hashCode();
    hashCode += logs.hashCode();
    hashCode += notifyErr.hashCode();
    hashCode += notifyComp.hashCode();
    hashCode += schedTab.hashCode();
    hashCode += taskSpecificAttrValues.hashCode();
  }
 
  private boolean hasAnyNameOrOID(AttributeType attrType, Set<String> attrNames)
  {
    for (String attrName : attrNames)
    {
      if (attrType.hasNameOrOID(attrName))
      {
        return true;
      }
    }
    return false;
  }
 
  @Override
  public int hashCode()
  {
    return hashCode;
  }
 
  @Override
  public boolean equals(Object o)
  {
    if (this == o)
    {
      return true;
    }
    if (! (o instanceof TaskEntry))
    {
      return false;
    }
 
    TaskEntry e = (TaskEntry) o;
 
    return e.id.equals(id) &&
    e.className.equals(className) &&
    e.state.equals(state) &&
    e.schedStart.equals(schedStart) &&
    e.actStart.equals(actStart) &&
    e.compTime.equals(compTime) &&
    e.depends.equals(depends) &&
    e.depFailAct.equals(depFailAct) &&
    e.logs.equals(logs) &&
    e.notifyErr.equals(notifyErr) &&
    e.notifyComp.equals(notifyComp) &&
    e.schedTab.equals(schedTab) &&
    e.taskSpecificAttrValues.equals(taskSpecificAttrValues);
  }
 
  /**
   * Gets the DN of the wrapped entry.
   *
   * @return DN of entry
   */
  public DN getDN() {
    return dn;
  }
 
  /**
   * Gets the ID of the task.
   *
   * @return String ID of the task
   */
  public String getId() {
    return id;
  }
 
  /**
   * Gets the name of the class implementing the task represented here.
   *
   * @return String name of class
   */
  public String getClassName() {
    return className;
  }
 
  /**
   * Gets the state of the task.
   *
   * @return LocalizableMessage representing state
   */
  public LocalizableMessage getState() {
    LocalizableMessage m = LocalizableMessage.EMPTY;
    if (state != null) {
      TaskState ts = TaskState.fromString(state);
      if (ts != null) {
        m = ts.getDisplayName();
      }
    }
    return m;
  }
 
  /**
   * Gets the human-friendly scheduled time.
   *
   * @return String time
   */
  public LocalizableMessage getScheduledStartTime() {
    return formatTimeString(schedStart);
  }
 
  /**
   * Gets the human-friendly start time.
   *
   * @return String time
   */
  public LocalizableMessage getActualStartTime() {
    return formatTimeString(actStart);
  }
 
  /**
   * Gets the human-friendly completion time.
   *
   * @return String time
   */
  public LocalizableMessage getCompletionTime() {
    return formatTimeString(compTime);
  }
 
  /**
   * Gets recurring schedule tab.
   *
   * @return LocalizableMessage tab string
   */
  public LocalizableMessage getScheduleTab() {
    return LocalizableMessage.raw(schedTab);
  }
 
  /**
   * Gets the IDs of tasks upon which this task depends.
   *
   * @return array of IDs
   */
  public List<String> getDependencyIds() {
    return Collections.unmodifiableList(depends);
  }
 
  /**
   * Gets the action to take if this task fails.
   *
   * @return String action
   */
  public LocalizableMessage getFailedDependencyAction() {
    LocalizableMessage m = null;
    if (depFailAct != null) {
      FailedDependencyAction fda =
              FailedDependencyAction.fromString(depFailAct);
      if (fda != null) {
        m = fda.getDisplayName();
      }
    }
    return m;
  }
 
  /**
   * Gets the logs associated with this task's execution.
   *
   * @return array of log messages
   */
  public List<LocalizableMessage> getLogMessages() {
    List<LocalizableMessage> formattedLogs = new ArrayList<>();
    for (String aLog : logs) {
      formattedLogs.add(LocalizableMessage.raw(aLog));
    }
    return Collections.unmodifiableList(formattedLogs);
  }
 
  /**
   * Gets the email messages that will be used for notifications
   * when the task completes.
   *
   * @return array of email addresses
   */
  public List<String> getCompletionNotificationEmailAddresses() {
    return Collections.unmodifiableList(notifyComp);
  }
 
  /**
   * Gets the email messages that will be used for notifications
   * when the task encounters an error.
   *
   * @return array of email addresses
   */
  public List<String> getErrorNotificationEmailAddresses() {
    return Collections.unmodifiableList(notifyErr);
  }
 
  /**
   * Gets a user presentable string indicating the type of this task.
   *
   * @return LocalizableMessage type
   */
  public LocalizableMessage getType() {
    LocalizableMessage type = LocalizableMessage.EMPTY;
    if (className != null) {
      type = mapClassToTypeName.get(className);
      if (type == null) {
        Task task = getTask();
        if (task != null) {
          LocalizableMessage message = task.getDisplayName();
          mapClassToTypeName.put(className, message);
          type = message;
        }
      }
 
      // If we still can't get the type just resort
      // to the class displayName
      if (type == null) {
        type = LocalizableMessage.raw(className);
      }
    }
    return type;
  }
 
  /**
   * Indicates whether this task supports a cancel operation.
   *
   * @return boolean where true means this task supports being canceled.
   */
  public boolean isCancelable() {
    TaskState state = getTaskState();
    if (state != null) {
      Task task = getTask();
      return TaskState.isPending(state)
          || TaskState.isRecurring(state)
          || (TaskState.isRunning(state)
              && task != null
              && task.isInterruptable());
    }
    return false;
  }
 
  /**
   * Gets a mapping of attributes that are specific to the implementing
   * task as opposed to the superior, or base, task.
   *
   * @return mapping of attribute field labels to lists of string values for each field.
   */
  public Map<LocalizableMessage, List<String>> getTaskSpecificAttributeValuePairs() {
    return taskSpecificAttrValues;
  }
 
  /**
   * Gets the task state.
   *
   * @return TaskState of task
   */
  public TaskState getTaskState() {
    TaskState ts = null;
    if (state != null) {
      ts = TaskState.fromString(state);
    }
    return ts;
  }
 
  /**
   * Indicates whether this task is done.
   *
   * @return boolean where true means this task is done
   */
  public boolean isDone() {
    TaskState ts = getTaskState();
    return ts != null && TaskState.isDone(ts);
  }
 
  private String getSingleStringValue(Entry entry, String attrName) {
    Iterator<Attribute> attrs = entry.getAllAttributes(attrName).iterator();
    if (attrs.hasNext()) {
      Attribute attr = attrs.next();
      if (attrs.hasNext()) {
        return "";
      }
      if (!attr.isEmpty()) {
        return attr.iterator().next().toString();
      }
    }
    return "";
  }
 
  private List<String> getMultiStringValue(Entry entry, String attrName) {
    List<String> valuesList = new ArrayList<>();
    for (Attribute attr : entry.getAllAttributes(attrName)) {
      for (ByteString value : attr) {
        valuesList.add(value.toString());
      }
    }
    return valuesList;
  }
 
  private LocalizableMessage getAttributeDisplayName(AttributeType attrType) {
    final String attrName = StaticUtils.toLowerCase(attrType.getNameOrOID());
    LocalizableMessage name = mapAttrToDisplayName.get(attrName);
    if (name == null) {
      Task task = getTask();
      if (task != null) {
        try {
          Object o = task.getAttributeDisplayName(attrName);
          if (o instanceof LocalizableMessage) {
            name= (LocalizableMessage)o;
            mapAttrToDisplayName.put(attrName, name);
          }
        } catch (Exception e) {
          // ignore
        }
      }
    }
    if (name == null) {
      name = LocalizableMessage.raw(attrName);
    }
    return name;
  }
 
  /**
   * Formats a time string into a human friendly format.
   * @param timeString the is human hostile
   * @return string of time that is human friendly
   */
  private LocalizableMessage formatTimeString(String timeString) {
    LocalizableMessage ret = LocalizableMessage.EMPTY;
    if (timeString != null && timeString.length() > 0) {
      try {
        SimpleDateFormat dateFormat;
        if (timeString.endsWith("Z")) {
          dateFormat = new SimpleDateFormat(DATE_FORMAT_GMT_TIME);
          dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        } else {
          dateFormat = new SimpleDateFormat(DATE_FORMAT_COMPACT_LOCAL_TIME);
        }
        Date date = dateFormat.parse(timeString);
        DateFormat df = DateFormat.getDateTimeInstance(
                DateFormat.MEDIUM,
                DateFormat.LONG);
        String dateString = df.format(date);
        ret = LocalizableMessage.raw(dateString);
      } catch (ParseException pe){
        ret = LocalizableMessage.raw(timeString);
      }
    }
    return ret;
  }
 
  private Task getTask() {
    if (task == null && className != null) {
      try {
        Class<?> clazz = Class.forName(className);
        Object o = clazz.newInstance();
        if (Task.class.isAssignableFrom(o.getClass())) {
          this.task = (Task) o;
        }
      } catch (Exception e) {
        // ignore; this is best effort
      }
    }
    return task;
  }
}