Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9aa6e4b1007bba20b168e7d15660b788f79519dd (plain) (blame)
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
/*
 * Copyright (c) 2010-2016, 2018 Eike Stepper (Loehne, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.server;

import org.eclipse.emf.cdo.common.branch.CDOBranch;
import org.eclipse.emf.cdo.common.branch.CDOBranchHandler;
import org.eclipse.emf.cdo.common.branch.CDOBranchPoint;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfo;
import org.eclipse.emf.cdo.common.commit.CDOCommitInfoHandler;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDProvider;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.common.lob.CDOBlob;
import org.eclipse.emf.cdo.common.lob.CDOClob;
import org.eclipse.emf.cdo.common.lob.CDOLobHandler;
import org.eclipse.emf.cdo.common.model.CDOClassInfo;
import org.eclipse.emf.cdo.common.model.CDOClassifierRef;
import org.eclipse.emf.cdo.common.model.CDOPackageRegistry;
import org.eclipse.emf.cdo.common.model.CDOPackageUnit;
import org.eclipse.emf.cdo.common.model.CDOPackageUnit.Type;
import org.eclipse.emf.cdo.common.model.EMFUtil;
import org.eclipse.emf.cdo.common.protocol.CDODataOutput;
import org.eclipse.emf.cdo.common.revision.CDORevision;
import org.eclipse.emf.cdo.common.revision.CDORevisionData;
import org.eclipse.emf.cdo.common.revision.CDORevisionHandler;
import org.eclipse.emf.cdo.common.security.CDOPermissionProvider;
import org.eclipse.emf.cdo.spi.common.branch.InternalCDOBranch;
import org.eclipse.emf.cdo.spi.common.branch.InternalCDOBranchManager;
import org.eclipse.emf.cdo.spi.common.commit.InternalCDOCommitInfoManager;
import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageInfo;
import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageRegistry;
import org.eclipse.emf.cdo.spi.common.model.InternalCDOPackageUnit;
import org.eclipse.emf.cdo.spi.common.protocol.CDODataOutputImpl;
import org.eclipse.emf.cdo.spi.common.revision.DetachedCDORevision;
import org.eclipse.emf.cdo.spi.common.revision.InternalCDORevision;
import org.eclipse.emf.cdo.spi.server.InternalRepository;
import org.eclipse.emf.cdo.spi.server.InternalSession;

import org.eclipse.net4j.util.Handler;
import org.eclipse.net4j.util.HexUtil;
import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.io.ExtendedDataOutputStream;
import org.eclipse.net4j.util.io.XMLOutput;
import org.eclipse.net4j.util.lifecycle.LifecycleUtil;

import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.FeatureMap;
import org.eclipse.emf.ecore.util.FeatureMapUtil;

import org.xml.sax.SAXException;

import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;

/**
 * Exports the complete contents of a {@link IRepository repository} in a format suitable for {@link CDOServerImporter
 * imports} into new repositories.
 * <p>
 * Subtypes specify the actual exchange format.
 *
 * @author Eike Stepper
 * @since 4.0
 */
public abstract class CDOServerExporter<OUT>
{
  InternalRepository repository;

  private boolean exportSystemPackages;

  private String branchPath;

  private long timeStamp = CDOBranchPoint.INVALID_DATE;

  private final Statistics statistics = new Statistics();

  public CDOServerExporter(IRepository repository)
  {
    this.repository = (InternalRepository)repository;
  }

  public final IRepository getRepository()
  {
    return repository;
  }

  /**
   * @since 4.8
   */
  public Statistics getStatistics()
  {
    return statistics;
  }

  /**
   * @since 4.7
   */
  public boolean isExportSystemPackages()
  {
    return exportSystemPackages;
  }

  /**
   * @since 4.7
   */
  public void setExportSystemPackages(boolean exportSystemPackages)
  {
    this.exportSystemPackages = exportSystemPackages;
  }

  /**
   * @since 4.8
   */
  public String getBranchPath()
  {
    return branchPath;
  }

