private File getDefaultBootstrapFile(ConfigLocation.FileAccessType accessType) { Iterable<ConfigLocation> configLocations = configLocationFileStore.getAll(); StringBuilder sb = new StringBuilder(""); for (ConfigLocation configLocation : configLocations) { sb.append(configLocation.getLocation()).append(' '); try { return configLocation.getFile(BOOTSTRAP_PROPERTIES, accessType); } catch (MotechConfigurationException e) { LOG.warn(e.getMessage()); } } throw new MotechConfigurationException(String.format("%s file is not %s from any of the default locations. Searched directories: %s.", BOOTSTRAP_PROPERTIES, accessType.toString(), sb)); }
private Iterable<ConfigLocation> map(List<String> configPaths) { List<ConfigLocation> configLocations = new ArrayList<>(); for (String configLocation : configPaths) { configLocations.add(new ConfigLocation(configLocation)); } return configLocations; }
/** * Finds all configs from the config location * * @throws IOException If there is any error while handling the files. */ public List<File> findExistingConfigs() throws IOException { final ConfigLocation currentConfigLocation = coreConfigurationService.getConfigLocation(); return currentConfigLocation.getExistingConfigFiles(); }
@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)); }
private String getConfigDir() { if (coreConfigurationService == null) { return System.getProperty("user.home") + "/config"; } return coreConfigurationService.getConfigLocation().getLocation(); }
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; }
/** * Resource corresponding to the config location. * * @return resource */ public Resource toResource() { if (configLocation.startsWith(File.separator) || new File(configLocation).isAbsolute()) { try { return getUrlResource(); } catch (MalformedURLException e) { throw new MotechConfigurationException(String.format("Invalid config location %s.", configLocation), e); } } else { return new ClassPathResource(configLocation); } }
@Override public boolean requiresConfigurationFiles() { try { if (getConfigSource() == null) { configSource = loadBootstrapConfig().getConfigSource(); } if (!configSource.isFile()) { return false; } ConfigLocation configLocation = coreConfigurationService.getConfigLocation(); return !configLocation.hasPlatformConfigurationFile(); } catch (MotechConfigurationException ex) { logger.error(ex.getMessage(), ex); return true; } }
private void setupLocation() throws FileSystemException { ConfigLocation configLocation = coreConfigurationService.getConfigLocation(); monitoredDir = VFS.getManager().resolveFile(configLocation.getLocation()); fileMonitor.addFile(monitoredDir); LOGGER.info(String.format("Setting up monitoring for location: %s", monitoredDir)); }
/** * <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); } }