/** * * @param url * @param username * @param password * @throws org.motechproject.config.core.MotechConfigurationException if given url is invalid. */ public DBConfig(String url, String username, String password) { super(url, "none", username, password); validate(); }
/** * @param url * @param driver * @param username * @param password * @throws org.motechproject.config.core.MotechConfigurationException if given url is invalid. */ public SQLDBConfig(String url, String driver, String username, String password) { super(url, driver, username, password); validate(); }
public static boolean isValid(String name) { try { valueOf(name); return true; } catch (MotechConfigurationException e) { return false; } }
/** * Map from BootstrapConfig to Properties * * @param bootstrapConfig * @return Properties mapped from provided bootstrapConfig. */ public static Properties toProperties(BootstrapConfig bootstrapConfig) { Properties properties = new Properties(); properties.setProperty(BootstrapConfig.COUCHDB_URL, bootstrapConfig.getCouchDbConfig().getUrl()); setIfNotBlank(properties, BootstrapConfig.COUCHDB_USERNAME, bootstrapConfig.getCouchDbConfig().getUsername()); setIfNotBlank(properties, BootstrapConfig.COUCHDB_PASSWORD, bootstrapConfig.getCouchDbConfig().getPassword()); properties.setProperty(BootstrapConfig.SQL_URL, bootstrapConfig.getSqlConfig().getUrl()); properties.setProperty(BootstrapConfig.SQL_DRIVER, bootstrapConfig.getSqlConfig().getDriver()); setIfNotBlank(properties, BootstrapConfig.SQL_USER, bootstrapConfig.getSqlConfig().getUsername()); setIfNotBlank(properties, BootstrapConfig.SQL_PASSWORD, bootstrapConfig.getSqlConfig().getPassword()); properties.setProperty(BootstrapConfig.TENANT_ID, bootstrapConfig.getTenantId()); properties.setProperty(BootstrapConfig.CONFIG_SOURCE, bootstrapConfig.getConfigSource().getName()); return properties; }
private void setSqlProperties() { SQLDBConfig sqlConfig = coreConfigurationService.loadBootstrapConfig().getSqlConfig(); String sqlUrl = sqlConfig.getUrl(); sqlProperties.setProperty("sql.url", sqlUrl); String sqlUser = sqlConfig.getUsername(); if (sqlUser != null) { sqlProperties.setProperty("sql.user", sqlUser); } String sqlPassword = sqlConfig.getPassword(); if (sqlPassword != null) { sqlProperties.setProperty("sql.password", sqlPassword); } String sqlDriver = sqlConfig.getDriver(); if (sqlDriver != null) { sqlProperties.setProperty("sql.driver", sqlDriver); } }
private Properties getCouchdbProperties() { DBConfig dbConfig = coreConfigurationService.loadBootstrapConfig().getCouchDbConfig(); Properties mergedProps = new Properties(); mergedProps.putAll(couchdbProperties); mergedProps.setProperty(DB_URL, dbConfig.getUrl()); setKeyIfValueNotNull(mergedProps, DB_USERNAME, dbConfig.getUsername()); setKeyIfValueNotNull(mergedProps, DB_PASSWORD, dbConfig.getPassword()); return mergedProps; }
@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; } }
/** * Map from properties to BootstrapConfig object * * @param bootstrapProperties * @return BootstrapConfig object mapped from provided properties. */ public static BootstrapConfig fromProperties(Properties bootstrapProperties) { return new BootstrapConfig(new DBConfig(bootstrapProperties.getProperty(BootstrapConfig.COUCHDB_URL), bootstrapProperties.getProperty(BootstrapConfig.COUCHDB_USERNAME), bootstrapProperties.getProperty(BootstrapConfig.COUCHDB_PASSWORD)), new SQLDBConfig(bootstrapProperties.getProperty(BootstrapConfig.SQL_URL), bootstrapProperties.getProperty(BootstrapConfig.SQL_DRIVER), bootstrapProperties.getProperty(BootstrapConfig.SQL_USER), bootstrapProperties.getProperty(BootstrapConfig.SQL_PASSWORD)), bootstrapProperties.getProperty(BootstrapConfig.TENANT_ID), ConfigSource.valueOf(bootstrapProperties.getProperty(BootstrapConfig.CONFIG_SOURCE))); } }
/** * <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); } }
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 void validate() { if (StringUtils.isBlank(getUrl())) { throw new MotechConfigurationException("Motech SQL URL cannot be null or empty."); } if (StringUtils.isBlank(getDriver())) { throw new MotechConfigurationException("Motech SQL Driver cannot be null or empty."); } if (!getUrl().matches("jdbc:(\\w+:)+//(\\w+\\.)*\\w+:\\d+/")) { throw new MotechConfigurationException("Motech SQL URL is invalid."); } } }
@Override public BootstrapConfig loadBootstrapConfig() { if (logger.isDebugEnabled()) { logger.debug("Loading bootstrap configuration."); } final BootstrapConfig bootstrapConfig; try { bootstrapConfig = coreConfigurationService == null ? null : coreConfigurationService.loadBootstrapConfig(); } catch (MotechConfigurationException e) { return null; } if (null != bootstrapConfig) { configSource = bootstrapConfig.getConfigSource(); } if (logger.isDebugEnabled()) { logger.debug("BootstrapConfig:" + bootstrapConfig); } return bootstrapConfig; }
@Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } BootstrapConfig that = (BootstrapConfig) o; if (!configSource.equals(that.configSource)) { return false; } if (!couchDbConfig.equals(that.couchDbConfig)) { return false; } if (!sqlConfig.equals(that.sqlConfig)) { return false; } if (!tenantId.equals(that.tenantId)) { return false; } return true; }
private String getConfigDir() { if (coreConfigurationService == null) { return System.getProperty("user.home") + "/config"; } return coreConfigurationService.getConfigLocation().getLocation(); }
@Override public void validate(StartupForm target, List<String> errors, ConfigSource configSource) { if (isNullOrEmpty(fieldValue)) { if (!configSource.isFile()) { errors.add(String.format(ERROR_REQUIRED, fieldName)); } } }
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(); }
private BootstrapConfig readBootstrapConfigFromEnvironment() { String couchDbUrl = environment.getCouchDBUrl(); String couchDbUsername = environment.getCouchDBUsername(); String couchDbPassword = environment.getCouchDBPassword(); String sqlUrl = environment.getSqlUrl(); String sqlUsername = environment.getSqlUsername(); String sqlPassword = environment.getSqlPassword(); String tenantId = environment.getTenantId(); String configSource = environment.getConfigSource(); String sqlDriver = environment.getSqlDriver(); return new BootstrapConfig(new DBConfig(couchDbUrl, couchDbUsername, couchDbPassword), new SQLDBConfig(sqlUrl, sqlDriver, sqlUsername, sqlPassword), tenantId, ConfigSource.valueOf(configSource)); }