Path: blob/master/sourcetools/objectmodel/com/ibm/j9tools/om/ConfigDirectory.java
6004 views
/*******************************************************************************1* Copyright (c) 2007, 2011 IBM Corp. and others2*3* This program and the accompanying materials are made available under4* the terms of the Eclipse Public License 2.0 which accompanies this5* distribution and is available at https://www.eclipse.org/legal/epl-2.0/6* or the Apache License, Version 2.0 which accompanies this distribution and7* is available at https://www.apache.org/licenses/LICENSE-2.0.8*9* This Source Code may also be made available under the following10* Secondary Licenses when the conditions for such availability set11* forth in the Eclipse Public License, v. 2.0 are satisfied: GNU12* General Public License, version 2 with the GNU Classpath13* Exception [1] and GNU General Public License, version 2 with the14* OpenJDK Assembly Exception [2].15*16* [1] https://www.gnu.org/software/classpath/license.html17* [2] http://openjdk.java.net/legal/assembly-exception.html18*19* 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-exception20*******************************************************************************/21package com.ibm.j9tools.om;2223import java.io.File;24import java.io.FileNotFoundException;25import java.text.MessageFormat;2627import com.ibm.j9tools.om.io.FileExtensionFilter;2829/**30* The ConfigDirectory class keeps track of various files (or rather full filenames)31* found in the a configuration directory. This includes all the Build Spec,32* Feature, Build Info, Flag Definition files. Use this class in order to find33* files describing desired build spec or a feature XML etc.34*/35public class ConfigDirectory {3637public static final String FEATURE_FILE_EXTENSION = ".feature"; //$NON-NLS-1$38public static final String BUILD_SPEC_FILE_EXTENSION = ".spec"; //$NON-NLS-1$39public static final String FLAG_DEFINITIONS_FILE_EXTENSION = ".flags"; //$NON-NLS-1$40public static final String BUILD_INFO_FILE_EXTENSION = ".build-info"; //$NON-NLS-1$4142public static final String BUILD_SPEC_SCHEMA_FILENAME = "spec-v1.xsd"; //$NON-NLS-1$43public static final String FEATURE_SCHEMA_FILENAME = "feature-v1.xsd"; //$NON-NLS-1$44public static final String FLAGS_SCHEMA_FILENAME = "flags-v1.xsd"; //$NON-NLS-1$45public static final String BUILD_INFO_SCHEMA_FILENAME = "build-v4.xsd"; //$NON-NLS-1$4647public String configDir = null; /* Configuration file directory */48private final File[] featureFiles; /* A list of feature files */49private final File[] buildSpecFiles; /* A list of build spec files */50private final File[] flagDefinitionFiles; /* A list of flag definition files. */51private final File[] buildInformationFiles; /* A list of build information files. */52private final File flagDefinitionsFile; /* The singleton flag definitions file. */53private final File buildInfoFile; /* The singleton build information file. */5455/**56* Creates a new ConfigurationDirectory object on the directory called <code>directoryName</code>.57*58* @param directoryName The relative or absolute path to scan.59*60* @throws InvalidConfigurationException61*/62public ConfigDirectory(String directoryName) throws InvalidConfigurationException {63File configDirectory = new File(directoryName);6465if (!configDirectory.exists()) {66throw new InvalidConfigurationException(MessageFormat.format(Messages.getString("ConfigDirectory.0"), new Object[] { configDirectory.getAbsolutePath() })); //$NON-NLS-1$67}6869if (!configDirectory.isDirectory()) {70throw new InvalidConfigurationException(Messages.getString("ConfigDirectory.1")); //$NON-NLS-1$71}7273// Use absolute paths to simplify debugging74this.configDir = configDirectory.getAbsolutePath();7576/* Look for and remember full path of each feature file in the config directory */77this.featureFiles = initializeFileSet(configDirectory, FEATURE_FILE_EXTENSION);7879/* Look for and remember full path of each build spec file in the config directory */80this.buildSpecFiles = initializeFileSet(configDirectory, BUILD_SPEC_FILE_EXTENSION);8182/* Look for and remember full path of flag definitions file in the config directory */83this.flagDefinitionFiles = initializeFileSet(configDirectory, FLAG_DEFINITIONS_FILE_EXTENSION);84int definitionCount = flagDefinitionFiles.length;85if (definitionCount != 1) {86throw new InvalidConfigurationException(MessageFormat.format(Messages.getString("ConfigDirectory.2"), new Object[] { definitionCount })); //$NON-NLS-1$87}8889flagDefinitionsFile = flagDefinitionFiles[0];9091/* Look for and remember full path of build information file in the config directory */92this.buildInformationFiles = initializeFileSet(configDirectory, BUILD_INFO_FILE_EXTENSION);93int buildInfoCount = buildInformationFiles.length;94if (buildInfoCount != 1) {95throw new InvalidConfigurationException(MessageFormat.format(Messages.getString("ConfigDirectory.3"), new Object[] { buildInfoCount })); //$NON-NLS-1$96}9798buildInfoFile = buildInformationFiles[0];99}100101/**102* Get a set of feature ids that have been found in the config directory and the user103* is allowed to load.104*105* @return a set of Feature IDs106*/107public File[] getFeatureFiles() {108return featureFiles;109}110111/**112* Retrieves the file for the feature with the given ID.113*114* @param featureID115* @return the file for the feature with the given ID116*117* @throws FileNotFoundException118*/119public File getFeatureFileByID(String featureID) throws FileNotFoundException {120File featureFile = new File(configDir, featureID + FEATURE_FILE_EXTENSION);121if (featureFile.exists()) {122throw new FileNotFoundException(MessageFormat.format(Messages.getString("ConfigDirectory.4"), new Object[] { featureFile.getAbsolutePath() })); //$NON-NLS-1$123}124125if (featureFile.isFile()) {126throw new FileNotFoundException(MessageFormat.format(Messages.getString("ConfigDirectory.5"), new Object[] { featureFile.getAbsolutePath() })); //$NON-NLS-1$127}128return featureFile;129}130131/**132* Get a set of build spec ids that have been found in the config directory and the user133* is allowed to load.134*135* @return a set of BuildSpec IDs136*/137public File[] getBuildSpecFiles() {138return buildSpecFiles;139}140141/**142* Get the flag definitions filename as present in the config directory defined for this class143*144* @return A full filename of the requested flag definitions file, null if not found145*/146public File getFlagDefinitionsFile() {147return flagDefinitionsFile;148}149150/**151* Get the build information filename as present in the config directory defined for this class152*153* @return A full filename of the requested build information file, null if not found154*/155public File getBuildInfoFile() {156return buildInfoFile;157}158159/**160* Find all the files in configDirectory that match the passed extension.161*162* @param configDirectory Directory containing build configuration and definition files163* @param extension File extensions to be looked for164*/165private File[] initializeFileSet(File configDirectory, String extension) {166FileExtensionFilter ext = new FileExtensionFilter(extension);167return configDirectory.listFiles(ext);168}169170}171172173