  /**
   * @since 4.8
   */
  public void setBranchPath(String branchPath)
  {
    this.branchPath = branchPath;
  }

  /**
   * @since 4.8
   */
  public long getTimeStamp()
  {
    return timeStamp;
  }

  /**
   * @since 4.8
   */
  public void setTimeStamp(long timeStamp)
  {
    this.timeStamp = timeStamp;
  }

  public final void exportRepository(OutputStream out) throws Exception
  {
    boolean wasActive = LifecycleUtil.isActive(repository);
    if (!wasActive)
    {
      LifecycleUtil.activate(repository);
    }

    InternalSession session = repository.getSessionManager().openSession(null);
    StoreThreadLocal.setSession(session);

    try
    {
      if (!(out instanceof BufferedOutputStream))
      {
        out = new BufferedOutputStream(out);
      }

      OUT output = createOutput(out);
      exportAll(output);
      out.flush();
    }
    finally
    {
      StoreThreadLocal.release();
      if (!wasActive)
      {
        LifecycleUtil.deactivate(repository);
      }

      repository = null;
    }
  }

  protected abstract OUT createOutput(OutputStream out) throws Exception;

  protected void exportAll(final OUT out) throws Exception
  {
    try
    {
      exportPackages(out);
      exportBranches(out);
      exportLobs(out);
      exportCommits(out);
    }
    catch (WrappedException ex)
    {
      throw WrappedException.unwrap(ex);
    }
  }

  protected void exportPackages(OUT out) throws Exception
  {
    InternalCDOPackageRegistry packageRegistry = repository.getPackageRegistry(false);
    InternalCDOPackageUnit[] packageUnits = packageRegistry.getPackageUnits(true);
    for (InternalCDOPackageUnit packageUnit : packageUnits)
    {
      if (packageUnit.isSystem() && !exportSystemPackages)
      {
        continue;
      }

      String id = packageUnit.getID();
      CDOPackageUnit.Type type = packageUnit.getOriginalType();
      long time = packageUnit.getTimeStamp();

      EPackage ePackage = packageUnit.getTopLevelPackageInfo().getEPackage();
      String data = new String(EMFUtil.getEPackageBytes(ePackage, false, packageRegistry));

      startPackageUnit(out, id, type, time, data);
      for (InternalCDOPackageInfo packageInfo : packageUnit.getPackageInfos())
      {
        String packageURI = packageInfo.getPackageURI();
        exportPackageInfo(out, packageURI);
        ++statistics.packageInfos;
      }

      endPackageUnit(out);
      ++statistics.packageUnits;
    }
  }

  protected abstract void startPackageUnit(OUT out, String id, CDOPackageUnit.Type type, long time, String data) throws Exception;

  protected abstract void endPackageUnit(OUT out) throws Exception;

  protected abstract void exportPackageInfo(OUT out, String packageURI) throws Exception;

  protected void exportBranches(final OUT out) throws Exception
  {
    InternalCDOBranchManager branchManager = repository.getBranchManager();

    if (branchPath != null)
    {
      InternalCDOBranch branch = branchManager.getBranch(branchPath);
      if (branch == null)
      {
        throw new IllegalStateException("Branch '" + branchPath + "' does not exist");
      }

      exportBranch(out, branch);
    }
    else
    {
      exportBranch(out, branchManager.getMainBranch());

      if (repository.isSupportingBranches())
      {
        branchManager.getBranches(0, 0, new CDOBranchHandler()
        {
          public void handleBranch(CDOBranch branch)
          {
            try
            {
              exportBranch(out, branch);
            }
            catch (Exception ex)
            {
              throw WrappedException.wrap(ex);
            }
          }
        });
      }
    }
  }

  protected void exportBranch(OUT out, CDOBranch branch) throws Exception
  {
    exportRevisions(out, branch);
    ++statistics.branches;
  }

