Skip to main content
summaryrefslogtreecommitdiffstats
blob: e6ec0d1331b7509c0c6d54a1e1ba9e334aa6e460 (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
/*******************************************************************************
 *  Copyright (c) 2006, 2007 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.ui.internal.platform;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
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.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jpt.ui.JpaPlatformUi;
import org.eclipse.jpt.ui.JptUiPlugin;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.iterators.CompositeIterator;
import org.eclipse.jpt.utility.internal.iterators.TransformationIterator;

public class JpaPlatformUiRegistry 
{
	// singleton
	private static final JpaPlatformUiRegistry INSTANCE = new JpaPlatformUiRegistry();

	/**
	 * Return the singleton.
	 */
	public static JpaPlatformUiRegistry 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_CLASS =
		"class"; //$NON-NLS-1$	
		
	// key: String jpaPlatform id  value: IConfigurationElement class descriptor
	private Map<String, IConfigurationElement> jpaPlatforms;
	
	
	/* (non Java doc)
	 * restrict access
	 */
	private JpaPlatformUiRegistry() {
		buildJpaPlatforms();
	}
	
	
	private void buildJpaPlatforms() {
		this.jpaPlatforms = new HashMap<String, IConfigurationElement>();
		
		for (Iterator<IConfigurationElement> stream = allConfigElements(); stream.hasNext(); ) {
			buildJpaPlatform(stream.next());
		}
	}
	
	private void buildJpaPlatform(IConfigurationElement configElement) {
		if (! configElement.getName().equals(EL_PLATFORM)) {
			return;
		}
		
		String platformId = configElement.getAttribute(AT_ID);
		String platformClass = configElement.getAttribute(AT_CLASS);
		
		if ((platformId == null) || (platformClass == null)) {
			if (platformId == null) {
				reportMissingAttribute(configElement, AT_ID);
			}
			if (platformClass == null) {
				reportMissingAttribute(configElement, AT_CLASS);
			}
			return;
		}
		
		if (this.jpaPlatforms.containsKey(platformId)) {
			IConfigurationElement otherConfigElement = this.jpaPlatforms.get(platformId);
			reportDuplicatePlatform(configElement, otherConfigElement);
		}
		
		this.jpaPlatforms.put(platformId, configElement);
	}
	
	public JpaPlatformUi jpaPlatform(String platformId) {
		IConfigurationElement registeredConfigElement = this.jpaPlatforms.get(platformId);
		
		if (registeredConfigElement == null) {
			return null;
		}
		
		try {
			return (JpaPlatformUi) registeredConfigElement.createExecutableExtension(AT_CLASS);
		}
		catch (CoreException ce) {
			reportFailedInstantiation(registeredConfigElement);
			return null;
		}
	}
	
	private Iterator<IConfigurationElement> allConfigElements() {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint extensionPoint = 
			registry.getExtensionPoint(JptUiPlugin.PLUGIN_ID, EXTENSION_ID);
		IExtension[] extensions = extensionPoint.getExtensions();
		
		return new CompositeIterator<IConfigurationElement>(
				new TransformationIterator<IExtension, Iterator<IConfigurationElement>>(CollectionTools.iterator(extensions)) {
					@Override
					protected Iterator<IConfigurationElement> transform(IExtension extension) {
						return CollectionTools.iterator(extension.getConfigurationElements());
					}
				}
			);
	}
	
	// TODO externalize strings
	private void reportMissingAttribute(IConfigurationElement configElement, String attributeName) {
		String message = 
			"An extension element \""
			+ configElement.getName()
			+ "\" in plugin \""
			+ configElement.getContributor().getName()
			+ "\" is missing a required attribute \""
			+ attributeName
			+ "\".";
		JptUiPlugin.log(message);
	}
	
	// TODO externalize strings
	private void reportDuplicatePlatform(
			IConfigurationElement oneConfigElement, IConfigurationElement otherConfigElement) {
		String message =
			"The plugins \""
			+ oneConfigElement.getContributor().getName()
			+ "\" and \""
			+ otherConfigElement.getContributor().getName()
			+ "\" have registered a duplicate attribute \"id\" "
			+ "for the extension element \"jpaVendor\".";
		JptUiPlugin.log(message);
	}
		
	// TODO externalize strings
	private void reportFailedInstantiation(IConfigurationElement configElement) {
		String message =
			"Could not instantiate the class \""
			+ configElement.getAttribute(AT_CLASS)
			+ "\" for the extension element \""
			+ configElement.getName()
			+ "\" in the plugin \""
			+ configElement.getContributor().getName()
			+ "\".";
		JptUiPlugin.log(message);
	}
}

Back to the top