Debian Codegraph @

bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/internal/p2/engine/TouchpointManager.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2007, 2017 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.equinox.internal.p2.engine;
15
16 import java.util.HashMap;
17 import java.util.Map;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.equinox.internal.p2.core.helpers.LogHelper;
20 import org.eclipse.equinox.p2.engine.spi.Touchpoint;
21 import org.eclipse.equinox.p2.metadata.*;
22 import org.eclipse.osgi.util.NLS;
23
24 //TODO This needs to support multiple version of each touchpoint and have a lookup that supports version semantics
25 public class TouchpointManager implements IRegistryChangeListener {
26
27 private static final String PT_TOUCHPOINTS = "touchpoints"; //$NON-NLS-1$
28 private static final String ELEMENT_TOUCHPOINT = "touchpoint"; //$NON-NLS-1$
29 private static final String ATTRIBUTE_CLASS = "class"; //$NON-NLS-1$
30 private static final String ATTRIBUTE_TYPE = "type"; //$NON-NLS-1$
31 private static final String ATTRIBUTE_VERSION = "version"; //$NON-NLS-1$
32
33 private class TouchpointEntry {
34
35 private IConfigurationElement element;
36 private boolean createdExtension = false;
37 private Touchpoint touchpoint = null;
38
39 public TouchpointEntry(IConfigurationElement element) {
40 this.element = element;
41 }
42
43 public Touchpoint getTouchpoint() {
44 if (!createdExtension) {
45 String id = getType();
46 try {
47 Touchpoint touchpointInstance = (Touchpoint) element.createExecutableExtension(ATTRIBUTE_CLASS);
48 if (touchpointInstance != null) {
49 this.touchpoint = touchpointInstance;
50 } else {
51 String errorMsg = NLS.bind(Messages.TouchpointManager_Null_Creating_Touchpoint_Extension, id);
52 throw new CoreException(new Status(IStatus.ERROR, EngineActivator.ID, 1, errorMsg, null));
53 }
54 } catch (CoreException cexcpt) {
55 LogHelper.log(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.TouchpointManager_Exception_Creating_Touchpoint_Extension, id), cexcpt));
56 } catch (ClassCastException ccexcpt) {
57 LogHelper.log(new Status(IStatus.ERROR, EngineActivator.ID, NLS.bind(Messages.TouchpointManager_Exception_Creating_Touchpoint_Extension, id), ccexcpt));
58 }
59 // Mark as created even in error cases,
60 // so exceptions are not logged multiple times
61 createdExtension = true;
62 }
63 return this.touchpoint;
64 }
65
66 public Version getVersion() {
67 try {
68 return Version.create(element.getAttribute(ATTRIBUTE_VERSION));
69 } catch (InvalidRegistryObjectException e) {
70 return null;
71 }
72 }
73
74 public String getType() {
75 try {
76 return element.getAttribute(ATTRIBUTE_TYPE);
77 } catch (InvalidRegistryObjectException e) {
78 return null;
79 }
80 }
81
82 @Override
83 public String toString() {
84 StringBuilder result = new StringBuilder(element.toString());
85 if (createdExtension) {
86 String touchpointString = touchpoint != null ? touchpoint.toString() : "not created"; //$NON-NLS-1$
87 result.append(" => " + touchpointString); //$NON-NLS-1$
88 }
89 return result.toString();
90 }
91 }
92
93 // TODO: Do we really want to store the touchpoints? The danger is
94 // that if two installations are performed simultaneously, then...
95 // TODO: Figure out locking, concurrency requirements for touchpoints.
96 private Map<String, TouchpointEntry> touchpointEntries;
97
98 public TouchpointManager() {
99 RegistryFactory.getRegistry().addRegistryChangeListener(this, EngineActivator.ID);
100 }
101
102 /*
103 * Return the touchpoint which is registered for the given type,
104 * or <code>null</code> if none are registered.
105 */
106 public synchronized Touchpoint getTouchpoint(ITouchpointType type) {
107 if (type == null)
108 throw new IllegalArgumentException(Messages.TouchpointManager_Null_Touchpoint_Type_Argument);
109 return getTouchpoint(type.getId(), type.getVersion().toString());
110 }
111
112 /*
113 * Return the touchpoint which is registered for the given type and optionally version,
114 * or <code>null</code> if none are registered.
115 */
116 public Touchpoint getTouchpoint(String typeId, String versionRange) {
117 if (typeId == null || typeId.length() == 0)
118 throw new IllegalArgumentException(Messages.TouchpointManager_Null_Touchpoint_Type_Argument);
119
120 TouchpointEntry entry = getTouchpointEntries().get(typeId);
121 if (entry == null)
122 return null;
123 if (versionRange != null) {
124 VersionRange range = VersionRange.create(versionRange);
125 if (!range.isIncluded(entry.getVersion()))
126 return null;
127 }
128
129 return entry.getTouchpoint();
130 }
131
132 /*
133 * Construct a map of the extensions that implement the touchpoints extension point.
134 */
135 private synchronized Map<String, TouchpointEntry> getTouchpointEntries() {
136 if (touchpointEntries != null)
137 return touchpointEntries;
138
139 IExtensionPoint point = RegistryFactory.getRegistry().getExtensionPoint(EngineActivator.ID, PT_TOUCHPOINTS);
140 IExtension[] extensions = point.getExtensions();
141 touchpointEntries = new HashMap<>(extensions.length);
142 for (IExtension extension : extensions) {
143 try {
144 IConfigurationElement[] elements = extension.getConfigurationElements();
145 for (IConfigurationElement element : elements) {
146 String elementName = element.getName();
147 if (!ELEMENT_TOUCHPOINT.equalsIgnoreCase(elementName)) {
148 reportError(NLS.bind(Messages.TouchpointManager_Incorrectly_Named_Extension, element.getName(), ELEMENT_TOUCHPOINT));
149 continue;
150 }
151 String id = element.getAttribute(ATTRIBUTE_TYPE);
152 if (id == null) {
153 reportError(NLS.bind(Messages.TouchpointManager_Attribute_Not_Specified, ATTRIBUTE_TYPE));
154 continue;
155 }
156 if (touchpointEntries.get(id) == null) {
157 touchpointEntries.put(id, new TouchpointEntry(element));
158 } else {
159 reportError(NLS.bind(Messages.TouchpointManager_Conflicting_Touchpoint_Types, id));
160 }
161 }
162 }catch (InvalidRegistryObjectException e) {
163 //skip this extension
164 }
165 }
166 return touchpointEntries;
167 }
168
169 static void reportError(String errorMsg) {
170 Status errorStatus = new Status(IStatus.ERROR, EngineActivator.ID, 1, errorMsg, null);
171 LogHelper.log(errorStatus);
172 }
173
174 @Override
175 public synchronized void registryChanged(IRegistryChangeEvent event) {
176 // just flush the cache when something changed. It will be recomputed on demand.
177 touchpointEntries = null;
178 }
179 }