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

Jean-Noel Rouvignac
17.41.2013 c1fa743ae813a9e636244e113a5588d02f50db75
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
/*
 * 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 2007-2009 Sun Microsystems, Inc.
 *      Portions copyright 2011-2013 ForgeRock AS
 */
package org.opends.server.replication.server;
 
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.*;
 
import org.opends.messages.Message;
import org.opends.server.admin.Configuration;
import org.opends.server.admin.server.ServerManagementContext;
import org.opends.server.admin.std.server.*;
import org.opends.server.api.Backend;
import org.opends.server.api.SynchronizationProvider;
import org.opends.server.backends.jeb.BackupManager;
import org.opends.server.config.ConfigException;
import org.opends.server.core.*;
import org.opends.server.loggers.debug.DebugTracer;
import org.opends.server.protocols.internal.InternalClientConnection;
import org.opends.server.protocols.internal.InternalSearchOperation;
import org.opends.server.replication.common.CSN;
import org.opends.server.replication.common.ServerState;
import org.opends.server.replication.plugin.MultimasterReplication;
import org.opends.server.replication.plugin.ReplicationServerListener;
import org.opends.server.replication.protocol.*;
import org.opends.server.replication.server.changelog.api.ReplicaDBCursor;
import org.opends.server.types.*;
import org.opends.server.util.*;
 
import static java.util.Collections.*;
 
import static org.opends.messages.BackendMessages.*;
import static org.opends.messages.JebMessages.*;
import static org.opends.messages.ReplicationMessages.*;
import static org.opends.server.config.ConfigConstants.*;
import static org.opends.server.loggers.ErrorLogger.*;
import static org.opends.server.loggers.debug.DebugLogger.*;
import static org.opends.server.types.FilterType.*;
import static org.opends.server.util.ServerConstants.*;
import static org.opends.server.util.StaticUtils.*;
 
/**
 * This class defines a backend that stores its information in an associated
 * replication server object.
 * <p>
 * This is primarily intended to take advantage of the backup/restore/
 * import/export of the backend API, and to provide an LDAP access to the
 * replication server database.
 * <p>
 * Entries stored in this backend are held in the DB associated with the
 * replication server.
 * <p>
 * Currently are only implemented the create and restore backup features.
 */
