Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openj9
Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/ConfigDirectory.java
6004 views
1
/*******************************************************************************
2
* Copyright (c) 2007, 2011 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.j9tools.om;
23
24
import java.io.File;
25
import java.io.FileNotFoundException;
26
import java.text.MessageFormat;
27
28
import com.ibm.j9tools.om.io.FileExtensionFilter;
29
30
/**
31
* The ConfigDirectory class keeps track of various files (or rather full filenames)
32
* found in the a configuration directory. This includes all the Build Spec,
33
* Feature, Build Info, Flag Definition files. Use this class in order to find
34
* files describing desired build spec or a feature XML etc.
35
*/
36
public class ConfigDirectory {
37
38
public static final String FEATURE_FILE_EXTENSION = ".feature"; //$NON-NLS-1$
39
public static final String BUILD_SPEC_FILE_EXTENSION = ".spec"; //$NON-NLS-1$
40
public static final String FLAG_DEFINITIONS_FILE_EXTENSION = ".flags"; //$NON-NLS-1$
41
public static final String BUILD_INFO_FILE_EXTENSION = ".build-info"; //$NON-NLS-1$
42
43
public static final String BUILD_SPEC_SCHEMA_FILENAME = "spec-v1.xsd"; //$NON-NLS-1$
44
public static final String FEATURE_SCHEMA_FILENAME = "feature-v1.xsd"; //$NON-NLS-1$
45
public static final String FLAGS_SCHEMA_FILENAME = "flags-v1.xsd"; //$NON-NLS-1$
46
public static final String BUILD_INFO_SCHEMA_FILENAME = "build-v4.xsd"; //$NON-NLS-1$
47
48
public String configDir = null; /* Configuration file directory */
49
private final File[] featureFiles; /* A list of feature files */
50
private final File[] buildSpecFiles; /* A list of build spec files */
51
private final File[] flagDefinitionFiles; /* A list of flag definition files. */
52
private final File[] buildInformationFiles; /* A list of build information files. */
53
private final File flagDefinitionsFile; /* The singleton flag definitions file. */
54
private final File buildInfoFile; /* The singleton build information file. */
55
56
/**
57
* Creates a new ConfigurationDirectory object on the directory called <code>directoryName</code>.
58
*
59
* @param directoryName The relative or absolute path to scan.
60
*
61
* @throws InvalidConfigurationException
62
*/
63
public ConfigDirectory(String directoryName) throws InvalidConfigurationException {
64
File configDirectory = new File(directoryName);
65
66
if (!configDirectory.exists()) {
67
throw new InvalidConfigurationException(MessageFormat.format(Messages.getString("ConfigDirectory.0"), new Object[] { configDirectory.getAbsolutePath() })); //$NON-NLS-1$
68
}
69
70
if (!configDirectory.isDirectory()) {
71
throw new InvalidConfigurationException(Messages.getString("ConfigDirectory.1")); //$NON-NLS-1$
72
}
73
74
// Use absolute paths to simplify debugging
75
this.configDir = configDirectory.getAbsolutePath();
76
77
/* Look for and remember full path of each feature file in the config directory */
78
this.featureFiles = initializeFileSet(configDirectory, FEATURE_FILE_EXTENSION);
79
80
/* Look for and remember full path of each build spec file in the config directory */
81
this.buildSpecFiles = initializeFileSet(configDirectory, BUILD_SPEC_FILE_EXTENSION);
82
83
/* Look for and remember full path of flag definitions file in the config directory */
84
this.flagDefinitionFiles = initializeFileSet(configDirectory, FLAG_DEFINITIONS_FILE_EXTENSION);
85
int definitionCount = flagDefinitionFiles.length;
86
if (definitionCount != 1) {
87
throw new InvalidConfigurationException(MessageFormat.format(Messages.getString("ConfigDirectory.2"), new Object[] { definitionCount })); //$NON-NLS-1$
88
}
89
90
flagDefinitionsFile = flagDefinitionFiles[0];
91
92
/* Look for and remember full path of build information file in the config directory */
93
this.buildInformationFiles = initializeFileSet(configDirectory, BUILD_INFO_FILE_EXTENSION);
94
int buildInfoCount = buildInformationFiles.length;
95
if (buildInfoCount != 1) {
96
throw new InvalidConfigurationException(MessageFormat.format(Messages.getString("ConfigDirectory.3"), new Object[] { buildInfoCount })); //$NON-NLS-1$
97
}
98
99
buildInfoFile = buildInformationFiles[0];
100
}
101
102
/**
103
* Get a set of feature ids that have been found in the config directory and the user
104
* is allowed to load.
105
*
106
* @return a set of Feature IDs
107
*/
108
public File[] getFeatureFiles() {
109
return featureFiles;
110
}
111
112
/**
113
* Retrieves the file for the feature with the given ID.
114
*
115
* @param featureID
116
* @return the file for the feature with the given ID
117
*
118
* @throws FileNotFoundException
119
*/
120
public File getFeatureFileByID(String featureID) throws FileNotFoundException {
121
File featureFile = new File(configDir, featureID + FEATURE_FILE_EXTENSION);
122
if (featureFile.exists()) {
123
throw new FileNotFoundException(MessageFormat.format(Messages.getString("ConfigDirectory.4"), new Object[] { featureFile.getAbsolutePath() })); //$NON-NLS-1$
124
}
125
126
if (featureFile.isFile()) {
127
throw new FileNotFoundException(MessageFormat.format(Messages.getString("ConfigDirectory.5"), new Object[] { featureFile.getAbsolutePath() })); //$NON-NLS-1$
128
}
129
return featureFile;
130
}
131
132
/**
133
* Get a set of build spec ids that have been found in the config directory and the user
134
* is allowed to load.
135
*
136
* @return a set of BuildSpec IDs
137
*/
138
public File[] getBuildSpecFiles() {
139
return buildSpecFiles;
140
}
141
142
/**
143
* Get the flag definitions filename as present in the config directory defined for this class
144
*
145
* @return A full filename of the requested flag definitions file, null if not found
146
*/
147
public File getFlagDefinitionsFile() {
148
return flagDefinitionsFile;
149
}
150
151
/**
152
* Get the build information filename as present in the config directory defined for this class
153
*
154
* @return A full filename of the requested build information file, null if not found
155
*/
156
public File getBuildInfoFile() {
157
return buildInfoFile;
158
}
159
160
/**
161
* Find all the files in configDirectory that match the passed extension.
162
*
163
* @param configDirectory Directory containing build configuration and definition files
164
* @param extension File extensions to be looked for
165
*/
166
private File[] initializeFileSet(File configDirectory, String extension) {
167
FileExtensionFilter ext = new FileExtensionFilter(extension);
168
return configDirectory.listFiles(ext);
169
}
170
171
}
172
173