  protected void exportRevisions(final OUT out, CDOBranch branch) throws Exception
  {
    repository.handleRevisions(null, branch, true, timeStamp, false, new CDORevisionHandler()
    {
      public boolean handleRevision(CDORevision revision)
      {
        try
        {
          exportRevision(out, revision);
          ++statistics.revisions;
          return true;
        }
        catch (Exception ex)
        {
          throw WrappedException.wrap(ex);
        }
      }
    });
  }

  protected abstract void exportRevision(OUT out, CDORevision revision) throws Exception;

  protected void exportLobs(final OUT out) throws Exception
  {
    repository.handleLobs(0, 0, new CDOLobHandler()
    {
      public OutputStream handleBlob(byte[] id, long size)
      {
        try
        {
          ++statistics.blobs;
          return startBlob(out, id, size);
        }
        catch (Exception ex)
        {
          throw WrappedException.wrap(ex);
        }
      }

      public Writer handleClob(byte[] id, long size)
      {
        try
        {
          ++statistics.clobs;
          return startClob(out, id, size);
        }
        catch (Exception ex)
        {
          throw WrappedException.wrap(ex);
        }
      }
    });
  }

  protected abstract OutputStream startBlob(OUT out, byte[] id, long size) throws Exception;

  protected abstract Writer startClob(OUT out, byte[] id, long size) throws Exception;

  protected void exportCommits(final OUT out) throws Exception
  {
    InternalCDOCommitInfoManager commitInfoManager = repository.getCommitInfoManager();
    commitInfoManager.getCommitInfos(null, 0L, 0L, new CDOCommitInfoHandler()
    {
      public void handleCommitInfo(CDOCommitInfo commitInfo)
      {
        try
        {
          exportCommit(out, commitInfo);
          ++statistics.commits;
        }
        catch (Exception ex)
        {
          throw WrappedException.wrap(ex);
        }
      }
    });
  }

  protected abstract void exportCommit(OUT out, CDOCommitInfo commitInfo) throws Exception;

  /**
   * @author Eike Stepper
   * @since 4.8
   */
  public static final class Statistics
  {
    public long packageUnits;

    public long packageInfos;

    public long branches;

    public long revisions;

    public long blobs;

    public long clobs;

    public long commits;

    public void dump(Handler<String> handler)
    {
      handler.handle("Package Units: " + packageUnits);
      handler.handle("Package Infos: " + packageInfos);
      handler.handle("Branches:      " + branches);
      handler.handle("Revisions:     " + revisions);
      handler.handle("Blobs:         " + blobs);
      handler.handle("Clobs:         " + clobs);
      handler.handle("Commits:       " + commits);
    }
  }

  /**
   * XML constants being used by both {@link CDOServerExporter exporters} and {@link CDOServerImporter importers}.
   *
   * @noextend This interface is not intended to be extended by clients.
   * @noimplement This interface is not intended to be implemented by clients.
   * @author Eike Stepper
   */
  public static interface XMLConstants
  {
    public static final String REPOSITORY = "repository";

    public static final String REPOSITORY_NAME = "name";

    public static final String REPOSITORY_UUID = "uuid";

    public static final String REPOSITORY_ROOT = "root";

    public static final String REPOSITORY_CREATED = "created";

    public static final String REPOSITORY_COMMITTED = "committed";

    /**
     * @since 4.8
     */
    public static final String REPOSITORY_EXPORT_BRANCH = "exportBranch";

    /**
     * @since 4.8
     */
    public static final String REPOSITORY_EXPORT_TIME = "exportTime";

    public static final String MODELS = "models";

    public static final String PACKAGE_UNIT = "packageUnit";

    public static final String PACKAGE_UNIT_ID = "id";

    public static final String PACKAGE_UNIT_TYPE = "type";

    public static final String PACKAGE_UNIT_TIME = "time";

    public static final String PACKAGE_UNIT_DATA = "data";

    public static final String PACKAGE_INFO = "packageInfo";

    public static final String PACKAGE_INFO_URI = "uri";

    public static final String INSTANCES = "instances";

