KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > core > ExceptionCollector


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.core;
12
13 import java.util.*;
14
15 import org.eclipse.core.runtime.*;
16
17 /**
18  * Collects exceptions and can be configured to ignore duplicates exceptions. Exceptions can be logged
19  * and a MultiStatus containing all collected exceptions can be returned.
20  *
21  * @see org.eclipse.core.runtime.MultiStatus
22  * @see org.eclipse.core.runtime.IStatus
23  *
24  * @since 3.0
25  */

26 public class ExceptionCollector {
27
28     private List statuses = new ArrayList();
29     private String JavaDoc message;
30     private String JavaDoc pluginId;
31     private int severity;
32     private ILog log;
33         
34     /**
35      * Creates a collector and initializes the parameters for the top-level exception
36      * that would be returned from <code>getStatus</code> is exceptions are collected.
37      *
38      * @param message a human-readable message, localized to the current locale
39      * @param pluginId the unique identifier of the relevant plug-in
40      * @param severity the severity; one of <code>OK</code>,
41      * <code>ERROR</code>, <code>INFO</code>, or <code>WARNING</code>
42      * @param log the log to output the exceptions to, or <code>null</code> if
43      * exceptions should not be logged.
44      */

45     public ExceptionCollector(String JavaDoc message, String JavaDoc pluginId, int severity, ILog log) {
46         this.message = message;
47         this.pluginId = pluginId;
48         this.severity = severity;
49         this.log = log;
50     }
51
52     /**
53      * Clears the exceptions collected.
54      */

55     public void clear() {
56         statuses.clear();
57     }
58
59     /**
60      * Returns a status that represents the exceptions collected. If the collector
61      * is empty <code>IStatus.OK</code> is returned. Otherwise a MultiStatus containing
62      * all collected exceptions is returned.
63      * @return a multistatus containing the exceptions collected or IStatus.OK if
64      * the collector is empty.
65      */

66     public IStatus getStatus() {
67         if(statuses.isEmpty()) {
68             return Status.OK_STATUS;
69         } else {
70             MultiStatus multiStatus = new MultiStatus(pluginId, severity, message, null);
71             Iterator it = statuses.iterator();
72             while (it.hasNext()) {
73                 IStatus status = (IStatus) it.next();
74                 multiStatus.merge(status);
75             }
76             return multiStatus;
77         }
78     }
79     
80     /**
81      * Add this exception to the collector. If a log was specified in the constructor
82      * then the exception will be output to the log. You can retreive exceptions
83      * using <code>getStatus</code>.
84      *
85      * @param exception the exception to collect
86      */

87     public void handleException(CoreException exception) {
88         // log the exception if we have a log
89
if(log != null) {
90             log.log(new Status(severity, pluginId, 0, message, exception));
91         }
92         // Record each status individually to flatten the resulting multi-status
93
IStatus exceptionStatus = exception.getStatus();
94         // Wrap the exception so the stack trace is not lost.
95
IStatus status = new Status(exceptionStatus.getSeverity(), exceptionStatus.getPlugin(), exceptionStatus.getCode(), exceptionStatus.getMessage(), exception);
96         recordStatus(status);
97         IStatus[] children = status.getChildren();
98         for (int i = 0; i < children.length; i++) {
99             IStatus status2 = children[i];
100             recordStatus(status2);
101         }
102     }
103
104     private void recordStatus(IStatus status) {
105         statuses.add(status);
106     }
107 }
108
Popular Tags