Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ce7c2eb179842fce4bc9fbcd8e3c286226da5214 (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
/*******************************************************************************
 * Copyright (c) 2010, 2011 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 static org.eclipse.jpt.common.core.internal.utility.XPointTools.*;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.core.expressions.Expression;
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.Platform;
import org.eclipse.jpt.common.core.JptCommonCorePlugin;
import org.eclipse.jpt.common.core.internal.utility.XPointTools.XPointException;
import org.eclipse.jpt.common.core.libprov.JptLibraryProviderInstallOperationConfig;
import org.eclipse.jpt.common.core.libval.LibraryValidator;
import org.eclipse.jpt.common.utility.internal.KeyedSet;
import org.eclipse.jpt.common.utility.internal.iterables.FilteringIterable;
import org.eclipse.jpt.common.utility.internal.iterables.TransformationIterable;

public class LibraryValidatorManager {
	
	public static final String EXTENSION_POINT_ID = "libraryValidators"; //$NON-NLS-1$
	public static final String QUALIFIED_EXTENSION_POINT_ID = JptCommonCorePlugin.PLUGIN_ID_ + EXTENSION_POINT_ID;
	public static final String LIBRARY_VALIDATOR_ELEMENT = "libraryValidator"; //$NON-NLS-1$
	public static final String ID_ATTRIBUTE = "id"; //$NON-NLS-1$
	public static final String CLASS_ATTRIBUTE = "class"; //$NON-NLS-1$
	public static final String ENABLEMENT_ELEMENT = "enablement"; //$NON-NLS-1$
	
	
	private static LibraryValidatorManager INSTANCE = new LibraryValidatorManager();
	
	
	public static LibraryValidatorManager instance() {
		return INSTANCE;
	}
	
	
	private KeyedSet<String, LibraryValidatorConfig> libraryValidatorConfigs;
	
	
	private LibraryValidatorManager() {
		this.libraryValidatorConfigs = new KeyedSet<String, LibraryValidatorConfig>();
		readExtensions();
	}
	
	
	/**
	 * Return the {@link LibraryValidator}s 
	 */
	public Iterable<LibraryValidator> getLibraryValidators(final JptLibraryProviderInstallOperationConfig config) {
		return new FilteringIterable<LibraryValidator>(
				new TransformationIterable<LibraryValidatorConfig, LibraryValidator>(
						new FilteringIterable<LibraryValidatorConfig>(
								this.libraryValidatorConfigs.getItemSet()) {
							@Override
							protected boolean accept(LibraryValidatorConfig o) {
								return o.isEnabledFor(config);
							}
						}) {
					@Override
					protected LibraryValidator transform(LibraryValidatorConfig o) {
						return o.getLibraryValidator();
					}
				}) {
			@Override
			protected boolean accept(LibraryValidator o) {
				return o != null;
			}
		};
	}
	
	private void readExtensions() {
		final IExtensionRegistry registry = Platform.getExtensionRegistry();
		
		final IExtensionPoint xpoint 
				= registry.getExtensionPoint(QUALIFIED_EXTENSION_POINT_ID);
		
		if (xpoint == null) {
			throw new IllegalStateException();
		}
		
		final List<IConfigurationElement> configs = new ArrayList<IConfigurationElement>();
		
		for (IExtension extension : xpoint.getExtensions()) {
        	for (IConfigurationElement element : extension.getConfigurationElements()) {
        		configs.add(element);
        	}
		}
		
		for (IConfigurationElement element : configs) {
			if (element.getName().equals(LIBRARY_VALIDATOR_ELEMENT)) {
                readExtension(element);
			}
		}
	}
	
	private void readExtension(IConfigurationElement element) {
		try {
			final LibraryValidatorConfig lvConfig = new LibraryValidatorConfig();
			
			// plug-in id
			lvConfig.setPluginId(element.getContributor().getName());
			
			// resource locator id
			lvConfig.setId(findRequiredAttribute(element, ID_ATTRIBUTE));
			
			if (this.libraryValidatorConfigs.containsKey(lvConfig.getId())) {
				logDuplicateExtension(QUALIFIED_EXTENSION_POINT_ID, ID_ATTRIBUTE, lvConfig.getId());
				throw new XPointException();
			}
			
			// resource locator class name
			lvConfig.setClassName(findRequiredAttribute(element, CLASS_ATTRIBUTE));
			
			// enablement
			for (IConfigurationElement child : element.getChildren()) {
				String childName = child.getName();
				if (childName.equals(ENABLEMENT_ELEMENT)) {
					Expression expr;
					try {
						expr = ExpressionConverter.getDefault().perform(child);
					}
					catch (CoreException e) {
						log(e);
						throw new XPointException();
					}
					lvConfig.setEnablementCondition(expr);
				}
			}
			
			this.libraryValidatorConfigs.addItem(lvConfig.getId(), lvConfig);
		}
		catch (XPointException e) {
			// Ignore and continue. The problem has already been reported to the user
			// in the log.
		}
	}
}

Back to the top