    public static final String BRANCH = "branch";

    public static final String BRANCH_ID = "id";

    public static final String BRANCH_NAME = "name";

    public static final String BRANCH_TIME = "time";

    public static final String BRANCH_PARENT = "parent";

    public static final String REVISION = "revision";

    public static final String REVISION_ID = "id";

    public static final String REVISION_CLASS = "class";

    public static final String REVISION_VERSION = "version";

    public static final String REVISION_TIME = "time";

    public static final String REVISION_REVISED = "revised";

    /**
     * @since 4.7
     */
    public static final String REVISION_DETACHED = "detached";

    public static final String REVISION_RESOURCE = "resource";

    public static final String REVISION_CONTAINER = "container";

    public static final String REVISION_FEATURE = "feature";

    public static final String FEATURE = "feature";

    public static final String FEATURE_NAME = "name";

    public static final String FEATURE_TYPE = "type";

    public static final String FEATURE_INNER_FEATURE = "innerFeature";

    public static final String FEATURE_INNER_TYPE = "innerType";

    public static final String FEATURE_VALUE = "value";

    /**
     * @since 4.7
     */
    public static final String FEATURE_ISSET = "isset";

    /**
     * @since 4.7
     */
    public static final String FEATURE_ISNULL = "isnull";

    public static final String FEATURE_ID = "id";

    public static final String FEATURE_SIZE = "size";

    public static final String TYPE_BLOB = "Blob";

    public static final String TYPE_CLOB = "Clob";

    /**
     * @since 4.7
     */
    public static final String TYPE_BYTE_ARRAY = "ByteArray";

    public static final String TYPE_FEATURE_MAP = "FeatureMap";

    public static final String LOBS = "lobs";

    public static final String LOB_ID = "id";

    public static final String LOB_SIZE = "size";

    public static final String BLOB = "blob";

    public static final String CLOB = "clob";

    public static final String COMMITS = "commits";

    public static final String COMMIT = "commit";

    public static final String COMMIT_TIME = "time";

    public static final String COMMIT_PREVIOUS = "previous";

    public static final String COMMIT_BRANCH = "branch";

    public static final String COMMIT_USER = "user";

    public static final String COMMIT_COMMENT = "comment";

    /**
     * @since 4.6
     */
    public static final String MERGE_SOURCE_BRANCH = "mergeSourceBranch";

    /**
     * @since 4.6
     */
    public static final String MERGE_SOURCE_TIME = "mergeSourceTime";
  }

  /**
   * An {@link CDOServerExporter exporter} that creates XML output suitable to be interpreted by an
   * {@link CDOServerImporter.XML XML importer}.
   *
   * @author Eike Stepper
   */
  public static class XML extends CDOServerExporter<XMLOutput> implements XMLConstants
  {
    public XML(IRepository repository)
    {
      super(repository);
    }

    @Override
    protected final XMLOutput createOutput(OutputStream out) throws Exception
    {
      return new XMLOutput(out);
    }

    @Override
    protected void exportAll(XMLOutput out) throws Exception
    {
      out.element(REPOSITORY);
      out.attribute(REPOSITORY_NAME, getRepository().getName());
      out.attribute(REPOSITORY_UUID, getRepository().getUUID());
      out.attribute(REPOSITORY_ROOT, str(getRepository().getRootResourceID()));
      out.attribute(REPOSITORY_CREATED, getRepository().getStore().getCreationTime());
      out.attribute(REPOSITORY_COMMITTED, getRepository().getLastCommitTimeStamp());

      String branchPath = getBranchPath();
      if (branchPath != null)
      {
        out.attribute(REPOSITORY_EXPORT_BRANCH, branchPath);
      }

      long timeStamp = getTimeStamp();
      if (timeStamp != CDOBranchPoint.INVALID_DATE)
      {
        out.attribute(REPOSITORY_EXPORT_TIME, timeStamp);
      }

      out.push();
      super.exportAll(out);
      out.done();
    }

