@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 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()); }
.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());
.withCurrentProjectStage(true) .parameterizedBy(property) .evaluateVariables(true)
/** * 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()); }
/** * {@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(); }
@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(); }
/** * {@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 testProjectStageAware() { Assert.assertEquals("unittestvalue", ConfigResolver.resolve("testkey") .withCurrentProjectStage(true) .getValue()); Assert.assertEquals("testvalue", ConfigResolver.resolve("testkey") .withCurrentProjectStage(false) .getValue()); // property without PS, with PS-aware Assert.assertEquals("testvalue", ConfigResolver.resolve("testkey2") .withCurrentProjectStage(true) .getValue()); }