public class ReplicationBackend
       extends Backend
{
  private static final String CHANGE_NUMBER = "replicationChangeNumber";
 
  /**
   * The tracer object for the debug logger.
   */
  private static final DebugTracer TRACER = getTracer();
 
  private static final String BASE_DN = "dc=replicationchanges";
 
  /** The base DNs for this backend. */
  private DN[] baseDNs;
 
  /** The base DNs for this backend, in a hash set. */
  private Set<DN> baseDNSet;
 
  /** The set of supported controls for this backend. */
  private Set<String> supportedControls;
 
  /** The set of supported features for this backend. */
  private Set<String> supportedFeatures;
 
  private ReplicationServer server;
 
  /**
   * The number of milliseconds between job progress reports.
   */
  private long progressInterval = 10000;
 
  /**
   * The current number of entries exported.
   */
  private long exportedCount = 0;
 
  /**
   * The current number of entries skipped.
   */
  private long skippedCount = 0;
 
  /** Objectclass for getEntry root entries. */
  private Map<ObjectClass, String> rootObjectclasses;
 
  /** Attributes used for getEntry root entries. */
  private Map<AttributeType, List<Attribute>> attributes;
 
  /** Operational attributes used for getEntry root entries. */
  private Map<AttributeType,List<Attribute>> operationalAttributes;
 
 
  /**
   * Creates a new backend with the provided information.  All backend
   * implementations must implement a default constructor that use
   * <CODE>super()</CODE> to invoke this constructor.
   */
  public ReplicationBackend()
  {
    super();
    // Perform all initialization in initializeBackend.
  }
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public void configureBackend(Configuration config) throws ConfigException
  {
    if (config != null)
    {
      Validator.ensureTrue(config instanceof BackendCfg);
      BackendCfg cfg = (BackendCfg) config;
      DN[] newBaseDNs = new DN[cfg.getBaseDN().size()];
      cfg.getBaseDN().toArray(newBaseDNs);
      this.baseDNs = newBaseDNs;
    }
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public synchronized void initializeBackend()
       throws ConfigException, InitializationException
  {
    if ((baseDNs == null) || (baseDNs.length != 1))
    {
      Message message = ERR_MEMORYBACKEND_REQUIRE_EXACTLY_ONE_BASE.get();
      throw new ConfigException(message);
    }
 
    baseDNSet = new HashSet<DN>(Arrays.asList(baseDNs));
 
    supportedControls = new HashSet<String>();
    supportedFeatures = new HashSet<String>();
 
    for (DN dn : baseDNs)
    {
      try
      {
        DirectoryServer.registerBaseDN(dn, this, true);
      }
      catch (Exception e)
      {
        if (debugEnabled())
        {
          TRACER.debugCaught(DebugLogLevel.ERROR, e);
        }
 
        Message message = ERR_BACKEND_CANNOT_REGISTER_BASEDN.get(
            dn.toString(), getExceptionMessage(e));
        throw new InitializationException(message, e);
      }
    }
    rootObjectclasses = new LinkedHashMap<ObjectClass,String>(3);
    rootObjectclasses.put(DirectoryServer.getTopObjectClass(), OC_TOP);
    ObjectClass domainOC = DirectoryServer.getObjectClass("domain", true);
    rootObjectclasses.put(domainOC, "domain");
    ObjectClass objectclassOC =
                   DirectoryServer.getObjectClass(ATTR_OBJECTCLASSES_LC, true);
    rootObjectclasses.put(objectclassOC, ATTR_OBJECTCLASSES_LC);
 
    attributes = new LinkedHashMap<AttributeType,List<Attribute>>();
    Attribute a = Attributes.create("changetype", "add");
    List<Attribute> attrList = new ArrayList<Attribute>(1);
    attrList.add(a);
    attributes.put(a.getAttributeType(), attrList);
    operationalAttributes = new LinkedHashMap<AttributeType,List<Attribute>>();
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public synchronized void finalizeBackend()
  {
    for (DN dn : baseDNs)
    {
      try
      {
        DirectoryServer.deregisterBaseDN(dn);
      }
      catch (Exception e)
      {
        if (debugEnabled())
        {
          TRACER.debugCaught(DebugLogLevel.ERROR, e);
        }
      }
    }
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public DN[] getBaseDNs()
  {
    return baseDNs;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public synchronized long getEntryCount()
  {
    if (server==null)
    {
      try
      {
        server = getReplicationServer();
        if (server == null)
        {
          return 0;
        }
      }
      catch(Exception e)
      {
        return 0;
      }
    }
 
    //This method only returns the number of actual change entries, the
    //domain and any baseDN entries are not counted.
    long retNum=0;
    for (ReplicationServerDomain rsd : toIterable(server.getDomainIterator()))
    {
      retNum += rsd.getChangesCount();
    }
    return retNum;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public boolean isLocal()
  {
    return true;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public boolean isIndexed(AttributeType attributeType, IndexType indexType)
  {
    return true;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public synchronized Entry getEntry(DN entryDN)
  {
    try {
      if (baseDNSet.contains(entryDN)) {
           return new Entry(entryDN, rootObjectclasses, attributes,
                            operationalAttributes);
      }
 
      InternalClientConnection conn =
          InternalClientConnection.getRootConnection();
      SearchFilter filter =
          SearchFilter.createFilterFromString("(changetype=*)");
      InternalSearchOperation searchOp = new InternalSearchOperation(conn,
              InternalClientConnection.nextOperationID(),
              InternalClientConnection.nextMessageID(),
              null, entryDN, SearchScope.BASE_OBJECT,
              DereferencePolicy.NEVER_DEREF_ALIASES, 0, 0, false,
              filter, null, null);
      search(searchOp);
      List<SearchResultEntry> resultEntries = searchOp.getSearchEntries();
      if (resultEntries.size() != 0)
      {
        return resultEntries.get(0);
      }
    }
    catch (DirectoryException ignored)
    {
    }
    return null;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public synchronized boolean entryExists(DN entryDN)
  {
   return getEntry(entryDN) != null;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public synchronized void addEntry(Entry entry, AddOperation addOperation)
         throws DirectoryException
  {
    Message message = ERR_BACKUP_ADD_NOT_SUPPORTED.get();
    throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public synchronized void deleteEntry(DN entryDN,
                                       DeleteOperation deleteOperation)
         throws DirectoryException
  {
    Message message = ERR_BACKUP_DELETE_NOT_SUPPORTED.get();
    throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public synchronized void replaceEntry(Entry oldEntry, Entry newEntry,
                                        ModifyOperation modifyOperation)
         throws DirectoryException
  {
    Message message = ERR_BACKUP_MODIFY_NOT_SUPPORTED.get();
    throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public synchronized void renameEntry(DN currentDN, Entry entry,
                                       ModifyDNOperation modifyDNOperation)
         throws DirectoryException
  {
    Message message = ERR_BACKUP_MODIFY_DN_NOT_SUPPORTED.get();
    throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public Set<String> getSupportedControls()
  {
    return supportedControls;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public Set<String> getSupportedFeatures()
  {
    return supportedFeatures;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public boolean supportsLDIFExport()
  {
    return true;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public synchronized void exportLDIF(LDIFExportConfig exportConfig)
  throws DirectoryException
  {
    if(server == null) {
       Message message = ERR_REPLICATONBACKEND_EXPORT_LDIF_FAILED.get();
      throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM,message);
    }
 
    final List<ReplicationServerDomain> exportContainers =
        findExportContainers(exportConfig);
 
    // Make a note of the time we started.
    long startTime = System.currentTimeMillis();
 
    // Start a timer for the progress report.
    Timer timer = new Timer();
    TimerTask progressTask = new ProgressTask();
    timer.scheduleAtFixedRate(progressTask, progressInterval,
        progressInterval);
 
    // Create the LDIF writer.
    LDIFWriter ldifWriter;
    try
    {
      ldifWriter = new LDIFWriter(exportConfig);
    }
    catch (Exception e)
    {
      if (debugEnabled())
      {
        TRACER.debugCaught(DebugLogLevel.ERROR, e);
      }
 
      Message message =
        ERR_BACKEND_CANNOT_CREATE_LDIF_WRITER.get(String.valueOf(e));
      throw new DirectoryException(DirectoryServer.getServerErrorResultCode(),
          message, e);
    }
 
    exportRootChanges(exportContainers, exportConfig, ldifWriter);
 
    try
    {
      // Iterate through the containers.
      for (ReplicationServerDomain exportContainer : exportContainers)
      {
        if (exportConfig.isCancelled())
        {
          break;
        }
        writeChangesAfterCSN(exportContainer, exportConfig, ldifWriter, null,
            null);
      }
    }
    finally
    {
      timer.cancel();
 
      close(ldifWriter);
    }
 
    long finishTime = System.currentTimeMillis();
    long totalTime = finishTime - startTime;
 
    float rate = 0;
    if (totalTime > 0)
    {
      rate = 1000f*exportedCount / totalTime;
    }
 
    Message message = NOTE_JEB_EXPORT_FINAL_STATUS.get(
        exportedCount, skippedCount, totalTime/1000, rate);
    logError(message);
  }
 
  private List<ReplicationServerDomain> findExportContainers(
      LDIFExportConfig exportConfig) throws DirectoryException
  {
    List<DN> includeBranches = exportConfig.getIncludeBranches();
    List<ReplicationServerDomain> exportContainers =
        new ArrayList<ReplicationServerDomain>();
    for (Iterator<ReplicationServerDomain> iter = server.getDomainIterator();
         iter.hasNext();)
    {
      ReplicationServerDomain rsd = iter.next();
 
      // Skip containers that are not covered by the include branches.
      if (includeBranches == null || includeBranches.isEmpty())
      {
        exportContainers.add(rsd);
      }
      else
      {
        DN baseDN = DN.decode(rsd.getBaseDn() + "," + BASE_DN);
        for (DN includeBranch : includeBranches)
        {
          if (includeBranch.isDescendantOf(baseDN)
              || includeBranch.isAncestorOf(baseDN))
          {
            exportContainers.add(rsd);
          }
        }
      }
    }
    return exportContainers;
  }
 
  /**
   * Exports the root changes of the export, and one entry by domain.
   */
  private void exportRootChanges(List<ReplicationServerDomain> exportContainers,
      final LDIFExportConfig exportConfig, LDIFWriter ldifWriter)
  {
    AttributeType ocType = DirectoryServer.getObjectClassAttributeType();
    AttributeBuilder builder = new AttributeBuilder(ocType);
    builder.add("top");
    builder.add("domain");
    Attribute ocAttr = builder.toAttribute();
 
    Map<AttributeType, List<Attribute>> attrs =
        new HashMap<AttributeType, List<Attribute>>();
    attrs.put(ocType, singletonList(ocAttr));
 
    try
    {
      ChangeRecordEntry changeRecord =
        new AddChangeRecordEntry(DN.decode(BASE_DN), attrs);
      ldifWriter.writeChangeRecord(changeRecord);
    }
    catch (Exception e) { /* do nothing */ }
 
    if (exportConfig == null)
    {
      return;
    }
 
    for (ReplicationServerDomain exportContainer : exportContainers)
    {
      if (exportConfig.isCancelled())
      {
        break;
      }
 
      final ServerState serverState = exportContainer.getDbServerState();
      TRACER.debugInfo("State=" + serverState);
      Attribute stateAttr = Attributes.create("state", serverState.toString());
      Attribute genidAttr = Attributes.create("generation-id",
          exportContainer.getGenerationId() + exportContainer.getBaseDn());
 
      attrs.clear();
      attrs.put(ocType, singletonList(ocAttr));
      attrs.put(stateAttr.getAttributeType(), singletonList(stateAttr));
      attrs.put(genidAttr.getAttributeType(), singletonList(genidAttr));
 
      final String dnString = exportContainer.getBaseDn() + "," + BASE_DN;
      try
      {
        DN dn = DN.decode(dnString);
        ChangeRecordEntry changeRecord = new AddChangeRecordEntry(dn, attrs);
        ldifWriter.writeChangeRecord(changeRecord);
      }
      catch (Exception e)
      {
        if (debugEnabled())
        {
          TRACER.debugCaught(DebugLogLevel.ERROR, e);
        }
        logError(ERR_BACKEND_EXPORT_ENTRY.get(dnString, String.valueOf(e)));
      }
    }
  }
 
  /**
   * Exports or returns all the changes from a ReplicationServerDomain coming
   * after the CSN specified in the searchOperation.
   */
  private void writeChangesAfterCSN(ReplicationServerDomain rsd,
      final LDIFExportConfig exportConfig, LDIFWriter ldifWriter,
      SearchOperation searchOperation, final CSN previousCSN)
  {
    for (int serverId : rsd.getServerIds())
    {
      if (exportConfig != null && exportConfig.isCancelled())
      { // Abort if cancelled
        return;
      }
 
      ReplicaDBCursor cursor = rsd.getCursorFrom(serverId, previousCSN);
      if (cursor != null)
      {
        try
        {
          int lookthroughCount = 0;
 
          // Walk through the changes
          while (cursor.getChange() != null)
          {
            if (exportConfig != null && exportConfig.isCancelled())
            { // abort if cancelled
              return;
            }
            if (!canContinue(searchOperation, lookthroughCount))
            {
              break;
            }
            lookthroughCount++;
            writeChange(cursor.getChange(), ldifWriter, searchOperation,
                rsd.getBaseDn(), exportConfig != null);
            cursor.next();
          }
        }
        finally
        {
          close(cursor);
        }
      }
    }
  }
 
  private boolean canContinue(SearchOperation searchOperation,
      int lookthroughCount)
  {
    if (searchOperation == null)
    {
      return true;
    }
 
    int limit = searchOperation.getClientConnection().getLookthroughLimit();
    if (lookthroughCount > limit && limit > 0)
    {
      // lookthrough limit exceeded
      searchOperation.setResultCode(ResultCode.ADMIN_LIMIT_EXCEEDED);
      searchOperation.setErrorMessage(null);
      return false;
    }
 
    try
    {
      searchOperation.checkIfCanceled(false);
      return true;
    }
    catch (CanceledOperationException e)
    {
      searchOperation.setResultCode(ResultCode.CANCELED);
      searchOperation.setErrorMessage(null);
      return false;
    }
  }
 
  private CSN extractCSN(SearchOperation searchOperation)
  {
    if (searchOperation != null)
    {
      return extractCSN(searchOperation.getFilter());
    }
    return null;
  }
 
  /**
   * Attempt to extract a CSN from searchFilter like
   * ReplicationChangeNumber=xxxx or ReplicationChangeNumber>=xxxx.
   *
   * @param filter
   *          The filter to evaluate.
   * @return The extracted CSN or null if no CSN was found.
   */
  private CSN extractCSN(SearchFilter filter)
  {
    // Try to optimize for filters like replicationChangeNumber>=xxxxx
    // or replicationChangeNumber=xxxxx :
    // If the search filter is one of these 2 filters, move directly to
    // ChangeNumber=xxxx before starting the iteration.
    final FilterType filterType = filter.getFilterType();
    if (GREATER_OR_EQUAL.equals(filterType) || EQUALITY.equals(filterType))
    {
      AttributeType changeNumberAttrType =
          DirectoryServer.getDefaultAttributeType(CHANGE_NUMBER);
      if (filter.getAttributeType().equals(changeNumberAttrType))
      {
        try
        {
          CSN startingCSN =
             new CSN(filter.getAssertionValue().getValue().toString());
          return new CSN(startingCSN.getTime(),
              startingCSN.getSeqnum() - 1, startingCSN.getServerId());
        }
        catch (Exception e)
        {
          // don't try to optimize the search if the ChangeNumber is
          // not a valid replication CSN.
        }
      }
    }
    else if (AND.equals(filterType))
    {
      for (SearchFilter filterComponent : filter.getFilterComponents())
      {
        // This code does not expect more than one CSN in the search filter.
        // It is ok, since it is only used by developers/testers for debugging.
        final CSN previousCSN = extractCSN(filterComponent);
        if (previousCSN != null)
        {
          return previousCSN;
        }
      }
    }
    return null;
  }
 
 
  /**
   * Exports one change.
   */
  private void writeChange(UpdateMsg updateMsg, LDIFWriter ldifWriter,
      SearchOperation searchOperation, String baseDN, boolean isExport)
  {
    InternalClientConnection conn =
      InternalClientConnection.getRootConnection();
    Entry entry = null;
    DN dn = null;
 
    ObjectClass extensibleObjectOC =
      DirectoryServer.getDefaultObjectClass("extensibleObject");
 
    try
    {
      if (updateMsg instanceof LDAPUpdateMsg)
      {
        LDAPUpdateMsg msg = (LDAPUpdateMsg) updateMsg;
 
        if (msg instanceof AddMsg)
        {
          AddMsg addMsg = (AddMsg)msg;
          AddOperation addOperation = (AddOperation)msg.createOperation(conn);
 
          dn = DN.decode("puid=" + addMsg.getParentEntryUUID() + "+" +
              CHANGE_NUMBER + "=" + msg.getCSN() + "+" +
              msg.getDn() + "," + BASE_DN);
 
          Map<AttributeType,List<Attribute>> attrs =
            new HashMap<AttributeType,List<Attribute>>();
          Map<ObjectClass, String> objectclasses =
            new HashMap<ObjectClass, String>();
 
          for (RawAttribute a : addOperation.getRawAttributes())
          {
            Attribute attr = a.toAttribute();
            if (attr.getAttributeType().isObjectClassType())
            {
              for (ByteString os : a.getValues())
              {
                String ocName = os.toString();
                ObjectClass oc =
                  DirectoryServer.getObjectClass(toLowerCase(ocName));
                if (oc == null)
                {
                  oc = DirectoryServer.getDefaultObjectClass(ocName);
                }
 
                objectclasses.put(oc,ocName);
              }
            }
            else
            {
              addAttribute(attrs, attr);
            }
          }
          addAttribute(attrs, "changetype", "add");
 
          if (isExport)
          {
            ChangeRecordEntry changeRecord =
              new AddChangeRecordEntry(dn, attrs);
            ldifWriter.writeChangeRecord(changeRecord);
          }
          else
          {
            entry = new Entry(dn, objectclasses, attrs, null);
          }
        }
        else if (msg instanceof DeleteMsg)
        {
          dn = computeDN(msg);
          ChangeRecordEntry changeRecord = new DeleteChangeRecordEntry(dn);
          entry = writeChangeRecord(ldifWriter, changeRecord, isExport);
        }
        else if (msg instanceof ModifyMsg)
        {
          ModifyOperation op = (ModifyOperation)msg.createOperation(conn);
 
          dn = computeDN(msg);
          ChangeRecordEntry changeRecord =
            new ModifyChangeRecordEntry(dn, op.getRawModifications());
          entry = writeChangeRecord(ldifWriter, changeRecord, isExport);
        }
        else if (msg instanceof ModifyDNMsg)
        {
          ModifyDNOperation op = (ModifyDNOperation)msg.createOperation(conn);
 
          dn = computeDN(msg);
          ChangeRecordEntry changeRecord = new ModifyDNChangeRecordEntry(
              dn, op.getNewRDN(), op.deleteOldRDN(), op.getNewSuperior());
          entry = writeChangeRecord(ldifWriter, changeRecord, isExport);
        }
 
 
        if (isExport)
        {
          this.exportedCount++;
        }
        else
        {
          // Add extensibleObject objectclass and the ChangeNumber in the entry.
          if (!entry.getObjectClasses().containsKey(extensibleObjectOC))
            entry.addObjectClass(extensibleObjectOC);
 
          addAttribute(entry.getUserAttributes(), CHANGE_NUMBER,
              msg.getCSN().toString());
          addAttribute(entry.getUserAttributes(), "replicationDomain", baseDN);
 
          // Get the base DN, scope, and filter for the search.
          DN     searchBaseDN = searchOperation.getBaseDN();
          SearchScope  scope  = searchOperation.getScope();
          SearchFilter filter = searchOperation.getFilter();
 
          if (entry.matchesBaseAndScope(searchBaseDN, scope)
              && filter.matchesEntry(entry))
          {
            searchOperation.returnEntry(entry, new LinkedList<Control>());
          }
        }
      }
    }
    catch (Exception e)
    {
      this.skippedCount++;
      if (debugEnabled())
      {
        TRACER.debugCaught(DebugLogLevel.ERROR, e);
      }
 
      final String dnStr = (dn != null) ? dn.toNormalizedString() : "Unknown";
 
      Message message;
      if (isExport)
      {
        message = ERR_BACKEND_EXPORT_ENTRY.get(dnStr, String.valueOf(e));
      }
      else
      {
        message = ERR_BACKEND_SEARCH_ENTRY.get(dnStr, e.getLocalizedMessage());
      }
      logError(message);
    }
  }
 
  private DN computeDN(LDAPUpdateMsg msg) throws DirectoryException
  {
    return DN.decode("uuid=" + msg.getEntryUUID() + "," + CHANGE_NUMBER + "="
        + msg.getCSN() + "," + msg.getDn() + "," + BASE_DN);
  }
 
  private Entry writeChangeRecord(LDIFWriter ldifWriter,
      ChangeRecordEntry changeRecord, boolean isExport) throws IOException,
      LDIFException
  {
    if (isExport)
    {
      ldifWriter.writeChangeRecord(changeRecord);
      return null;
    }
 
    final Writer writer = new Writer();
    writer.getLDIFWriter().writeChangeRecord(changeRecord);
    return writer.getLDIFReader().readEntry();
  }
 
  private void addAttribute(Map<AttributeType, List<Attribute>> attributes,
      String attrName, String attrValue)
  {
    addAttribute(attributes, Attributes.create(attrName, attrValue));
  }
 
  /**
   * Add an attribute to a provided Map of attribute.
   *
   * @param attributes The Map that should be updated.
   * @param attribute  The attribute that should be added to the Map.
   */
  private void addAttribute(
      Map<AttributeType,List<Attribute>> attributes, Attribute attribute)
  {
    AttributeType attrType = attribute.getAttributeType();
    List<Attribute> attrs = attributes.get(attrType);
    if (attrs == null)
    {
      attrs = new ArrayList<Attribute>(1);
      attrs.add(attribute);
      attributes.put(attrType, attrs);
    }
    else
    {
      attrs.add(attribute);
    }
  }
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public boolean supportsLDIFImport()
  {
    return false;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public synchronized LDIFImportResult importLDIF(LDIFImportConfig importConfig)
         throws DirectoryException
  {
    Message message = ERR_REPLICATONBACKEND_IMPORT_LDIF_NOT_SUPPORTED.get();
    throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM, message);
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public boolean supportsBackup()
  {
    // This backend does not provide a backup/restore mechanism.
    return true;
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public boolean supportsBackup(BackupConfig backupConfig,
                                StringBuilder unsupportedReason)
  {
    return true;
  }
 
  /** {@inheritDoc} */
  @Override()
  public void createBackup(BackupConfig backupConfig)
         throws DirectoryException
  {
    createBackupManager().createBackup(getBackendDir(), backupConfig);
  }
 
  /** {@inheritDoc} */
  @Override()
  public void restoreBackup(RestoreConfig restoreConfig)
         throws DirectoryException
  {
    createBackupManager().restoreBackup(getBackendDir(), restoreConfig);
  }
 
  /** {@inheritDoc} */
  @Override()
  public void removeBackup(BackupDirectory backupDirectory, String backupID)
      throws DirectoryException
  {
    createBackupManager().removeBackup(backupDirectory, backupID);
  }
 
  private BackupManager createBackupManager()
  {
    return new BackupManager(getBackendID());
  }
 
  private File getBackendDir() throws DirectoryException
  {
   return getFileForPath(getReplicationServerCfg().getReplicationDBDirectory());
  }
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public boolean supportsRestore()
  {
    return true;
  }
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public long numSubordinates(DN entryDN, boolean subtree)
      throws DirectoryException
  {
    throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM,
                                 ERR_NUM_SUBORDINATES_NOT_SUPPORTED.get());
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public ConditionResult hasSubordinates(DN entryDN)
        throws DirectoryException
  {
    throw new DirectoryException(ResultCode.UNWILLING_TO_PERFORM,
                                 ERR_HAS_SUBORDINATES_NOT_SUPPORTED.get());
  }
 
  /**
   * Set the replication server associated with this backend.
   * @param server The replication server.
   */
  public void setServer(ReplicationServer server)
  {
    this.server = server;
  }
 
  /**
   * This class reports progress of the export job at fixed intervals.
   */
  private final class ProgressTask extends TimerTask
  {
    /**
     * The number of entries that had been exported at the time of the
     * previous progress report.
     */
    private long previousCount = 0;
 
    /**
     * The time in milliseconds of the previous progress report.
     */
    private long previousTime;
 
    /**
     * Create a new export progress task.
     */
    public ProgressTask()
    {
      previousTime = System.currentTimeMillis();
    }
 
    /**
     * The action to be performed by this timer task.
     */
    @Override
    public void run()
    {
      long latestCount = exportedCount;
      long deltaCount = latestCount - previousCount;
      long latestTime = System.currentTimeMillis();
      long deltaTime = latestTime - previousTime;
 
      if (deltaTime == 0)
      {
        return;
      }
 
      float rate = 1000f*deltaCount / deltaTime;
 
      Message message =
          NOTE_JEB_EXPORT_PROGRESS_REPORT.get(latestCount, skippedCount, rate);
      logError(message);
 
      previousCount = latestCount;
      previousTime = latestTime;
    }
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override()
  public synchronized void search(SearchOperation searchOperation)
         throws DirectoryException
  {
    //This check is for GroupManager initialization. It currently doesn't
    //come into play because the replication server variable is null in
    //the check above. But if the order of initialization of the server variable
    //is ever changed, the following check will keep replication change entries
    //from being added to the groupmanager cache erroneously.
    List<Control> requestControls = searchOperation.getRequestControls();
    if (requestControls != null)
    {
      for (Control c : requestControls)
      {
        if (OID_INTERNAL_GROUP_MEMBERSHIP_UPDATE.equals(c.getOID()))
        {
          return;
        }
      }
    }
 
    // don't do anything if the search is a base search on the backend suffix.
    try
    {
      DN backendBaseDN = DN.decode(BASE_DN);
      if ( searchOperation.getScope().equals(SearchScope.BASE_OBJECT) &&
           backendBaseDN.equals(searchOperation.getBaseDN()) )
      {
        return;
      }
    }
    catch (Exception e)
    {
      return;
    }
 
    // Make sure the base entry exists if it's supposed to be in this backend.
    final DN searchBaseDN = searchOperation.getBaseDN();
    if (!handlesEntry(searchBaseDN))
    {
      DN matchedDN = searchBaseDN.getParentDNInSuffix();
      while (matchedDN != null)
      {
        if (handlesEntry(matchedDN))
        {
          break;
        }
        matchedDN = matchedDN.getParentDNInSuffix();
      }
 
      Message message = ERR_REPLICATIONBACKEND_ENTRY_DOESNT_EXIST.
        get(String.valueOf(searchBaseDN));
      throw new DirectoryException(
          ResultCode.NO_SUCH_OBJECT, message, matchedDN, null);
    }
 
    if (server==null)
    {
      server = getReplicationServer();
      if (server == null)
      {
        if (!baseDNSet.contains(searchBaseDN))
        {
          Message message = ERR_REPLICATIONBACKEND_ENTRY_DOESNT_EXIST.get(
              String.valueOf(searchBaseDN));
          throw new DirectoryException(
              ResultCode.NO_SUCH_OBJECT, message, null, null);
        }
        return;
      }
    }
 
    // Walk through all entries and send the ones that match.
    final List<ReplicationServerDomain> searchContainers =
        findSearchContainers(searchBaseDN);
    for (ReplicationServerDomain exportContainer : searchContainers)
    {
      final CSN previousCSN = extractCSN(searchOperation);
      writeChangesAfterCSN(exportContainer, null, null, searchOperation,
          previousCSN);
    }
  }
 
  private List<ReplicationServerDomain> findSearchContainers(DN searchBaseDN)
      throws DirectoryException
  {
    List<ReplicationServerDomain> searchContainers =
        new ArrayList<ReplicationServerDomain>();
    for (Iterator<ReplicationServerDomain> iter = server.getDomainIterator();
         iter.hasNext();)
    {
      ReplicationServerDomain rsd = iter.next();
 
      // Skip containers that are not covered by the include branches.
      DN baseDN = DN.decode(rsd.getBaseDn() + "," + BASE_DN);
      if (searchBaseDN.isDescendantOf(baseDN)
          || searchBaseDN.isAncestorOf(baseDN))
      {
        searchContainers.add(rsd);
      }
    }
    return searchContainers;
  }
 
 
  /**
   * Retrieves the replication server associated to this backend.
   *
   * @return The server retrieved
   * @throws DirectoryException When it occurs.
   */
  private ReplicationServer getReplicationServer() throws DirectoryException
  {
    for (SynchronizationProvider<?> provider :
      DirectoryServer.getSynchronizationProviders())
    {
      if (provider instanceof MultimasterReplication)
      {
        MultimasterReplication mmp = (MultimasterReplication)provider;
        ReplicationServerListener list = mmp.getReplicationServerListener();
        if (list != null)
        {
          return list.getReplicationServer();
        }
      }
    }
    return null;
  }
 
  /**
   * Find the replication server configuration associated with this replication
   * backend.
   */
  private ReplicationServerCfg getReplicationServerCfg()
      throws DirectoryException {
    RootCfg root = ServerManagementContext.getInstance().getRootConfiguration();
 
    for (String name : root.listSynchronizationProviders()) {
      SynchronizationProviderCfg syncCfg;
      try {
        syncCfg = root.getSynchronizationProvider(name);
      } catch (ConfigException e) {
        throw new DirectoryException(ResultCode.OPERATIONS_ERROR,
            ERR_REPLICATION_SERVER_CONFIG_NOT_FOUND.get(), e);
      }
      if (syncCfg instanceof ReplicationSynchronizationProviderCfg) {
        ReplicationSynchronizationProviderCfg scfg =
          (ReplicationSynchronizationProviderCfg) syncCfg;
        try {
          return scfg.getReplicationServer();
        } catch (ConfigException e) {
          throw new DirectoryException(ResultCode.OPERATIONS_ERROR,
              ERR_REPLICATION_SERVER_CONFIG_NOT_FOUND.get(), e);
        }
      }
    }
 
    // No replication server found.
    throw new DirectoryException(ResultCode.OPERATIONS_ERROR,
        ERR_REPLICATION_SERVER_CONFIG_NOT_FOUND.get());
  }
 
  /**
   * Writer class to read/write from/to a bytearray.
   */
  private static final class Writer
  {
    /** The underlying output stream. */
    private final ByteArrayOutputStream stream;
 
    /** The underlying LDIF config. */
    private final LDIFExportConfig config;
 
    /** The LDIF writer. */
    private final LDIFWriter writer;
 
    /**
     * Create a new string writer.
     */
    public Writer() {
      this.stream = new ByteArrayOutputStream();
      this.config = new LDIFExportConfig(stream);
      try {
        this.writer = new LDIFWriter(config);
      } catch (IOException e) {
        // Should not happen.
        throw new RuntimeException(e);
      }
    }
 
    /**
     * Get the LDIF writer.
     *
     * @return Returns the LDIF writer.
     */
    public LDIFWriter getLDIFWriter() {
      return writer;
    }
 
 
 
    /**
     * Close the writer and get an LDIF reader for the LDIF content.
     *
     * @return Returns an LDIF Reader.
     * @throws IOException
     *           If an error occurred closing the writer.
     */
    public LDIFReader getLDIFReader() throws IOException {
      writer.close();
      String ldif = stream.toString("UTF-8");
      ldif = ldif.replace("\n-\n", "\n");
      ByteArrayInputStream istream = new ByteArrayInputStream(ldif.getBytes());
      LDIFImportConfig newConfig = new LDIFImportConfig(istream);
      // ReplicationBackend may contain entries that are not schema
      // compliant. Let's ignore them for now.
      newConfig.setValidateSchema(false);
      return new LDIFReader(newConfig);
    }
  }
 
 
 
  /**
   * {@inheritDoc}
   */
  @Override
  public void preloadEntryCache() throws UnsupportedOperationException {
    throw new UnsupportedOperationException("Operation not supported.");
  }
}