Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b8f36b2e7deeb66adbe453e5377b25f65d3a4c51 (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
/*
 * Copyright (c) 2014-2017 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.installer;

import org.eclipse.oomph.p2.core.Agent;
import org.eclipse.oomph.p2.core.P2Util;
import org.eclipse.oomph.p2.core.Profile;
import org.eclipse.oomph.p2.core.ProfileTransaction;
import org.eclipse.oomph.p2.core.ProfileTransaction.Resolution;
import org.eclipse.oomph.p2.internal.core.CachingRepositoryManager;
import org.eclipse.oomph.setup.User;
import org.eclipse.oomph.setup.internal.core.util.SetupCoreUtil;
import org.eclipse.oomph.setup.ui.SelfCommitContext;
import org.eclipse.oomph.ui.ErrorDialog;
import org.eclipse.oomph.ui.UICallback;
import org.eclipse.oomph.util.ExceptionHandler;
import org.eclipse.oomph.util.IRunnable;
import org.eclipse.oomph.util.OomphPlugin;
import org.eclipse.oomph.util.PropertiesUtil;
import org.eclipse.oomph.util.StringUtil;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.equinox.p2.query.QueryUtil;
import org.eclipse.swt.widgets.Shell;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import java.lang.reflect.InvocationTargetException;

/**
 * @author Eike Stepper
 */
public class SelfUpdate
{
  public static final String SELF_HOSTING = "Self Hosting";

  public static String getProductVersion()
  {
    Agent agent = P2Util.getAgentManager().getCurrentAgent();

    Profile profile = agent.getCurrentProfile();
    if (profile == null || profile.isSelfHosting())
    {
      return SELF_HOSTING;
    }

    String firstBuildID = null;
    int highestBuildID = 0;

    BundleContext bundleContext = SetupInstallerPlugin.INSTANCE.getBundleContext();
    for (Bundle bundle : bundleContext.getBundles())
    {
      String symbolicName = bundle.getSymbolicName();
      if (symbolicName.startsWith(SetupCoreUtil.OOMPH_NAMESPACE))
      {
        String buildID = OomphPlugin.getBuildID(bundle);
        if (buildID != null)
        {
          if (firstBuildID == null)
          {
            firstBuildID = buildID;
          }

          try
          {
            int id = Integer.parseInt(buildID);
            if (id > highestBuildID)
            {
              highestBuildID = id;
            }
          }
          catch (NumberFormatException ex)
          {
            //$FALL-THROUGH$
          }
        }
      }
    }

    String buildID = highestBuildID != 0 ? Integer.toString(highestBuildID) : firstBuildID;

    for (IInstallableUnit iu : P2Util.asIterable(profile.query(QueryUtil.createIUQuery(PropertiesUtil.getProductID()), null)))
    {
      String label;

      Version version = iu.getVersion();
      if (buildID != null && version.getSegmentCount() > 3)
      {
        label = version.getSegment(0) + "." + version.getSegment(1) + "." + version.getSegment(2);
      }
      else
      {
        label = version.toString();
      }

      if (buildID != null)
      {
        label += " Build " + buildID;
      }

      return label;
    }

    return null;
  }

  public static Resolution resolve(User user, IProgressMonitor monitor) throws CoreException
  {
    Agent agent = P2Util.getAgentManager().getCurrentAgent();
    Profile profile = agent.getCurrentProfile();
    if (profile == null)
    {
      return null;
    }

    ProfileTransaction transaction = profile.change();

    SelfCommitContext commitContext = new SelfCommitContext(user);
    transaction = commitContext.migrateProfile(transaction);

    return transaction.resolve(commitContext, monitor);
  }

  public static void update(Shell shell, final Resolution resolution, final Runnable successRunnable, final ExceptionHandler<CoreException> exceptionHandler,
      final Runnable finalRunnable)
  {
    String shellText = shell.getText();
    if (!StringUtil.isEmpty(shellText))
    {
      shellText += " ";
    }

    final UICallback callback = new UICallback(shell, shellText + "Update");
    callback.runInProgressDialog(false, new IRunnable()
    {
      public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException
      {
        boolean originalBetterMirrorSelection = CachingRepositoryManager.enableBetterMirrorSelection();

        try
        {
          resolution.commit(monitor);

          callback.execInUI(true, new Runnable()
          {
            public void run()
            {
              callback.information(false, "Updates were installed. Press OK to restart.");

              if (successRunnable != null)
              {
                successRunnable.run();
              }
            }
          });
        }
        catch (OperationCanceledException ex)
        {
          // Ignore.
        }
        catch (final CoreException ex)
        {
          if (exceptionHandler != null)
          {
            exceptionHandler.handleException(ex);
          }
          else
          {
            ErrorDialog.open(ex);
          }
        }
        finally
        {
          CachingRepositoryManager.setBetterMirrorSelection(originalBetterMirrorSelection);

          if (finalRunnable != null)
          {
            finalRunnable.run();
          }
        }
      }
    });
  }
}

Back to the top