Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 73aef23d1471ae2988b266fa06a68cb39f63d0fb (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*******************************************************************************
 * Copyright (c) 2007, 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.equinox.internal.p2.engine;

import java.util.*;
import org.eclipse.core.runtime.*;
import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
import org.eclipse.equinox.internal.provisional.p2.engine.Touchpoint;
import org.eclipse.equinox.internal.provisional.p2.metadata.TouchpointType;
import org.eclipse.osgi.util.NLS;

//TODO This needs to support multiple version of each touchpoint and have a lookup that supports version semantics
public class TouchpointManager implements IRegistryChangeListener {

	private static TouchpointManager instance;

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

	private static final String PT_TOUCHPOINTS = "touchpoints"; //$NON-NLS-1$
	private static final String ELEMENT_TOUCHPOINT = "touchpoint"; //$NON-NLS-1$
	private static final String ELEMENT_TOUCHPOINT_DATA = "data"; //$NON-NLS-1$
	private static final String ATTRIBUTE_CLASS = "class"; //$NON-NLS-1$
	private static final String ATTRIBUTE_TYPE = "type"; //$NON-NLS-1$

	private class TouchpointEntry {

		private IConfigurationElement element;
		private boolean createdExtension;
		private Touchpoint touchpoint;

		public TouchpointEntry(IConfigurationElement element) {
			super();
			this.element = element;
			this.touchpoint = null;
			this.createdExtension = false;
		}

		public TouchpointEntry(IConfigurationElement element, Touchpoint touchpoint) {
			super();
			this.element = element;
			this.touchpoint = touchpoint;
			this.createdExtension = (touchpoint != null ? true : false);
		}

		public boolean hasTouchpoint() {
			return (this.touchpoint != null);
		}

		public Touchpoint getTouchpoint() {
			if (!createdExtension) {
				String id = element.getAttribute(ATTRIBUTE_TYPE);
				try {
					Touchpoint touchpointInstance = (Touchpoint) element.createExecutableExtension(ATTRIBUTE_CLASS);
					if (touchpointInstance != null) {
						if (!id.equals(touchpointInstance.getTouchpointType().getId())) {
							reportError(NLS.bind(Messages.TouchpointManager_Touchpoint_Type_Mismatch, id, touchpointInstance.getTouchpointType().getId()), null);
						}
						this.touchpoint = touchpointInstance;
					} else {
						String errorMsg = NLS.bind(Messages.TouchpointManager_Null_Creating_Touchpoint_Extension, id);
						throw new CoreException(new Status(IStatus.ERROR, EngineActivator.ID, 1, errorMsg, null));
					}
				} catch (CoreException cexcpt) {
					LogHelper.log(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.TouchpointManager_Exception_Creating_Touchpoint_Extension, id), cexcpt));
				} catch (ClassCastException ccexcpt) {
					LogHelper.log(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.TouchpointManager_Exception_Creating_Touchpoint_Extension, id), ccexcpt));
				}
				// Mark as created even in error cases,
				// so exceptions are not logged multiple times
				createdExtension = true;
			}
			return this.touchpoint;
		}

		public String toString() {
			StringBuffer result = new StringBuffer(element.toString());
			if (createdExtension) {
				String touchpointString = touchpoint != null ? touchpoint.toString() : "not created"; //$NON-NLS-1$
				result.append(" => " + touchpointString); //$NON-NLS-1$
			}
			return result.toString();
		}
	}

	// TODO: Do we really want to store the touchpoints? The danger is 
	//	     that if two installations are performed simultaneously, then...
	// TODO: Figure out locking, concurrency requirements for touchpoints.
	private Map touchpointEntries;

	private TouchpointManager() {
		RegistryFactory.getRegistry().addRegistryChangeListener(this, EngineActivator.ID);
	}

	/*
	 * Return the touchpoint which is registered for the given id,
	 * or <code>null</code> if none are registered.
	 */
	public Touchpoint getTouchpoint(TouchpointType id) {
		if (id == null || "".equals(id.getId())) //$NON-NLS-1$
			throw new IllegalArgumentException(Messages.TouchpointManager_Null_Touchpoint_Type_Argument);
		if (touchpointEntries == null) {
			initializeTouchpoints();
		}
		TouchpointEntry entry = (TouchpointEntry) touchpointEntries.get(id.getId());
		return entry == null ? null : entry.getTouchpoint();
	}

	public Touchpoint[] getAllTouchpoints() {
		if (touchpointEntries == null) {
			initializeTouchpoints();
		}
		Collection adapters = touchpointEntries.values();

		ArrayList touchpoints = new ArrayList(adapters.size());
		for (Iterator iter = adapters.iterator(); iter.hasNext();) {
			TouchpointEntry entry = (TouchpointEntry) iter.next();
			Touchpoint touchpoint = entry.getTouchpoint();
			if (touchpoint != null) {
				touchpoints.add(touchpoint);
			}
		}
		return (Touchpoint[]) touchpoints.toArray(new Touchpoint[touchpoints.size()]);
	}

	public Touchpoint[] getCreatedTouchpoints() {
		if (touchpointEntries == null)
			return new Touchpoint[0];
		Collection adapters = touchpointEntries.values();

		ArrayList touchpoints = new ArrayList(adapters.size());
		for (Iterator iter = adapters.iterator(); iter.hasNext();) {
			TouchpointEntry entry = (TouchpointEntry) iter.next();
			if (entry.hasTouchpoint()) {
				Touchpoint touchpoint = entry.getTouchpoint();
				if (touchpoint != null) {
					touchpoints.add(touchpoint);
				}
			}
		}
		return (Touchpoint[]) touchpoints.toArray(new Touchpoint[touchpoints.size()]);
	}

	public IStatus validateTouchpoints(String[] requiredTypes) {
		MultiStatus status = touchpointEntries == null ? initializeTouchpoints() : new MultiStatus(EngineActivator.ID, IStatus.OK, null, null);

		for (int i = 0; i < requiredTypes.length; i++) {
			TouchpointEntry entry = (TouchpointEntry) touchpointEntries.get(requiredTypes[i]);
			if (entry == null) {
				reportError(NLS.bind(Messages.TouchpointManager_Required_Touchpoint_Not_Found, requiredTypes[i]), status);
			}
		}
		return status;
	}

	/*
	 * Construct a map of the extensions that implement the touchpoints extension point.
	 */
	private MultiStatus initializeTouchpoints() {
		MultiStatus status = new MultiStatus(EngineActivator.ID, IStatus.OK, null, null);
		IExtensionPoint point = RegistryFactory.getRegistry().getExtensionPoint(EngineActivator.ID, PT_TOUCHPOINTS);
		if (point == null) {
			reportError(NLS.bind(Messages.TouchpointManager_No_Extension_Point, EngineActivator.ID, PT_TOUCHPOINTS), status);
			touchpointEntries = new HashMap(0);
			return status;
		}

		IExtension[] extensions = point.getExtensions();
		touchpointEntries = new HashMap(extensions.length);
		for (int i = 0; i < extensions.length; i++) {
			IConfigurationElement[] elements = extensions[i].getConfigurationElements();
			for (int j = 0; j < elements.length; j++) {
				String elementName = elements[j].getName();
				if (!ELEMENT_TOUCHPOINT.equalsIgnoreCase(elements[j].getName())) {
					if (!ELEMENT_TOUCHPOINT_DATA.equals(elementName)) { // TODO: are 'data' elements still needed?
						reportError(NLS.bind(Messages.TouchpointManager_Incorrectly_Named_Extension, elements[j].getName(), ELEMENT_TOUCHPOINT), status);
					}
					continue;
				}
				String id = elements[j].getAttribute(ATTRIBUTE_TYPE);
				if (id == null) {
					reportError(NLS.bind(Messages.TouchpointManager_Attribute_Not_Specified, ATTRIBUTE_TYPE), status);
					continue;
				}
				if (touchpointEntries.get(id) == null) {
					touchpointEntries.put(id, new TouchpointEntry(elements[j]));
				} else {
					reportError(NLS.bind(Messages.TouchpointManager_Conflicting_Touchpoint_Types, ATTRIBUTE_TYPE, id), status);
				}
			}
		}
		return status;
	}

	static void reportError(String errorMsg, MultiStatus status) {
		Status errorStatus = new Status(IStatus.ERROR, EngineActivator.ID, 1, errorMsg, null);
		if (status != null && !status.isOK())
			status.add(errorStatus);
		LogHelper.log(errorStatus);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IRegistryChangeListener#registryChanged(org.eclipse.core.runtime.IRegistryChangeEvent)
	 */
	public void registryChanged(IRegistryChangeEvent event) {
		// just flush the cache when something changed.  It will be recomputed on demand.
		touchpointEntries = null;
	}
}

Back to the top