Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a77eff735323210cdcdea27223502341d7324c6c (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
/*******************************************************************************
 * Copyright (c) 2008, 2018 QNX Software Systems 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:
 *     QNX Software Systems - Initial API and implementation
 *     Andy Jin - Hardware debugging UI improvements, bug 229946
 *     Bruce Griffith, Sage Electronic Engineering, LLC - bug 305943
 *              - API generalization to become transport-independent (allow
 *                connections via serial ports and pipes).
 *     Torbjörn Svensson (STMicroelectronics) - Bug 535024
 *******************************************************************************/
package org.eclipse.cdt.debug.gdbjtag.core.jtagdevice;

import java.util.ArrayList;

import org.eclipse.cdt.debug.gdbjtag.core.Activator;
import org.eclipse.cdt.debug.gdbjtag.core.IGDBJtagConstants;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;

public class GDBJtagDeviceContributionFactory {
	private static final String EXTENSION_POINT_NAME = "JTagDevice"; //$NON-NLS-1$
	private static final String MAIN_ELEMENT = "device"; //$NON-NLS-1$

	private static GDBJtagDeviceContributionFactory instance;
	protected ArrayList<GDBJtagDeviceContribution> contributions;

	private GDBJtagDeviceContributionFactory() {
		contributions = new ArrayList<GDBJtagDeviceContribution>();
		loadSubtypeContributions();
	}
	
	public GDBJtagDeviceContribution[] getGDBJtagDeviceContribution() {
		return contributions.toArray(
				new GDBJtagDeviceContribution[contributions.size()]);
	}

	private void loadSubtypeContributions() {

		IExtensionPoint ep = Platform.getExtensionRegistry().getExtensionPoint(
				Activator.getUniqueIdentifier(), EXTENSION_POINT_NAME);
		if (ep == null)
			return;
		IConfigurationElement[] elements = ep.getConfigurationElements();
		for (int i = 0; i < elements.length; i++) {
			IConfigurationElement configurationElement = elements[i];
			if (configurationElement.getName().equals(MAIN_ELEMENT)) {
				String id = getRequired(configurationElement, "id"); //$NON-NLS-1$
				String name = getRequired(configurationElement, "name"); //$NON-NLS-1$
				String className = getRequired(configurationElement, "class"); //$NON-NLS-1$
				String connection = getOptional(configurationElement, "default_connection", IGDBJtagConstants.DEFAULT_CONNECTION); //$NON-NLS-1$ 
	            GDBJtagDeviceContribution adapter = new GDBJtagDeviceContribution();
				adapter.setDeviceId(id);
				adapter.setDeviceName(name);
				adapter.setDeviceClassName(className);
				adapter.setDeviceDefaultConnection(connection);
				adapter.setDeviceClassBundleName(configurationElement.getContributor().getName());
				addContribution(adapter);
			}
		}
	}

	public void addContribution(GDBJtagDeviceContribution contribution) {
		contributions.add(contribution);

	}

	public static GDBJtagDeviceContributionFactory getInstance() {
		if (instance == null) {
			instance = new GDBJtagDeviceContributionFactory();
		}
		return instance;
	}

	private static String getRequired(IConfigurationElement configurationElement, String name) {
		String elementValue = configurationElement.getAttribute(name);
		if (elementValue == null)
			Activator.log(new Status(IStatus.ERROR, Activator.getUniqueIdentifier(),
					DebugPlugin.INTERNAL_ERROR, "Extension "
							+ configurationElement.getDeclaringExtension().getUniqueIdentifier()
							+ " missing required attribute: " + name, null));
		return elementValue;
	}

	private static String getOptional(IConfigurationElement configurationElement, String name, String defaultValue) {
		String elementValue = configurationElement.getAttribute(name);
		return (elementValue != null) ? elementValue : defaultValue;
	}

	/**
	 * @since 9.2
	 */
	public GDBJtagDeviceContribution findByDeviceName(String name) {
		for (GDBJtagDeviceContribution contribution : getGDBJtagDeviceContribution()) {
			if (contribution.getDeviceName().equals(name)) {
				return contribution;
			}
		}
		return null;
	}

	/**
	 * @since 9.2
	 */
	public GDBJtagDeviceContribution findByDeviceId(String id) {
		for (GDBJtagDeviceContribution contribution : getGDBJtagDeviceContribution()) {
			if (contribution.getDeviceId().equals(id)) {
				return contribution;
			}
		}
		return null;
	}
}

Back to the top