git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[EGIT PATCH 04/11] Add new class ExceptionCollector for grouping exceptions

From
Tor Arne Vestbø <torarnv@gmail.com>
Date
Feb 5, 2009, 01:00 UTC
Message-ID
<1233795618-20249-5-git-send-email-torarnv@gmail.com>
In-Reply-To
<1233795618-20249-4-git-send-email-torarnv@gmail.com>
Copied from org.eclipse.team.internal.core
Signed-off-by: Tor Arne Vestbø <torarnv@gmail.com>
---
 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 e13732b..20df15f 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 0000000..d99d651
--- /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<IStatus> statuses = new ArrayList<IStatus>();
+
+	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 <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 {
+			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 <code>getStatus</code>.
+	 * 
+	 * @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);
+	}
+}
-- 
1.6.1.2.309.g2ea3
Previous: Tor Arne VestbøNext: Tor Arne Vestbø
Message 5 of 31 in “Support customizable label decorations”
  1. 00/11 Support customizable label decorationsTor Arne Vestbø, Feb 5, 2009
  2. 01/11 Add support code to handle plugin property changesTor Arne Vestbø, Feb 5, 2009
  3. 02/11 Use Set instead of array to keep track of change listenersTor Arne Vestbø, Feb 5, 2009
  4. 03/11 Add a specialized team exception for GitTor Arne Vestbø, Feb 5, 2009
  5. 04/11 Add new class ExceptionCollector for grouping exceptionsTor Arne Vestbø, Feb 5, 2009
  6. 05/11 Add new class SWTUtils with helper-methods for creating controlsTor Arne Vestbø, Feb 5, 2009
  7. 06/11 Implement basic customizable label decorations with preferencesTor Arne Vestbø, Feb 5, 2009
  8. 07/11 Add binding for name of the current branchTor Arne Vestbø, Feb 5, 2009
  9. 08/11 Add icon decoration for tracked and untracked resourcesTor Arne Vestbø, Feb 5, 2009
  10. 09/11 Implement decorations of dirty, staged, and conflicting resourcesTor Arne Vestbø, Feb 5, 2009
  11. 10/11 Don't decorate every single resource on repository changeTor Arne Vestbø, Feb 5, 2009
  12. 11/11 Implement label decorations for folders and projectsTor Arne Vestbø, Feb 5, 2009
  13. Robin RosenbergFeb 5, 2009
  14. Tor Arne VestbøFeb 5, 2009
  15. Tor Arne VestbøFeb 5, 2009
  16. Robin RosenbergFeb 5, 2009
  17. Tor Arne VestbøFeb 5, 2009
  18. Robin RosenbergFeb 5, 2009
  19. Shawn O. PearceFeb 5, 2009
  20. Tor Arne VestbøFeb 5, 2009
  21. 02/11 Use Set instead of array to keep track of change listenersTor Arne Vestbø, Feb 5, 2009
  22. Shawn O. PearceFeb 5, 2009
  23. Tor Arne VestbøFeb 5, 2009
  24. Shawn O. PearceFeb 5, 2009
  25. Add support code to handle plugin property changesTor Arne Vestbø, Feb 5, 2009
  26. Tor Arne VestbøFeb 5, 2009
  27. Shawn O. PearceFeb 5, 2009
  28. Tor Arne VestbøFeb 5, 2009
  29. Robin RosenbergFeb 5, 2009
  30. Tor Arne VestbøFeb 5, 2009
  31. Robin RosenbergFeb 5, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.