| /******************************************************************************* |
| * Copyright (c) 2012 EclipseSource Muenchen GmbH. |
| * |
| * 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: |
| ******************************************************************************/ |
| package org.eclipse.emf.emfstore.client.ui.errorreporting; |
| |
| import java.text.MessageFormat; |
| import java.util.HashSet; |
| import java.util.Set; |
| |
| import org.eclipse.core.commands.ExecutionEvent; |
| import org.eclipse.core.commands.ExecutionException; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.emf.emfstore.client.model.Configuration; |
| import org.eclipse.emf.emfstore.client.model.exceptions.MissingCommandException; |
| import org.eclipse.emf.emfstore.client.model.observers.ExceptionObserver; |
| import org.eclipse.emf.emfstore.client.model.util.WorkspaceUtil; |
| import org.eclipse.emf.emfstore.common.extensionpoint.ExtensionPoint; |
| import org.eclipse.jface.dialogs.ErrorDialog; |
| import org.eclipse.ui.PlatformUI; |
| |
| /** |
| * An exception observer that collects logs. |
| * |
| * @author Maximilian Koegel |
| */ |
| public class EmfStoreExceptionObserver implements ExceptionObserver { |
| |
| private static EmfStoreExceptionObserver instance; |
| private Set<ExceptionLog> exceptionLogs; |
| private Boolean enabled; |
| |
| /** |
| * Constructor. |
| */ |
| public EmfStoreExceptionObserver() { |
| instance = this; |
| exceptionLogs = new HashSet<ExceptionLog>(); |
| ExtensionPoint extensionPoint = new ExtensionPoint(Activator.PLUGIN_ID); |
| enabled = extensionPoint.getBoolean("enabled", true); |
| } |
| |
| /** |
| * Returns the instance. |
| * |
| * @return the instance |
| */ |
| public static EmfStoreExceptionObserver getInstance() { |
| if (instance == null) { |
| instance = new EmfStoreExceptionObserver(); |
| } |
| return instance; |
| } |
| |
| /** |
| * {@inheritDoc} |
| */ |
| public boolean handleError(RuntimeException e) { |
| |
| if (!enabled) { |
| return false; |
| } |
| |
| if (Configuration.isDebugMode()) { |
| |
| MailBugHandler bugHandler = new MailBugHandler(); |
| |
| try { |
| bugHandler.execute(new ExecutionEvent()); |
| } catch (ExecutionException exception) { |
| WorkspaceUtil.handleException(exception); |
| } |
| |
| ErrorDialog |
| .openError( |
| PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), |
| "DEBUG MODE ACTIVE: An Error Occured", |
| MessageFormat |
| .format( |
| "This error message only appears if debug mode is active. \n" |
| + "Although this error message might be an error itself, it might help to find bugs faster. \n" |
| + "The most recent log has been copied to the clipboard, please add it to the bug report. Thanks! \n" |
| + "To disable, launch without {0}={1} switch.", Configuration.DEBUG_SWITCH, |
| Configuration.DEBUG_SWITCH_ENABLED_VALUE), new Status(Status.ERROR, |
| "org.eclipse.emf.emfstore.client", "An error occured.", e)); |
| |
| throw e; |
| } |
| |
| // MissingCommandException should only be logged if the debug switch is not set |
| if (e.getCause() instanceof MissingCommandException) { |
| return true; |
| } |
| |
| for (ExceptionLog exceptionLog : exceptionLogs) { |
| exceptionLog.addException(e); |
| } |
| |
| return true; |
| } |
| |
| /** |
| * Adds a log. |
| * |
| * @param log |
| * the log being added |
| */ |
| public void addLog(ExceptionLog log) { |
| exceptionLogs.add(log); |
| } |
| |
| /** |
| * Removes a log. |
| * |
| * @param log |
| * the log to be removed |
| */ |
| public void removeLog(ExceptionLog log) { |
| exceptionLogs.remove(log); |
| } |
| |
| /** |
| * Returns the log. |
| * |
| * @return the log |
| */ |
| public ExceptionLog getLog() { |
| return new ExceptionLog(this); |
| } |
| |
| } |