Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b3fb4273e6840cd451a8381aea5e0319fc0c50a4 (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
/*
 * Copyright (c) 2004 - 2012 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.net4j.util.event.Event;
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.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.Resource.Factory;
import org.eclipse.emf.ecore.resource.Resource.Factory.Registry;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceFactoryRegistryImpl;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;

import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;

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

/**
 * @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.UNKNOWN;

  private ResourceSet sourceResourceSet;

  private ResourceSet targetResourceSet;

  private Set<Resource> resourcesToSave = new HashSet<Resource>();

  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 final ResourceSet getSourceResourceSet()
  {
    if (sourceResourceSet == null)
    {
      sourceResourceSet = sourceSystem.provideResourceSet();
      if (sourceResourceSet == null)
      {
        sourceResourceSet = createResourceSet(sourceSystem);
      }
    }

    return sourceResourceSet;
  }

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

    return targetResourceSet;
  }

  protected ResourceSet createResourceSet(CDOTransferSystem system)
  {
    Resource.Factory.Registry registry = new ResourceFactoryRegistryImpl()
    {
      {
        getProtocolToFactoryMap().putAll(Resource.Factory.Registry.INSTANCE.getProtocolToFactoryMap());
        getExtensionToFactoryMap().putAll(Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap());
        getContentTypeToFactoryMap().putAll(Resource.Factory.Registry.INSTANCE.getContentTypeToFactoryMap());

        getExtensionToFactoryMap().remove(Resource.Factory.Registry.DEFAULT_EXTENSION);
        getContentTypeToFactoryMap().remove(Resource.Factory.Registry.DEFAULT_CONTENT_TYPE_IDENTIFIER);
      }

      @Override
      protected Factory delegatedGetFactory(URI uri, String contentTypeIdentifier)
      {
        return null;
      }
    };

    ResourceSet resourceSet = new ResourceSetImpl();
    resourceSet.setResourceFactoryRegistry(registry);
    return resourceSet;
  }

  public final CDOTransferMapping getRootMapping()
  {
    return rootMapping;
  }

  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 CDOTransferMapping map(IPath sourcePath) throws IOException
  {
    CDOTransferElement source = sourceSystem.getElement(sourcePath);
    return map(source);
  }

  public CDOTransferMapping map(String sourcePath) throws IOException
  {
    return map(new Path(sourcePath));
  }

  public CDOTransferMapping map(CDOTransferElement source) throws IOException
  {
    return map(source, rootMapping);
  }

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

    return mapping;
  }

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

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

  protected boolean hasResourceFactory(CDOTransferElement source)
  {
    URI uri = source.getURI();
    // TODO Derive resourceSet from element.getSystem()?
    Registry registry = getSourceResourceSet().getResourceFactoryRegistry();
    return registry.getFactory(uri) != null;
  }

  protected CDOTransferType getTransferType(CDOTransferElement source)
  {
    CDOTransferType type = source.getSystem().getDefaultTransferType(source);
    if (type == CDOTransferType.UNKNOWN)
    {
      if (hasResourceFactory(source))
      {
        return CDOTransferType.MODEL;
      }
    }

    if (type == CDOTransferType.UNKNOWN)
    {
      type = getDefaultTransferType();
    }

    return type;
  }

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

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

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

    for (CDOTransferMapping child : mapping.getChildren())
    {
      validate(child);
    }
  }

  public void perform() throws IOException
  {
    validate(rootMapping);
    perform(rootMapping);

    for (Resource resource : resourcesToSave)
    {
      resource.save(null);
    }

    resourcesToSave.clear();
  }

  protected void perform(CDOTransferMapping mapping) throws IOException
  {
    CDOTransferType transferType = mapping.getTransferType();
    if (transferType == CDOTransferType.FOLDER)
    {
      performFolder(mapping);
    }
    else if (transferType == CDOTransferType.MODEL)
    {
      performModel(mapping);
    }
    else if (transferType == CDOTransferType.BINARY)
    {
      performBinary(mapping);
    }
    else if (transferType instanceof CDOTransferType.Text)
    {
      String encoding = ((CDOTransferType.Text)transferType).getEncoding();
      performText(mapping, encoding);
    }
  }

  protected void performFolder(CDOTransferMapping mapping) throws IOException
  {
    if (mapping.getStatus() == CDOTransferMapping.Status.NEW)
    {
      targetSystem.createFolder(mapping.getFullPath());
    }

    for (CDOTransferMapping child : mapping.getChildren())
    {
      perform(child);
    }
  }

  protected void performModel(CDOTransferMapping mapping) throws IOException
  {
    Resource sourceResource = getSourceResource(mapping);
    Resource targetResource = getTargetResource(mapping);

    EList<EObject> sourceContents = sourceResource.getContents();
    Collection<EObject> targetContents = EcoreUtil.copyAll(sourceContents);

    EList<EObject> contents = targetResource.getContents();
    contents.addAll(targetContents);
    resourcesToSave.add(targetResource);
  }

  protected void performBinary(CDOTransferMapping mapping) throws IOException
  {
    IPath path = mapping.getFullPath();
    InputStream source = mapping.getSource().openInputStream();

    try
    {
      targetSystem.createBinary(path, source);
    }
    finally
    {
      IOUtil.close(source);
    }
  }

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

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

  /**
   * @author Eike Stepper
   */
  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();
  }

  /**
   * @author Eike Stepper
   */
  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;
    }

    /**
     * @author Eike Stepper
     */
    public enum Kind
    {
      MAPPED, UNMAPPED
    }
  }

  /**
   * @author Eike Stepper
   */
  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;
    }
  }

  /**
   * @author Eike Stepper
   */
  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;
    }
  }
}

Back to the top