Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/com.ibm.jpp.preprocessor/com/ibm/jpp/om/ConfigurationRegistry.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 1999, 2019 IBM Corp. and others
3
*
4
* This program and the accompanying materials are made available under
5
* the terms of the Eclipse Public License 2.0 which accompanies this
6
* distribution and is available at https://www.eclipse.org/legal/epl-2.0/
7
* or the Apache License, Version 2.0 which accompanies this distribution and
8
* is available at https://www.apache.org/licenses/LICENSE-2.0.
9
*
10
* This Source Code may also be made available under the following
11
* Secondary Licenses when the conditions for such availability set
12
* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU
13
* General Public License, version 2 with the GNU Classpath
14
* Exception [1] and GNU General Public License, version 2 with the
15
* OpenJDK Assembly Exception [2].
16
*
17
* [1] https://www.gnu.org/software/classpath/license.html
18
* [2] http://openjdk.java.net/legal/assembly-exception.html
19
*
20
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
21
*******************************************************************************/
22
package com.ibm.jpp.om;
23
24
import java.io.File;
25
import java.io.FileNotFoundException;
26
import java.util.ArrayList;
27
import java.util.Collection;
28
import java.util.HashMap;
29
import java.util.HashSet;
30
import java.util.List;
31
import java.util.Map;
32
import java.util.Set;
33
34
import com.ibm.jpp.xml.XMLException;
35
36
public class ConfigurationRegistry {
37
38
/**
39
* The default configuration XML for a registry.
40
*/
41
public static final String DEFAULT_XML = "jpp_configuration.xml";
42
43
private int configVersion;
44
45
private final String name;
46
private final Map<String, ConfigObject> configsByName;
47
private final List<ConfigObject> configs;
48
private final List<ConfigObject> localConfigs;
49
private final Set<String> validFlags;
50
private String baseDir;
51
private String srcRoot;
52
private final Map<String, String> incompleteConfigs;
53
54
// Automated Test Metadata
55
private String testsProject;
56
private final List<ClassPathEntry> testsClassPaths;
57
private final List<Src> testsSources;
58
59
/**
60
* Constructor
61
*
62
* @param baseDir the registry's base directory
63
* @param srcRoot the registry's source root path
64
*/
65
public ConfigurationRegistry(String baseDir, String srcRoot) {
66
testsClassPaths = new ArrayList<>();
67
testsSources = new ArrayList<>();
68
69
configsByName = new HashMap<>();
70
configs = new ArrayList<>();
71
localConfigs = new ArrayList<>();
72
validFlags = new HashSet<>();
73
incompleteConfigs = new HashMap<>();
74
75
this.name = srcRoot;
76
77
if (baseDir.endsWith(File.separator)) {
78
this.baseDir = baseDir;
79
} else {
80
StringBuffer buffer = new StringBuffer(baseDir);
81
buffer.append(File.separator);
82
this.baseDir = buffer.toString();
83
}
84
85
if (srcRoot.endsWith(File.separator)) {
86
this.srcRoot = srcRoot;
87
} else {
88
StringBuffer buffer = new StringBuffer(srcRoot);
89
buffer.append(File.separator);
90
this.srcRoot = buffer.toString();
91
}
92
}
93
94
/**
95
* Returns the name of the registry.
96
*
97
* @return Returns the configuration registry name.
98
*/
99
public String getName() {
100
return name;
101
}
102
103
/**
104
* Returns the name of the automated tests project associated with this
105
* registry (and all of its configurations).
106
*
107
* @return the automated tests project name
108
*/
109
public String getTestsProject() {
110
return testsProject;
111
}
112
113
/**
114
* Returns the {@link ConfigObject} specified by name.
115
*
116
* @param name the name of the desired configuration
117
* @return the configuration
118
*/
119
public ConfigObject getConfiguration(String name) {
120
return (name == null) ? null : configsByName.get(name);
121
}
122
123
/**
124
* Returns a {@link Set} of the registered configurations.
125
*
126
* @return the registered configurations as a Set
127
*/
128
public Collection<ConfigObject> getConfigurationsAsCollection() {
129
return configs;
130
}
131
132
/**
133
* Returns a {@link Set} of the names of the registered configurations.
134
*
135
* @return the registered configuration names as a Set
136
*/
137
public Set<String> getConfigurationNamesAsSet() {
138
return configsByName.keySet();
139
}
140
141
/**
142
* Returns an array of all configurations in this registry. This includes all
143
* configurations inherited from required XML files.
144
*
145
* @return the configurations
146
*/
147
public ConfigObject[] getConfigurations() {
148
return configs.toArray(new ConfigObject[configs.size()]);
149
}
150
151
/**
152
* Returns an array of the configurations defined in the parent XML file. This
153
* <b>does not include</b> the configurations inherited from the required XML files.
154
*
155
* @return the local configurations
156
*/
157
public ConfigObject[] getLocalConfigurations() {
158
return localConfigs.toArray(new ConfigObject[localConfigs.size()]);
159
}
160
161
/**
162
* Checks to see if the specified flag is valid.
163
*
164
* @param name the name of the flag to be check
165
* @return <code>true</code> for a valid flag, <code>false</code> otherwise
166
*/
167
public boolean isValidFlag(String name) {
168
return validFlags.contains(name);
169
}
170
171
/**
172
* Returns a HashSet of the Valid flags.
173
*
174
* @return the HashSet of the valid flags.
175
*/
176
public Set<String> getValidFlags() {
177
return validFlags;
178
}
179
180
/**
181
* Returns a list of all the configurations that are invalid as a result of
182
* improper or invalid tags in the XML file.
183
*
184
* @return the list of the incomplete configurations
185
*/
186
public Map<String, String> getIncompleteConfigs() {
187
return incompleteConfigs;
188
}
189
190
/**
191
* Sets the new base directory for all the configurations to be added to this
192
* registry.
193
*
194
* @param newBaseDir the new base dir path
195
*/
196
public void setBaseDir(String newBaseDir) {
197
this.baseDir = (newBaseDir != null) ? newBaseDir : "";
198
}
199
200
/**
201
* Returns the base directory for all the configurations.
202
*
203
* @return the base directory
204
*/
205
public String getBaseDir() {
206
return baseDir;
207
}
208
209
/**
210
* Sets the new source root for all the configurations to be added to this
211
* registry.
212
*
213
* @param newSrcRoot the new source root path
214
*/
215
public void setSourceRoot(String newSrcRoot) {
216
this.srcRoot = (newSrcRoot == null) ? "" : File.pathSeparator + newSrcRoot + File.separator;
217
}
218
219
/**
220
* Returns the source root for all the configurations
221
*
222
* @return the source root path
223
*/
224
public String getSourceRoot() {
225
return srcRoot;
226
}
227
228
/**
229
* Registers a configuration with this configuration registry.
230
* <p>
231
* NOTE: A configuration can be defined as local if it was defined in the registry's
232
* own XML file as opposed to one of the registry's required XMLs.
233
*
234
* @param config the new configuration
235
* @param local <code>true</code> if the config was declared in this registry's XML, <code>false</code> otherwise
236
* @return <code>true</code> if the registration was successful, <code>false</code> otherwise
237
*/
238
public boolean registerConfiguration(ConfigObject config, boolean local) {
239
config.setRegistry(this);
240
if (config.checkDependencies()) {
241
config.updateWithDependencies();
242
243
if (local) {
244
localConfigs.add(config);
245
}
246
configs.add(config);
247
configsByName.put(config.getName(), config);
248
validFlags.addAll(config.getFlags());
249
return true;
250
} else {
251
return false;
252
}
253
}
254
255
/**
256
* Used if a configuration is added with the same name as a previous one.
257
* It is not a recommended practice to override the an existing configuration
258
* as it may produce unexpected results.
259
* <p>
260
* A configuration can be also defined as local if it was defined in the registry's
261
* own XML file as opposed to one of the registry's required XMLs.
262
*
263
* @param config the new configuration
264
* @param local <code>true</code> if the config was declared in this registry's XML, <code>false</code> otherwise
265
* @return <code>true</code> if the registration was successful, <code>false</code> otherwise
266
*/
267
public boolean overrideConfiguration(ConfigObject config, boolean local) {
268
config.setRegistry(this);
269
if (config.checkDependencies()) {
270
config.updateWithDependencies();
271
272
if (local) {
273
localConfigs.remove(config);
274
localConfigs.add(config);
275
}
276
configs.remove(config);
277
configs.add(config);
278
configsByName.put(config.getName(), config);
279
validFlags.addAll(config.getFlags());
280
return true;
281
} else {
282
return false;
283
}
284
}
285
286
/**
287
* Method to load registry configurations from the specified XML file
288
*
289
* @param filename the location of the jpp_configuration.xml
290
*
291
* @throws FileNotFoundException if the file specified by filename cannot be found.
292
* @throws XMLException if there are errors in the XML file
293
*/
294
public void registerXMLSet(String filename) throws FileNotFoundException, XMLException {
295
if (!filename.startsWith(File.separator) && !filename.startsWith(":", 1)) {
296
StringBuffer buffer = new StringBuffer(baseDir);
297
// buffer.append(File.separator);
298
buffer.append(srcRoot);
299
// buffer.append(File.separator);
300
buffer.append(filename);
301
filename = buffer.toString();
302
}
303
304
ConfigXMLHandler xmlHandler = new ConfigXMLHandler(filename);
305
xmlHandler.setBaseDir(baseDir);
306
xmlHandler.setSourceRoot(srcRoot);
307
xmlHandler.parseInput();
308
309
// Gets the automated tests variables
310
testsProject = xmlHandler.getTestsProject();
311
testsClassPaths.addAll(xmlHandler.getTestsClassPaths());
312
testsSources.addAll(xmlHandler.getTestsSources());
313
314
setConfigVersion(xmlHandler.getConfigVersion());
315
316
List<ConfigObject> tempLocalConfigs = xmlHandler.getLocalConfigs();
317
List<ConfigObject> tempConfigs = xmlHandler.getAllConfigs();
318
319
for (ConfigObject currentConfig : tempConfigs) {
320
boolean failed = false;
321
322
if (currentConfig.isSet() || (currentConfig.getSourceCount() > 0 && currentConfig.getOutputPath() != null)) {
323
if (getConfiguration(currentConfig.getName()) != null) {
324
//Note that overriding is being done
325
StringBuffer buffer = new StringBuffer("Warning: Multiple definitions of ");
326
buffer.append(currentConfig.getName());
327
buffer.append(" possible errors in dependencies");
328
System.err.println(buffer.toString());
329
failed = !overrideConfiguration(currentConfig, tempLocalConfigs.contains(currentConfig));
330
} else {
331
failed = !registerConfiguration(currentConfig, tempLocalConfigs.contains(currentConfig));
332
}
333
334
if (failed) {
335
StringBuffer buffer = new StringBuffer("Warning dependencies could not be resolved: ");
336
buffer.append(currentConfig.getName());
337
System.err.println(buffer.toString());
338
// unresolvedDependencies.add(currentConfig.getName());
339
}
340
} else {
341
StringBuffer buffer = new StringBuffer();
342
343
if (currentConfig.getSourceCount() == 0) {
344
buffer.append("Warning ");
345
buffer.append(currentConfig.getName());
346
buffer.append(" does not have sources");
347
}
348
349
if (currentConfig.getSourceCount() == 0 && currentConfig.getOutputPath() == null) {
350
buffer.append("\n");
351
}
352
353
if (currentConfig.getOutputPath() == null) {
354
buffer.append("Warning ");
355
buffer.append(currentConfig.getName());
356
buffer.append(" does not have an output path");
357
}
358
359
System.err.println(buffer.toString());
360
incompleteConfigs.put(currentConfig.getName(), buffer.toString());
361
}
362
}
363
}
364
365
/**
366
* Adds a classpath entry to this registry. This classpath entry will be
367
* used by the eclipse plugin to generate an automated test project classpath.
368
*
369
* @param path the classpath
370
* @param type the type of classpath
371
* @param isExported <code>true</code> if this entry is contributed to dependent projects, <code>false</code> otherwise
372
*/
373
public void addClassPathEntry(String path, String type, boolean isExported) {
374
testsClassPaths.add(new ClassPathEntry(path, type, isExported));
375
}
376
377
/**
378
* Adds a classpath entry to this registry. This classpath entry will be
379
* used by the eclipse plugin to generate an automated test project classpath.
380
*
381
* @param path the classpath
382
* @param type the type of classpath
383
* @param sourcePath the classpath's source path
384
* @param isExported <code>true</code> if this entry is contributed to dependent projects, <code>false</code> otherwise
385
*/
386
public void addClassPathEntry(String path, String type, String sourcePath, boolean isExported) {
387
testsClassPaths.add(new ClassPathEntry(path, type, sourcePath, isExported));
388
}
389
390
/**
391
* Returns this registry's automated tests' classpath entries.
392
*
393
* @return the classpath entries
394
*/
395
public ClassPathEntry[] getClassPathEntries() {
396
return (!testsClassPaths.isEmpty()) ? (ClassPathEntry[]) testsClassPaths.toArray(new ClassPathEntry[testsClassPaths.size()]) : new ClassPathEntry[0];
397
}
398
399
/**
400
* Returns this registry's automated tests' source entries.
401
*
402
* @return the source entries
403
*/
404
public List<Src> getTestsSources() {
405
return testsSources;
406
}
407
408
/**
409
* @see java.lang.Object#toString()
410
*/
411
@Override
412
public String toString() {
413
StringBuffer buffer = new StringBuffer("\nName: ");
414
buffer.append(name);
415
416
buffer.append("\nConfigurations: ");
417
buffer.append(getConfigurationNamesAsSet().toString());
418
return buffer.toString();
419
}
420
421
/**
422
* @see java.lang.Object#equals(java.lang.Object)
423
*/
424
@Override
425
public boolean equals(Object obj) {
426
if (obj instanceof ConfigurationRegistry) {
427
ConfigurationRegistry objReg = (ConfigurationRegistry) obj;
428
return (name.equals(objReg.getName()) && baseDir.equals(objReg.getBaseDir()) && srcRoot.equals(objReg.getSourceRoot()));
429
} else {
430
return false;
431
}
432
}
433
434
/**
435
* @see java.lang.Object#hashCode()
436
*/
437
@Override
438
public int hashCode() {
439
return name.hashCode() + baseDir.hashCode() + srcRoot.hashCode();
440
}
441
442
private void setConfigVersion(int ver) {
443
this.configVersion = ver;
444
}
445
446
public int getConfigVersion() {
447
return configVersion;
448
}
449
450
}
451
452