Skip to main content
summaryrefslogtreecommitdiffstats
blob: 93af5168bd0b6ae69fd713f8e8afce8aa52012fd (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
 * Copyright (c) 2011 Ericsson 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:
 *    Ericsson Research Canada - Initial API and implementation
 * 
 */
package org.eclipse.mylyn.reviews.notifications.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.reviews.notifications.internal.core.MeetingData;
import org.eclipse.mylyn.reviews.notifications.spi.NotificationsConnector;
import org.eclipse.osgi.util.NLS;

/**
 * @author Alvaro Sanchez-Leon
 * 
 */
public class NotificationsCore {
	// ------------------------------------------------------------------------
	// Constants
	// ------------------------------------------------------------------------
	public static final String	PLUGIN_ID	= "org.eclipse.mylyn.reviews.notifications";	//$NON-NLS-1$

	// ------------------------------------------------------------------------
	// Methods
	// ------------------------------------------------------------------------
	/**
	 * @return
	 */
	public static List<String> getConnectorIds() {
		List<String> connectorIds = new ArrayList<String>();

		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint connectorsExtensionPoint = registry.getExtensionPoint(PLUGIN_ID + ".connectors"); //$NON-NLS-1$
		IExtension[] extensions = connectorsExtensionPoint.getExtensions();
		for (IExtension extension : extensions) {
			IConfigurationElement[] elements = extension.getConfigurationElements();
			for (IConfigurationElement element : elements) {
				connectorIds.add(element.getAttribute("id"));
			}
		}

		return connectorIds;
	}

	/**
	 * Get the first connector with status Enabled attempting the ids in the order of appearance within the provided
	 * array
	 * 
	 * @param ids
	 * @return
	 */
	public static NotificationsConnector getFirstEnabled(String[] ids) {
		Assert.isNotNull(ids);
		MultiStatus result = new MultiStatus(PLUGIN_ID, 0, "Notifications connectors failed to load.", null); //$NON-NLS-1$

		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint connectorsExtensionPoint = registry.getExtensionPoint(PLUGIN_ID + ".connectors"); //$NON-NLS-1$
		IExtension[] extensions = connectorsExtensionPoint.getExtensions();

		Map<String, IConfigurationElement> configElements = new HashMap<String, IConfigurationElement>();

		// Build a map of ids
		for (IExtension extension : extensions) {
			IConfigurationElement[] elements = extension.getConfigurationElements();
			for (IConfigurationElement element : elements) {
				configElements.put(element.getAttribute("id"), element);
			}
		}

		// scan for the id in the order provided by the array
		IConfigurationElement dConfigElement = null;
		for (int i = 0; i < ids.length; i++) {
			dConfigElement = configElements.get(ids[i]);
			if (dConfigElement != null) {
				// make sure it's enabled
				NotificationsConnector connector = loadElement(dConfigElement, result);
				if (connector != null && connector.isEnabled()) {
					return connector;
				}
			}
		}

		if (!result.isOK()) {
			StatusHandler.log(result);
		}

		return null;
	}

	private static NotificationsConnector loadElement(IConfigurationElement aElement, MultiStatus aStatus) {
		try {
			Object object = aElement.createExecutableExtension("core"); //$NON-NLS-1$
			if (object instanceof NotificationsConnector) {
				// success
				return (NotificationsConnector) object;
			} else {
				aStatus.add(new Status(
						IStatus.ERROR,
						PLUGIN_ID,
						NLS.bind(
								"Notifications Connector core ''{0}'' does not extend expected class for extension contributed by {1}", //$NON-NLS-1$
								object.getClass().getCanonicalName(), aElement.getContributor().getName())));
			}
		} catch (Throwable e) {
			aStatus.add(new Status(
					IStatus.ERROR,
					PLUGIN_ID,
					NLS.bind(
							"Notifications Connector core failed to load for extension contributed by {0}", aElement.getContributor().getName()), e)); //$NON-NLS-1$
		}

		return null;
	}

	/**
	 * Search for a connector with given id, return it if found
	 * 
	 * @param id
	 * @return
	 */
	public static NotificationsConnector loadConnector(String id) {
		Assert.isNotNull(id);
		MultiStatus result = new MultiStatus(PLUGIN_ID, 0, "Notifications connectors failed to load.", null); //$NON-NLS-1$

		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint connectorsExtensionPoint = registry.getExtensionPoint(PLUGIN_ID + ".connectors"); //$NON-NLS-1$
		IExtension[] extensions = connectorsExtensionPoint.getExtensions();
		for (IExtension extension : extensions) {
			IConfigurationElement[] elements = extension.getConfigurationElements();
			for (IConfigurationElement element : elements) {
				if (id.equals(element.getAttribute("id"))) { //$NON-NLS-1$
					try {
						Object object = element.createExecutableExtension("core"); //$NON-NLS-1$
						if (object instanceof NotificationsConnector) {
							return (NotificationsConnector) object;
						} else {
							result.add(new Status(
									IStatus.ERROR,
									PLUGIN_ID,
									NLS.bind(
											"Notifications Connector core ''{0}'' does not extend expected class for extension contributed by {1}", //$NON-NLS-1$
											object.getClass().getCanonicalName(), element.getContributor().getName())));
						}
					} catch (Throwable e) {
						result.add(new Status(
								IStatus.ERROR,
								PLUGIN_ID,
								NLS.bind(
										"Notifications Connector core failed to load for extension contributed by {0}", element.getContributor().getName()), e)); //$NON-NLS-1$
					}
				}
			}
		}

		if (!result.isOK()) {
			StatusHandler.log(result);
		}

		return null;
	}

	/**
	 * Access factory method
	 * 
	 * @param aCustomId
	 * @param aSubject
	 * @param aBody
	 * @param aLocation
	 * @param aStartTimeMilli
	 * @param aEndTimeMilli
	 * @return
	 * @throws CoreException
	 */
	public static IMeetingData createMeetingData(String aCustomId, String aSubject, String aBody, String aLocation,
			long aStartTimeMilli, long aEndTimeMilli) throws CoreException {
		return new MeetingData(aCustomId, aSubject, aBody, aLocation, aStartTimeMilli,
			 aEndTimeMilli);
	}

}

Back to the top