@Override public ConfigLocation getConfigLocation() { Iterable<ConfigLocation> configLocations = configLocationFileStore.getAll(); StringBuilder sb = new StringBuilder(""); for (ConfigLocation configLocation : configLocations) { sb.append(configLocation.getLocation()).append(' '); Resource configLocationResource = configLocation.toResource(); try { Resource motechSettings = configLocationResource.createRelative(ConfigurationConstants.SETTINGS_FILE_NAME); if (motechSettings.isReadable()) { return configLocation; } logger.warn("Could not read motech-settings.properties from: " + configLocationResource.toString()); } catch (IOException e) { logger.warn("Problem reading motech-settings.properties from location: " + configLocationResource.toString(), e); } } throw new MotechConfigurationException(String.format("Could not read settings from any of the config locations. Searched directories: %s.", sb)); }
public SettingsRecord loadMotechSettings() { SettingsRecord settingsRecord; ConfigLocation configLocation = coreConfigurationService.getConfigLocation(); Resource configLocationResource = configLocation.toResource(); try { Resource motechSettings = configLocationResource.createRelative(ConfigurationConstants.SETTINGS_FILE_NAME); settingsRecord = loadSettingsFromStream(motechSettings); settingsRecord.setFilePath(configLocationResource.getURL().getPath()); checkSettingsRecord(settingsRecord); if (eventAdmin != null) { Map<String, String> properties = new HashMap<>(); Properties activemqProperties = settingsRecord.getActivemqProperties(); if (activemqProperties != null && activemqProperties.containsKey(AMQ_BROKER_URL)) { properties.put(AMQ_BROKER_URL, activemqProperties.getProperty(AMQ_BROKER_URL)); eventAdmin.postEvent(new Event("org/motechproject/osgi/event/RELOAD", properties)); } } } catch (IOException e) { throw new MotechConfigurationException(String.format("Could not read settings from file at location %s", configLocation), e); } return settingsRecord; }
/** * <p> * This method Returns the {@link java.io.File} object for the given file name relative to the config location. * It also checks for the requested file accessibility. If the requested access type check is * {@link ConfigLocation.FileAccessType.READABLE}, the file's existence and readability will be checked. * Similarly, if the requested access type check is {@link ConfigLocation.FileAccessType.WRITABLE}, then the * write accessibility to the file will be checked. If the file does not exists, write accessibility of its * ancestors will be checked. * </p> * * @param fileName Name of the file to be added to the config location. * @param accessType One of {@link ConfigLocation.FileAccessType.READABLE} or {@link ConfigLocation.FileAccessType.WRITABLE}. * @return File relative to the config location. * @throws MotechConfigurationException if the file is not readable or writable depending on the given access type. */ public File getFile(String fileName, FileAccessType accessType) { try { Resource resource = toResource().createRelative(fileName); if (accessType.isAccessible(resource)) { return resource.getFile(); } throw new MotechConfigurationException(String.format("%s file in the location %s is not %s.", fileName, configLocation, accessType.toString())); } catch (IOException e) { throw new MotechConfigurationException(String.format("Error while checking if file %s in the location %s is %s.", fileName, configLocation, accessType.toString()), e); } }