/** * Builds the Configuration object from the properties file (apache commons * properties file format). <br> * The values provided in the config file will be overwritten environment * variables (if present) * * List of the properties <br> * loginsight.host = host name <br> * loginsight.port = port number <br> * loginsight.user = User name <br> * loginsight.password = password <br> * loginsight.ingestion.agentId = agentId <br> * loginsight.connection.scheme = http protocol scheme <br> * loginsight.ingestion.port = Ingestion port number <br> * * @param configFileName * Name of the config file to read * @return Newly created Configuration object */ public static Configuration buildFromConfig(String configFileName) { try { List<FileLocationStrategy> subs = Arrays.asList(new ProvidedURLLocationStrategy(), new FileSystemLocationStrategy(), new ClasspathLocationStrategy()); FileLocationStrategy strategy = new CombinedLocationStrategy(subs); FileBasedConfigurationBuilder<PropertiesConfiguration> builder = new FileBasedConfigurationBuilder<PropertiesConfiguration>( PropertiesConfiguration.class).configure( new Parameters().fileBased().setLocationStrategy(strategy).setFileName(configFileName)); PropertiesConfiguration propConfig = builder.getConfiguration(); Map<String, String> propMap = new HashMap<String, String>(); Iterator<String> keys = propConfig.getKeys(); keys.forEachRemaining(key -> { logger.info(key + ":" + propConfig.getString(key)); propMap.put(key, propConfig.getString(key)); }); Configuration config = Configuration.buildConfig(propMap); config.loadFromEnv(); return config; } catch (ConfigurationException e1) { throw new RuntimeException("Unable to load config", e1); } }
private void init() { this.setListDelimiterHandler(new DefaultListDelimiterHandler(',')); FileLocationStrategy strategy = new MyFileLocationStrategy(); String basePath = _fileName == null ? "." : new File(_fileName).getParent(); FileLocator locator = FileLocatorUtils.fileLocator() .locationStrategy(strategy) .basePath(basePath) .create(); this.initFileLocator(locator); }
/** * The file {@link URL} can be absolute or relative to the working folder. The configuration file is located using a {@link FileLocatorUtils#DEFAULT_LOCATION_STRATEGY strategy} that uses a number of techniques to determine the file location. * <p> * If no file is found, the {@link #FILENAME resource file} is used. * * @return the configuration file {@link URL}. */ public URL getFileName() { if (this.propertiesFile == null) { final List<FileLocationStrategy> subs = Arrays.asList( new ProvidedURLLocationStrategy(), new FileSystemLocationStrategy(), new ClasspathLocationStrategy()); final FileLocationStrategy strategy = new CombinedLocationStrategy(subs); FileLocator fileLocator = FileLocatorUtils.fileLocator() .basePath(this.getDefaultFile().getParent()) .fileName(this.getDefaultFile().getName()) .create(); if (!new File(FileLocatorUtils.locate(fileLocator).getFile()).exists()) { fileLocator = FileLocatorUtils.fileLocator() .fileName(this.getDefaultFile().getName()) .locationStrategy(strategy) .create(); } this.propertiesFile = FileLocatorUtils.locate(fileLocator); // Ensure that the ConfigurationBuilder is now initialised as that may force a re-initialisation of this FileLocator. this.getConfigurationBuilder(); } return this.propertiesFile; }
public FallbackBasePathLocationStrategy(FileLocationStrategy baseStrategy, String fallbackBasePath) { this.baseStrategy = baseStrategy; this.fallbackBasePath = fallbackBasePath; }