AlertsPropertiesConfiguration extends AbstractAlertsConfiguration { @PostConstruct void initProps() { Assert.notNull(env, "env must not be null"); List<String> propNames = new ArrayList<String>(); for (String propName : Tools.getAllKnownPropertyNames(env)) { if (propName.startsWith(ALERT_PROP_PREFIX)) { propNames.add(propName); } } Set<String> orders = new HashSet<String>(); Pattern pattern = Pattern.compile("alerts\\.(\\d+)\\.id"); for (String propName : propNames) { Matcher matcher = pattern.matcher(propName); if (matcher.matches()) { String order = StringUtils.substringBetween(propName, ALERT_PROP_PREFIX, "."); if (orders.contains(order)) { throw new IllegalStateException("Wrong alert's configuration - alert order '" + order + "' was already used."); } else { orders.add(order); } } } Set<String> ids = new HashSet<String>(); for (String order : orders) { String propPrefix = ALERT_PROP_PREFIX + order + "."; String id = env.getProperty(propPrefix + ID_PROP); if (ids.contains(id)) { throw new IllegalStateException("Wrong alert's configuration - id '" + id + "' is not unique."); } else { ids.add(id); } String limit = env.getProperty(propPrefix + LIMIT_PROP); String sql = env.getProperty(propPrefix + SQL_PROP); if (!StringUtils.containsIgnoreCase(sql, "count(")) { throw new IllegalStateException("Wrong alert's configuration - SQL clause for id '" + id + "' doesn't contain count()."); } String enabled = env.getProperty(propPrefix + ENABLED_PROP); enabled = enabled == null ? "true" : enabled; String subject = env.getProperty(propPrefix + MAIL_SBJ_PROP); String body = env.getProperty(propPrefix + MAIL_BODY_PROP); try { AlertInfo alertInfo = new AlertInfo(id, Long.valueOf(limit), sql, BooleanUtils.toBoolean(enabled), subject, body); addAlert(alertInfo); } catch (Exception ex) { throw new IllegalStateException("Wrong alert's configuration - conversion error", ex); } } } @Autowired  AlertsPropertiesConfiguration(ConfigurableEnvironment env);  static final String ALERT_PROP_PREFIX; static final String ID_PROP; static final String LIMIT_PROP; static final String SQL_PROP; static final String ENABLED_PROP; static final String MAIL_SBJ_PROP; static final String MAIL_BODY_PROP; }
@Test public void testConf() { Properties props = new Properties(); String prefix = AlertsPropertiesConfiguration.ALERT_PROP_PREFIX + "1."; props.put(prefix + AlertsPropertiesConfiguration.ID_PROP, "ID"); props.put(prefix + AlertsPropertiesConfiguration.LIMIT_PROP, "11"); props.put(prefix + AlertsPropertiesConfiguration.SQL_PROP, "select COUNT()"); prefix = AlertsPropertiesConfiguration.ALERT_PROP_PREFIX + "66."; props.put(prefix + AlertsPropertiesConfiguration.ID_PROP, "ID2"); props.put(prefix + AlertsPropertiesConfiguration.LIMIT_PROP, "11"); props.put(prefix + AlertsPropertiesConfiguration.SQL_PROP, "select COUNT()"); props.put(prefix + AlertsPropertiesConfiguration.ENABLED_PROP, "true"); props.put(prefix + AlertsPropertiesConfiguration.MAIL_SBJ_PROP, "subject"); props.put(prefix + AlertsPropertiesConfiguration.MAIL_BODY_PROP, "body"); env.getPropertySources().addFirst(new PropertiesPropertySource("alerts-test", props)); AlertsPropertiesConfiguration conf = new AlertsPropertiesConfiguration(env); conf.initProps(); assertThat(conf.getAlert("ID"), notNullValue()); assertThat(conf.getAlert("ID2"), notNullValue()); AlertInfo alert1 = conf.getAlert("ID"); assertThat(alert1.isEnabled(), is(true)); assertThat(alert1.getSql(), is("select COUNT()")); assertThat(alert1.getLimit(), is(11L)); assertThat(alert1.getNotificationSubject(), nullValue()); assertThat(alert1.getNotificationBody(), nullValue()); AlertInfo alert2 = conf.getAlert("ID2"); assertThat(alert2.isEnabled(), is(true)); assertThat(alert2.getSql(), is("select COUNT()")); assertThat(alert2.getLimit(), is(11L)); assertThat(alert2.getNotificationSubject(), is("subject")); assertThat(alert2.getNotificationBody(), is("body")); }
@Test(expected = IllegalStateException.class) public void testDuplicateId() { Properties props = new Properties(); String prefix = AlertsPropertiesConfiguration.ALERT_PROP_PREFIX + "1."; props.put(prefix + AlertsPropertiesConfiguration.ID_PROP, "ID"); props.put(prefix + AlertsPropertiesConfiguration.ID_PROP, "ID"); env.getPropertySources().addFirst(new PropertiesPropertySource("alerts-test", props)); AlertsPropertiesConfiguration conf = new AlertsPropertiesConfiguration(env); conf.initProps(); }