From 0b323ec335439a27f220d41efeb098d1c05c7b7f Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Wed, 11 Feb 2009 19:40:06 +0100 Subject: [PATCH] Add new class ExceptionCollector for grouping exceptions MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Copied from org.eclipse.team.internal.core Signed-off-by: Tor Arne Vestbø Signed-off-by: Robin Rosenberg --- org.spearce.egit.core/META-INF/MANIFEST.MF | 5 +- .../core/internal/util/ExceptionCollector.java | 128 +++++++++++++++++++++ 2 files changed, 131 insertions(+), 2 deletions(-) create mode 100644 org.spearce.egit.core/src/org/spearce/egit/core/internal/util/ExceptionCollector.java diff --git a/org.spearce.egit.core/META-INF/MANIFEST.MF b/org.spearce.egit.core/META-INF/MANIFEST.MF index e13732b2..20df15f2 100644 --- a/org.spearce.egit.core/META-INF/MANIFEST.MF +++ b/org.spearce.egit.core/META-INF/MANIFEST.MF @@ -12,8 +12,9 @@ Require-Bundle: org.eclipse.core.runtime, org.spearce.jgit, org.eclipse.core.filesystem, org.eclipse.ui -Export-Package: org.spearce.egit.core.internal.storage;x-friends:="org.spearce.egit.ui", - org.spearce.egit.core, +Export-Package: org.spearce.egit.core, + org.spearce.egit.core.internal.storage;x-friends:="org.spearce.egit.ui", + org.spearce.egit.core.internal.util;x-friends:="org.spearce.egit.ui", org.spearce.egit.core.op, org.spearce.egit.core.project Bundle-ActivationPolicy: lazy diff --git a/org.spearce.egit.core/src/org/spearce/egit/core/internal/util/ExceptionCollector.java b/org.spearce.egit.core/src/org/spearce/egit/core/internal/util/ExceptionCollector.java new file mode 100644 index 00000000..404ee009 --- /dev/null +++ b/org.spearce.egit.core/src/org/spearce/egit/core/internal/util/ExceptionCollector.java @@ -0,0 +1,128 @@ +/******************************************************************************* + * Copyright (c) 2000, 2006 IBM Corporation and others. + * 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.spearce.egit.core.internal.util; + +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 final List statuses = new ArrayList(); + + private final String message; + + private final String pluginId; + + private final int severity; + + private final ILog log; + + /** + * Creates a collector and initializes the parameters for the top-level + * exception that would be returned from getStatus 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 OK, ERROR, + * INFO, or WARNING + * @param log + * the log to output the exceptions to, or null 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 IStatus.OK 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 { + final MultiStatus multiStatus = new MultiStatus(pluginId, severity, + message, null); + final Iterator it = statuses.iterator(); + while (it.hasNext()) { + final 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 getStatus. + * + * @param exception + * the exception to collect + */ + public void handleException(CoreException exception) { + if (log != null) { + log.log(new Status(severity, pluginId, 0, message, exception)); + } + + // Record each status individually to flatten the resulting multi-status + final IStatus exceptionStatus = exception.getStatus(); + + // Wrap the exception so the stack trace is not lost. + final IStatus status = new Status(exceptionStatus.getSeverity(), + exceptionStatus.getPlugin(), exceptionStatus.getCode(), + exceptionStatus.getMessage(), exception); + + recordStatus(status); + for (IStatus childStatus : status.getChildren()) + recordStatus(childStatus); + } + + private void recordStatus(IStatus status) { + statuses.add(status); + } +} -- 2.11.4.GIT