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

Jean-Noel Rouvignac
01.51.2014 02bbeacbfb05101989dac510cbef7815fdf28a2e
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2007-2008 Sun Microsystems, Inc.
 */
package org.opends.server.core;
 
 
import java.util.ArrayList;
 
import org.opends.server.types.*;
import org.opends.server.workflowelement.WorkflowElement;
 
 
/**
 * This class implements a workflow node. A workflow node is used
 * to build a tree of workflows (aka workflow topology). Each node
 * may have a parent node and/or subordinate nodes. A node with no
 * parent is a naming context.
 *
 * Each node in the workflow topology is linked to a WorkflowImpl
 * which contains the real processing. The base DN of the workflow
 * node is the base DN of the related WorkflowImpl.
 *
 * How the workflow topology is built?
 * A workflow node is a subordinate of another workflow node when
 * the base DN of the former workflow is an ancestor of the base DN
 * of the latter workflow.
 *
 * A subtree search on a workflow node is performed on the node itself as
 * well as on all the subordinate nodes.
 */
public class WorkflowTopologyNode extends WorkflowTopology
{
  // Parent node of the current workflow node.
  private WorkflowTopologyNode parent = null;
 
 
  // The list of subordinate nodes of the current workflow node.
  private ArrayList<WorkflowTopologyNode> subordinates =
    new ArrayList<WorkflowTopologyNode>();
 
 
  /**
   * Creates a new node for a workflow topology. The new node is initialized
   * with a WorkflowImpl which contains the real processing. Optionally,
   * the node may have tasks to be executed before and/or after the real
   * processing. In the current implementation, such pre and post workflow
   * elements are not used.
   *
   * @param workflowImpl          the real processing attached to the node
   * @param preWorkflowElements   the list of tasks to be executed before
   *                              the real processing
   * @param postWorkflowElements  the list of tasks to be executed after
   *                              the real processing
   */
  public WorkflowTopologyNode(
      WorkflowImpl workflowImpl,
      WorkflowElement<?>[] preWorkflowElements,
      WorkflowElement<?>[] postWorkflowElements
      )
  {
    super(workflowImpl);
  }
 
 
  /**
   * Executes an operation on a set of data being identified by the
   * workflow node base DN.
   *
   * @param operation the operation to execute
   *
   * @throws CanceledOperationException if this operation should
   * be canceled.
   */
  public void execute(Operation operation)
      throws CanceledOperationException {
    // Execute the operation
    getWorkflowImpl().execute(operation);
 
    // For subtree search operation we need to go through the subordinate
    // nodes.
    if (operation.getOperationType() == OperationType.SEARCH)
    {
      executeSearchOnSubordinates((SearchOperation) operation);
    }
  }
 
 
  /**
   * Executes a search operation on the subordinate workflows.
   *
   * @param searchOp the search operation to execute
   *
   * @throws CanceledOperationException if this operation should
   * be canceled.
   */
  private void executeSearchOnSubordinates(SearchOperation searchOp)
      throws CanceledOperationException {
    // If the scope of the search is 'base' then it's useless to search
    // in the subordinate workflows.
    SearchScope originalScope = searchOp.getScope();
    if (originalScope == SearchScope.BASE_OBJECT)
    {
      return;
    }
 
    // Elaborate the new search scope before executing the search operation
    // in the subordinate workflows.
    SearchScope newScope = elaborateScopeForSearchInSubordinates(originalScope);
    searchOp.setScope(newScope);
 
    // Let's search in the subordinate workflows.
    WorkflowResultCode workflowResultCode = new WorkflowResultCode(
        searchOp.getResultCode(), searchOp.getErrorMessage());
    DN originalBaseDN = searchOp.getBaseDN();
    for (WorkflowTopologyNode subordinate: getSubordinates())
    {
      // We have to change the operation request base DN to match the
      // subordinate workflow base DN. Otherwise the workflow will
      // return a no such entry result code as the operation request
      // base DN is a superior of the subordinate workflow base DN.
      DN subordinateDN = subordinate.getBaseDN();
 
      // If the new search scope is 'base' and the search base DN does not
      // map the subordinate workflow then skip the subordinate workflow.
      if ((newScope == SearchScope.BASE_OBJECT)
          && !subordinateDN.getParent().equals(originalBaseDN))
      {
        continue;
      }
 
      // If the request base DN is not a subordinate of the subordinate
      // workflow base DN then don't search in the subordinate workflow.
      if (! originalBaseDN.isAncestorOf(subordinateDN))
      {
        continue;
      }
 
      // Set the new request base DN and do execute the
      // operation in the subordinate workflow.
      searchOp.setBaseDN(subordinateDN);
      subordinate.execute(searchOp);
      boolean sendReferenceEntry =
        workflowResultCode.elaborateGlobalResultCode(
          searchOp.getResultCode(), searchOp.getErrorMessage());
      if (sendReferenceEntry)
      {
        // TODO jdemendi - turn a referral result code into a reference entry
        // and send the reference entry to the client application
      }
    }
 
    // Now we are done with the operation, let's restore the original
    // base DN and search scope in the operation.
    searchOp.setBaseDN(originalBaseDN);
    searchOp.setScope(originalScope);
 
    // Update the operation result code and error message
    searchOp.setResultCode(workflowResultCode.resultCode());
    searchOp.setErrorMessage(workflowResultCode.errorMessage());
  }
 
 
  /**
   * Sets the parent workflow.
   *
   * @param parent  the parent workflow of the current workflow
   */
  public void setParent(WorkflowTopologyNode parent)
  {
    this.parent = parent;
  }
 
 
  /**
   * Gets the parent workflow.
   *
   * @return the parent workflow.
   */
  public WorkflowTopologyNode getParent()
  {
    return parent;
  }
 
 
  /**
   * Indicates whether the root workflow element is encapsulating a private
   * local backend or not.
   *
   * @return <code>true</code> if the root workflow element encapsulates
   *         a private local backend
   */
  public boolean isPrivate()
  {
    return getWorkflowImpl().isPrivate();
  }
 
 
  /**
   * Gets the base DN of the workflow that handles a given dn. The elected
   * workflow may be the current workflow or one of its subordinate workflows.
   *
   * @param  dn  the DN for which we are looking a parent DN
   * @return the base DN which is the parent of the <code>dn</code>,
   *         <code>null</code> if no parent DN was found
   */
  public DN getParentBaseDN(DN dn)
  {
    if (dn == null)
    {
      return null;
    }
 
    // parent base DN to return
    DN parentBaseDN = null;
 
    // Is the dn a subordinate of the current base DN?
    DN curBaseDN = getBaseDN();
    if (curBaseDN != null)
    {
      if (dn.isDescendantOf(curBaseDN))
      {
        // The dn may be handled by the current workflow.
        // Now we have to check whether the dn is handled by
        // a subordinate.
        for (WorkflowTopologyNode subordinate: getSubordinates())
        {
          parentBaseDN = subordinate.getParentBaseDN(dn);
          if (parentBaseDN != null)
          {
            // the dn is handled by a subordinate
            break;
          }
        }
 
        // If the dn is not handled by any subordinate, then it is
        // handled by the current workflow.
        if (parentBaseDN == null)
        {
          parentBaseDN = curBaseDN;
        }
      }
    }
 
    return parentBaseDN;
  }
 
 
  /**
   * Adds a workflow to the list of workflow subordinates without
   * additional control.
   *
   * @param newWorkflow     the workflow to add to the subordinate list
   * @param parentWorkflow  the parent workflow of the new workflow
   */
  private void addSubordinateNoCheck(
      WorkflowTopologyNode newWorkflow,
      WorkflowTopologyNode parentWorkflow
      )
  {
    subordinates.add(newWorkflow);
    newWorkflow.setParent(parentWorkflow);
  }
 
 
  /**
   * Adds a workflow to the subordinate list of the current workflow.
   * Before we can add the new workflow, we have to check whether
   * the new workflow is a parent workflow of any of the current
   * subordinates (if so, then we have to add the subordinate in the
   * subordinate list of the new workflow).
   *
   * @param newWorkflow  the workflow to add in the subordinate list
   */
  private void addSubordinate(
      WorkflowTopologyNode newWorkflow
      )
  {
    // Don't try to add the workflow to itself.
    if (newWorkflow == this)
    {
      return;
    }
 
    // Check whether subordinates of current workflow should move to the
    // new workflow subordinate list.
    ArrayList<WorkflowTopologyNode> curSubordinateList =
        new ArrayList<WorkflowTopologyNode>(getSubordinates());
 
    for (WorkflowTopologyNode curSubordinate: curSubordinateList)
    {
      DN newDN = newWorkflow.getBaseDN();
      DN subordinateDN = curSubordinate.getBaseDN();
 
      // Don't try to add workflow when baseDNs are
      // the same on both workflows.
      if (newDN.equals(subordinateDN)) {
        return;
      }
 
      if (subordinateDN.isDescendantOf(newDN))
      {
        removeSubordinate(curSubordinate);
        newWorkflow.addSubordinate(curSubordinate);
      }
    }
 
    // add the new workflow in the current workflow subordinate list
    addSubordinateNoCheck(newWorkflow, this);
  }
 
 
  /**
   * Remove a workflow from the subordinate list.
   *
   * @param subordinate  the subordinate to remove from the subordinate list
   */
  public void removeSubordinate(
      WorkflowTopologyNode subordinate
      )
  {
    subordinates.remove(subordinate);
  }
 
 
  /**
   * Tries to insert a new workflow in the subordinate list of one of the
   * current workflow subordinate, or in the current workflow subordinate list.
   *
   * @param newWorkflow  the new workflow to insert
   *
   * @return <code>true</code> if the new workflow has been inserted
   *         in any subordinate list
   */
  public boolean insertSubordinate(
      WorkflowTopologyNode newWorkflow
      )
  {
    // don't try to insert the workflow in itself!
    if (newWorkflow == this)
    {
      return false;
    }
 
    // the returned status
    boolean insertDone = false;
 
    DN parentBaseDN = getBaseDN();
    DN newBaseDN    = newWorkflow.getBaseDN();
 
    // don't try to insert workflows when baseDNs are the same on both
    // workflows
    if (parentBaseDN.equals(newBaseDN))
    {
      return false;
    }
 
    // try to insert the new workflow
    if (newBaseDN.isDescendantOf(parentBaseDN))
    {
      // the new workflow is a subordinate for this parent DN, let's
      // insert the new workflow in the list of subordinates
      for (WorkflowTopologyNode subordinate: getSubordinates())
      {
        insertDone = subordinate.insertSubordinate(newWorkflow);
        if (insertDone)
        {
          // the newBaseDN is handled by a subordinate
          break;
        }
      }
 
      // if the newBaseDN is not handled by a subordinate then the workflow
      // is inserted it in the current workflow subordinate list
      if (! insertDone)
      {
        addSubordinate(newWorkflow);
        insertDone = true;
      }
    }
 
    return insertDone;
  }
 
 
  /**
   * Removes the current workflow from the parent subordinate list
   * and attach the workflow subordinates to the parent workflow.
   *
   * Example: the workflow to remove is w2
   *
   *        w1             w1
   *        |             / \
   *        w2     ==>   w3  w4
   *       / \
   *     w3   w4
   *
   * - Subordinate list of w1 is updated with w3 and w4.
   * - Parent workflow of w3 and w4 is now w1.
   */
  public void remove()
  {
    // First of all, remove the workflow from the parent subordinate list
    WorkflowTopologyNode parent = getParent();
    if (parent != null)
    {
      parent.removeSubordinate(this);
    }
 
    // Then set the parent of each subordinate and attach the subordinate to
    // the parent.
    for (WorkflowTopologyNode subordinate: getSubordinates())
    {
      subordinate.setParent(parent);
      if (parent != null)
      {
        parent.addSubordinateNoCheck(subordinate, parent);
      }
    }
  }
 
 
  /**
   * Gets the list of workflow subordinates.
   *
   * @return the list of workflow subordinates
   */
  public ArrayList<WorkflowTopologyNode> getSubordinates()
  {
    return subordinates;
  }
 
 
  /**
   * Gets the highest workflow in the topology that can handle the requestDN.
   * The highest workflow is either the current workflow or one of its
   * subordinates.
   *
   * @param requestDN  The DN for which we search for a workflow
   * @return the highest workflow that can handle the requestDN
   *         <code>null</code> if none was found
   */
  public WorkflowTopologyNode getWorkflowCandidate(
      DN requestDN
      )
  {
    // the returned workflow
    WorkflowTopologyNode workflowCandidate = null;
 
    // does the current workflow handle the request baseDN?
    DN baseDN = getParentBaseDN(requestDN);
    if (baseDN == null)
    {
      // the current workflow does not handle the requestDN,
      // let's return null
    }
    else
    {
      // is there any subordinate that can handle the requestDN?
      for (WorkflowTopologyNode subordinate: getSubordinates())
      {
        workflowCandidate = subordinate.getWorkflowCandidate(requestDN);
        if (workflowCandidate != null)
        {
          break;
        }
      }
 
      // none of the subordinates can handle the requestDN, so the current
      // workflow is the best root workflow candidate
      if (workflowCandidate == null)
      {
        workflowCandidate = this;
      }
    }
 
    return workflowCandidate;
  }
 
 
  /**
   * Dumps info from the current workflow for debug purpose.
   *
   * @param leftMargin  white spaces used to indent the traces
   * @return a string buffer that contains trace information
   */
  public StringBuilder toString(String leftMargin)
  {
    StringBuilder sb = new StringBuilder();
 
    // display the baseDN
    DN baseDN = getBaseDN();
    String workflowID = this.getWorkflowImpl().getWorkflowId();
    sb.append(leftMargin + "Workflow ID = " + workflowID + "\n");
    sb.append(leftMargin + "         baseDN:[");
    if (baseDN.isNullDN())
    {
      sb.append(" \"\"");
    }
    else
    {
      sb.append(" \"" + baseDN.toString() + "\"");
    }
    sb.append(" ]\n");
 
    // display the root workflow element
    sb.append(leftMargin
        + "         Root Workflow Element: "
        + getWorkflowImpl().getRootWorkflowElement() + "\n");
 
    // display parent workflow
    sb.append(leftMargin + "         Parent: " + getParent() + "\n");
 
    // dump each subordinate
    sb.append(leftMargin + "         List of subordinates:\n");
    ArrayList<WorkflowTopologyNode> subordinates = getSubordinates();
    if (subordinates.isEmpty())
    {
      sb.append(leftMargin + "            NONE\n");
    }
    else
    {
      for (WorkflowTopologyNode subordinate: getSubordinates())
      {
        sb.append(subordinate.toString(leftMargin + "            "));
      }
    }
 
    return sb;
  }
 
}