1994
|
1 |
/*
|
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
package com.nokia.carbide.internal.discovery.ui.p2;
|
|
18 |
|
|
19 |
import java.lang.reflect.InvocationTargetException;
|
|
20 |
import java.net.URI;
|
|
21 |
import java.util.ArrayList;
|
|
22 |
import java.util.Collection;
|
|
23 |
import java.util.HashSet;
|
|
24 |
import java.util.Set;
|
|
25 |
|
|
26 |
import org.eclipse.core.runtime.CoreException;
|
|
27 |
import org.eclipse.core.runtime.IProgressMonitor;
|
|
28 |
import org.eclipse.core.runtime.IStatus;
|
|
29 |
import org.eclipse.core.runtime.NullProgressMonitor;
|
|
30 |
import org.eclipse.core.runtime.OperationCanceledException;
|
|
31 |
import org.eclipse.core.runtime.SubMonitor;
|
|
32 |
import org.eclipse.equinox.p2.core.ProvisionException;
|
|
33 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit;
|
|
34 |
import org.eclipse.equinox.p2.operations.InstallOperation;
|
|
35 |
import org.eclipse.equinox.p2.operations.ProvisioningSession;
|
|
36 |
import org.eclipse.equinox.p2.operations.RepositoryTracker;
|
|
37 |
import org.eclipse.equinox.p2.query.IQuery;
|
|
38 |
import org.eclipse.equinox.p2.query.IQueryResult;
|
|
39 |
import org.eclipse.equinox.p2.query.IQueryable;
|
|
40 |
import org.eclipse.equinox.p2.query.QueryUtil;
|
|
41 |
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository;
|
|
42 |
import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager;
|
|
43 |
import org.eclipse.equinox.p2.ui.ProvisioningUI;
|
|
44 |
import org.eclipse.jface.operation.IRunnableWithProgress;
|
|
45 |
import org.eclipse.swt.widgets.Display;
|
|
46 |
|
|
47 |
import com.nokia.carbide.discovery.ui.Messages;
|
|
48 |
|
|
49 |
public class FeatureInstallOperation implements IRunnableWithProgress {
|
|
50 |
|
|
51 |
private Collection<URI> uris;
|
|
52 |
private Collection<FeatureInfo> featureInfos;
|
|
53 |
private boolean wantVersions;
|
|
54 |
private ProvisioningUI provisioningUI;
|
|
55 |
private Collection<IMetadataRepository> repositories;
|
|
56 |
private Collection<IInstallableUnit> ius;
|
|
57 |
private Collection<URI> urisUsed;
|
|
58 |
|
|
59 |
public FeatureInstallOperation(Collection<URI> uris, Collection<FeatureInfo> featureInfos, boolean wantVersions) {
|
|
60 |
this.uris = uris;
|
|
61 |
this.featureInfos = featureInfos;
|
|
62 |
this.wantVersions = wantVersions;
|
|
63 |
provisioningUI = ProvisioningUI.getDefaultUI();
|
|
64 |
}
|
|
65 |
|
|
66 |
@Override
|
|
67 |
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
|
|
68 |
try {
|
|
69 |
doInstall(monitor);
|
|
70 |
} catch (OperationCanceledException e) {
|
|
71 |
throw new InterruptedException();
|
|
72 |
} catch (CoreException e) {
|
|
73 |
throw new InvocationTargetException(e);
|
|
74 |
}
|
|
75 |
}
|
|
76 |
|
|
77 |
public void doInstall(IProgressMonitor monitor) throws OperationCanceledException, CoreException {
|
|
78 |
SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.FeatureInstallOperation_ConfiguringTaskName, 100);
|
|
79 |
getRepositories(subMonitor.newChild(30));
|
|
80 |
findInstallableUnits(subMonitor.newChild(40));
|
|
81 |
final InstallOperation operation = resolve(subMonitor.newChild(30));
|
|
82 |
Display.getDefault().asyncExec(new Runnable() {
|
|
83 |
@Override
|
|
84 |
public void run() {
|
|
85 |
IQueryable<IInstallableUnit> additions = operation.getProvisioningPlan().getAdditions();
|
|
86 |
IQueryResult<IInstallableUnit> result = additions.query(QueryUtil.createIUGroupQuery(), new NullProgressMonitor());
|
|
87 |
provisioningUI.openInstallWizard(result.toSet(), operation, null);
|
|
88 |
}
|
|
89 |
});
|
|
90 |
}
|
|
91 |
|
|
92 |
private void getRepositories(SubMonitor monitor) throws ProvisionException, OperationCanceledException {
|
|
93 |
monitor.setWorkRemaining(uris.size());
|
|
94 |
repositories = new ArrayList<IMetadataRepository>();
|
|
95 |
ProvisioningSession session = provisioningUI.getSession();
|
|
96 |
RepositoryTracker repositoryTracker = provisioningUI.getRepositoryTracker();
|
|
97 |
IMetadataRepositoryManager manager =
|
|
98 |
(IMetadataRepositoryManager) session.getProvisioningAgent().getService(IMetadataRepositoryManager.SERVICE_NAME);
|
|
99 |
for (URI uri : uris) {
|
|
100 |
checkIfCanceled(monitor);
|
|
101 |
repositoryTracker.addRepository(uri, null, session);
|
|
102 |
repositories.add(manager.loadRepository(uri, monitor.newChild(1)));
|
|
103 |
}
|
|
104 |
}
|
|
105 |
|
|
106 |
private void findInstallableUnits(SubMonitor monitor) throws ProvisionException {
|
|
107 |
monitor.setWorkRemaining(repositories.size() * featureInfos.size());
|
|
108 |
ius = new ArrayList<IInstallableUnit>();
|
|
109 |
urisUsed = new HashSet<URI>();
|
|
110 |
Set<FeatureInfo> remainingInfos = new HashSet<FeatureInfo>(featureInfos);
|
|
111 |
for (IMetadataRepository repository : repositories) {
|
|
112 |
checkIfCanceled(monitor);
|
|
113 |
IQueryResult<IInstallableUnit> iusInRepository = repository.query(QueryUtil.createIUGroupQuery(), monitor.newChild(1));
|
|
114 |
for (FeatureInfo featureInfo : new HashSet<FeatureInfo>(remainingInfos)) {
|
|
115 |
String id = featureInfo.getId();
|
|
116 |
IQuery<IInstallableUnit> iuQuery = wantVersions ?
|
|
117 |
QueryUtil.createIUQuery(id, featureInfo.getVersion()) :
|
|
118 |
QueryUtil.createLatestQuery(QueryUtil.createIUQuery(id));
|
|
119 |
IQueryResult<IInstallableUnit> result = iusInRepository.query(iuQuery, monitor.newChild(1));
|
|
120 |
if (!result.isEmpty()) {
|
|
121 |
ius.add(result.iterator().next());
|
|
122 |
urisUsed.add(repository.getLocation());
|
|
123 |
remainingInfos.remove(featureInfo);
|
|
124 |
if (remainingInfos.isEmpty())
|
|
125 |
break;
|
|
126 |
}
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
if (!remainingInfos.isEmpty()) {
|
|
131 |
StringBuilder sb = new StringBuilder();
|
|
132 |
sb.append(Messages.FeatureInstallOperation_MissingFeaturesError);
|
|
133 |
for (FeatureInfo featureInfo : remainingInfos) {
|
|
134 |
sb.append(Messages.FeatureInstallOperation_IdLabel);
|
|
135 |
sb.append(featureInfo.getId());
|
|
136 |
if (wantVersions) {
|
|
137 |
sb.append(Messages.FeatureInstallOperation_VersionLabel);
|
|
138 |
sb.append(featureInfo.getVersion().toString());
|
|
139 |
}
|
|
140 |
sb.append("\n"); //$NON-NLS-1$
|
|
141 |
}
|
|
142 |
throw new ProvisionException(sb.toString());
|
|
143 |
}
|
|
144 |
monitor.done();
|
|
145 |
}
|
|
146 |
|
|
147 |
private InstallOperation resolve(SubMonitor monitor) throws CoreException {
|
|
148 |
checkIfCanceled(monitor);
|
|
149 |
URI[] uris = (URI[]) urisUsed.toArray(new URI[urisUsed.size()]);
|
|
150 |
InstallOperation installOperation = provisioningUI.getInstallOperation(ius, uris);
|
|
151 |
IStatus operationStatus = installOperation.resolveModal(monitor);
|
|
152 |
if (operationStatus.getSeverity() > IStatus.WARNING) {
|
|
153 |
throw new CoreException(operationStatus);
|
|
154 |
}
|
|
155 |
return installOperation;
|
|
156 |
}
|
|
157 |
|
|
158 |
private void checkIfCanceled(IProgressMonitor monitor) {
|
|
159 |
if (monitor.isCanceled()) {
|
|
160 |
throw new OperationCanceledException();
|
|
161 |
}
|
|
162 |
}
|
|
163 |
}
|