    @Override
    protected void exportPackages(XMLOutput out) throws Exception
    {
      out.element(MODELS);

      out.push();
      super.exportPackages(out);
      out.pop();
    }

    @Override
    protected void startPackageUnit(XMLOutput out, String id, CDOPackageUnit.Type type, long time, String data) throws Exception
    {
      out.element(PACKAGE_UNIT);
      out.attribute(PACKAGE_UNIT_ID, id);
      out.attribute(PACKAGE_UNIT_TYPE, type);
      out.attribute(PACKAGE_UNIT_TIME, time);
      out.attribute(PACKAGE_UNIT_DATA, data);
      out.push();
    }

    @Override
    protected void endPackageUnit(XMLOutput out) throws Exception
    {
      out.pop();
    }

    @Override
    protected void exportPackageInfo(XMLOutput out, String uri) throws Exception
    {
      out.element(PACKAGE_INFO);
      out.attribute(PACKAGE_INFO_URI, uri);
    }

    @Override
    protected void exportBranches(XMLOutput out) throws Exception
    {
      out.element(INSTANCES);

      out.push();
      super.exportBranches(out);
      out.pop();
    }

    @Override
    protected void exportBranch(XMLOutput out, CDOBranch branch) throws Exception
    {
      out.element(BRANCH);
      out.attribute(BRANCH_ID, branch.getID());
      out.attribute(BRANCH_NAME, branch.getName());
      out.attribute(BRANCH_TIME, branch.getBase().getTimeStamp());
      if (!branch.isMainBranch())
      {
        out.attribute(BRANCH_PARENT, branch.getBase().getBranch().getID());
      }

      out.push();
      super.exportBranch(out, branch);
      out.pop();
    }

    @Override
    protected void exportRevision(XMLOutput out, CDORevision revision) throws Exception
    {
      InternalCDORevision rev = (InternalCDORevision)revision;
      boolean detached = rev instanceof DetachedCDORevision;

      out.element(REVISION);
      out.attribute(REVISION_ID, str(rev.getID()));
      out.attribute(REVISION_CLASS, new CDOClassifierRef(rev.getEClass()).getURI());
      out.attribute(REVISION_VERSION, rev.getVersion());
      out.attribute(REVISION_TIME, rev.getTimeStamp());

      long revised = rev.getRevised();
      if (revised != CDOBranchPoint.UNSPECIFIED_DATE)
      {
        out.attribute(REVISION_REVISED, revised);
      }

      if (detached)
      {
        out.attribute(REVISION_DETACHED, true);
        return;
      }

      CDOID resourceID = rev.getResourceID();
      if (!CDOIDUtil.isNull(resourceID))
      {
        out.attribute(REVISION_RESOURCE, str(resourceID));
      }

      CDOID containerID = (CDOID)rev.getContainerID();
      if (!CDOIDUtil.isNull(containerID))
      {
        out.attribute(REVISION_CONTAINER, str(containerID));
      }

      int containingFeatureID = rev.getContainingFeatureID();
      if (containingFeatureID != 0)
      {
        out.attribute(REVISION_FEATURE, containingFeatureID);
      }

      out.push();

      InternalRepository repository = (InternalRepository)getRepository();
      repository.ensureChunks(rev, CDORevision.UNCHUNKED);

      CDOClassInfo classInfo = rev.getClassInfo();
      for (EStructuralFeature feature : classInfo.getAllPersistentFeatures())
      {
        if (feature.isMany())
        {
          @SuppressWarnings("unchecked")
          List<Object> list = (List<Object>)rev.getValue(feature);
          if (list != null && !list.isEmpty())
          {
            for (Object value : list)
            {
              exportFeature(out, feature, value);
            }
          }
          else if (feature.isUnsettable())
          {
            out.element(FEATURE);
            out.attribute(FEATURE_NAME, feature.getName());
            out.attribute(FEATURE_ISSET, list != null);
          }
        }
        else
        {
          Object value = rev.getValue(feature);
          if (value != null)
          {
            exportFeature(out, feature, value);
          }
        }
      }

      out.pop();
    }

