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

abobrov
13.03.2009 27ede8298aee9ed2b6f83863173ba2415189b4f6
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
/*
 * 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
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * 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
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  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 2008-2009 Sun Microsystems, Inc.
 */
package org.opends.server.workflowelement.ndb;
 
 
 
import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;
 
import org.opends.messages.Message;
import org.opends.server.admin.server.ServerManagementContext;
import org.opends.server.admin.std.server.BackendCfg;
import org.opends.server.admin.std.server.LocalBackendWorkflowElementCfg;
import org.opends.server.admin.std.server.RootCfg;
import org.opends.server.api.Backend;
import org.opends.server.config.ConfigException;
import org.opends.server.core.AddOperation;
import org.opends.server.core.BindOperation;
import org.opends.server.core.CompareOperation;
import org.opends.server.core.DeleteOperation;
import org.opends.server.core.DirectoryServer;
import org.opends.server.core.ModifyDNOperation;
import org.opends.server.core.ModifyOperation;
import org.opends.server.core.SearchOperation;
import org.opends.server.types.*;
import
  org.opends.server.workflowelement.localbackend.LocalBackendWorkflowElement;
 
import static org.opends.server.config.ConfigConstants.*;
 
 
 
/**
 * This class defines a NDB backend workflow element; e-g an entity that
 * handle the processing of an operation against a NDB backend.
 */
public class NDBWorkflowElement extends LocalBackendWorkflowElement
{
  // The backend associated with the NDB workflow element.
  private Backend backend;
 
 
  // The set of NDB backend workflow elements registered with the server.
  private static TreeMap<String, NDBWorkflowElement>
       registeredNDBBackends =
            new TreeMap<String, NDBWorkflowElement>();
 
 
  // The lock to guarantee safe concurrent access to the
  // registeredNDBBackends variable.
  private static final Object registeredNDBBackendsLock = new Object();
 
 
  // The string indicating the type of the workflow element.
  private final String BACKEND_WORKFLOW_ELEMENT = "Backend";
 
 
  /**
   * Creates a new instance of the NDB backend workflow element.
   */
  public NDBWorkflowElement()
  {
    // There is nothing to do in this constructor.
  }
 
 
  /**
   * Initializes a new instance of the NDB backend workflow element.
   * This method is intended to be called by DirectoryServer when
   * workflow configuration mode is auto as opposed to
   * initializeWorkflowElement which is invoked when workflow
   * configuration mode is manual.
   *
   * @param workflowElementID  the workflow element identifier
   * @param backend  the backend associated to that workflow element
   */
  private void initialize(String workflowElementID, Backend backend)
  {
    // Initialize the workflow ID
    super.initialize(workflowElementID, BACKEND_WORKFLOW_ELEMENT);
 
    this.backend  = backend;
 
    if (this.backend != null)
    {
      setPrivate(this.backend.isPrivateBackend());
    }
  }
 
 
  /**
   * Initializes a new instance of the NDB backend workflow element.
   * This method is intended to be called by DirectoryServer when
   * workflow configuration mode is manual as opposed to
   * initialize(String,Backend) which is invoked when workflow
   * configuration mode is auto.
   *
   * @param  configuration  The configuration for this NDB backend
   *                        workflow element.
   *
   * @throws  ConfigException  If there is a problem with the provided
   *                           configuration.
   *
   * @throws  InitializationException  If an error occurs while trying
   *                                   to initialize this workflow
   *                                   element that is not related to
   *                                   the provided configuration.
   */
  @Override
  public void initializeWorkflowElement(
      LocalBackendWorkflowElementCfg configuration
      ) throws ConfigException, InitializationException
  {
    configuration.addLocalBackendChangeListener(this);
 
    // Read configuration and apply changes.
    processWorkflowElementConfig(configuration, true);
  }
 
 
  /**
   * {@inheritDoc}
   */
  @Override
  public void finalizeWorkflowElement()
  {
    // null all fields so that any use of the finalized object will raise
    // an NPE
    super.initialize(null, null);
    backend = null;
  }
 
 
  /**
   * {@inheritDoc}
   */
  @Override
  public boolean isConfigurationChangeAcceptable(
      LocalBackendWorkflowElementCfg configuration,
      List<Message>                  unacceptableReasons
      )
  {
    boolean isAcceptable =
      processWorkflowElementConfig(configuration, false);
 
    return isAcceptable;
  }
 
 
  /**
   * {@inheritDoc}
   */
  @Override
  public ConfigChangeResult applyConfigurationChange(
      LocalBackendWorkflowElementCfg configuration
      )
  {
    // Returned result.
    ConfigChangeResult changeResult = new ConfigChangeResult(
        ResultCode.SUCCESS, false, new ArrayList<Message>()
        );
 
    processWorkflowElementConfig(configuration, true);
 
    return changeResult;
  }
 
 
  /**
   * Parses the provided configuration and configure the workflow element.
   *
   * @param configuration  The new configuration containing the changes.
   * @param applyChanges   If true then take into account the new configuration.
   *
   * @return  <code>true</code> if the configuration is acceptable.
   */
  private boolean processWorkflowElementConfig(
      LocalBackendWorkflowElementCfg configuration,
      boolean                        applyChanges
      )
  {
    // returned status
    boolean isAcceptable = true;
 
    // If the workflow element is disabled then do nothing. Note that the
    // configuration manager could have finalized the object right before.
    if (configuration.isEnabled())
    {
      // Read configuration.
      String newBackendID = configuration.getBackend();
      Backend newBackend  = DirectoryServer.getBackend(newBackendID);
 
      // If the backend is null (i.e. not found in the list of
      // registered backends, this is probably because we are looking
      // for the config backend
      if (newBackend == null) {
        ServerManagementContext context = ServerManagementContext.getInstance();
        RootCfg root = context.getRootConfiguration();
        try {
          BackendCfg backendCfg = root.getBackend(newBackendID);
          if (backendCfg.getBaseDN().contains(DN.decode(DN_CONFIG_ROOT))) {
            newBackend = DirectoryServer.getConfigHandler();
          }
        } catch (Exception ex) {
          // Unable to find the backend
          newBackend = null;
        }
      }
 
      // Get the new configuration
      if (applyChanges)
      {
        super.initialize(
          configuration.getWorkflowElementId(), BACKEND_WORKFLOW_ELEMENT);
        backend = newBackend;
      }
    }
 
    return isAcceptable;
  }
 
 
  /**
   * Creates and registers a NDB backend with the server.
   *
   * @param workflowElementID  the identifier of the workflow element to create
   * @param backend            the backend to associate with the NDB backend
   *                           workflow element
   *
   * @return the existing NDB backend workflow element if it was
   *         already created or a newly created NDB backend workflow
   *         element.
   */
  public static NDBWorkflowElement createAndRegister(
      String workflowElementID,
      Backend backend)
  {
    NDBWorkflowElement ndbBackend = null;
 
    // If the requested workflow element does not exist then create one.
    ndbBackend = registeredNDBBackends.get(workflowElementID);
    if (ndbBackend == null)
    {
      ndbBackend = new NDBWorkflowElement();
      ndbBackend.initialize(workflowElementID, backend);
 
      // store the new NDB backend in the list of registered backends
      registerNDBBackend(ndbBackend);
    }
 
    return ndbBackend;
  }
 
 
 
