Skip to main content
summaryrefslogtreecommitdiffstats
blob: 2c5a85d70c526e827c94dce5850c779c63a03dcc (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*******************************************************************************
 * Copyright (c) 2004, 2013 Tasktop Technologies 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:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.tasks.bugs;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ISafeRunnable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.SafeRunner;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.commons.core.ExtensionPointReader;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.tasks.bugs.AbstractSupportHandler;
import org.eclipse.mylyn.tasks.bugs.ISupportResponse;
import org.eclipse.mylyn.tasks.bugs.ITaskContribution;

/**
 * @author Steffen Pingel
 */
public class SupportHandlerManager {

	private static final String ELEMENT_CLASS = "class"; //$NON-NLS-1$

	private static final String ELEMENT_TASK_HANDLER = "handler"; //$NON-NLS-1$

	private static final String EXTENSION_ID_TASK_CONTRIBUTORS = "support"; //$NON-NLS-1$

	private final DefaultSupportHandler defaultSupportHandler = new DefaultSupportHandler();

	private boolean readExtensions;

	private final List<AbstractSupportHandler> taskContributors = new CopyOnWriteArrayList<AbstractSupportHandler>();

	public SupportHandlerManager() {
	}

	public void addErrorReporter(AbstractSupportHandler taskContributor) {
		taskContributors.add(taskContributor);
	}

	public void process(final ITaskContribution contribution, final IProgressMonitor monitor) {
		readExtensions();

		for (final AbstractSupportHandler contributor : taskContributors) {
			SafeRunner.run(new ISafeRunnable() {
				public void handleException(Throwable e) {
					StatusHandler.log(new Status(IStatus.ERROR, TasksBugsPlugin.ID_PLUGIN, "Task contributor failed", e)); //$NON-NLS-1$
				}

				public void run() throws Exception {
					contributor.process(contribution, monitor);
				}
			});
			if (contribution.isHandled()) {
				break;
			}
		}
		if (!contribution.isHandled()) {
			defaultSupportHandler.process(contribution, monitor);
		}
	}

	public void postProcess(final ISupportResponse response, final IProgressMonitor monitor) {
		readExtensions();

		for (final AbstractSupportHandler contributor : taskContributors) {
			SafeRunner.run(new ISafeRunnable() {
				public void handleException(Throwable e) {
					StatusHandler.log(new Status(IStatus.ERROR, TasksBugsPlugin.ID_PLUGIN, "Task contributor failed", e)); //$NON-NLS-1$
				}

				public void run() throws Exception {
					contributor.postProcess(response, monitor);
				}
			});
		}
		defaultSupportHandler.postProcess(response, monitor);
	}

	public void preProcess(final SupportRequest request) {
		readExtensions();

		for (final AbstractSupportHandler contributor : taskContributors) {
			SafeRunner.run(new ISafeRunnable() {
				public void handleException(Throwable e) {
					StatusHandler.log(new Status(IStatus.ERROR, TasksBugsPlugin.ID_PLUGIN, "Task contributor failed", e)); //$NON-NLS-1$
				}

				public void run() throws Exception {
					contributor.preProcess(request);
				}
			});
		}
		defaultSupportHandler.preProcess(request);
	}

	private synchronized void readExtensions() {
		if (readExtensions) {
			return;
		}
		readExtensions = true;

		ExtensionPointReader<AbstractSupportHandler> reader = new ExtensionPointReader<AbstractSupportHandler>(
				TasksBugsPlugin.ID_PLUGIN, EXTENSION_ID_TASK_CONTRIBUTORS, ELEMENT_TASK_HANDLER,
				AbstractSupportHandler.class) {
			@Override
			protected AbstractSupportHandler readElement(IConfigurationElement element,
					org.eclipse.core.runtime.MultiStatus result) {
				return readTaskContributor(element, result);
			}
		};
		reader.read();
		taskContributors.addAll(reader.getItems());
	}

	private AbstractSupportHandler readTaskContributor(IConfigurationElement element, MultiStatus result) {
		try {
			Object object = element.createExecutableExtension(ELEMENT_CLASS);
			if (object instanceof AbstractSupportHandler) {
				return (AbstractSupportHandler) object;
			} else {
				result.add(new Status(IStatus.WARNING, TasksBugsPlugin.ID_PLUGIN,
						"Could not load task contributor extenstion: \"" + object.getClass().getCanonicalName() + "\"" //$NON-NLS-1$ //$NON-NLS-2$
								+ " does not implement \"" + AbstractSupportHandler.class.getCanonicalName() + "\"")); //$NON-NLS-1$ //$NON-NLS-2$
			}
		} catch (Throwable e) {
			result.add(new Status(IStatus.WARNING, TasksBugsPlugin.ID_PLUGIN,
					"Could not load task contributor extension", e)); //$NON-NLS-1$
		}
		return null;
	}

	public void removeErrorReporter(AbstractSupportHandler taskContributor) {
		taskContributors.remove(taskContributor);
	}

}

Back to the top