    protected void exportFeature(XMLOutput out, EStructuralFeature feature, Object value) throws Exception
    {
      out.element(FEATURE);
      out.attribute(FEATURE_NAME, feature.getName());
      exportFeature(out, feature, FEATURE_TYPE, value);
    }

    protected void exportFeature(XMLOutput out, EStructuralFeature feature, String featureType, Object value) throws SAXException
    {
      if (value instanceof CDOID)
      {
        out.attribute(featureType, Object.class.getSimpleName());
        out.attribute(FEATURE_VALUE, str((CDOID)value));
      }
      else if (value instanceof CDOBlob)
      {
        CDOBlob blob = (CDOBlob)value;
        out.attribute(featureType, TYPE_BLOB);
        out.attribute(FEATURE_ID, HexUtil.bytesToHex(blob.getID()));
        out.attribute(FEATURE_SIZE, blob.getSize());
      }
      else if (value instanceof CDOClob)
      {
        CDOClob clob = (CDOClob)value;
        out.attribute(featureType, TYPE_CLOB);
        out.attribute(FEATURE_ID, HexUtil.bytesToHex(clob.getID()));
        out.attribute(FEATURE_SIZE, clob.getSize());
      }
      else if (value instanceof Date)
      {
        Date date = (Date)value;
        out.attribute(featureType, Date.class.getSimpleName());
        out.attribute(FEATURE_VALUE, date.getTime());
      }
      else if (FeatureMapUtil.isFeatureMap(feature))
      {
        FeatureMap.Entry entry = (FeatureMap.Entry)value;
        EStructuralFeature innerFeature = entry.getEStructuralFeature();
        Object innerValue = entry.getValue();

        out.attribute(featureType, TYPE_FEATURE_MAP);
        out.attribute(FEATURE_INNER_FEATURE, innerFeature.getName());
        exportFeature(out, innerFeature, FEATURE_INNER_TYPE, innerValue);
      }
      else if (value instanceof byte[])
      {
        byte[] array = (byte[])value;
        out.attribute(featureType, TYPE_BYTE_ARRAY);
        out.attribute(FEATURE_VALUE, HexUtil.bytesToHex(array));
      }
      else if (value == null || value == CDORevisionData.NIL)
      {
        out.attribute(FEATURE_ISNULL, true);
      }
      else
      {
        if (!(value instanceof String))
        {
          out.attribute(featureType, type(value));
        }

        out.attributeOrNull(FEATURE_VALUE, value);
      }
    }

    @Override
    protected void exportLobs(XMLOutput out) throws Exception
    {
      out.element(LOBS);

      out.push();
      super.exportLobs(out);
      out.pop();
    }

    @Override
    protected OutputStream startBlob(XMLOutput out, byte[] id, long size) throws Exception
    {
      out.element(BLOB);
      out.attribute(LOB_ID, HexUtil.bytesToHex(id));
      out.attribute(LOB_SIZE, size);
      return out.bytes();
    }

    @Override
    protected Writer startClob(XMLOutput out, byte[] id, long size) throws Exception
    {
      out.element(CLOB);
      out.attribute(LOB_ID, HexUtil.bytesToHex(id));
      out.attribute(LOB_SIZE, size);
      return out.characters();
    }

    @Override
    protected void exportCommits(XMLOutput out) throws Exception
    {
      out.element(COMMITS);

      out.push();
      super.exportCommits(out);
      out.pop();
    }

