Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8c956d1a99151f8c62b31389a23470ef729a9be4 (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
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.core;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Status;

/**
 * Collects exceptions and can be configured to ignore duplicates exceptions. Exceptions can be logged
 * and a MultiStatus containing all collected exceptions can be returned.
 *
 * @see org.eclipse.core.runtime.MultiStatus
 * @see org.eclipse.core.runtime.IStatus
 *
 * @since 3.0
 */
public class ExceptionCollector {

	private List<IStatus> statuses = new ArrayList<>();
	private String message;
	private String pluginId;
	private int severity;
	private ILog log;

	/**
	 * Creates a collector and initializes the parameters for the top-level exception
	 * that would be returned from <code>getStatus</code> is exceptions are collected.
	 *
	 * @param message a human-readable message, localized to the current locale
	 * @param pluginId the unique identifier of the relevant plug-in
	 * @param severity the severity; one of <code>OK</code>,
	 *   <code>ERROR</code>, <code>INFO</code>, or <code>WARNING</code>
	 * @param log the log to output the exceptions to, or <code>null</code> if
	 *   exceptions should not be logged.
	 */
	public ExceptionCollector(String message, String pluginId, int severity, ILog log) {
		this.message = message;
		this.pluginId = pluginId;
		this.severity = severity;
		this.log = log;
	}

	/**
	 * Clears the exceptions collected.
	 */
	public void clear() {
		statuses.clear();
	}

	/**
	 * Returns a status that represents the exceptions collected. If the collector
	 * is empty <code>IStatus.OK</code> is returned. Otherwise a MultiStatus containing
	 * all collected exceptions is returned.
	 * @return a multistatus containing the exceptions collected or IStatus.OK if
	 * the collector is empty.
	 */
	public IStatus getStatus() {
		if(statuses.isEmpty()) {
			return Status.OK_STATUS;
		} else {
			MultiStatus multiStatus = new MultiStatus(pluginId, severity, message, null);
			Iterator it = statuses.iterator();
			while (it.hasNext()) {
				IStatus status = (IStatus) it.next();
				multiStatus.merge(status);
			}
			return multiStatus;
		}
	}

	/**
	 * Add this exception to the collector. If a log was specified in the constructor
	 * then the exception will be output to the log. You can retreive exceptions
	 * using <code>getStatus</code>.
	 *
	 * @param exception the exception to collect
	 */
	public void handleException(CoreException exception) {
		// log the exception if we have a log
		if(log != null) {
			log.log(new Status(severity, pluginId, 0, message, exception));
		}
		// Record each status individually to flatten the resulting multi-status
		IStatus exceptionStatus = exception.getStatus();
		// Wrap the exception so the stack trace is not lost.
		IStatus status = new Status(exceptionStatus.getSeverity(), exceptionStatus.getPlugin(), exceptionStatus.getCode(), exceptionStatus.getMessage(), exception);
		recordStatus(status);
		IStatus[] children = status.getChildren();
		for (int i = 0; i < children.length; i++) {
			IStatus status2 = children[i];
			recordStatus(status2);
		}
	}

	private void recordStatus(IStatus status) {
		statuses.add(status);
	}
}

Back to the top