Skip to main content
summaryrefslogtreecommitdiffstats
blob: b2bcd0bc5cb645116c4262c9dd09c97beb3aa53d (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package org.eclipse.wst.web.internal.facet;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.Platform;
import org.eclipse.wst.common.project.facet.core.IProjectFacet;
import org.eclipse.wst.common.project.facet.core.IProjectFacetVersion;
import org.eclipse.wst.common.project.facet.core.ProjectFacetsManager;
import org.eclipse.wst.common.project.facet.core.runtime.IRuntimeComponentType;
import org.eclipse.wst.common.project.facet.core.runtime.IRuntimeComponentVersion;
import org.eclipse.wst.common.project.facet.core.runtime.RuntimeManager;
import org.eclipse.wst.web.internal.WSTWebPlugin;

public class RuntimePresetMappingRegistry {

	static final String EXTENSION_POINT = "runtimePresetMappings"; //$NON-NLS-1$

	static final String ELEMENT_MAPPING = "mapping"; //$NON-NLS-1$

	static final String ATTRIBUTE_ID = "id"; //$NON-NLS-1$

	static final String ATTRIBUTE_FACET_RUNTIME_TYPE_ID = "facetRuntimeTypeID"; //$NON-NLS-1$

	static final String ATTRIBUTE_FACET_RUNTIME_VERSION = "facetRuntimeVersion"; //$NON-NLS-1$

	static final String ATTRIBUTE_FACET_ID = "facetID"; //$NON-NLS-1$

	static final String ATTRIBUTE_FACET_VERSION = "facetVersion"; //$NON-NLS-1$

	static final String ATTRIBUTE_PRESET_ID = "presetID"; //$NON-NLS-1$

	public static RuntimePresetMappingRegistry INSTANCE = new RuntimePresetMappingRegistry();

	private List<MappingDescriptor> descriptors = null;

	public String getPresetID(String facetRuntimeTypeID,
			String facetRuntimeVersion, String facetID, String facetVersion) {
		if (descriptors == null) {
			readDescriptors();
		}
		for (MappingDescriptor descriptor : descriptors) {
			if(matches(facetRuntimeTypeID, descriptor.getFacetRuntimeTypeID()) &&
			   matches(facetRuntimeVersion, descriptor.getFacetRuntimeVersion()) &&
			   matches(facetID, descriptor.getFacetID()) &&
			   matches(facetVersion, descriptor.getFacetVersion())){
				return descriptor.getPresetID();
			}
		}
		return null;
	}

	/**
	 * Returns true if the value is matched by the pattern The pattern consists
	 * of a common deliminated list of simple patterns Each simple pattern has
	 * an optional starting or ending * so a String.startsWith() or
	 * String.endsWith(). Both may be combined to compute a String.indexOf() !=
	 * -1
	 *
	 * @param value
	 * @param pattern
	 * @return
	 */
	private static boolean matches(String value, String pattern){
		StringTokenizer strTok = new StringTokenizer(pattern, ","); //$NON-NLS-1$
		while(strTok.hasMoreTokens()){
			String simplePattern = strTok.nextToken().trim();
			if(simplePattern.startsWith("*")){ //$NON-NLS-1$
				if(simplePattern.length() < 2){
					return true; // i.e. *
				}
				if(simplePattern.endsWith("*")){ //$NON-NLS-1$
					if(simplePattern.length() < 3){ 
						return true; // i.e. **
					}
					if(value.indexOf(simplePattern.substring(1, simplePattern.length()-2)) != -1){
						return true;
					}
				} else {
					if(value.endsWith(simplePattern.substring(1))){
						return true;
					}
				}
			} else if(simplePattern.endsWith("*")){ //$NON-NLS-1$
				if(value.startsWith(simplePattern.substring(0, simplePattern.length()-2))){
					return true;
				}
			} else if(value.equals(simplePattern)){
				return true;
			}
		}
		return false;
	}
	
	private static List <String> getStaticTokens(String pattern){
		List <String> staticTokens = new ArrayList <String> ();
		StringTokenizer strTok = new StringTokenizer(pattern, ","); //$NON-NLS-1$
		while(strTok.hasMoreTokens()){
			String simplePattern = strTok.nextToken().trim();
			if(!simplePattern.startsWith("*") && !simplePattern.endsWith("*")){ //$NON-NLS-1$ //$NON-NLS-2$
				staticTokens.add(simplePattern);
			}
		}
		return staticTokens;
	}
	
	
	private void readDescriptors() {
		descriptors = new ArrayList<MappingDescriptor>();
		IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(WSTWebPlugin.PLUGIN_ID, EXTENSION_POINT);
		if (point == null)
			return;
		IConfigurationElement[] elements = point.getConfigurationElements();
		for (int i = 0; i < elements.length; i++) {
			IConfigurationElement element = elements[i];
			if (ELEMENT_MAPPING.equals(element.getName())) {
				String id = element.getAttribute(ATTRIBUTE_ID);
				if (null == id || id.trim().length() == 0) {
					WSTWebPlugin.logError("Extension: " + EXTENSION_POINT + " Element: " + ELEMENT_MAPPING + " is missing attribute " + ATTRIBUTE_ID); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					continue;
				}

				String runtimeID = element.getAttribute(ATTRIBUTE_FACET_RUNTIME_TYPE_ID);
				if (null == runtimeID || runtimeID.trim().length() == 0) {
					WSTWebPlugin.logError("Extension: " + EXTENSION_POINT + " Element: " + ELEMENT_MAPPING + " is missing attribute " + ATTRIBUTE_FACET_RUNTIME_TYPE_ID); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					continue;
				}

				List <String> staticRuntimeIDs = getStaticTokens(runtimeID);
				List <IRuntimeComponentType> staticRuntimeTypes = new ArrayList<IRuntimeComponentType>();
				for(String staticRuntimeID : staticRuntimeIDs){
					try {
						IRuntimeComponentType runtimeType = RuntimeManager.getRuntimeComponentType(staticRuntimeID);
						if(runtimeType != null){
							staticRuntimeTypes.add(runtimeType);
						}
					} catch (IllegalArgumentException e) {
						WSTWebPlugin.logError("Extension: " + EXTENSION_POINT + " Element: " + ELEMENT_MAPPING + " defined invalid attribute " + ATTRIBUTE_FACET_RUNTIME_TYPE_ID + ": " + runtimeID +" unable to resolve runtime: "+staticRuntimeID, e); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
					}
				}
					
				String runtimeVersionStr = element.getAttribute(ATTRIBUTE_FACET_RUNTIME_VERSION);
				if (null == runtimeVersionStr || runtimeVersionStr.trim().length() == 0) {
					WSTWebPlugin.logError("Extension: " + EXTENSION_POINT + " Element: " + ELEMENT_MAPPING + " is missing attribute " + ATTRIBUTE_FACET_RUNTIME_VERSION); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					continue;
				}
				if(!staticRuntimeTypes.isEmpty()){
					List <String> staticRuntimeVersions = getStaticTokens(runtimeVersionStr);
					for(String staticVersion : staticRuntimeVersions){
						boolean foundVersion = false;
						for(int k=0;k<staticRuntimeTypes.size() && !foundVersion;k++){
							IRuntimeComponentType runtimeType = staticRuntimeTypes.get(k);
							try {
								runtimeType.getVersion(staticVersion);
								foundVersion = true;
							} catch (IllegalArgumentException e) {
								//eat it
							}
						}
						if(!foundVersion){
							StringBuffer validVersions = new StringBuffer(" valid versions include: "); //$NON-NLS-1$
							for(IRuntimeComponentType runtimeType : staticRuntimeTypes) {
								validVersions.append("\n"); //$NON-NLS-1$
								validVersions.append(runtimeType.getId());
								validVersions.append(": "); //$NON-NLS-1$
								for (Iterator<IRuntimeComponentVersion> iterator = runtimeType.getVersions().iterator(); iterator.hasNext();) {
									validVersions.append(iterator.next().getVersionString());
									if (iterator.hasNext()) {
										validVersions.append(" "); //$NON-NLS-1$
									}
								}
							}
							WSTWebPlugin.logError("Extension: " + EXTENSION_POINT + " Element: " + ELEMENT_MAPPING + " defined invalid attribute " + ATTRIBUTE_FACET_RUNTIME_VERSION + ": " + staticVersion //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
									+ validVersions);
						}
					}
				}
				
				String facetID = element.getAttribute(ATTRIBUTE_FACET_ID);
				if (null == facetID || facetID.trim().length() == 0) {
					WSTWebPlugin.logError("Extension: " + EXTENSION_POINT + " Element: " + ELEMENT_MAPPING + " is missing attribute " + ATTRIBUTE_FACET_ID); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					continue;
				}
				
				List <String> staticFacetIDs = getStaticTokens(facetID);
				List <IProjectFacet> staticFacets = new ArrayList <IProjectFacet>();
				for(String staticFacetID:staticFacetIDs){
					try {
						IProjectFacet facet = ProjectFacetsManager.getProjectFacet(staticFacetID);
						if(null != facet){
							staticFacets.add(facet);
						}
					} catch (IllegalArgumentException e) {
						WSTWebPlugin.logError("Extension: " + EXTENSION_POINT + " Element: " + ELEMENT_MAPPING + " defined invalid attribute " + ATTRIBUTE_FACET_ID + ": " + staticFacetID, e); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
					}
				}
				
				String facetVersionStr = element.getAttribute(ATTRIBUTE_FACET_VERSION);
				if (null == facetVersionStr || facetVersionStr.trim().length() == 0) {
					WSTWebPlugin.logError("Extension: " + EXTENSION_POINT + " Element: " + ELEMENT_MAPPING + " is missing attribute " + ATTRIBUTE_FACET_VERSION); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					continue;
				}
				List <String>staticFacetVersionStrs = getStaticTokens(facetVersionStr);
				if(!staticFacets.isEmpty() && !staticFacetVersionStrs.isEmpty()){
					for(String staticFacetVersion:staticFacetVersionStrs){
						boolean foundFacetVersion = false;
						for(int k=0;k< staticFacets.size() && !foundFacetVersion; k++) {
							IProjectFacet staticFacet = staticFacets.get(k);
							try {
								IProjectFacetVersion staticVersion = staticFacet.getVersion(staticFacetVersion);
								if(staticVersion != null){
									foundFacetVersion = true;
								}
							} catch (IllegalArgumentException e) {
								//eat it
							}
						}
						if(!foundFacetVersion){
							StringBuffer validVersions = new StringBuffer(" valid versions include: "); //$NON-NLS-1$
							for(IProjectFacet staticFacet:staticFacets){
								validVersions.append("\n"); //$NON-NLS-1$
								validVersions.append(staticFacet.getId());
								validVersions.append(": "); //$NON-NLS-1$
								for (Iterator<IProjectFacetVersion> iterator = staticFacet.getVersions().iterator(); iterator.hasNext();) {
									validVersions.append(iterator.next().getVersionString());
									if (iterator.hasNext()) {
										validVersions.append(" "); //$NON-NLS-1$
									}
								}
							}
							WSTWebPlugin.logError("Extension: " + EXTENSION_POINT + " Element: " + ELEMENT_MAPPING + " defined invalid attribute " + ATTRIBUTE_FACET_VERSION + ": " + staticFacetVersion //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
									+ validVersions);
							continue;
						}
					}	
				}
				String presetID = element.getAttribute(ATTRIBUTE_PRESET_ID);
				if (null == presetID || presetID.trim().length() == 0) {
					WSTWebPlugin.logError("Extension: " + EXTENSION_POINT + " Element: " + ELEMENT_MAPPING + " is missing attribute " + ATTRIBUTE_PRESET_ID); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
					continue;
				}
				try {
					ProjectFacetsManager.getPreset(presetID);
				} catch (IllegalArgumentException e) {
					WSTWebPlugin.logError("Extension: " + EXTENSION_POINT + " Element: " + ELEMENT_MAPPING + " defined invalid attribute " + ATTRIBUTE_PRESET_ID + ": " + presetID, e); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
					continue;
				}
				MappingDescriptor descriptor = new MappingDescriptor(element);
				descriptors.add(descriptor);
			} else {
				WSTWebPlugin.logError("Elements must be named: " + ELEMENT_MAPPING + " within the extension: " + EXTENSION_POINT); //$NON-NLS-1$ //$NON-NLS-2$
				WSTWebPlugin.logError("Element: " + element.getName() + " is invalid within the extension: " + EXTENSION_POINT); //$NON-NLS-1$ //$NON-NLS-2$
			}
		}
	}

}

Back to the top