    @Override
    protected void exportCommit(XMLOutput out, CDOCommitInfo commitInfo) throws Exception
    {
      out.element(COMMIT);
      out.attribute(COMMIT_TIME, commitInfo.getTimeStamp());
      long previous = commitInfo.getPreviousTimeStamp();
      if (previous != CDOBranchPoint.UNSPECIFIED_DATE)
      {
        out.attribute(COMMIT_PREVIOUS, previous);
      }

      int branch = commitInfo.getBranch().getID();
      if (branch != CDOBranch.MAIN_BRANCH_ID)
      {
        out.attribute(COMMIT_BRANCH, branch);
      }

      out.attribute(COMMIT_USER, commitInfo.getUserID());
      out.attribute(COMMIT_COMMENT, commitInfo.getComment());

      CDOBranchPoint mergeSource = commitInfo.getMergeSource();
      if (mergeSource != null)
      {
        out.attribute(MERGE_SOURCE_BRANCH, mergeSource.getBranch().getID());
        out.attribute(MERGE_SOURCE_TIME, mergeSource.getTimeStamp());
      }
    }

    protected final String str(CDOID id)
    {
      StringBuilder builder = new StringBuilder();
      CDOIDUtil.write(builder, id);
      return builder.toString();
    }

    protected String type(Object value)
    {
      if (value instanceof Boolean)
      {
        return Boolean.class.getSimpleName();
      }

      if (value instanceof Character)
      {
        return Character.class.getSimpleName();
      }

      if (value instanceof Byte)
      {
        return Byte.class.getSimpleName();
      }

      if (value instanceof Short)
      {
        return Short.class.getSimpleName();
      }

      if (value instanceof Integer)
      {
        return Integer.class.getSimpleName();
      }

      if (value instanceof Long)
      {
        return Long.class.getSimpleName();
      }

      if (value instanceof Float)
      {
        return Float.class.getSimpleName();
      }

      if (value instanceof Double)
      {
        return Double.class.getSimpleName();
      }

      if (value instanceof String)
      {
        return String.class.getSimpleName();
      }

      if (value instanceof BigDecimal)
      {
        return BigDecimal.class.getSimpleName();
      }

      if (value instanceof BigInteger)
      {
        return BigInteger.class.getSimpleName();
      }

      throw new IllegalArgumentException("Invalid type: " + value.getClass().getName());
    }
  }

  /**
   * Binary constants being used by both {@link CDOServerExporter exporters} and {@link CDOServerImporter importers}.
   *
   * @noextend This interface is not intended to be extended by clients.
   * @noimplement This interface is not intended to be implemented by clients.
   * @author Eike Stepper
   * @since 4.8
   */
  public static interface BinaryConstants
  {
    public static final byte REPOSITORY = 0;

    public static final byte PACKAGE_UNIT = 1;

    public static final byte PACKAGE_INFO = 2;

    public static final byte BRANCH = 3;

    public static final byte REVISION = 4;

    public static final byte BLOB = 5;

    public static final byte CLOB = 6;

    public static final byte COMMIT = 7;

    public static final byte EOF = -1;
  }

  /**
   * @author Eike Stepper
   * @since 4.8
   */
  public static class Binary extends CDOServerExporter<CDODataOutput> implements BinaryConstants
  {
    public Binary(IRepository repository)
    {
      super(repository);
    }

    @Override
    protected CDODataOutput createOutput(OutputStream out) throws Exception
    {
      return new CDODataOutputImpl(new ExtendedDataOutputStream(out))
      {
        @Override
        public CDOPackageRegistry getPackageRegistry()
        {
          return ((InternalRepository)getRepository()).getPackageRegistry(false);
        }

        @Override
        public CDOIDProvider getIDProvider()
        {
          return CDOIDProvider.NOOP;
        }

        @Override
        public CDOPermissionProvider getPermissionProvider()
        {
          return CDOPermissionProvider.WRITE;
        }

        @Override
        protected boolean isXCompression()
        {
          return true;
        }
      };
    }

    @Override
    protected void exportAll(CDODataOutput out) throws Exception
    {
      out.writeByte(REPOSITORY);
      out.writeString(getRepository().getName());
      out.writeString(getRepository().getUUID());
      out.writeCDOID(getRepository().getRootResourceID());
      out.writeLong(getRepository().getStore().getCreationTime());
      out.writeLong(getRepository().getLastCommitTimeStamp());
      out.writeString(getBranchPath());
      out.writeXLong(getTimeStamp());

      super.exportAll(out);

      out.writeByte(EOF);
    }