  /**
   * Removes a NDB backend that was registered with the server.
   *
   * @param workflowElementID  the identifier of the workflow element to remove
   */
  public static void remove(String workflowElementID)
  {
    deregisterNDBBackend(workflowElementID);
  }
 
 
 
  /**
   * Removes all the NDB backends that were registered with the server.
   * This function is intended to be called when the server is shutting down.
   */
  public static void removeAll()
  {
    synchronized (registeredNDBBackendsLock)
    {
      for (NDBWorkflowElement ndbBackend:
           registeredNDBBackends.values())
      {
        deregisterNDBBackend(ndbBackend.getWorkflowElementID());
      }
    }
  }
 
 
 
  /**
   * Registers a NDB backend with the server.
   *
   * @param ndbBackend  the NDB backend to register with the server
   */
  private static void registerNDBBackend(
                           NDBWorkflowElement ndbBackend)
  {
    synchronized (registeredNDBBackendsLock)
    {
      String ndbBackendID = ndbBackend.getWorkflowElementID();
      NDBWorkflowElement existingNDBBackend =
        registeredNDBBackends.get(ndbBackendID);
 
      if (existingNDBBackend == null)
      {
        TreeMap<String, NDBWorkflowElement> newNDBBackends =
          new TreeMap
            <String, NDBWorkflowElement>(registeredNDBBackends);
        newNDBBackends.put(ndbBackendID, ndbBackend);
        registeredNDBBackends = newNDBBackends;
      }
    }
  }
 
 
 
  /**
   * Deregisters a NDB backend with the server.
   *
   * @param workflowElementID  the identifier of the workflow element to remove
   */
  private static void deregisterNDBBackend(String workflowElementID)
  {
    synchronized (registeredNDBBackendsLock)
    {
      NDBWorkflowElement existingNDBBackend =
        registeredNDBBackends.get(workflowElementID);
 
      if (existingNDBBackend != null)
      {
        TreeMap<String, NDBWorkflowElement> newNDBBackends =
             new TreeMap<String, NDBWorkflowElement>(
                      registeredNDBBackends);
        newNDBBackends.remove(workflowElementID);
        registeredNDBBackends = newNDBBackends;
      }
    }
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override
  public void execute(Operation operation) throws CanceledOperationException {
    switch (operation.getOperationType())
    {
      case BIND:
        NDBBindOperation bindOperation =
             new NDBBindOperation((BindOperation) operation);
        bindOperation.processLocalBind(this);
        break;
 
      case SEARCH:
        NDBSearchOperation searchOperation =
             new NDBSearchOperation((SearchOperation) operation);
        searchOperation.processLocalSearch(this);
        break;
 
      case ADD:
        NDBAddOperation addOperation =
             new NDBAddOperation((AddOperation) operation);
        addOperation.processLocalAdd(this);
        break;
 
      case DELETE:
        NDBDeleteOperation deleteOperation =
             new NDBDeleteOperation((DeleteOperation) operation);
        deleteOperation.processLocalDelete(this);
        break;
 
      case MODIFY:
        NDBModifyOperation modifyOperation =
             new NDBModifyOperation((ModifyOperation) operation);
        modifyOperation.processLocalModify(this);
        break;
 
      case MODIFY_DN:
        NDBModifyDNOperation modifyDNOperation =
             new NDBModifyDNOperation((ModifyDNOperation) operation);
        modifyDNOperation.processLocalModifyDN(this);
        break;
 
      case COMPARE:
        NDBCompareOperation compareOperation =
             new NDBCompareOperation((CompareOperation) operation);
        compareOperation.processLocalCompare(this);
        break;
 
      case ABANDON:
        // There is no processing for an abandon operation.
        break;
 
      default:
        throw new AssertionError("Attempted to execute an invalid operation " +
                                 "type:  " + operation.getOperationType() +
                                 " (" + operation + ")");
    }
  }
 
 
 
  /**
   * Gets the backend associated with this NDB backend workflow element.
   *
   * @return The backend associated with this NDB backend workflow
   *         element.
   */
  @Override
  public Backend getBackend()
  {
    return backend;
  }
}