Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 91b336e58ab9e6725356c1cef3dceacf548eb0c1 (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
/*******************************************************************************
 * Copyright (c) 2011 Sonatype, Inc.
 * 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:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.m2e.internal.discovery.operation;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.operations.InstallOperation;
import org.eclipse.equinox.p2.operations.ProfileModificationJob;
import org.eclipse.equinox.p2.operations.ProvisioningJob;
import org.eclipse.equinox.p2.operations.ProvisioningSession;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.m2e.core.ui.internal.wizards.AbstractCreateMavenProjectsOperation;
import org.eclipse.m2e.internal.discovery.startup.UpdateConfigurationStartup;


/*
 * This operation allows altering the restart policy for the ProvisioningJob returned from getProvisioningJob calls 
 */
public class RestartInstallOperation extends InstallOperation {

  private int restartPolicy;

  private final ProvisioningSession session;

  private final Collection<IInstallableUnit> toInstall;

  private final IRunnableWithProgress postInstallHook;

  private Collection<String> projectsToConfigure;

  public RestartInstallOperation(ProvisioningSession session, Collection<IInstallableUnit> toInstall,
      IRunnableWithProgress postInstallHook) {
    this(session, toInstall, postInstallHook, null, ProvisioningJob.RESTART_ONLY);
  }

  public RestartInstallOperation(ProvisioningSession session, Collection<IInstallableUnit> toInstall,
      IRunnableWithProgress postInstallHook, Collection<String> projectsToConfigure, int restartPolicy) {
    super(session, toInstall);
    this.session = session;
    this.toInstall = toInstall;
    this.postInstallHook = postInstallHook;
    this.projectsToConfigure = projectsToConfigure;
    this.restartPolicy = restartPolicy;
  }

  @Override
  public ProvisioningJob getProvisioningJob(IProgressMonitor monitor) {
    ProvisioningJob job = super.getProvisioningJob(monitor);
    if(job != null && job instanceof ProfileModificationJob) {
      ((ProfileModificationJob) job).setRestartPolicy(restartPolicy);
      UpdateMavenConfigurationProvisioningJob ucJob = new UpdateMavenConfigurationProvisioningJob(
          ((ProfileModificationJob) job), session, postInstallHook, projectsToConfigure);
      return ucJob;
    }
    return job;
  }

  public int getRestartPolicy() {
    return restartPolicy;
  }

  public void setRestartPolicy(int restartPolicy) {
    this.restartPolicy = restartPolicy;
  }

  public Collection<IInstallableUnit> getIUs() {
    return toInstall;
  }

  /*
   * Creates a shallow copy of this operation changing IUs to install. 
   */
  public RestartInstallOperation copy(Collection<IInstallableUnit> toInstall) {
    return new RestartInstallOperation(session, toInstall, postInstallHook, projectsToConfigure, restartPolicy);
  }

  /*
   * The ProfileModificationJob is wrapped to allow us to know when the job finishes successfully so we can 
   * ensure that early startup for update configuration is enabled.
   */
  private static class UpdateMavenConfigurationProvisioningJob extends ProfileModificationJob {

    private ProfileModificationJob job;

    private final IRunnableWithProgress postInstallHook;

    private Collection<String> projectsToConfigure;

    public UpdateMavenConfigurationProvisioningJob(ProfileModificationJob job, ProvisioningSession session,
        IRunnableWithProgress postInstallHook, Collection<String> projectsToConfigure) {
      super(job.getName(), session, job.getProfileId(), null, null);
      this.job = job;
      this.postInstallHook = postInstallHook;
      this.projectsToConfigure = projectsToConfigure;
    }

    @Override
    public IStatus runModal(IProgressMonitor monitor) {
      // install
      IStatus status = job.run(monitor);

      if (status.isOK() && postInstallHook != null) {
        try {
          postInstallHook.run(monitor);
        } catch(InvocationTargetException e) {
          // TODO need a better place for this helper
          return AbstractCreateMavenProjectsOperation.toStatus(e);
        } catch(InterruptedException e) {
          return Status.CANCEL_STATUS;
        }
      }

      if(status.isOK()) {
        // If the installation doesn't require a restart, launch the reconfiguration now.
        if(getRestartPolicy() == ProvisioningJob.RESTART_NONE) {
          UpdateConfigurationStartup.updateConfiguration();
        } else {
          UpdateConfigurationStartup.enableStartup(projectsToConfigure);
        }
      }
      return status;
    }

    @Override
    public String getProfileId() {
      return job.getProfileId();
    }

    @Override
    public int getRestartPolicy() {
      return job.getRestartPolicy();
    }
  }
}

Back to the top