    @Override
    protected void startPackageUnit(CDODataOutput out, String id, Type type, long time, String data) throws Exception
    {
      out.writeByte(PACKAGE_UNIT);
      out.writeString(id);
      out.writeEnum(type);
      out.writeXLong(time);
      out.writeString(data);
    }

    @Override
    protected void endPackageUnit(CDODataOutput out) throws Exception
    {
      // Do nothing.
    }

    @Override
    protected void exportPackageInfo(CDODataOutput out, String packageURI) throws Exception
    {
      out.writeByte(PACKAGE_INFO);
      out.writeString(packageURI);
    }

    @Override
    protected void exportBranch(CDODataOutput out, CDOBranch branch) throws Exception
    {
      out.writeByte(BRANCH);
      out.writeXInt(branch.getID());
      out.writeString(branch.getName());
      out.writeXLong(branch.getBase().getTimeStamp());
      if (branch.isMainBranch())
      {
        out.writeXInt(CDOBranch.MAIN_BRANCH_ID);
      }
      else
      {
        out.writeXInt(branch.getBase().getBranch().getID());
      }

      super.exportBranch(out, branch);
    }

    @Override
    protected void exportRevision(CDODataOutput out, CDORevision revision) throws Exception
    {
      out.writeByte(REVISION);
      if (revision instanceof DetachedCDORevision)
      {
        out.writeBoolean(true);
        out.writeCDOID(revision.getID());
        out.writeCDOClassifierRef(revision.getEClass());
        out.writeCDOBranch(revision.getBranch());
        out.writeXInt(revision.getVersion());
        out.writeXLong(revision.getTimeStamp());
        out.writeXLong(revision.getRevised());
      }
      else
      {
        out.writeBoolean(false);
        out.writeCDORevision(revision, CDORevision.UNCHUNKED);
      }
    }

    @Override
    protected OutputStream startBlob(final CDODataOutput out, byte[] id, long size) throws Exception
    {
      out.writeByte(BLOB);
      out.writeByteArray(id);
      out.writeXLong(size);

      return new OutputStream()
      {
        @Override
        public void write(byte[] b, int off, int len) throws IOException
        {
          out.write(b, off, len);
        }

        @Override
        public void write(int b) throws IOException
        {
          out.write(b);
        }

        @Override
        public void close() throws IOException
        {
          // Do nothing.
        }
      };
    }

    @Override
    protected Writer startClob(final CDODataOutput out, byte[] id, long size) throws Exception
    {
      out.writeByte(CLOB);
      out.writeByteArray(id);
      out.writeXLong(size);

      return new Writer()
      {
        @Override
        public void write(char[] cbuf, int off, int len) throws IOException
        {
          for (int i = off; i < len; i++)
          {
            char c = cbuf[i];
            out.writeChar(c);
          }
        }

        @Override
        public void close() throws IOException
        {
          // Do nothing.
        }

        @Override
        public void flush() throws IOException
        {
          // Do nothing.
        }
      };
    }

    @Override
    protected void exportCommit(CDODataOutput out, CDOCommitInfo commitInfo) throws Exception
    {
      out.writeByte(COMMIT);
      out.writeXLong(commitInfo.getTimeStamp());
      out.writeXLong(commitInfo.getPreviousTimeStamp());
      out.writeXInt(commitInfo.getBranch().getID());
      out.writeString(commitInfo.getUserID());
      out.writeString(commitInfo.getComment());

      CDOBranchPoint mergeSource = commitInfo.getMergeSource();
      if (mergeSource != null)
      {
        out.writeBoolean(true);
        out.writeXInt(mergeSource.getBranch().getID());
        out.writeXLong(mergeSource.getTimeStamp());
      }
      else
      {
        out.writeBoolean(false);
      }
    }
  }
}

Back to the top