| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2009, 2017 IBM Corporation and others. |
| 3 |
* |
| 4 |
* This program and the accompanying materials |
| 5 |
* are made available under the terms of the Eclipse Public License 2.0 |
| 6 |
* which accompanies this distribution, and is available at |
| 7 |
* https://www.eclipse.org/legal/epl-2.0/ |
| 8 |
* |
| 9 |
* SPDX-License-Identifier: EPL-2.0 |
| 10 |
* |
| 11 |
* Contributors: |
| 12 |
* IBM Corporation - initial API and implementation |
| 13 |
******************************************************************************/ |
| 14 |
|
| 15 |
package org.eclipse.equinox.internal.p2.operations; |
| 16 |
|
| 17 |
import java.util.HashMap; |
| 18 |
import org.eclipse.core.runtime.*; |
| 19 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| 20 |
|
| 21 |
/** |
| 22 |
* ResolutionResult describes problems in a provisioning plan in a structured |
| 23 |
* way that can be presented to a user. |
| 24 |
* |
| 25 |
* @since 2.0 |
| 26 |
*/ |
| 27 |
public class ResolutionResult { |
| 28 |
private static final String NESTING_INDENT = " "; //$NON-NLS-1$ |
| 29 |
|
| 30 |
private final HashMap<IInstallableUnit, MultiStatus> iuToStatusMap = new HashMap<>(); |
| 31 |
private MultiStatus summaryStatus; |
| 32 |
|
| 33 |
public IStatus getSummaryStatus() { |
| 34 |
if (summaryStatus != null) |
| 35 |
return summaryStatus; |
| 36 |
return Status.OK_STATUS; |
| 37 |
} |
| 38 |
|
| 39 |
public void setSummaryStatus(MultiStatus status) { |
| 40 |
summaryStatus = status; |
| 41 |
} |
| 42 |
|
| 43 |
public void addSummaryStatus(IStatus status) { |
| 44 |
if (summaryStatus == null) { |
| 45 |
summaryStatus = new MultiStatus(Constants.BUNDLE_ID, 0, Messages.ResolutionResult_SummaryStatus, null); |
| 46 |
} |
| 47 |
summaryStatus.add(status); |
| 48 |
} |
| 49 |
|
| 50 |
public IStatus statusOf(IInstallableUnit iu) { |
| 51 |
return iuToStatusMap.get(iu); |
| 52 |
} |
| 53 |
|
| 54 |
public void addStatus(IInstallableUnit iu, IStatus status) { |
| 55 |
MultiStatus iuSummaryStatus = iuToStatusMap.get(iu); |
| 56 |
if (iuSummaryStatus == null) { |
| 57 |
iuSummaryStatus = new MultiStatus(Constants.BUNDLE_ID, IStatusCodes.IU_REQUEST_ALTERED, new IStatus[] {status}, getIUString(iu), null); |
| 58 |
} else |
| 59 |
iuSummaryStatus.add(status); |
| 60 |
} |
| 61 |
|
| 62 |
private String getIUString(IInstallableUnit iu) { |
| 63 |
if (iu == null) |
| 64 |
return Messages.PlanAnalyzer_Items; |
| 65 |
// Get the iu name in the default locale |
| 66 |
String name = iu.getProperty(IInstallableUnit.PROP_NAME, null); |
| 67 |
if (name != null) |
| 68 |
return name; |
| 69 |
return iu.getId(); |
| 70 |
} |
| 71 |
|
| 72 |
public String getSummaryReport() { |
| 73 |
if (summaryStatus != null) { |
| 74 |
StringBuilder buffer = new StringBuilder(); |
| 75 |
appendDetailText(summaryStatus, buffer, -1, false); |
| 76 |
return buffer.toString(); |
| 77 |
} |
| 78 |
return ""; //$NON-NLS-1$ |
| 79 |
} |
| 80 |
|
| 81 |
// Answers null if there is nothing to say about the ius |
| 82 |
public String getDetailedReport(IInstallableUnit[] ius) { |
| 83 |
StringBuilder buffer = new StringBuilder(); |
| 84 |
for (IInstallableUnit iu : ius) { |
| 85 |
MultiStatus iuStatus = iuToStatusMap.get(iu); |
| 86 |
if (iuStatus != null) |
| 87 |
appendDetailText(iuStatus, buffer, 0, true); |
| 88 |
} |
| 89 |
String report = buffer.toString(); |
| 90 |
if (report.length() == 0) |
| 91 |
return null; |
| 92 |
return report; |
| 93 |
} |
| 94 |
|
| 95 |
void appendDetailText(IStatus status, StringBuilder buffer, int indent, boolean includeTopLevelMessage) { |
| 96 |
if (includeTopLevelMessage) { |
| 97 |
for (int i = 0; i < indent; i++) |
| 98 |
buffer.append(NESTING_INDENT); |
| 99 |
if (status.getMessage() != null) |
| 100 |
buffer.append(status.getMessage()); |
| 101 |
} |
| 102 |
Throwable t = status.getException(); |
| 103 |
if (t != null) { |
| 104 |
// A provision (or core) exception occurred. Get its status message or if none, its top level message. |
| 105 |
// Indent by one more level (note the <=) |
| 106 |
buffer.append('\n'); |
| 107 |
for (int i = 0; i <= indent; i++) |
| 108 |
buffer.append(NESTING_INDENT); |
| 109 |
if (t instanceof CoreException) { |
| 110 |
IStatus exceptionStatus = ((CoreException) t).getStatus(); |
| 111 |
if (exceptionStatus != null && exceptionStatus.getMessage() != null) |
| 112 |
buffer.append(exceptionStatus.getMessage()); |
| 113 |
else { |
| 114 |
String details = t.getLocalizedMessage(); |
| 115 |
if (details != null) |
| 116 |
buffer.append(details); |
| 117 |
} |
| 118 |
} else { |
| 119 |
String details = t.getLocalizedMessage(); |
| 120 |
if (details != null) |
| 121 |
buffer.append(details); |
| 122 |
} |
| 123 |
} |
| 124 |
// Now print the children status info (if there are children) |
| 125 |
IStatus[] children = status.getChildren(); |
| 126 |
for (IStatus child : children) { |
| 127 |
if (buffer.length() > 0) |
| 128 |
buffer.append('\n'); |
| 129 |
appendDetailText(child, buffer, indent + 1, true); |
| 130 |
} |
| 131 |
} |
| 132 |
} |