.as(Integer.class) .withDefault(Math.max(2, Runtime.getRuntime().availableProcessors())) .getValue(); final int maxSize = ConfigResolver.resolve(configPrefix + "maxSize") .as(Integer.class) .withDefault(coreSize) .getValue(); final long keepAlive = ConfigResolver.resolve(configPrefix + "keepAlive.value") .as(Long.class) .withDefault(0L) .getValue(); final String keepAliveUnit = ConfigResolver.resolve(configPrefix + "keepAlive.unit") .as(String.class) .withDefault("MILLISECONDS") .getValue(); .as(String.class) .withDefault("LINKED") .getValue(); .as(Integer.class) .withDefault(1024) .getValue(); final boolean fair = ConfigResolver.resolve(configPrefix + "queue.fair") .as(Boolean.class) .withDefault(false) .getValue(); .as(Boolean.class)
@Override public ConfigResolver.TypedResolver<T> parameterizedBy(String propertyName) { this.propertyParameter = propertyName; if (propertyParameter != null && !propertyParameter.isEmpty()) { String parameterValue = ConfigResolver .resolve(propertyParameter) .withCurrentProjectStage(projectStageAware) .getValue(); if (parameterValue != null && !parameterValue.isEmpty()) { this.parameterValue = parameterValue; } } return this; }
@Test public void testConfiguredListValues_WithWhitespace() { List<String> emails = ConfigResolver.resolve("test.list.value.emails").asList().getValue(); Assert.assertNotNull(emails); Assert.assertEquals(3, emails.size()); Assert.assertTrue(emails.contains("test1@apache.org")); Assert.assertTrue(emails.contains("test2@apache.org")); Assert.assertTrue(emails.contains("test3@apache.org")); }
.withCurrentProjectStage(true) .parameterizedBy(property) .evaluateVariables(true)
.withCurrentProjectStage(true) .parameterizedBy(property) .evaluateVariables(true)
.withCurrentProjectStage(true) .parameterizedBy("dbvendor") .getValue()); .withCurrentProjectStage(false) .parameterizedBy("dbvendor") .getValue()); .withCurrentProjectStage(true) .parameterizedBy("INVALIDPARAMETER") .getValue()); .withCurrentProjectStage(true) .parameterizedBy("dbvendor3") .getValue()); .withCurrentProjectStage(false) .parameterizedBy("dbvendor3") .getValue());
/** * Resolves the value configured for the given key in the current * {@link org.apache.deltaspike.core.api.projectstage.ProjectStage}. * * <p> * First, it will search for a value configured for the given key suffixed with the current ProjectStage (e.g. * 'myproject.myconfig.Production'), and in case this value is not found (null or empty), it will look up the given * key without any suffix.</p> * * <p> * <b>Attention</b> This method must only be used after all ConfigSources got registered and it also must not be * used to determine the ProjectStage itself.</p> * * @param key * * @return the value configured for {@code <given key>.<current project stage>}, or just the configured value of * {@code <given key>} if the project-stage-specific value is not found (null or empty) * */ public static String getProjectStageAwarePropertyValue(String key) { return getConfigProvider().getConfig().resolve(key) .withCurrentProjectStage(true) .evaluateVariables(true) .getValue(); } /**
/** * Resolves the value configured for the given key in the current * {@link org.apache.deltaspike.core.api.projectstage.ProjectStage}. * * <p> * First, it will search for a value configured for the given key suffixed with the current ProjectStage (e.g. * 'myproject.myconfig.Production'), and in case this value is not found (null or empty), it will look up the given * key without any suffix.</p> * * <p> * <b>Attention</b> This method must only be used after all ConfigSources got registered and it also must not be * used to determine the ProjectStage itself.</p> * * @param key * * @return the value configured for {@code <given key>.<current project stage>}, or just the configured value of * {@code <given key>} if the project-stage-specific value is not found (null or empty) * */ public static String getProjectStageAwarePropertyValue(String key) { return getConfigProvider().getConfig().resolve(key) .withCurrentProjectStage(true) .evaluateVariables(true) .getValue(); } /**
@Test public void testStrict() { Assert.assertEquals("TestDataSource", ConfigResolver.resolve("dataSource") .withCurrentProjectStage(true) .parameterizedBy("dbvendor") .strictly(true) .getValue()); // no base.param, no value for base.param.ps Assert.assertEquals(null, ConfigResolver.resolve("dataSource") .withCurrentProjectStage(true) .parameterizedBy("dbvendor3") .strictly(true) .getValue()); // valid base.param, but no base.param.ps Assert.assertEquals(null, ConfigResolver.resolve("dataSource") .withCurrentProjectStage(true) .parameterizedBy("dbvendor2") .strictly(true) .getValue()); }
@Test public void testTypedResolver_OnChange() { final String key = "non.existing.key"; final AtomicInteger valueChanged = new AtomicInteger(0); ConfigResolver.TypedResolver<String> resolver = ConfigResolver.resolve(key) .logChanges(true) .onChange((k, oldValue, newValue) -> { Assert.assertEquals(key, k); valueChanged.incrementAndGet(); }); Assert.assertNull(resolver.getValue()); setTestConfigSourceValue(key, "somevalue"); Assert.assertEquals("somevalue", resolver.getValue()); Assert.assertEquals(1, valueChanged.get()); setTestConfigSourceValue(key, "newvalue"); Assert.assertEquals("newvalue", resolver.getValue()); Assert.assertEquals(2, valueChanged.get()); // this time we do not change anything Assert.assertEquals("newvalue", resolver.getValue()); Assert.assertEquals(2, valueChanged.get()); // last change setTestConfigSourceValue(key, null); Assert.assertNull(resolver.getValue()); Assert.assertEquals(3, valueChanged.get()); }
public <T> ConfigResolver.TypedResolver<T> asResolver(final String key, final String stringDefault, final Type ipCls, final Class<? extends ConfigResolver.Converter> converterType, final String parameterizedBy, final boolean projectStageAware, final boolean evaluate) { final ConfigResolver.UntypedResolver<String> untypedResolver = ConfigResolver.resolve(key); final ConfigResolver.TypedResolver<T> resolver = (ConfigResolver.Converter.class == converterType ? untypedResolver.as(Class.class.cast(ipCls)) : untypedResolver.as(ipCls, BeanProvider.getContextualReference(converterType))) .withCurrentProjectStage(projectStageAware); if (!ConfigProperty.NULL.equals(stringDefault)) { resolver.withStringDefault(stringDefault); } if (!ConfigProperty.NULL.equals(parameterizedBy)) { resolver.parameterizedBy(parameterizedBy); } return resolver.evaluateVariables(evaluate); } }
/** * {@link #getPropertyAwarePropertyValue(java.lang.String, java.lang.String)} which returns the provided default * value if no configured value can be found (<code>null</code> or empty). * * <p> * <b>Attention</b> This method must only be used after all ConfigSources got registered and it also must not be * used to determine the ProjectStage itself.</p> * * @param key * @param property the property to look up first and use as the parameter for the main lookup * @param defaultValue fallback value * * @return the configured value or if non found the defaultValue * */ public static String getPropertyAwarePropertyValue(String key, String property, String defaultValue) { return getConfigProvider().getConfig().resolve(key) .withCurrentProjectStage(true) .parameterizedBy(property) .withDefault(defaultValue) .evaluateVariables(true) .getValue(); }
/** * {@link #getPropertyAwarePropertyValue(java.lang.String, java.lang.String)} which returns the provided default * value if no configured value can be found (<code>null</code> or empty). * * <p> * <b>Attention</b> This method must only be used after all ConfigSources got registered and it also must not be * used to determine the ProjectStage itself.</p> * * @param key * @param property the property to look up first and use as the parameter for the main lookup * @param defaultValue fallback value * * @return the configured value or if non found the defaultValue * */ public static String getPropertyAwarePropertyValue(String key, String property, String defaultValue) { return getConfigProvider().getConfig().resolve(key) .withCurrentProjectStage(true) .parameterizedBy(property) .withDefault(defaultValue) .evaluateVariables(true) .getValue(); }
private void logConfiguration() { Boolean logConfig = ConfigResolver.resolve(ConfigResolver.DELTASPIKE_LOG_CONFIG).as(Boolean.class).getValue(); if (logConfig != null && logConfig && LOG.isLoggable(Level.INFO)) { StringBuilder sb = new StringBuilder(1 << 16); // first log out the config sources in descendent ordinal order sb.append("ConfigSources: "); ConfigSource[] configSources = ConfigResolver.getConfigSources(); for (ConfigSource configSource : configSources) { sb.append("\n\t").append(configSource.getOrdinal()).append(" - ").append(configSource.getConfigName()); } // and all the entries in no guaranteed order Map<String, String> allProperties = ConfigResolver.getAllProperties(); sb.append("\n\nConfigured Values:"); for (Map.Entry<String, String> entry : allProperties.entrySet()) { sb.append("\n\t") .append(entry.getKey()) .append(" = ") .append(ConfigResolver.filterConfigValueForLog(entry.getKey(), entry.getValue())); } LOG.info(sb.toString()); } }
public <T> ConfigResolver.TypedResolver<T> asResolver(final String key, final String stringDefault, final Type ipCls, final Class<? extends ConfigResolver.Converter> converterType, final String parameterizedBy, final boolean projectStageAware, final boolean evaluate) { final ConfigResolver.UntypedResolver<String> untypedResolver = ConfigResolver.resolve(key); final ConfigResolver.TypedResolver<T> resolver = (ConfigResolver.Converter.class == converterType ? untypedResolver.as(Class.class.cast(ipCls)) : untypedResolver.as(ipCls, BeanProvider.getContextualReference(converterType))) .withCurrentProjectStage(projectStageAware); if (!ConfigProperty.NULL.equals(stringDefault)) { resolver.withStringDefault(stringDefault); } if (!ConfigProperty.NULL.equals(parameterizedBy)) { resolver.parameterizedBy(parameterizedBy); } return resolver.evaluateVariables(evaluate); } }
@Test public void testGets() { ConfigResolver.TypedResolver<String> resolver = ConfigResolver.resolve("dataSource") .withCurrentProjectStage(true) .parameterizedBy("dbvendor") .withDefault("TESTDEFAULT"); Assert.assertEquals("TestDataSource", resolver.getValue()); Assert.assertEquals("dataSource", resolver.getKey()); Assert.assertEquals("TESTDEFAULT", resolver.getDefaultValue()); Assert.assertEquals("dataSource.mysql.UnitTest", resolver.getResolvedKey()); ConfigResolver.TypedResolver<String> resolver2 = ConfigResolver.resolve("testkey2") .withCurrentProjectStage(true) .parameterizedBy("INVALIDPARAMETER"); Assert.assertEquals("testvalue", resolver2.getValue()); Assert.assertEquals("testkey2", resolver2.getResolvedKey()); }
/** * {@link #getProjectStageAwarePropertyValue(String)} which returns the provided default value if no configured * value can be found (<code>null</code> or empty). * * @param key * @param defaultValue fallback value * * @return the configured value or if non found the defaultValue * */ public static String getProjectStageAwarePropertyValue(String key, String defaultValue) { return getConfigProvider().getConfig().resolve(key) .withCurrentProjectStage(true) .withDefault(defaultValue) .evaluateVariables(true) .getValue(); }
@Test public void testGetProjectStageAwarePropertyValue() { ProjectStageProducer.setProjectStage(ProjectStage.UnitTest); Assert.assertNull(ConfigResolver.getProjectStageAwarePropertyValue("notexisting", null)); Assert.assertEquals("testvalue", ConfigResolver.getPropertyValue("testkey")); Assert.assertEquals("unittestvalue", ConfigResolver.getProjectStageAwarePropertyValue("testkey")); Assert.assertEquals("unittestvalue", ConfigResolver.getProjectStageAwarePropertyValue("testkey", null)); Assert.assertEquals("testvalue", ConfigResolver.getPropertyValue("testkey2")); Assert.assertEquals("testvalue", ConfigResolver.getProjectStageAwarePropertyValue("testkey2")); Assert.assertEquals("testvalue", ConfigResolver.getProjectStageAwarePropertyValue("testkey2", null)); Assert.assertEquals("testvalue", ConfigResolver.getPropertyValue("testkey3")); Assert.assertEquals("", ConfigResolver.getProjectStageAwarePropertyValue("testkey3")); Assert.assertEquals(DEFAULT_VALUE, ConfigResolver.getProjectStageAwarePropertyValue("testkey3", DEFAULT_VALUE)); Assert.assertEquals(DEFAULT_VALUE, ConfigResolver.getProjectStageAwarePropertyValue("deltaspike.test.projectstagefallback", DEFAULT_VALUE)); Assert.assertEquals("", ConfigResolver.getProjectStageAwarePropertyValue("deltaspike.test.projectstagefallback")); Assert.assertEquals(DEFAULT_VALUE, ConfigResolver.resolve("deltaspike.test.projectstagefallback").as(String.class).withDefault(DEFAULT_VALUE).withCurrentProjectStage(true).getValue()); Assert.assertEquals("", ConfigResolver.resolve("deltaspike.test.projectstagefallback").as(String.class).withCurrentProjectStage(true).getValue()); }
@Test public void testWithCacheTime() throws Exception { ConfigResolver.TypedResolver<String> resolver = ConfigResolver.resolve("dataSource") .withCurrentProjectStage(true) .parameterizedBy("dbvendor") .cacheFor(TimeUnit.MILLISECONDS, 5) .withDefault("TESTDEFAULT"); Assert.assertEquals("TestDataSource", resolver.getValue()); Assert.assertEquals("TestDataSource", resolver.getValue()); Assert.assertEquals("dataSource", resolver.getKey()); Assert.assertEquals("TESTDEFAULT", resolver.getDefaultValue()); Assert.assertEquals("dataSource.mysql.UnitTest", resolver.getResolvedKey()); // because the clock steps in certain OS is only 16ms Thread.sleep(35L); Assert.assertEquals("TestDataSource", resolver.getValue()); }