Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7b0a2f1e95653185f5c23871fbdb8000375bf824 (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
/**
 * Copyright (c) 2004 - 2010 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.workspace.internal.efs;

import org.eclipse.emf.cdo.CDOObject;
import org.eclipse.emf.cdo.common.id.CDOID;
import org.eclipse.emf.cdo.common.id.CDOIDUtil;
import org.eclipse.emf.cdo.common.model.CDOClassInfo;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.eresource.CDOResourceNode;
import org.eclipse.emf.cdo.transaction.CDOTransaction;
import org.eclipse.emf.cdo.util.CDOUtil;
import org.eclipse.emf.cdo.util.CommitException;
import org.eclipse.emf.cdo.view.CDOView;
import org.eclipse.emf.cdo.workspace.CDOWorkspace;
import org.eclipse.emf.cdo.workspace.efs.CDOWorkspaceFSUtil;

import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.io.IOUtil;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.xmi.XMIResource;
import org.eclipse.emf.spi.cdo.InternalCDOObject;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;

import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 * @author Eike Stepper
 */
public final class CDOWorkspaceStore extends AbstractResourceNodeStore
{
  private static final AbstractResourceNodeStore NO_PARENT = null;

  private String name;

  private File location;

  private CDOWorkspace workspace;

  private Map<String, Long> lastModifiedTimes = new HashMap<String, Long>();

  private CDOView view;

  private SaveContext saveContext;

  public CDOWorkspaceStore(String name, File location)
  {
    this.name = name;
    this.location = location;
  }

  public File getLocation()
  {
    return location;
  }

  public synchronized CDOWorkspace getWorkspace()
  {
    if (workspace == null)
    {
      workspace = openWorkspace();
    }

    return workspace;
  }

  public synchronized void setWorkspace(CDOWorkspace workspace)
  {
    this.workspace = workspace;
  }

  private CDOWorkspace openWorkspace()
  {
    try
    {
      return CDOWorkspaceFSUtil.open(name, location);
    }
    catch (Exception ex)
    {
      throw WrappedException.wrap(ex);
    }
  }

  @Override
  public AbstractResourceNodeStore getParent()
  {
    return NO_PARENT;
  }

  @Override
  public String getName()
  {
    return name;
  }

  @Override
  public URI toURI()
  {
    try
    {
      return new URI(CDOWorkspaceFileSystem.SCHEME + "://" + name);
    }
    catch (URISyntaxException ex)
    {
      throw WrappedException.wrap(ex);
    }
  }

  @Override
  public IFileStore getChild(String name)
  {
    if (CDOProjectDescriptionStore.DESCRIPTION_FILE_NAME.equals(name))
    {
      return new CDOProjectDescriptionStore(this);
    }

    return super.getChild(name);
  }

  @Override
  public IFileStore mkdir(int options, IProgressMonitor monitor) throws CoreException
  {
    return this;
  }

  @Override
  public InputStream openInputStream(int options, IProgressMonitor monitor) throws CoreException
  {
    throw new UnsupportedOperationException();
  }

  public void dispose()
  {
    if (view != null)
    {
      IOUtil.close(view);
      view = null;
    }
  }

  @Override
  public CDOWorkspaceStore getWorkspaceStore()
  {
    return this;
  }

  public long getLastModified(String path)
  {
    Long time = lastModifiedTimes.get(path);
    return time == null ? EFS.NONE : time;
  }

  public void setLastModified(String path, long time)
  {
    lastModifiedTimes.put(path, time);
    // TODO Save lastModifiedTimes
  }

  public SaveContext getSaveContext()
  {
    if (saveContext == null)
    {
      saveContext = new SaveContext();
    }

    return saveContext;
  }

  @Override
  protected synchronized CDOView getView()
  {
    if (view == null)
    {
      view = workspace.openView();
    }

    return view;
  }

  @Override
  protected CDOResourceNode getResourceNode(CDOView view)
  {
    return view.getRootResource();
  }

  @Override
  protected boolean isDirectory(CDOResourceNode node)
  {
    return true;
  }

  @Override
  protected void collectChildNames(CDOResourceNode node, List<String> childNames)
  {
    childNames.add(CDOProjectDescriptionStore.DESCRIPTION_FILE_NAME);

    CDOResource rootResource = (CDOResource)node;
    for (EObject content : rootResource.getContents())
    {
      if (content instanceof CDOResourceNode)
      {
        CDOResourceNode child = (CDOResourceNode)content;
        childNames.add(child.getName());
      }
    }
  }

  /**
   * @author Eike Stepper
   */
  public final class SaveContext
  {
    private CDOTransaction transaction = workspace.openTransaction();

    private Map<String, InternalCDOObject> newObjects = new HashMap<String, InternalCDOObject>();

    private Map<String, List<ForwardReference>> forwardReferences = new HashMap<String, List<ForwardReference>>();

    private XMIResource xmiResource;

    public SaveContext()
    {
    }

    public void save(XMIResource xmiResource, String cdoPath)
    {
      this.xmiResource = xmiResource;

      try
      {
        CDOResource cdoResource = transaction.getResource(cdoPath);
        saveContents(xmiResource.getContents(), cdoResource.getContents());
      }
      finally
      {
        this.xmiResource = null;
        done();
      }
    }

    private void saveContents(EList<EObject> xmiContents, EList<EObject> cdoContents)
    {
      int size = xmiContents.size();
      for (int i = 0; i < size; i++)
      {
        EObject xmiObject = xmiContents.get(i);
        CDOObject cdoObject = getCDOObjectByXMIID(xmiObject);
        if (cdoObject == null)
        {
          cdoObject = createNewCDOObject(xmiObject);
          cdoContents.add(i, cdoObject);
        }
        else
        {
          int index = cdoContents.indexOf(cdoObject);
          if (index != -1)
          {
            cdoContents.move(i, index);
          }
          else
          {
            cdoContents.add(i, cdoObject);
          }
        }

        saveObject((InternalEObject)xmiObject, (InternalCDOObject)cdoObject);
      }

      shortenList(cdoContents, size);
    }

    private void saveObject(InternalEObject xmiObject, InternalCDOObject cdoObject)
    {
      CDOClassInfo classInfo = cdoObject.cdoRevision().getClassInfo();
      for (EStructuralFeature feature : classInfo.getAllPersistentFeatures())
      {
        Object xmiValue = xmiObject.eGet(feature);
        if (feature instanceof EReference)
        {
          EReference reference = (EReference)feature;
          if (reference.isContainment())
          {
            if (reference.isMany())
            {
              // Many-valued containment reference
              @SuppressWarnings("unchecked")
              EList<EObject> xmiContents = (EList<EObject>)xmiValue;

              @SuppressWarnings("unchecked")
              EList<EObject> cdoContents = (EList<EObject>)cdoObject.eGet(reference);

              saveContents(xmiContents, cdoContents);
            }
            else
            {
              // Single-valued containment reference
              InternalCDOObject cdoValue = getCDOObjectByXMIID((EObject)xmiValue);
              if (cdoValue == null)
              {
                cdoValue = createNewCDOObject((EObject)xmiValue);
              }

              cdoObject.eSet(reference, cdoValue);
              saveObject((InternalEObject)xmiValue, cdoValue);
            }
          }
          else
          {
            if (reference.isMany())
            {
              // Many-valued cross reference
              @SuppressWarnings("unchecked")
              EList<EObject> xmiElements = (EList<EObject>)xmiValue;

              @SuppressWarnings("unchecked")
              EList<EObject> cdoElements = (EList<EObject>)cdoObject.eGet(reference);

              int size = xmiElements.size();
              for (int i = 0; i < size; i++)
              {
                InternalEObject xmiElement = (InternalEObject)xmiElements.get(i);
                String href = xmiResource.getURIFragment(xmiElement);
                InternalCDOObject cdoElement = getCDOObjectByHREF(href);
                if (cdoElement == null)
                {
                  registerForwardReference(cdoObject, reference, i, href);

                  InternalCDOObject dummy = createNewCDOObject(xmiElement);
                  cdoElements.add(i, dummy);
                }
                else
                {
                  int index = cdoElements.indexOf(cdoElement);
                  if (index != -1)
                  {
                    cdoElements.move(i, index);
                  }
                  else
                  {
                    cdoElements.add(i, cdoElement);
                  }
                }
              }

              shortenList(cdoElements, size);
            }
            else
            {
              // Single-valued cross reference
              String href = xmiResource.getURIFragment((EObject)xmiValue);
              CDOObject cdoValue = getCDOObjectByHREF(href);
              if (cdoValue == null)
              {
                registerForwardReference(cdoObject, reference, -1, href);
              }

              cdoObject.eSet(reference, cdoValue);
            }
          }
        }
        else
        {
          EAttribute attribute = (EAttribute)feature;
          if (attribute.isMany())
          {
            // Many-valued attribute
            @SuppressWarnings("unchecked")
            EList<Object> xmiElements = (EList<Object>)xmiValue;

            @SuppressWarnings("unchecked")
            EList<Object> cdoElements = (EList<Object>)cdoObject.eGet(attribute);
            cdoElements.clear();

            int size = xmiElements.size();
            for (int i = 0; i < size; i++)
            {
              Object xmiElement = xmiElements.get(i);
              cdoElements.add(xmiElement);
            }
          }
          else
          {
            // Single-valued attribute
            cdoObject.eSet(attribute, xmiValue);
          }
        }
      }
    }

    private InternalCDOObject getCDOObjectByXMIID(EObject xmiObject)
    {
      String xmiID = xmiResource.getID(xmiObject);
      if (xmiID != null)
      {
        try
        {
          CDOID id = CDOIDUtil.read(xmiID);
          if (!CDOIDUtil.isNull(id))
          {
            return (InternalCDOObject)transaction.getObject(id);
          }
        }
        catch (Exception ex)
        {
          ex.printStackTrace();
          //$FALL-THROUGH$
        }
      }

      return null;
    }

    private InternalCDOObject createNewCDOObject(EObject xmiObject)
    {
      // Create new object
      EObject newInstance = EcoreUtil.create(xmiObject.eClass());
      InternalCDOObject cdoObject = (InternalCDOObject)CDOUtil.getCDOObject(newInstance);

      // Remember new object
      String fragment = xmiResource.getURIFragment(xmiObject);
      newObjects.put(fragment, cdoObject);

      return cdoObject;
    }

    private void shortenList(EList<EObject> list, int size)
    {
      int remove = list.size() - size;
      while (remove-- != 0)
      {
        list.remove(list.size() - 1);
      }
    }

    private InternalCDOObject getCDOObjectByHREF(String href)
    {
      InternalCDOObject cdoObject = null;

      try
      {
        CDOID id = CDOIDUtil.read(href);
        cdoObject = (InternalCDOObject)transaction.getObject(id);
      }
      catch (Exception ex)
      {
        //$FALL-THROUGH$
      }

      if (cdoObject == null)
      {
        cdoObject = newObjects.get(href);
      }

      return cdoObject;
    }

    private void done()
    {
      if (resolveForwardReferences())
      {
        commit();
      }
    }

    private void registerForwardReference(InternalCDOObject cdoObject, EReference reference, int index, String href)
    {
      List<ForwardReference> list = forwardReferences.get(href);
      if (list == null)
      {
        list = new ArrayList<ForwardReference>();
        forwardReferences.put(href, list);
      }

      list.add(new ForwardReference(cdoObject, reference, index));
    }

    private boolean resolveForwardReferences()
    {
      Set<Entry<String, List<ForwardReference>>> entrySet = forwardReferences.entrySet();
      for (Iterator<Entry<String, List<ForwardReference>>> it = entrySet.iterator(); it.hasNext();)
      {
        Entry<String, List<ForwardReference>> entry = it.next();
        String href = entry.getKey();

        InternalCDOObject target = getCDOObjectByHREF(href);
        if (target != null)
        {
          List<ForwardReference> list = entry.getValue();
          for (ForwardReference forwardReference : list)
          {
            forwardReference.resolve(target);
          }

          it.remove();
        }
      }

      return forwardReferences.isEmpty();
    }

    private void commit()
    {
      try
      {
        transaction.commit();
      }
      catch (CommitException ex)
      {
        throw WrappedException.wrap(ex);
      }
      finally
      {
        IOUtil.closeSilent(transaction);
        transaction = null;
        forwardReferences = null;
        newObjects = null;
        saveContext = null;
      }
    }

    /**
     * @author Eike Stepper
     */
    public final class ForwardReference
    {
      private final InternalCDOObject cdoObject;

      private final EReference reference;

      private final int index;

      public ForwardReference(InternalCDOObject cdoObject, EReference reference, int index)
      {
        this.cdoObject = cdoObject;
        this.reference = reference;
        this.index = index;
      }

      public void resolve(InternalCDOObject target)
      {
        if (reference.isMany())
        {
          @SuppressWarnings("unchecked")
          EList<EObject> list = (EList<EObject>)cdoObject.eGet(reference);
          list.set(index, target);
        }
        else
        {
          cdoObject.eSet(reference, target);
        }
      }
    }
  }
}

Back to the top