Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2ea9a8a1c5f1c80c2d6208268968e83d73874b83 (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) 2012, 2013 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.common.core.internal.libval;

import java.util.ArrayList;
import org.eclipse.core.expressions.ExpressionConverter;
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.RegistryFactory;
import org.eclipse.jpt.common.core.JptCommonCoreMessages;
import org.eclipse.jpt.common.core.internal.InternalJptWorkspace;
import org.eclipse.jpt.common.core.internal.plugin.JptCommonCorePlugin;
import org.eclipse.jpt.common.core.internal.utility.ExtensionPointTools;
import org.eclipse.jpt.common.core.libprov.JptLibraryProviderInstallOperationConfig;
import org.eclipse.jpt.common.core.libval.LibraryValidator;
import org.eclipse.jpt.common.core.libval.LibraryValidatorManager;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.StringTools;
import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
import org.eclipse.jpt.common.utility.internal.iterable.TransformationIterable;

/**
 * Library validator manager.
 */
public class InternalLibraryValidatorManager
	implements LibraryValidatorManager
{
	/**
	 * The library validator manager's Dali workspace.
	 */
	private final InternalJptWorkspace jptWorkspace;

	/**
	 * The library validator configs.
	 * Initialized during construction.
	 */
	private final ArrayList<LibraryValidatorConfig> libraryValidatorConfigs = new ArrayList<LibraryValidatorConfig>();


	// ********** extension point element and attribute names **********

	private static final String SIMPLE_EXTENSION_POINT_NAME = "libraryValidators"; //$NON-NLS-1$
	private static final String LIBRARY_VALIDATOR_ELEMENT = "libraryValidator"; //$NON-NLS-1$
	private static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
	private static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
	private static final String ENABLEMENT_ELEMENT = "enablement"; //$NON-NLS-1$


	/**
	 * Internal - called from only
	 * {@link InternalJptWorkspace#buildLibraryValidatorManager()}.
	 */
	public InternalLibraryValidatorManager(InternalJptWorkspace jptWorkspace) {
		super();
		this.jptWorkspace = jptWorkspace;
		this.initialize();
	}


	// ********** initialization **********

	private void initialize() {
		IExtensionPoint extensionPoint = this.getExtensionPoint();
		if (extensionPoint == null) {
			throw new IllegalStateException("missing extension point: " + this.getExtensionPointName()); //$NON-NLS-1$
		}

		for (IExtension extension : extensionPoint.getExtensions()) {
			for (IConfigurationElement element : extension.getConfigurationElements()) {
				String elementName = element.getName();  // probably cannot be null
				if (elementName.equals(LIBRARY_VALIDATOR_ELEMENT)) {
					LibraryValidatorConfig config = this.buildLibraryValidatorConfig(element);
					if (config != null) {
						this.libraryValidatorConfigs.add(config);
					}
				}
			}
		}
	}

	/**
	 * Return <code>null</code> if there is any sort of problem building a
	 * library validator config from the specified configuration element.
	 */
	private LibraryValidatorConfig buildLibraryValidatorConfig(IConfigurationElement element) {
		String contributor = element.getContributor().getName();
		// id
		String id = element.getAttribute(ID_ATTRIBUTE);
		if (StringTools.isBlank(id)) {
			this.logMissingAttribute(element, ID_ATTRIBUTE);
			return null;
		}
		if (this.containsLibraryValidatorConfig(id)) {
			this.logError(JptCommonCoreMessages.REGISTRY_DUPLICATE, this.getExtensionPointName(), contributor, ID_ATTRIBUTE, id);
			return null;  // drop any duplicate library validators
		}

		// library validator class name
		String className = element.getAttribute(CLASS_ATTRIBUTE);
		if (className == null) {
			this.logMissingAttribute(element, CLASS_ATTRIBUTE);
			return null;
		}

		LibraryValidatorConfig config = new LibraryValidatorConfig(this, id, className);

		// enablement (optional, but only one allowed)
		for (IConfigurationElement child : element.getChildren()) {
			if (child.getName().equals(ENABLEMENT_ELEMENT)) {
				if (config.getEnablementExpression() != null) {
					this.logMultipleEnablements(element);
					return null;
				}
				try {
					config.setEnablementExpression(ExpressionConverter.getDefault().perform(child));
				} catch (CoreException ex) {
					JptCommonCorePlugin.instance().logError(ex);
					return null;
				}
			}
		}

		config.setPluginID(contributor);
		return config;
	}


	// ********** library validators **********

	public Iterable<LibraryValidator> getLibraryValidators() {
		return IterableTools.removeNulls(this.getLibraryValidators_());
	}

	/**
	 * Result may contain <code>null</code>s.
	 */
	private Iterable<LibraryValidator> getLibraryValidators_() {
		return new TransformationIterable<LibraryValidatorConfig, LibraryValidator>(
				this.libraryValidatorConfigs,
				LibraryValidatorConfig.LIBRARY_VALIDATOR_TRANSFORMER
			);
	}

	public Iterable<LibraryValidator> getLibraryValidators(JptLibraryProviderInstallOperationConfig installConfig) {
		return IterableTools.removeNulls(this.getLibraryValidators_(installConfig));
	}

	/**
	 * Result may contain <code>null</code>s.
	 */
	private Iterable<LibraryValidator> getLibraryValidators_(JptLibraryProviderInstallOperationConfig installConfig) {
		return new TransformationIterable<LibraryValidatorConfig, LibraryValidator>(
				this.getLibraryValidatorConfigs(installConfig),
				LibraryValidatorConfig.LIBRARY_VALIDATOR_TRANSFORMER
			);
	}

	/**
	 * Return the library validator configs enabled for the specified
	 * install config.
	 */
	private Iterable<LibraryValidatorConfig> getLibraryValidatorConfigs(JptLibraryProviderInstallOperationConfig installConfig) {
		return IterableTools.filter(
				this.libraryValidatorConfigs,
				new LibraryValidatorConfig.IsEnabled(installConfig)
			);
	}

	private boolean containsLibraryValidatorConfig(String id) {
		return this.getLibraryValidatorConfig(id) != null;
	}

	private LibraryValidatorConfig getLibraryValidatorConfig(String id) {
		for (LibraryValidatorConfig config : this.libraryValidatorConfigs) {
			if (config.getID().equals(id)) {
				return config;
			}
		}
		return null;
	}


	// ********** logging **********

	private void logError(String msg, Object... args) {
		this.getPlugin().logError(msg, args);
	}

	private void logMissingAttribute(IConfigurationElement element, String attributeName) {
		this.getPlugin().logError(ExtensionPointTools.buildMissingAttributeMessage(element, attributeName));
	}

	private void logMultipleEnablements(IConfigurationElement element) {
		this.getPlugin().logError(JptCommonCoreMessages.MULTIPLE_LIBRARY_VALIDATOR_ENABLEMENTS,
				element.getDeclaringExtension().getExtensionPointUniqueIdentifier(),
				element.getContributor().getName()
			);
	}


	// ********** misc **********

	public InternalJptWorkspace getJptWorkspace() {
		return this.jptWorkspace;
	}

	String getExtensionPointName() {
		return this.getPluginID() + '.' + SIMPLE_EXTENSION_POINT_NAME;
	}

	private IExtensionPoint getExtensionPoint() {
		return this.getExtensionRegistry().getExtensionPoint(this.getPluginID(), SIMPLE_EXTENSION_POINT_NAME);
	}

	private IExtensionRegistry getExtensionRegistry() {
		return RegistryFactory.getRegistry();
	}

	private String getPluginID() {
		return this.getPlugin().getPluginID();
	}

	private JptCommonCorePlugin getPlugin() {
		return JptCommonCorePlugin.instance();
	}

	@Override
	public String toString() {
		return ObjectTools.toString(this, this.libraryValidatorConfigs);
	}
}

Back to the top