final int coreSize = ConfigResolver.resolve(configPrefix + "coreSize") .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(); .withDefault("LINKED") .getValue(); final BlockingQueue<Runnable> queue; if ("ARRAY".equalsIgnoreCase(queueType)) .withDefault(1024) .getValue(); final boolean fair = ConfigResolver.resolve(configPrefix + "queue.fair") .as(Boolean.class) .withDefault(false) .getValue(); queue = new ArrayBlockingQueue<Runnable>(size, fair);
typedResolver.cacheFor(MILLISECONDS, cacheMs); ConfigResolver.TypedResolver<? extends List<?>> listTypedResolver = typedResolver.asList(); final ConfigResolver.TypedResolver<? extends List<?>> resolver; if (!ConfigProperty.NULL.equals(defaultValue)) resolver = listTypedResolver.withStringDefault(defaultValue);
/** * recursively resolve any ${varName} in the value */ private String resolveVariables(String value) { int startVar = 0; while ((startVar = value.indexOf("${", startVar)) >= 0) { int endVar = value.indexOf("}", startVar); if (endVar <= 0) { break; } String varName = value.substring(startVar + 2, endVar); if (varName.isEmpty()) { break; } String variableValue = new TypedResolverImpl<String>(this.config, varName) .withCurrentProjectStage(this.projectStageAware) .evaluateVariables(true) .getValue(); if (variableValue != null) { value = value.replace("${" + varName + "}", variableValue); } startVar++; } return value; }
.parameterizedBy(property) .evaluateVariables(true) .getValue();
.parameterizedBy(property) .evaluateVariables(true) .getValue();
ConfigResolver.resolve("dataSource") .withCurrentProjectStage(true) .parameterizedBy("dbvendor") .getValue()); ConfigResolver.resolve("dataSource") .withCurrentProjectStage(false) .parameterizedBy("dbvendor") .getValue()); ConfigResolver.resolve("dataSource") .withCurrentProjectStage(true) .parameterizedBy("INVALIDPARAMETER") .getValue()); ConfigResolver.resolve("dataSource") .withCurrentProjectStage(true) .parameterizedBy("dbvendor3") .getValue()); ConfigResolver.resolve("dataSource") .withCurrentProjectStage(false) .parameterizedBy("dbvendor3") .getValue());
port2Cfg = cfg.resolve(PORT2_KEY).as(Integer.class); assertEquals("host1", hostCfg.getValue()); assertEquals(Integer.valueOf(1), port1Cfg.getValue()); assertEquals(Integer.valueOf(1), port2Cfg.getValue()); assertEquals("host1", hostCfg.getValue(configSnapshot)); assertEquals(Integer.valueOf(1), port1Cfg.getValue(configSnapshot)); assertEquals(Integer.valueOf(1), port2Cfg.getValue(configSnapshot)); configSource.setValues(newVals); assertEquals("host1", hostCfg.getValue(configSnapshot)); assertEquals(Integer.valueOf(1), port1Cfg.getValue(configSnapshot)); assertEquals(Integer.valueOf(1), port2Cfg.getValue(configSnapshot)); assertEquals("host2", hostCfg.getValue()); assertEquals(Integer.valueOf(2), port1Cfg.getValue()); assertEquals(Integer.valueOf(2), port2Cfg.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 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()); }
/** * 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()); }
@Override public ConfigSnapshot snapshotFor(ConfigResolver.TypedResolver<?>... typedResolvers) { // we implement kind of optimistic Locking // Means we try multiple time to resolve all the given values // until the config didn't change inbetween. for (int tries = 1; tries < 5; tries++) { Map<ConfigResolver.TypedResolver<?>, Object> configValues = new HashMap<>(); long startReadLastChanged = lastChanged; for (ConfigResolver.TypedResolver<?> typedResolver : typedResolvers) { configValues.put(typedResolver, typedResolver.getValue()); } if (startReadLastChanged == lastChanged) { return new ConfigSnapshotImpl(configValues); } } throw new IllegalStateException( "Could not resolve ConfigTransaction as underlying values are permanently changing!"); }
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()); } }
/** * {@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(); }
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); } }
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); } }
@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 testValidTypes() { Assert.assertEquals("configured", ConfigResolver.resolve("deltaspike.test.string-value").getValue()); Assert.assertEquals(Boolean.FALSE, ConfigResolver.resolve("deltaspike.test.boolean-value").as(Boolean.class) .getValue()); Assert.assertEquals(TestConfigSource.class, ConfigResolver.resolve("deltaspike.test.class-value").as(Class .class).getValue()); Assert.assertEquals(5l, (int) ConfigResolver.resolve("deltaspike.test.integer-value").as(Integer.class) .getValue()); Assert.assertEquals(8589934592l, (long) ConfigResolver.resolve("deltaspike.test.long-value").as(Long.class) .getValue()); Assert.assertEquals(-1.1f, (float) ConfigResolver.resolve("deltaspike.test.float-value").as(Float.class) .getValue(), 0); Assert.assertEquals(4e40d, (double) ConfigResolver.resolve("deltaspike.test.double-value").as(Double.class) .getValue(), 0); }
@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()); }