Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ec7125d5a46eca0f8d8ba12d982d7647d24b7822 (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
/*
 * Copyright (c) 2014-2016 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 v2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v20.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.oomph.setup.internal.core;

import org.eclipse.oomph.base.util.BaseUtil;
import org.eclipse.oomph.internal.setup.SetupProperties;
import org.eclipse.oomph.setup.Index;
import org.eclipse.oomph.setup.Installation;
import org.eclipse.oomph.setup.LocationCatalog;
import org.eclipse.oomph.setup.Product;
import org.eclipse.oomph.setup.ProductCatalog;
import org.eclipse.oomph.setup.ProductVersion;
import org.eclipse.oomph.setup.SetupFactory;
import org.eclipse.oomph.setup.SetupPackage;
import org.eclipse.oomph.setup.Stream;
import org.eclipse.oomph.setup.User;
import org.eclipse.oomph.setup.Workspace;
import org.eclipse.oomph.setup.impl.InstallationTaskImpl;
import org.eclipse.oomph.setup.internal.core.util.IndexManager;
import org.eclipse.oomph.setup.internal.core.util.SetupCoreUtil;
import org.eclipse.oomph.setup.internal.core.util.URIResolver;
import org.eclipse.oomph.util.IORuntimeException;
import org.eclipse.oomph.util.IOUtil;
import org.eclipse.oomph.util.OS;
import org.eclipse.oomph.util.PropertiesUtil;

import org.eclipse.emf.common.CommonPlugin;
import org.eclipse.emf.common.util.ECollections;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.plugin.EcorePlugin;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.eclipse.emf.ecore.resource.URIHandler;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.ecore.util.InternalEList;

import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.osgi.service.datalocation.Location;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author Eike Stepper
 */
public class SetupContext
{
  public static final boolean USE_RESOURCES_BUNDLE = PropertiesUtil.getProperty(SetupProperties.PROP_DO_NOT_LOAD_RESOURCES_PLUGIN) == null
      && CommonPlugin.IS_RESOURCES_BUNDLE_AVAILABLE;

  public static final String OOMPH_NODE = "org.eclipse.oomph.setup";

  public static final String LOG_FILE_NAME = "setup.log";

  public static final String USER_SCHEME = "user";

  public static boolean isUserScheme(String scheme)
  {
    return USER_SCHEME.equals(scheme) || "user-ext".equals(scheme);
  }

  /**
   * Resolves using the appropriate URI handler's implementation.
   * E.g., it resolves a '{@link #USER_SCHEME user}' scheme URI to a 'file' scheme URI.
   */
  public static URI resolve(URI uri)
  {
    for (URIHandler uriHandler : SetupCoreUtil.URI_CONVERTER.getURIHandlers())
    {
      if (uriHandler.canHandle(uri))
      {
        if (uriHandler instanceof URIResolver)
        {
          URIResolver uriResolver = (URIResolver)uriHandler;
          return uriResolver.resolve(uri);
        }

        break;
      }
    }

    return uri;
  }

  // Basic locations

  public static final URI CONFIGURATION_LOCATION_URI = getStaticConfigurationLocation();

  public static final URI WORKSPACE_LOCATION_URI = USE_RESOURCES_BUNDLE ? WorkspaceUtil.getStaticWorkspaceLocationURI() : null;

  public static final URI PRODUCT_LOCATION = getStaticInstallLocation();

  public static final URI PRODUCT_ROOT_LOCATION = PRODUCT_LOCATION.trimSegments(OS.INSTANCE.isMac() ? 3 : 1);

  // State locations

  public static final URI GLOBAL_STATE_LOCATION_URI = URI.createFileURI(PropertiesUtil.getUserHome()).appendSegments(new String[] { ".eclipse", OOMPH_NODE });

  public static final URI GLOBAL_SETUPS_URI = URI.createURI(USER_SCHEME + ":/");

  public static final URI GLOBAL_SETUPS_LOCATION_URI = GLOBAL_STATE_LOCATION_URI.appendSegment("setups");

  public static final URI CONFIGURATION_STATE_LOCATION_URI = CONFIGURATION_LOCATION_URI.appendSegment(OOMPH_NODE);

  public static final URI WORKSPACE_STATE_LOCATION_URI = WORKSPACE_LOCATION_URI == null ? null
      : WORKSPACE_LOCATION_URI.appendSegments(new String[] { ".metadata", ".plugins", OOMPH_NODE });

  // Resource locations

  public static final URI SETUP_LOG_URI = CONFIGURATION_STATE_LOCATION_URI.appendSegment(LOG_FILE_NAME);

  public static final String INDEX_SETUP_NAME = "org.eclipse.setup";

  public static final URI INDEX_ROOT_URI = URI.createURI("index:/");

  public static final URI INDEX_SETUP_URI = INDEX_ROOT_URI.appendSegment(INDEX_SETUP_NAME);

  public static final URI INDEX_ROOT_LOCATION_URI = URI.createURI("http://git.eclipse.org/c/oomph/org.eclipse.oomph.git/plain/setups/");

  public static final URI INDEX_SETUP_LOCATION_URI = INDEX_ROOT_LOCATION_URI.trimSegments(1).appendSegment(INDEX_SETUP_NAME);

  public static final URI INDEX_SETUP_ARCHIVE_LOCATION_URI = URI.createURI("http://www.eclipse.org/setups/setups.zip");

  public static final URI INSTALLATION_SETUP_FILE_NAME_URI = URI.createURI("installation.setup");

  public static final URI INSTALLATION_SETUP_URI = CONFIGURATION_STATE_LOCATION_URI.appendSegment(INSTALLATION_SETUP_FILE_NAME_URI.lastSegment());

  public static final URI WORKSPACE_SETUP_FILE_NAME_URI = URI.createURI("workspace.setup");

  public static final URI WORKSPACE_SETUP_URI = WORKSPACE_STATE_LOCATION_URI == null ? null
      : WORKSPACE_STATE_LOCATION_URI.appendSegment(WORKSPACE_SETUP_FILE_NAME_URI.lastSegment());

  public static final URI WORKSPACE_SETUP_RELATIVE_URI = URI
      .createHierarchicalURI(new String[] { ".metadata", ".plugins", OOMPH_NODE, WORKSPACE_SETUP_FILE_NAME_URI.lastSegment() }, null, null);

  public static final URI USER_SETUP_URI = GLOBAL_SETUPS_URI.appendSegment("user.setup");

  public static final URI USER_SETUP_LOCATION_URI = GLOBAL_SETUPS_LOCATION_URI.appendSegment("user.setup");

  public static final URI CATALOG_SELECTION_SETUP_URI = GLOBAL_SETUPS_URI.appendSegment("catalogs.setup");

  public static final URI CATALOG_SELECTION_SETUP_LOCATION_URI = GLOBAL_SETUPS_LOCATION_URI.appendSegment("catalogs.setup");

  public static final URI LOCATION_CATALOG_SETUP_URI = GLOBAL_SETUPS_URI.appendSegment("locations.setup");

  private static volatile SetupContext self = new SetupContext();

  // static creation methods

  // private constructors

  private final Installation installation;

  private final Workspace workspace;

  private final User user;

  private SetupContext()
  {
    installation = createInstallation();
    ((InternalEObject)installation).eSetProxyURI(INSTALLATION_SETUP_URI.appendFragment("/"));

    workspace = createWorkspace();
    if (WORKSPACE_SETUP_URI != null)
    {
      ((InternalEObject)workspace).eSetProxyURI(WORKSPACE_SETUP_URI.appendFragment("/"));
    }

    user = createUser();
    ((InternalEObject)user).eSetProxyURI(USER_SETUP_URI.appendFragment("/"));
  }

  private SetupContext(Installation installation, Workspace workspace, User user)
  {
    this.installation = installation;
    this.workspace = workspace;
    this.user = user;
  }

  public Installation getInstallation()
  {
    return installation;
  }

  public Workspace getWorkspace()
  {
    return workspace;
  }

  public User getUser()
  {
    return user;
  }

  /**
   * Returns a setup context consisting purely of proxy instances for the current self-hosted installation.
   */
  public static SetupContext getSelf()
  {
    return self;
  }

  public static void setSelf(SetupContext setupContext)
  {
    self = setupContext;
  }

  // static creation methods

  public static SetupContext createSelf(Installation installation, Workspace workspace, User user)
  {
    return proxify(installation, workspace, user);
  }

  public static SetupContext createSelf(ResourceSet resourceSet)
  {
    Resource indexResource = resourceSet.getResource(INDEX_SETUP_URI, false);
    if (indexResource != null)
    {
      Index index = (Index)EcoreUtil.getObjectByType(indexResource.getContents(), SetupPackage.Literals.INDEX);
      if (index != null)
      {
        new IndexManager().addIndex(index);
      }
    }

    Installation installation = getInstallation(resourceSet, true, Mode.CREATE_AND_SAVE);
    Workspace workspace = getWorkspace(resourceSet, true, Mode.CREATE_AND_SAVE);

    Installation effectiveInstallation = null;
    URI uri = installation.eResource().getURI();
    if (uri.segmentCount() > 3)
    {
      String eclipseLauncher = PropertiesUtil.getProperty("eclipse.launcher");
      if (eclipseLauncher != null)
      {
        File eclipseLauncherExecutable = new File(eclipseLauncher);
        if (eclipseLauncherExecutable.exists())
        {
          effectiveInstallation = installation;
        }
      }
    }

    SetupContext.associate(effectiveInstallation, workspace);

    User user = getUser(resourceSet, false);
    return createSelf(installation, workspace, user);
  }

  public static SetupContext createInstallationAndUser(ResourceSet resourceSet)
  {
    return new SetupContext(getInstallation(resourceSet, true, Mode.CREATE), null, getUser(resourceSet, true));
  }

  public static SetupContext create(ResourceSet resourceSet)
  {
    return new SetupContext(getInstallation(resourceSet, true, Mode.CREATE), getWorkspace(resourceSet, true, Mode.CREATE), getUser(resourceSet, true));
  }

  public static SetupContext createInstallationWorkspaceAndUser(ResourceSet resourceSet)
  {
    return new SetupContext(getInstallation(resourceSet, false, Mode.NONE), getWorkspace(resourceSet, false, Mode.NONE), getUser(resourceSet, false));
  }

  public static SetupContext createUserOnly(ResourceSet resourceSet)
  {
    return new SetupContext(null, null, getUser(resourceSet, true));
  }

  public static SetupContext create(ResourceSet resourceSet, ProductVersion productVersion)
  {
    Installation installation = getInstallation(resourceSet, false, Mode.CREATE);
    installation.setProductVersion(productVersion);
    return new SetupContext(installation, getWorkspace(resourceSet, false, Mode.NONE), getUser(resourceSet, true));
  }

  public static SetupContext create(ProductVersion productVersion, Stream stream)
  {
    User user = createUser();

    Workspace workspace = createWorkspace();
    if (stream != null)
    {
      workspace.getStreams().add(stream);
    }

    Installation installation = createInstallation();
    installation.setProductVersion(productVersion);

    return new SetupContext(installation, workspace, user);
  }

  public static SetupContext create(Installation installation, Collection<? extends Stream> streams, User user)
  {
    Workspace workspace = getWorkspace(installation.eResource().getResourceSet(), false, Mode.CREATE);
    workspace.getStreams().addAll(streams);
    return new SetupContext(installation, workspace, user);
  }

  public static SetupContext create()
  {
    return new SetupContext();
  }

  public static SetupContext create(Installation installation, Workspace workspace, User user)
  {
    return new SetupContext(installation, workspace, user);
  }

  public static SetupContext create(Installation installation, Workspace workspace)
  {
    User user = createUser();
    Resource userResource = installation.eResource().getResourceSet().getResourceFactoryRegistry().getFactory(USER_SETUP_URI).createResource(USER_SETUP_URI);
    userResource.getContents().add(user);
    return new SetupContext(installation, workspace, user);
  }

  public static SetupContext createCopy(Installation installation, Workspace workspace, User user)
  {
    Resource.Factory.Registry resourceFactoryRegistry = SetupCoreUtil.createResourceSet().getResourceFactoryRegistry();

    Installation copiedInstallation = EcoreUtil.copy(installation);
    Resource installationResource = resourceFactoryRegistry.getFactory(INSTALLATION_SETUP_FILE_NAME_URI).createResource(INSTALLATION_SETUP_FILE_NAME_URI);
    installationResource.getContents().add(copiedInstallation);

    Workspace copiedWorkspace = EcoreUtil.copy(workspace);
    if (workspace != null)
    {
      Resource workResource = resourceFactoryRegistry.getFactory(WORKSPACE_SETUP_FILE_NAME_URI).createResource(WORKSPACE_SETUP_FILE_NAME_URI);
      workResource.getContents().add(copiedWorkspace);
    }

    User copiedUser = EcoreUtil.copy(user);
    Resource userResource = resourceFactoryRegistry.getFactory(USER_SETUP_URI).createResource(USER_SETUP_URI);
    userResource.getContents().add(copiedUser);

    return new SetupContext(copiedInstallation, copiedWorkspace, copiedUser);
  }

  public static User createUser()
  {
    User user = SetupFactory.eINSTANCE.createUser();
    user.setName(PropertiesUtil.getProperty("user.name", "user"));
    return user;
  }

  public static Workspace createWorkspace()
  {
    Workspace workspace = SetupFactory.eINSTANCE.createWorkspace();
    workspace.setName("workspace");
    return workspace;
  }

  public static Installation createInstallation()
  {
    Installation installation = SetupFactory.eINSTANCE.createInstallation();
    installation.setName("installation");
    return installation;
  }

  public static void associate(final Installation installation, final Workspace workspace)
  {
    try
    {
      final ResourceSet resourceSet = SetupCoreUtil.createResourceSet();
      URIConverter uriConverter = resourceSet.getURIConverter();
      BaseUtil.execute(5000, new Runnable()
      {
        public void run()
        {
          associate(resourceSet, installation, workspace);
        }
      }, uriConverter, LOCATION_CATALOG_SETUP_URI, installation == null ? null : installation.eResource().getURI(),
          workspace == null ? null : workspace.eResource().getURI());
    }
    catch (Throwable throwable)
    {
      SetupCorePlugin.INSTANCE.log(throwable, IStatus.WARNING);
    }
  }

  public static LocationCatalog getLocationCatalog(ResourceSet resourceSet)
  {
    return associate(resourceSet, null, null);
  }

  private static LocationCatalog associate(ResourceSet resourceSet, Installation installation, Workspace workspace)
  {
    URIConverter uriConverter = resourceSet.getURIConverter();

    Resource resource;
    if (uriConverter.exists(LOCATION_CATALOG_SETUP_URI, null))
    {
      resource = BaseUtil.loadResourceSafely(resourceSet, LOCATION_CATALOG_SETUP_URI);
    }
    else
    {
      resource = resourceSet.createResource(LOCATION_CATALOG_SETUP_URI);
    }

    EList<EObject> contents = resource.getContents();
    LocationCatalog locationCatalog = (LocationCatalog)EcoreUtil.getObjectByType(contents, SetupPackage.Literals.LOCATION_CATALOG);
    if (locationCatalog == null)
    {
      locationCatalog = SetupFactory.eINSTANCE.createLocationCatalog();
      contents.add(locationCatalog);
    }

    EMap<Installation, EList<Workspace>> installations = locationCatalog.getInstallations();
    removeProxies(installations);

    EMap<Workspace, EList<Installation>> workspaces = locationCatalog.getWorkspaces();
    removeProxies(workspaces);

    Installation localInstallation = installation == null ? null : (Installation)resourceSet.getEObject(EcoreUtil.getURI(installation), true);
    Workspace localWorkspace = workspace == null ? null : (Workspace)resourceSet.getEObject(EcoreUtil.getURI(workspace), true);

    if (installation != null)
    {
      int installationEntryIndex = installations.indexOfKey(localInstallation);
      if (installationEntryIndex == -1)
      {
        installations.put(localInstallation, localWorkspace == null ? ECollections.<Workspace> emptyEList() : ECollections.singletonEList(localWorkspace));
        installations.move(0, installations.size() - 1);
      }
      else if (localWorkspace != null)
      {
        EList<Workspace> associatedWorkspaces = installations.get(installationEntryIndex).getValue();
        int position = associatedWorkspaces.indexOf(localWorkspace);
        if (position == -1)
        {
          associatedWorkspaces.add(0, localWorkspace);
        }
        else
        {
          associatedWorkspaces.move(0, position);
        }

        installations.move(0, installationEntryIndex);
      }
    }

    if (localWorkspace != null)
    {
      int workspaceEntryIndex = workspaces.indexOfKey(localWorkspace);
      if (workspaceEntryIndex == -1)
      {
        workspaces.put(localWorkspace, localInstallation == null ? ECollections.<Installation> emptyEList() : ECollections.singletonEList(localInstallation));
        workspaces.move(0, workspaces.size() - 1);
      }
      else if (localInstallation != null)
      {
        EList<Installation> associatedInstallations = workspaces.get(workspaceEntryIndex).getValue();
        int position = associatedInstallations.indexOf(localInstallation);
        if (position == -1)
        {
          associatedInstallations.add(0, localInstallation);
        }
        else
        {
          associatedInstallations.move(0, position);
        }

        workspaces.move(0, workspaceEntryIndex);
      }
    }

    try
    {
      resource.save(Collections.singletonMap(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER));
    }
    catch (IOException ex)
    {
      SetupCorePlugin.INSTANCE.log(ex);
    }

    return locationCatalog;
  }

  private static <K extends EObject, V extends EObject> void removeProxies(EMap<K, EList<V>> map)
  {
    for (Iterator<Entry<K, EList<V>>> it = map.iterator(); it.hasNext();)
    {
      Entry<K, EList<V>> entry = it.next();
      K key = entry.getKey();
      if (key == null || key.eIsProxy())
      {
        it.remove();
      }
      else
      {
        for (Iterator<V> it2 = entry.getValue().iterator(); it2.hasNext();)
        {
          V value = it2.next();
          if (value.eIsProxy())
          {
            it2.remove();
          }
        }
      }
    }
  }

  private static URI getStaticInstallLocation()
  {
    try
    {
      if (!Platform.isRunning())
      {
        return URI.createFileURI(File.createTempFile("installation", "").toString());
      }

      Location location = Platform.getInstallLocation();
      URI result = getURI(location);
      if (OS.INSTANCE.isMac())
      {
        result = result.trimSegments(1).appendSegment("Eclipse");
      }

      return result;
    }
    catch (IOException ex)
    {
      throw new RuntimeException(ex);
    }
  }

  private static URI getURI(Location location) throws IOException
  {
    URI result = URI.createURI(FileLocator.resolve(location.getURL()).toString());
    if (result.isFile())
    {
      result = URI.createFileURI(result.toFileString());
    }

    return result.hasTrailingPathSeparator() ? result.trimSegments(1) : result;
  }

  private static URI getStaticConfigurationLocation()
  {
    try
    {
      if (!Platform.isRunning())
      {
        return URI.createFileURI(File.createTempFile(InstallationTaskImpl.CONFIGURATION_FOLDER_NAME, "").toString());
      }

      Location location = Platform.getConfigurationLocation();
      URI result = getURI(location);

      Location parentLocation = location.getParentLocation();
      if (parentLocation != null)
      {
        URI targetInstallation = result.appendSegment(OOMPH_NODE).appendSegment("installation.setup");
        File target = new File(targetInstallation.toFileString());
        if (!target.exists())
        {
          URI parentURI = getURI(parentLocation);
          URI sourceInstallation = parentURI.appendSegment(OOMPH_NODE).appendSegment("installation.setup");
          File source = new File(sourceInstallation.toFileString());
          if (source.exists())
          {
            try
            {
              IOUtil.copyFile(source, target);
            }
            catch (IORuntimeException ex)
            {
              // Ignore.
            }
          }
        }
      }

      return result;
    }
    catch (IOException ex)
    {
      throw new IORuntimeException(ex);
    }
  }

  private enum Mode
  {
    NONE, CREATE, CREATE_AND_SAVE
  }

  private static Installation getInstallation(ResourceSet resourceSet, boolean realInstallation, Mode mode)
  {
    Installation installation = null;
    Resource installationResource = null;
    if (realInstallation)
    {
      if (resourceSet.getURIConverter().exists(INSTALLATION_SETUP_URI, null))
      {
        installationResource = BaseUtil.loadResourceSafely(resourceSet, INSTALLATION_SETUP_URI);
        installation = (Installation)EcoreUtil.getObjectByType(installationResource.getContents(), SetupPackage.Literals.INSTALLATION);
      }
    }
    else
    {
      installationResource = resourceSet.getResource(INSTALLATION_SETUP_FILE_NAME_URI, false);
      if (installationResource == null)
      {
        installationResource = resourceSet.getResource(INSTALLATION_SETUP_URI, false);
      }

      if (installationResource != null)
      {
        EList<EObject> contents = installationResource.getContents();
        if (contents.isEmpty())
        {
          // If it's not in the resource, and we're not going to create it and save it, then make sure it exists.
          if (mode == Mode.NONE)
          {
            installation = createInstallation();
            contents.add(installation);
          }
        }
        else
        {
          installation = (Installation)contents.get(0);
        }
      }
    }

    boolean save = false;
    if (installation == null && mode != Mode.NONE)
    {
      if (installationResource == null)
      {
        installationResource = resourceSet.createResource(realInstallation ? INSTALLATION_SETUP_URI : INSTALLATION_SETUP_FILE_NAME_URI);
      }
      else
      {
        installationResource.unload();
      }

      installation = createInstallation();
      installationResource.getContents().add(installation);

      save = mode == Mode.CREATE_AND_SAVE;
    }

    if (realInstallation && installation != null && installation.getProductVersion() == null)
    {
      ProductCatalog productCatalog = null;

      Resource indexResource = BaseUtil.loadResourceSafely(resourceSet, INDEX_SETUP_URI);
      Index index = (Index)EcoreUtil.getObjectByType(indexResource.getContents(), SetupPackage.Literals.INDEX);
      if (index != null)
      {
        EList<ProductCatalog> productCatalogs = index.getProductCatalogs();
        if (!productCatalogs.isEmpty())
        {
          productCatalog = productCatalogs.get(0);
        }
      }

      if (productCatalog == null || productCatalog.getProducts().isEmpty() || productCatalog.getProducts().get(0).getVersions().isEmpty())
      {
        Resource selfProductCatalogResource = BaseUtil.loadResourceSafely(resourceSet, URI.createURI("catalog:/self-product-catalog.setup"));
        productCatalog = (ProductCatalog)EcoreUtil.getObjectByType(selfProductCatalogResource.getContents(), SetupPackage.Literals.PRODUCT_CATALOG);
      }

      EList<Product> products = productCatalog.getProducts();
      EList<ProductVersion> versions = products.get(0).getVersions();
      installation.setProductVersion(versions.get(0));

      if (save)
      {
        BaseUtil.saveEObject(installation);
      }
    }

    return installation;
  }

  private static Workspace getWorkspace(ResourceSet resourceSet, boolean realWorkspace, Mode mode)
  {
    Resource workspaceResource = null;
    Workspace workspace = null;
    if (realWorkspace)
    {
      if (resourceSet.getURIConverter().exists(WORKSPACE_SETUP_URI, null))
      {
        workspaceResource = BaseUtil.loadResourceSafely(resourceSet, WORKSPACE_SETUP_URI);
        workspace = (Workspace)EcoreUtil.getObjectByType(workspaceResource.getContents(), SetupPackage.Literals.WORKSPACE);
      }
    }
    else
    {
      workspaceResource = resourceSet.getResource(WORKSPACE_SETUP_FILE_NAME_URI, false);
      if (workspaceResource == null && WORKSPACE_SETUP_URI != null)
      {
        workspaceResource = resourceSet.getResource(WORKSPACE_SETUP_URI, false);
      }

      if (workspaceResource != null)
      {
        EList<EObject> contents = workspaceResource.getContents();
        if (contents.isEmpty())
        {
          // If it's not in the resource, and we're not going to create it and save it, then make sure it exists.
          if (mode == Mode.NONE)
          {
            workspace = createWorkspace();
            contents.add(workspace);
          }

        }
        else
        {
          workspace = (Workspace)workspaceResource.getContents().get(0);
        }
      }
    }

    if (workspace == null && mode != Mode.NONE)
    {
      if (workspaceResource == null)
      {
        workspaceResource = resourceSet.createResource(realWorkspace ? WORKSPACE_SETUP_URI : WORKSPACE_SETUP_FILE_NAME_URI);
      }
      else
      {
        workspaceResource.unload();
      }

      workspace = createWorkspace();
      workspaceResource.getContents().add(workspace);

      if (mode == Mode.CREATE_AND_SAVE)
      {
        BaseUtil.saveEObject(workspace);
      }
    }

    return workspace;
  }

  private static User getUser(ResourceSet resourceSet, boolean demandCreate)
  {
    Resource userResource = null;
    User user = null;
    if (resourceSet.getURIConverter().exists(USER_SETUP_URI, null))
    {
      userResource = BaseUtil.loadResourceSafely(resourceSet, USER_SETUP_URI);
      user = (User)EcoreUtil.getObjectByType(userResource.getContents(), SetupPackage.Literals.USER);
    }

    if (user == null && demandCreate)
    {
      if (userResource == null)
      {
        userResource = resourceSet.createResource(USER_SETUP_URI);
      }
      else
      {
        userResource.unload();
      }

      user = createUser();
      userResource.getContents().add(user);

      try
      {
        userResource.save(Collections.singletonMap(Resource.OPTION_SAVE_ONLY_IF_CHANGED, Resource.OPTION_SAVE_ONLY_IF_CHANGED_MEMORY_BUFFER));
      }
      catch (IOException ex)
      {
        throw new IORuntimeException(ex);
      }
    }

    return user;
  }

  private static SetupContext proxify(Installation installation, Workspace workspace, User user)
  {
    EcoreUtil.Copier copier = new EcoreUtil.Copier(false, false)
    {
      private static final long serialVersionUID = 1L;

      @Override
      protected void copyContainment(EReference eReference, EObject eObject, EObject copyEObject)
      {
      }

      @Override
      protected void copyProxyURI(EObject eObject, EObject copyEObject)
      {
        ((InternalEObject)copyEObject).eSetProxyURI(EcoreUtil.getURI(eObject));
      }
    };

    Installation installationCopy = (Installation)copier.copy(installation);
    if (installation != null)
    {
      copier.copy(installation.getProductVersion());
    }

    Workspace workspaceCopy = (Workspace)copier.copy(workspace);
    if (workspace != null)
    {
      copier.copyAll(workspace.getStreams());
    }

    User userCopy = (User)copier.copy(user);

    for (Map.Entry<EObject, EObject> entry : new LinkedHashSet<Map.Entry<EObject, EObject>>(copier.entrySet()))
    {
      EObject eObject = entry.getKey();
      EObject copyEObject = entry.getValue();
      for (EObject eContainer = eObject.eContainer(); eContainer != null; eContainer = eContainer.eContainer())
      {
        EObject copyEContainer = copier.get(eContainer);
        if (copyEContainer == null)
        {
          copyEContainer = copier.copy(eContainer);
        }

        @SuppressWarnings("unchecked")
        InternalEList<EObject> list = (InternalEList<EObject>)copyEContainer.eGet(eObject.eContainmentFeature());
        list.addUnique(copyEObject);

        eObject = eContainer;
        copyEObject = copyEContainer;
      }
    }

    copier.copyReferences();

    return new SetupContext(installationCopy, workspaceCopy, userCopy);
  }

  /**
   * @author Eike Stepper
   */
  private static class WorkspaceUtil
  {
    private static URI getStaticWorkspaceLocationURI()
    {
      try
      {
        IWorkspaceRoot workspaceRoot = EcorePlugin.getWorkspaceRoot();
        return URI.createFileURI(workspaceRoot.getLocation().toOSString());
      }
      catch (Throwable throwable)
      {
        return null;
      }
    }
  }
}

Back to the top