Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3bd19338575028493ca07f3d1373fbbcee73e061 (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
238
239
240
241
/*******************************************************************************
 * Copyright (c) 2006, 2010 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;

import static org.eclipse.jpt.core.internal.XPointUtil.*;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.resources.IProject;
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.JpaPlatformFactory;
import org.eclipse.jpt.core.JptCorePlugin;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.iterables.CompositeIterable;
import org.eclipse.jpt.utility.internal.iterables.FilteringIterable;
import org.eclipse.jpt.utility.internal.iterables.TransformationIterable;

/**
 * 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 = "jpaPlatforms"; //$NON-NLS-1$
	private static final String QUALIFIED_EXTENSION_ID = JptCorePlugin.PLUGIN_ID_ + EXTENSION_ID;
	private static final String PLATFORM_ELEMENT_NAME = "jpaPlatform"; //$NON-NLS-1$
	private static final String ID_ATTRIBUTE_NAME = "id"; //$NON-NLS-1$
	private static final String LABEL_ATTRIBUTE_NAME = "label"; //$NON-NLS-1$
	private static final String FACTORY_CLASS_ATTRIBUTE_NAME = "factoryClass"; //$NON-NLS-1$
	private static final String JPA_FACET_VERSION_ATTRIBUTE_NAME = "jpaFacetVersion"; //$NON-NLS-1$
	private static final String DEFAULT_ATTRIBUTE_NAME = "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 (IConfigurationElement configElement : this.getConfigElements()) {
			this.addConfigElementTo(configElement, configElements);
		}
		return configElements;
	}

	/**
	 * Return the configuration elements from the Eclipse platform extension
	 * registry.
	 */
	private Iterable<IConfigurationElement> getConfigElements() {
		return new CompositeIterable<IConfigurationElement>(
				new TransformationIterable<IExtension, Iterable<IConfigurationElement>>(this.getExtensions()) {
					@Override
					protected Iterable<IConfigurationElement> transform(IExtension extension) {
						return CollectionTools.iterable(extension.getConfigurationElements());
					}
				}
		);
	}

	private Iterable<IExtension> getExtensions() {
		return CollectionTools.iterable(this.getExtensionPoint().getExtensions());
	}

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

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

		String id = configElement.getAttribute(ID_ATTRIBUTE_NAME);
		if (configElements.containsKey(id)) {
			logDuplicateExtension(QUALIFIED_EXTENSION_ID, id);
		}
		else {
			configElements.put(id, configElement);
		}
	}

	/**
	 * check *all* attributes before returning
	 */
	private boolean configElementIsValid(IConfigurationElement configElement) {
		boolean valid = true;
		if (configElement.getAttribute(ID_ATTRIBUTE_NAME) == null) {
			logMissingAttribute(configElement, ID_ATTRIBUTE_NAME);
			valid = false;
		}
		if (configElement.getAttribute(LABEL_ATTRIBUTE_NAME) == null) {
			logMissingAttribute(configElement, LABEL_ATTRIBUTE_NAME);
			valid = false;
		}
		if (configElement.getAttribute(FACTORY_CLASS_ATTRIBUTE_NAME) == null) {
			logMissingAttribute(configElement, FACTORY_CLASS_ATTRIBUTE_NAME);
			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 Iterable<String> getJpaPlatformIds() {
		return 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(LABEL_ATTRIBUTE_NAME);
	}
	
	/**
	 * Return whether the platform represented by the given id supports the specified JPA facet version.
	 * This does not active the JPA platform's plug-in.
	 */
	public boolean platformSupportsJpaFacetVersion(String platformId, String jpaFacetVersion) {
		IConfigurationElement configElement = this.jpaPlatformConfigurationElements.get(platformId);
		return configElementSupportsJpaFacetVersion(configElement, jpaFacetVersion);
	}
	
	boolean configElementSupportsJpaFacetVersion(IConfigurationElement configElement, String jpaFacetVersion) {
		
		// config element supports version if it explicitly sets it to that version
		// or if it specifies no version at all
		String ver = configElement.getAttribute(JPA_FACET_VERSION_ATTRIBUTE_NAME);
		return (ver == null) || ver.equals(jpaFacetVersion);
	}
	
	/**
	 * Return the IDs for the registered JPA platforms that support the
	 * specified JPA facet version.
	 * This does not activate the JPA platforms' plug-in.
	 */
	public Iterable<String> getJpaPlatformIdsForJpaFacetVersion(final String jpaFacetVersion) {
		return new TransformationIterable<IConfigurationElement, String>(this.getConfigurationElementsForJpaFacetVersion(jpaFacetVersion)) {
				@Override
				protected String transform(IConfigurationElement configElement) {
					return configElement.getAttribute(ID_ATTRIBUTE_NAME);
				}
			};
	}
	
	private Iterable<IConfigurationElement> getConfigurationElementsForJpaFacetVersion(final String jpaFacetVersion) {
		return new FilteringIterable<IConfigurationElement>(this.jpaPlatformConfigurationElements.values()) {
				@Override
				protected boolean accept(IConfigurationElement configElement) {
					return configElementSupportsJpaFacetVersion(configElement, jpaFacetVersion);
				}
			};
	}
	
	/**
	 * 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(String jpaFacetVersion) {
		for (Map.Entry<String, IConfigurationElement> entry: this.jpaPlatformConfigurationElements.entrySet()) {
			String defaultFlag = entry.getValue().getAttribute(DEFAULT_ATTRIBUTE_NAME);
			String platformId = entry.getKey();
			if ((defaultFlag != null) && defaultFlag.equals("true")
					&& platformSupportsJpaFacetVersion(platformId, jpaFacetVersion)) { //$NON-NLS-1$
				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(IProject project) {
		String id = JptCorePlugin.getJpaPlatformId(project);
		IConfigurationElement configElement = this.jpaPlatformConfigurationElements.get(id);
		if (configElement == null) {
			log(JptCoreMessages.PLATFORM_ID_DOES_NOT_EXIST, id, project.getName());
			return null;
		}
		JpaPlatformFactory platformFactory = instantiate(
					configElement.getContributor().getName(), QUALIFIED_EXTENSION_ID, 
					configElement.getAttribute(FACTORY_CLASS_ATTRIBUTE_NAME), JpaPlatformFactory.class);
		if (platformFactory == null) {
			throw new IllegalArgumentException(id);
		}
		return platformFactory.buildJpaPlatform(id);
	}
}

Back to the top