Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 68426e93012750422e26a895d3a4a071ec32898a (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
/*
 * Copyright (c) 2012, 2015 Eike Stepper (Berlin, 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.transfer;

import org.eclipse.emf.cdo.spi.transfer.ResourceFactoryRegistryWithoutDefaults;

import org.eclipse.net4j.util.event.Event;
import org.eclipse.net4j.util.event.IEvent;
import org.eclipse.net4j.util.event.IListener;
import org.eclipse.net4j.util.event.INotifier;
import org.eclipse.net4j.util.event.Notifier;
import org.eclipse.net4j.util.io.IOUtil;
import org.eclipse.net4j.util.lifecycle.ILifecycleEvent;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.EcoreUtil.Copier;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Path;
import org.eclipse.net4j.util.om.monitor.SubProgressMonitor;

import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Abstracts the transfer of a tree of {@link CDOTransferElement elements} for a
 * {@link #getSourceSystem() source} to a {@link #getTargetSystem() target} {@link CDOTransferSystem system}.
 * <p>
 * The mappings of the source elements to their target elements is represented as a tree of {@link CDOTransferMapping transfer mappings}.
 * <p>
 * A transfer fires the following {@link IEvent events}:
 * <ul>
 * <li>{@link ChildrenChangedEvent} when the {@link CDOTransferMapping#getChildren() children} of a mapping have changed.
 * <li>{@link RelativePathChangedEvent} when the {@link CDOTransferMapping#getRelativePath() relative path} of a mapping has changed.
 * <li>{@link TransferTypeChangedEvent} when the {@link CDOTransferMapping#getTransferType() transfer type} of a mapping has changed.
 * <li>{@link UnmappedModelsEvent} when the set of {@link CDOTransfer.ModelTransferContext#getUnmappedModels() unmapped models} has changed.
 * </ul>
 *
 * @author Eike Stepper
 * @since 4.2
 */
public class CDOTransfer implements INotifier
{
  protected final Notifier notifier = new Notifier();

  private final CDOTransferSystem sourceSystem;

  private final CDOTransferSystem targetSystem;

  private final CDOTransferMapping rootMapping = new CDOTransferMappingImpl(this);

  private final Map<CDOTransferElement, CDOTransferMapping> mappings = new HashMap<CDOTransferElement, CDOTransferMapping>();

  private CDOTransferType defaultTransferType = CDOTransferType.BINARY;

  private ModelTransferContext modelTransferContext = createModelTransferContext();

  public CDOTransfer(CDOTransferSystem sourceSystem, CDOTransferSystem targetSystem)
  {
    this.sourceSystem = sourceSystem;
    this.targetSystem = targetSystem;
  }

  public void addListener(IListener listener)
  {
    notifier.addListener(listener);
  }

  public void removeListener(IListener listener)
  {
    notifier.removeListener(listener);
  }

  public boolean hasListeners()
  {
    return notifier.hasListeners();
  }

  public IListener[] getListeners()
  {
    return notifier.getListeners();
  }

  public final CDOTransferSystem getSourceSystem()
  {
    return sourceSystem;
  }

  public final CDOTransferSystem getTargetSystem()
  {
    return targetSystem;
  }

  public ModelTransferContext getModelTransferContext()
  {
    return modelTransferContext;
  }

  public final CDOTransferType getDefaultTransferType()
  {
    return defaultTransferType;
  }

  public final void setDefaultTransferType(CDOTransferType defaultTransferType)
  {
    if (defaultTransferType == CDOTransferType.FOLDER)
    {
      throw new IllegalArgumentException();
    }

    this.defaultTransferType = defaultTransferType;
  }

  public Set<CDOTransferType> getUsedTransferTypes()
  {
    final Set<CDOTransferType> result = new HashSet<CDOTransferType>();
    rootMapping.accept(new CDOTransferMapping.Visitor()
    {
      public boolean visit(CDOTransferMapping mapping)
      {
        result.add(mapping.getTransferType());
        return true;
      }
    });

    return result;
  }

  public final CDOTransferMapping getRootMapping()
  {
    return rootMapping;
  }

  public IPath getTargetPath()
  {
    return rootMapping.getRelativePath();
  }

  public void setTargetPath(IPath targetPath)
  {
    rootMapping.setRelativePath(targetPath);
  }

  public void setTargetPath(String path)
  {
    rootMapping.setRelativePath(path);
  }

  public int getMappingCount()
  {
    return mappings.size();
  }

  public CDOTransferMapping map(IPath sourcePath, IProgressMonitor monitor)
  {
    CDOTransferElement source = sourceSystem.getElement(sourcePath);
    return map(source, monitor);
  }

  public CDOTransferMapping map(String sourcePath, IProgressMonitor monitor)
  {
    return map(new Path(sourcePath), monitor);
  }

  public CDOTransferMapping map(CDOTransferElement source, IProgressMonitor monitor)
  {
    return map(source, rootMapping, monitor);
  }

  protected CDOTransferMapping map(CDOTransferElement source, CDOTransferMapping parent, IProgressMonitor monitor)
  {
    CDOTransferMapping mapping = mappings.get(source);
    if (mapping == null)
    {
      mapping = createMapping(source, parent, monitor);
      mappings.put(source, mapping);
    }
    else
    {
      if (mapping.getParent() != parent)
      {
        throw new IllegalStateException();
      }
    }

    return mapping;
  }

  protected void unmap(CDOTransferMapping mapping)
  {
    mappings.remove(mapping.getSource());
    mapping.getChildren();
  }

  protected CDOTransferMapping createMapping(CDOTransferElement source, CDOTransferMapping parent,
      IProgressMonitor monitor)
  {
    return new CDOTransferMappingImpl(this, source, parent, monitor);
  }

  protected ModelTransferContext createModelTransferContext()
  {
    return new ModelTransferContext(this);
  }

  protected CDOTransferType getTransferType(CDOTransferElement source)
  {
    if (source.isDirectory())
    {
      return CDOTransferType.FOLDER;
    }

    CDOTransferType type = sourceSystem.getDefaultTransferType(source);
    if (type != null)
    {
      return type;
    }

    if (modelTransferContext.hasResourceFactory(source))
    {
      return CDOTransferType.MODEL;
    }

    return getDefaultTransferType();
  }

  protected void validate(CDOTransferMapping mapping, IProgressMonitor monitor)
  {
    if (monitor.isCanceled())
    {
      throw new OperationCanceledException();
    }

    if (mapping.getStatus() == CDOTransferMapping.Status.CONFLICT)
    {
      throw new IllegalStateException("Conflict: " + mapping);
    }

    monitor.worked(1);
    for (CDOTransferMapping child : mapping.getChildren())
    {
      validate(child, monitor);
    }
  }

  public void perform()
  {
    perform(new NullProgressMonitor());
  }

  public void perform(IProgressMonitor monitor)
  {
    try
    {
      int mappingCount = getMappingCount();
      monitor.beginTask("Perform transfer from " + getSourceSystem() + " to " + getTargetSystem(), 5 * mappingCount);

      monitor.subTask("Validating transfer");
      validate(rootMapping, new SubProgressMonitor(monitor, mappingCount));

      perform(rootMapping, monitor);
      modelTransferContext.performFinish(new SubProgressMonitor(monitor, 2 * mappingCount));
    }
    finally
    {
      monitor.done();
    }
  }

  protected void perform(CDOTransferMapping mapping, IProgressMonitor monitor)
  {
    monitor.subTask("Transferring " + mapping);
    if (monitor.isCanceled())
    {
      throw new OperationCanceledException();
    }

    CDOTransferType transferType = mapping.getTransferType();
    if (transferType == CDOTransferType.FOLDER)
    {
      performFolder(mapping, monitor);
    }
    else if (transferType == CDOTransferType.MODEL)
    {
      performModel(mapping, monitor);
    }
    else if (transferType == CDOTransferType.BINARY)
    {
      performBinary(mapping, monitor);
    }
    else if (transferType instanceof CDOTransferType.Text)
    {
      String encoding = ((CDOTransferType.Text)transferType).getEncoding();
      performText(mapping, encoding, monitor);
    }
  }

  protected void performFolder(CDOTransferMapping mapping, IProgressMonitor monitor)
  {
    if (mapping.getStatus() == CDOTransferMapping.Status.NEW)
    {
      targetSystem.createFolder(mapping.getFullPath());
    }

    monitor.worked(2);
    for (CDOTransferMapping child : mapping.getChildren())
    {
      perform(child, monitor);
    }
  }

  protected void performModel(CDOTransferMapping mapping, IProgressMonitor monitor)
  {
    modelTransferContext.perform(mapping, new SubProgressMonitor(monitor, 2));
  }

  protected void performBinary(CDOTransferMapping mapping, IProgressMonitor monitor)
  {
    IPath path = mapping.getFullPath();
    InputStream source = mapping.getSource().openInputStream();

    try
    {
      targetSystem.createBinary(path, source, new SubProgressMonitor(monitor, 2));
    }
    finally
    {
      IOUtil.close(source);
    }
  }

  protected void performText(CDOTransferMapping mapping, String encoding, IProgressMonitor monitor)
  {
    IPath path = mapping.getFullPath();
    InputStream source = mapping.getSource().openInputStream();

    try
    {
      targetSystem.createText(path, source, encoding, new SubProgressMonitor(monitor, 2));
    }
    finally
    {
      IOUtil.close(source);
    }
  }

  protected void childrenChanged(CDOTransferMapping mapping, CDOTransferMapping child, ChildrenChangedEvent.Kind kind)
  {
    if (child.getTransferType() == CDOTransferType.MODEL)
    {
      if (kind == ChildrenChangedEvent.Kind.MAPPED)
      {
        modelTransferContext.addModelMapping(child);
      }
      else
      {
        modelTransferContext.removeModelMapping(child);
      }
    }

    IListener[] listeners = notifier.getListeners();
    if (listeners != null)
    {
      notifier.fireEvent(new ChildrenChangedEvent(mapping, child, kind), listeners);
    }
  }

  protected void relativePathChanged(CDOTransferMapping mapping, IPath oldPath, IPath newPath)
  {
    IListener[] listeners = notifier.getListeners();
    if (listeners != null)
    {
      notifier.fireEvent(new RelativePathChangedEvent(mapping, oldPath, newPath), listeners);
    }
  }

  protected void transferTypeChanged(CDOTransferMapping mapping, CDOTransferType oldType, CDOTransferType newType)
  {
    if (oldType == CDOTransferType.MODEL)
    {
      modelTransferContext.removeModelMapping(mapping);
    }

    if (newType == CDOTransferType.MODEL)
    {
      modelTransferContext.addModelMapping(mapping);
    }

    IListener[] listeners = notifier.getListeners();
    if (listeners != null)
    {
      notifier.fireEvent(new TransferTypeChangedEvent(mapping, oldType, newType), listeners);
    }
  }

  /**
   * An abstract base implementation of a {@link CDOTransferMapping mapping} {@link ILifecycleEvent event}.
   *
   * @author Eike Stepper
   * @noextend This class is not intended to be subclassed by clients.
   */
  public static abstract class MappingEvent extends Event
  {
    private static final long serialVersionUID = 1L;

    private CDOTransferMapping mapping;

    private MappingEvent(CDOTransferMapping mapping)
    {
      super(mapping.getTransfer());
      this.mapping = mapping;
    }

    public CDOTransferMapping getMapping()
    {
      return mapping;
    }

    public abstract boolean hasTreeImpact();
  }

  /**
   * A {@link MappingEvent mapping event} fired from a {@link CDOTransfer transfer} when
   * the {@link CDOTransferMapping#getChildren() children} of a mapping have changed.
   *
   * @author Eike Stepper
   * @noextend This class is not intended to be subclassed by clients.
   */
  public static class ChildrenChangedEvent extends MappingEvent
  {
    private static final long serialVersionUID = 1L;

    private CDOTransferMapping child;

    private Kind kind;

    ChildrenChangedEvent(CDOTransferMapping mapping, CDOTransferMapping child, Kind kind)
    {
      super(mapping);
      this.child = child;
      this.kind = kind;
    }

    @Override
    public boolean hasTreeImpact()
    {
      return true;
    }

    public CDOTransferMapping getChild()
    {
      return child;
    }

    public Kind getKind()
    {
      return kind;
    }

    /**
     * Enumerates the possible values of {@link ChildrenChangedEvent#getKind()}.
     *
     * @author Eike Stepper
     */
    public enum Kind
    {
      MAPPED, UNMAPPED
    }
  }

  /**
   * A {@link MappingEvent mapping event} fired from a {@link CDOTransfer transfer} when
   * the {@link CDOTransferMapping#getRelativePath() relative path} of a mapping has changed.
   *
   * @author Eike Stepper
   * @noextend This class is not intended to be subclassed by clients.
   */
  public static class RelativePathChangedEvent extends MappingEvent
  {
    private static final long serialVersionUID = 1L;

    private IPath oldPath;

    private IPath newPath;

    RelativePathChangedEvent(CDOTransferMapping mapping, IPath oldPath, IPath newPath)
    {
      super(mapping);
      this.oldPath = oldPath;
      this.newPath = newPath;
    }

    @Override
    public boolean hasTreeImpact()
    {
      return true;
    }

    public IPath getOldPath()
    {
      return oldPath;
    }

    public IPath getNewPath()
    {
      return newPath;
    }
  }

  /**
   * A {@link MappingEvent mapping event} fired from a {@link CDOTransfer transfer} when
   * the {@link CDOTransferMapping#getTransferType() transfer type} of a mapping has changed.
   *
   * @author Eike Stepper
   * @noextend This class is not intended to be subclassed by clients.
   */
  public static class TransferTypeChangedEvent extends MappingEvent
  {
    private static final long serialVersionUID = 1L;

    private CDOTransferType oldType;

    private CDOTransferType newType;

    TransferTypeChangedEvent(CDOTransferMapping mapping, CDOTransferType oldType, CDOTransferType newType)
    {
      super(mapping);
      this.oldType = oldType;
      this.newType = newType;
    }

    @Override
    public boolean hasTreeImpact()
    {
      return false;
    }

    public CDOTransferType getOldType()
    {
      return oldType;
    }

    public CDOTransferType getNewType()
    {
      return newType;
    }
  }

  /**
   * An {@link IEvent event} fired from a {@link CDOTransfer transfer} when
   * the set of {@link CDOTransfer.ModelTransferContext#getUnmappedModels() unmapped models} has changed.
   *
   * @author Eike Stepper
   * @noextend This class is not intended to be subclassed by clients.
   */
  public static class UnmappedModelsEvent extends Event
  {
    private static final long serialVersionUID = 1L;

    private UnmappedModelsEvent(CDOTransfer transfer)
    {
      super(transfer);
    }
  }

  /**
   * Encapsulates the model-specific aspects of a {@link CDOTransfer transfer}.
   *
   * @author Eike Stepper
   */
  public static class ModelTransferContext
  {
    private final CDOTransfer transfer;

    private ResourceSet sourceResourceSet;

    private ResourceSet targetResourceSet;

    private Map<CDOTransferElement, Resource> elementResources = new HashMap<CDOTransferElement, Resource>();

    private Map<Resource, CDOTransferElement> resourceElements = new HashMap<Resource, CDOTransferElement>();

    private Set<Resource> unmappedModels;

    private Map<URI, ModelTransferResolution> resolutions = new HashMap<URI, ModelTransferResolution>();

    private ResolveProxyAdapter resolveProxyAdapter;

    private Copier copier;

    protected ModelTransferContext(CDOTransfer transfer)
    {
      this.transfer = transfer;
      resolveProxyAdapter = new ResolveProxyAdapter();
      copier = createCopier();
    }

    public final CDOTransfer getTransfer()
    {
      return transfer;
    }

    public final ResourceSet getSourceResourceSet()
    {
      if (sourceResourceSet == null)
      {
        CDOTransferSystem sourceSystem = transfer.getSourceSystem();
        sourceResourceSet = sourceSystem.provideResourceSet();
        if (sourceResourceSet == null)
        {
          sourceResourceSet = createResourceSet(sourceSystem);
        }
      }

      return sourceResourceSet;
    }

    public final ResourceSet getTargetResourceSet()
    {
      if (targetResourceSet == null)
      {
        CDOTransferSystem targetSystem = transfer.getTargetSystem();
        targetResourceSet = targetSystem.provideResourceSet();
        if (targetResourceSet == null)
        {
          targetResourceSet = createResourceSet(targetSystem);
        }
      }

      return targetResourceSet;
    }

    public void registerSourceExtension(String extension, Resource.Factory factory)
    {
      Map<String, Object> map = getSourceResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap();
      map.put(extension, factory);
    }

    public void registerSourceProtocol(String protocol, Resource.Factory factory)
    {
      Map<String, Object> map = getSourceResourceSet().getResourceFactoryRegistry().getProtocolToFactoryMap();
      map.put(protocol, factory);
    }

    public void registerSourceContentType(String contentType, Resource.Factory factory)
    {
      Map<String, Object> map = getSourceResourceSet().getResourceFactoryRegistry().getContentTypeToFactoryMap();
      map.put(contentType, factory);
    }

    public void registerTargetExtension(String extension, Resource.Factory factory)
    {
      Map<String, Object> map = getTargetResourceSet().getResourceFactoryRegistry().getExtensionToFactoryMap();
      map.put(extension, factory);
    }

    public void registerTargetProtocol(String protocol, Resource.Factory factory)
    {
      Map<String, Object> map = getTargetResourceSet().getResourceFactoryRegistry().getProtocolToFactoryMap();
      map.put(protocol, factory);
    }

    public void registerTargetContentType(String contentType, Resource.Factory factory)
    {
      Map<String, Object> map = getTargetResourceSet().getResourceFactoryRegistry().getContentTypeToFactoryMap();
      map.put(contentType, factory);
    }

    public Set<Resource> getUnmappedModels()
    {
      if (unmappedModels == null)
      {
        unmappedModels = resolve();
        fireUnmappedModelsEvent();
      }

      return unmappedModels;
    }

    public ModelTransferResolution getResolution(URI uri)
    {
      return resolutions.get(uri);
    }

    public ModelTransferResolution setResolution(URI uri, ModelTransferResolution resolution)
    {
      ModelTransferResolution old = resolutions.put(uri, resolution);
      if (resolution != old)
      {
        fireUnmappedModelsEvent();
      }

      return old;
    }

    protected Set<Resource> resolve()
    {
      Set<Resource> mappedModels = resourceElements.keySet();

      ResourceSet resourceSet = getSourceResourceSet();
      EList<Resource> resources = resourceSet.getResources();

      Set<Resource> unmappedModels = new HashSet<Resource>(resources);
      unmappedModels.removeAll(mappedModels);
      return unmappedModels;
    }

    protected void fireUnmappedModelsEvent()
    {
      transfer.notifier.fireEvent(new UnmappedModelsEvent(transfer));
    }

    protected void addModelMapping(CDOTransferMapping mapping)
    {
      CDOTransferElement element = mapping.getSource();
      URI uri = element.getURI();

      ResourceSet resourceSet = getSourceResourceSet();
      Resource resource = resourceSet.getResource(uri, true);
      elementResources.put(element, resource);
      resourceElements.put(resource, element);
      unmappedModels = null;
      fireUnmappedModelsEvent();
    }

    protected void removeModelMapping(CDOTransferMapping mapping)
    {
      CDOTransferElement element = mapping.getSource();
      Resource resource = elementResources.remove(element);
      if (resource != null)
      {
        resourceElements.remove(resource);
        resource.unload();

        ResourceSet resourceSet = getSourceResourceSet();
        resourceSet.getResources().remove(resource);
      }

      unmappedModels = null;
      fireUnmappedModelsEvent();
    }

    protected Resource getSourceResource(CDOTransferMapping mapping)
    {
      URI uri = mapping.getSource().getURI();
      ResourceSet sourceResourceSet = getSourceResourceSet();
      return sourceResourceSet.getResource(uri, true);
    }

    protected Resource getTargetResource(CDOTransferMapping mapping)
    {
      IPath path = mapping.getFullPath();
      ResourceSet targetResourceSet = getTargetResourceSet();
      CDOTransferSystem targetSystem = transfer.getTargetSystem();
      return targetSystem.createModel(targetResourceSet, path);
    }

    protected Copier createCopier()
    {
      return new Copier();
    }

    protected ResourceSet createResourceSet(CDOTransferSystem system)
    {
      ResourceSet resourceSet = new ResourceSetImpl();
      resourceSet.setResourceFactoryRegistry(new ResourceFactoryRegistryWithoutDefaults());
      resourceSet.eAdapters().add(resolveProxyAdapter);

      return resourceSet;
    }

    protected boolean hasResourceFactory(CDOTransferElement source)
    {
      URI uri = source.getURI();

      // TODO Derive resourceSet from element.getSystem()?
      Resource.Factory.Registry registry = getSourceResourceSet().getResourceFactoryRegistry();
      return registry.getFactory(uri) != null;
    }

    protected void perform(CDOTransferMapping mapping, IProgressMonitor monitor)
    {
      try
      {
        monitor.beginTask("", 2);

        Resource sourceResource = getSourceResource(mapping);
        Resource targetResource = getTargetResource(mapping); // Create target resource

        EList<EObject> sourceContents = sourceResource.getContents();
        Collection<EObject> targetContents = copier.copyAll(sourceContents);
        monitor.worked(1);

        EList<EObject> contents = targetResource.getContents();
        contents.addAll(targetContents);
        monitor.worked(1);
      }
      finally
      {
        monitor.done();
      }
    }

    protected void performFinish(IProgressMonitor monitor)
    {
      monitor.subTask("Copying model references");
      copier.copyReferences();

      CDOTransferSystem targetSystem = getTransfer().getTargetSystem();
      targetSystem.saveModels(getTargetResourceSet().getResources(), monitor);
    }

    /**
     * An {@link AdapterImpl adapter} for a {@link ResourceSet resource set} that resolves all proxies in all resources when they are loaded.
     *
     * @author Eike Stepper
     */
    public static class ResolveProxyAdapter extends AdapterImpl
    {
      private LoadResourceAdapter loadResourceAdapter;

      public ResolveProxyAdapter()
      {
        loadResourceAdapter = new LoadResourceAdapter();
      }

      @Override
      public void notifyChanged(Notification msg)
      {
        int eventType = msg.getEventType();
        switch (eventType)
        {
        case Notification.SET:
        {
          Resource oldValue = (Resource)msg.getOldValue();
          if (oldValue != null)
          {
            removeResource(oldValue);
          }

          Resource newValue = (Resource)msg.getNewValue();
          if (newValue != null)
          {
            addResource(newValue);
          }

          break;
        }
        case Notification.ADD:
        {
          Resource newValue = (Resource)msg.getNewValue();
          if (newValue != null)
          {
            addResource(newValue);
          }
          break;
        }
        case Notification.ADD_MANY:
        {
          @SuppressWarnings("unchecked")
          Collection<Resource> newValues = (Collection<Resource>)msg.getNewValue();
          for (Resource newValue : newValues)
          {
            addResource(newValue);
          }
          break;
        }
        case Notification.REMOVE:
        {
          Resource oldValue = (Resource)msg.getOldValue();
          if (oldValue != null)
          {
            removeResource(oldValue);
          }
          break;
        }
        case Notification.REMOVE_MANY:
        {
          @SuppressWarnings("unchecked")
          Collection<Resource> oldValues = (Collection<Resource>)msg.getOldValue();
          for (Resource oldContentValue : oldValues)
          {
            removeResource(oldContentValue);
          }
          break;
        }
        }
      }

      private void addResource(Resource resource)
      {
        EcoreUtil.resolveAll(resource);
        resource.eAdapters().add(loadResourceAdapter);
      }

      private void removeResource(Resource resource)
      {
      }

      /**
       * An {@link AdapterImpl adapter} for a {@link Resource resource} that resolves all proxies in that resource when it's demand loaded.
       *
       * @author Eike Stepper
       */
      public static class LoadResourceAdapter extends AdapterImpl
      {
        @Override
        public void notifyChanged(Notification msg)
        {
          if (msg.getFeatureID(Resource.class) == Resource.RESOURCE__IS_LOADED)
          {
            int eventType = msg.getEventType();
            switch (eventType)
            {
            case Notification.SET:
            {
              boolean isLoaded = msg.getNewBooleanValue();
              if (isLoaded)
              {
                Resource resource = (Resource)msg.getNotifier();
                EcoreUtil.resolveAll(resource);
              }
              break;
            }
            }
          }
        }
      }
    }
  }

  /**
   * Reserved for future use.
   *
   * @author Eike Stepper
   * @noextend This class is not intended to be subclassed by clients.
   * @noimplement This interface is not intended to be implemented by clients.
   */
  public interface ModelTransferResolution
  {
  }
}

Back to the top