Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 38213f9792442685a0f106af6c6af5de79596bb1 (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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*******************************************************************************
 * Copyright (c) 2006, 2008 Oracle. 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:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.core.internal.platform;

import java.util.HashMap;
import java.util.Iterator;
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.Platform;
import org.eclipse.jpt.core.JpaPlatform;
import org.eclipse.jpt.core.JptCorePlugin;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.iterators.CompositeIterator;
import org.eclipse.jpt.utility.internal.iterators.ReadOnlyIterator;
import org.eclipse.jpt.utility.internal.iterators.TransformationIterator;

/**
 * Singleton registry for storing all the registered JPA platform configuration
 * elements and instantiating JPA platforms from them.
 */
public class JpaPlatformRegistry {

	private final HashMap<String, IConfigurationElement> jpaPlatformConfigurationElements;


	// singleton
	private static final JpaPlatformRegistry INSTANCE = new JpaPlatformRegistry();

	/**
	 * Return the singleton.
	 */
	public static JpaPlatformRegistry instance() {
		return INSTANCE;
	}

	private static final String EXTENSION_ID = "jpaPlatform"; //$NON-NLS-1$
	private static final String EL_PLATFORM = "jpaPlatform"; //$NON-NLS-1$	
	private static final String AT_ID = "id"; //$NON-NLS-1$	
	private static final String AT_LABEL = "label"; //$NON-NLS-1$	
	private static final String AT_CLASS = "class"; //$NON-NLS-1$
	private static final String AT_DEFAULT = "default"; //$NON-NLS-1$


	// ********** constructor/initialization **********

	/**
	 * ensure single instance
	 */
	private JpaPlatformRegistry() {
		super();
		this.jpaPlatformConfigurationElements = this.buildJpaPlatformConfigurationElements();
	}
	
	
	private HashMap<String, IConfigurationElement> buildJpaPlatformConfigurationElements() {
		HashMap<String, IConfigurationElement> configElements = new HashMap<String, IConfigurationElement>();
		for (Iterator<IConfigurationElement> stream = this.configElements(); stream.hasNext(); ) {
			this.addConfigElementTo(stream.next(), configElements);
		}
		return configElements;
	}

	/**
	 * Return the configuration elements from the Eclipse platform extension
	 * registry.
	 */
	private Iterator<IConfigurationElement> configElements() {
		return new CompositeIterator<IConfigurationElement>(
				new TransformationIterator<IExtension, Iterator<IConfigurationElement>>(this.extensions()) {
					@Override
					protected Iterator<IConfigurationElement> transform(IExtension extension) {
						return CollectionTools.iterator(extension.getConfigurationElements());
					}
				}
		);
	}

	private Iterator<IExtension> extensions() {
		return CollectionTools.iterator(this.extensionPoint().getExtensions());
	}

	private IExtensionPoint extensionPoint() {
		return Platform.getExtensionRegistry().getExtensionPoint(JptCorePlugin.PLUGIN_ID, EXTENSION_ID);
	}

	private void addConfigElementTo(IConfigurationElement configElement, HashMap<String, IConfigurationElement> configElements) {
		if ( ! configElement.getName().equals(EL_PLATFORM)) {
			return;
		}
		if ( ! this.configElementIsValid(configElement)) {
			return;
		}

		String id = configElement.getAttribute(AT_ID);
		IConfigurationElement prev = configElements.get(id);
		if (prev == null) {
			configElements.put(id, configElement);
		} else {
			this.logDuplicatePlatform(prev, configElement);
		}
	}

	/**
	 * check *all* attributes before returning
	 */
	private boolean configElementIsValid(IConfigurationElement configElement) {
		boolean valid = true;
		if (configElement.getAttribute(AT_ID) == null) {
			this.logMissingAttribute(configElement, AT_ID);
			valid = false;
		}
		if (configElement.getAttribute(AT_LABEL) == null) {
			logMissingAttribute(configElement, AT_LABEL);
			valid = false;
		}
		if (configElement.getAttribute(AT_CLASS) == null) {
			logMissingAttribute(configElement, AT_CLASS);
			valid = false;
		}
		return valid;
	}


	// ********** public methods **********

	/**
	 * Return the IDs for the registered JPA platforms.
	 * This does not activate any of the JPA platforms' plug-ins.
	 */
	public Iterator<String> jpaPlatformIds() {
		return new ReadOnlyIterator<String>(this.jpaPlatformConfigurationElements.keySet());
	}
	
	/**
	 * Return whether the platform id is registered
	 */
	public boolean containsPlatform(String platformId) {
		return this.jpaPlatformConfigurationElements.containsKey(platformId);
	}
	
	/**
	 * Return the label for the JPA platform with the specified ID.
	 * This does not activate the JPA platform's plug-in.
	 */
	public String getJpaPlatformLabel(String id) {
		return this.jpaPlatformConfigurationElements.get(id).getAttribute(AT_LABEL);
	}
	
	/**
	 * Return the ID for a JPA platform registered as a default platform.
	 * Returns null if there are no such registered platforms.
	 * Returns the first platform ID if there are multiple such registered platforms.
	 */
	public String getDefaultJpaPlatformId() {
		for (String platformId : jpaPlatformConfigurationElements.keySet()) {
			if ("true".equals(jpaPlatformConfigurationElements.get(platformId).getAttribute(AT_DEFAULT))) {
				return platformId;
			}
		}
		return null;
	}
	
	/**
	 * Return a new JPA platform for the specified ID.
	 * NB: This should only be called when instantiating a JPA platform
	 * when building a new JPA project.
	 * Unlike other registry methods, invoking this method may activate 
	 * the plug-in.
	 */
	public JpaPlatform getJpaPlatform(String id) {
		IConfigurationElement configElement = this.jpaPlatformConfigurationElements.get(id);
		if (configElement == null) {
			throw new IllegalArgumentException(id);
		}
		JpaPlatform platform;
		try {
			platform = (JpaPlatform) configElement.createExecutableExtension(AT_CLASS);
		} catch (CoreException ex) {
			this.logFailedInstantiation(configElement, ex);
			throw new IllegalArgumentException(id);
		}
		platform.setId(id);
		return platform;
	}


	// ********** errors **********

	// TODO externalize strings
	private void logMissingAttribute(IConfigurationElement configElement, String attributeName) {
		String message = 
			"An extension element \""
			+ configElement.getName()
			+ "\" in plugin \""
			+ configElement.getContributor().getName()
			+ "\" is missing a required attribute \""
			+ attributeName
			+ "\".";
		JptCorePlugin.log(message);
	}

	// TODO externalize strings
	private void logDuplicatePlatform(IConfigurationElement prevConfigElement, IConfigurationElement newConfigElement) {
		String message =
			"The plugins \""
			+ prevConfigElement.getContributor().getName()
			+ "\" and \""
			+ newConfigElement.getContributor().getName()
			+ "\" have registered a duplicate attribute \"id\" "
			+ "for the extension element \"jpaPlatform\".";
		JptCorePlugin.log(message);
	}

	// TODO externalize strings
	private void logFailedInstantiation(IConfigurationElement configElement, CoreException ex) {
		String message =
			"Could not instantiate the class \""
			+ configElement.getAttribute(AT_CLASS)
			+ "\" for the extension element \""
			+ configElement.getName()
			+ "\" in the plugin \""
			+ configElement.getContributor().getName()
			+ "\".";
		JptCorePlugin.log(message);
		JptCorePlugin.log(ex);
	}

}

Back to the top