|
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.wizard; |
|
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.OperationCanceledException; |
|
30 import org.eclipse.core.runtime.SubMonitor; |
|
31 import org.eclipse.equinox.p2.core.ProvisionException; |
|
32 import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
|
33 import org.eclipse.equinox.p2.operations.InstallOperation; |
|
34 import org.eclipse.equinox.p2.operations.ProvisioningSession; |
|
35 import org.eclipse.equinox.p2.operations.RepositoryTracker; |
|
36 import org.eclipse.equinox.p2.query.IQuery; |
|
37 import org.eclipse.equinox.p2.query.IQueryResult; |
|
38 import org.eclipse.equinox.p2.query.QueryUtil; |
|
39 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; |
|
40 import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; |
|
41 import org.eclipse.equinox.p2.ui.ProvisioningUI; |
|
42 import org.eclipse.jface.operation.IRunnableWithProgress; |
|
43 import org.eclipse.swt.widgets.Display; |
|
44 |
|
45 import com.nokia.carbide.discovery.ui.Messages; |
|
46 |
|
47 public class FeatureInstallOperation implements IRunnableWithProgress { |
|
48 |
|
49 private Collection<URI> uris; |
|
50 private Collection<FeatureInfo> featureInfos; |
|
51 private boolean wantVersions; |
|
52 private ProvisioningUI provisioningUI; |
|
53 private Collection<IMetadataRepository> repositories; |
|
54 private Collection<IInstallableUnit> ius; |
|
55 private Collection<URI> urisUsed; |
|
56 |
|
57 public FeatureInstallOperation(Collection<URI> uris, Collection<FeatureInfo> featureInfos, boolean wantVersions) { |
|
58 this.uris = uris; |
|
59 this.featureInfos = featureInfos; |
|
60 this.wantVersions = wantVersions; |
|
61 provisioningUI = ProvisioningUI.getDefaultUI(); |
|
62 } |
|
63 |
|
64 @Override |
|
65 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { |
|
66 try { |
|
67 doInstall(monitor); |
|
68 } catch (OperationCanceledException e) { |
|
69 throw new InterruptedException(); |
|
70 } catch (CoreException e) { |
|
71 throw new InvocationTargetException(e); |
|
72 } |
|
73 } |
|
74 |
|
75 public void doInstall(IProgressMonitor monitor) throws OperationCanceledException, CoreException { |
|
76 SubMonitor subMonitor = SubMonitor.convert(monitor, Messages.FeatureInstallOperation_ConfiguringTaskName, 100); |
|
77 getRepositories(subMonitor.newChild(30)); |
|
78 findInstallableUnits(subMonitor.newChild(40)); |
|
79 final InstallOperation operation = resolve(subMonitor.newChild(30)); |
|
80 Display.getDefault().asyncExec(new Runnable() { |
|
81 @Override |
|
82 public void run() { |
|
83 provisioningUI.openInstallWizard(ius, operation, null); |
|
84 } |
|
85 }); |
|
86 } |
|
87 |
|
88 private void getRepositories(SubMonitor monitor) throws ProvisionException, OperationCanceledException { |
|
89 monitor.setWorkRemaining(uris.size()); |
|
90 repositories = new ArrayList<IMetadataRepository>(); |
|
91 ProvisioningSession session = provisioningUI.getSession(); |
|
92 RepositoryTracker repositoryTracker = provisioningUI.getRepositoryTracker(); |
|
93 IMetadataRepositoryManager manager = |
|
94 (IMetadataRepositoryManager) session.getProvisioningAgent().getService(IMetadataRepositoryManager.SERVICE_NAME); |
|
95 for (URI uri : uris) { |
|
96 checkIfCanceled(monitor); |
|
97 repositoryTracker.addRepository(uri, null, session); |
|
98 repositories.add(manager.loadRepository(uri, monitor.newChild(1))); |
|
99 } |
|
100 } |
|
101 |
|
102 private void findInstallableUnits(SubMonitor monitor) throws ProvisionException { |
|
103 monitor.setWorkRemaining(repositories.size() * featureInfos.size()); |
|
104 ius = new ArrayList<IInstallableUnit>(); |
|
105 urisUsed = new HashSet<URI>(); |
|
106 Set<FeatureInfo> remainingInfos = new HashSet<FeatureInfo>(featureInfos); |
|
107 for (IMetadataRepository repository : repositories) { |
|
108 checkIfCanceled(monitor); |
|
109 IQueryResult<IInstallableUnit> iusInRepository = repository.query(QueryUtil.createIUGroupQuery(), monitor.newChild(1)); |
|
110 for (FeatureInfo featureInfo : new HashSet<FeatureInfo>(remainingInfos)) { |
|
111 String id = featureInfo.getId(); |
|
112 IQuery<IInstallableUnit> iuQuery = wantVersions ? |
|
113 QueryUtil.createIUQuery(id, featureInfo.getVersion()) : |
|
114 QueryUtil.createLatestQuery(QueryUtil.createIUQuery(id)); |
|
115 IQueryResult<IInstallableUnit> result = iusInRepository.query(iuQuery, monitor.newChild(1)); |
|
116 if (!result.isEmpty()) { |
|
117 ius.add(result.iterator().next()); |
|
118 urisUsed.add(repository.getLocation()); |
|
119 remainingInfos.remove(featureInfo); |
|
120 if (remainingInfos.isEmpty()) |
|
121 break; |
|
122 } |
|
123 } |
|
124 } |
|
125 |
|
126 if (!remainingInfos.isEmpty()) { |
|
127 StringBuilder sb = new StringBuilder(); |
|
128 sb.append(Messages.FeatureInstallOperation_MissingFeaturesError); |
|
129 for (FeatureInfo featureInfo : remainingInfos) { |
|
130 sb.append(Messages.FeatureInstallOperation_IdLabel); |
|
131 sb.append(featureInfo.getId()); |
|
132 if (wantVersions) { |
|
133 sb.append(Messages.FeatureInstallOperation_VersionLabel); |
|
134 sb.append(featureInfo.getVersion().toString()); |
|
135 } |
|
136 sb.append("\n"); //$NON-NLS-1$ |
|
137 } |
|
138 throw new ProvisionException(sb.toString()); |
|
139 } |
|
140 monitor.done(); |
|
141 } |
|
142 |
|
143 // private FeatureInfo findInfo(Collection<FeatureInfo> featureInfos, String id, Version version) { |
|
144 // for (FeatureInfo featureInfo : featureInfos) { |
|
145 // boolean sameId = featureInfo.getId().equals(id); |
|
146 // if (sameId && (!wantVersions || version.toString().equals(featureInfo.getVersion().toString()))) |
|
147 // return featureInfo; |
|
148 // } |
|
149 // return null; |
|
150 // } |
|
151 |
|
152 private InstallOperation resolve(SubMonitor monitor) throws CoreException { |
|
153 checkIfCanceled(monitor); |
|
154 URI[] uris = (URI[]) urisUsed.toArray(new URI[urisUsed.size()]); |
|
155 InstallOperation installOperation = provisioningUI.getInstallOperation(ius, uris); |
|
156 IStatus operationStatus = installOperation.resolveModal(monitor); |
|
157 if (operationStatus.getSeverity() > IStatus.WARNING) { |
|
158 throw new CoreException(operationStatus); |
|
159 } |
|
160 return installOperation; |
|
161 } |
|
162 |
|
163 private void checkIfCanceled(IProgressMonitor monitor) { |
|
164 if (monitor.isCanceled()) { |
|
165 throw new OperationCanceledException(); |
|
166 } |
|
167 } |
|
168 } |