private Optional<String> findInMpSources(String propertyName) { String propertyValue = null; for (ConfigSource source : mpConfigSources) { propertyValue = source.getValue(propertyName); if (null != propertyValue) { break; } } return Optional.ofNullable(propertyValue); }
private void addConfigSource(ConfigSource source) { LOGGER.finest(() -> "Adding config source: " + source.getName() + " (" + source.getClass() .getName() + "), values: " + source.getProperties()); mpConfigSources.add(source); helidonConfigSources.add(wrapSource(source)); }
private OrdinalConfigSource wrapSource(ConfigSource source) { io.helidon.config.spi.ConfigSource myCs = ConfigSources.create(source.getProperties()).build(); return new OrdinalConfigSource(myCs, source.getOrdinal()); }
@Override public int compare(ConfigSource o1, ConfigSource o2) { int v1 = o1.getOrdinal(); int v2 = o2.getOrdinal(); if ( v1 > v2 ) { return -11; } if ( v1 < v2 ) { return +1; } // if 2 config sources have the same ordinal, // provide consistent order by sorting them // according to their name. if (o2.getName() != null && o1.getName() != null) { return o2.getName().compareTo(o1.getName()); } return 0; } });
/** * Create a new instance. * * @param config configuration * @param mpConfigSources config sources * @param converterClasses classes of converters * @param converters class to converter mapping */ MpConfig(Config config, List<ConfigSource> mpConfigSources, Set<Class<?>> converterClasses, Map<Class<?>, Converter<?>> converters) { final AtomicReference<Config> ref = new AtomicReference<>(config); config.onChange(newConfig -> { ref.set(newConfig); return true; }); this.config = ref::get; this.mpConfigSources = mpConfigSources; this.propertyNames = Stream.concat(mpConfigSources.stream() .flatMap(cs -> cs.getPropertyNames().stream()), config.traverse(Config::isLeaf) .map(Config::key) .map(Config.Key::toString)) .collect(Collectors.toSet()); this.converterClasses = new HashSet<>(converterClasses); this.converters = converters; }
public String getValue(String key) { for (ConfigSource configSource : configSources) { String value = configSource.getValue(key); if (value != null) { if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "found value {0} for key {1} in ConfigSource {2}.", new Object[]{value, key, configSource.getName()}); } return value; } } return null; }
/** * Get all properties of this config as a map. * * @return map where keys are configuration keys and values are associated string values */ public Map<String, String> asMap() { // config from helidon config instance Map<String, String> map = new HashMap<>(config.get().asMap().get()); // now add all properties from sources of MP config List<ConfigSource> configSources = new ArrayList<>(mpConfigSources); Collections.reverse(configSources); for (ConfigSource configSource : configSources) { map.putAll(configSource.getProperties()); } return map; }
private ConfigSource getMemoryConfigSource() { for(ConfigSource configSource:config.getConfigSources()){ if(configSource.getName().equals(MemoryConfigSource.NAME))return configSource; } return null; }
@Test public void testGetConfigSources() { Iterable<ConfigSource> configSources = config.getConfigSources(); Assert.assertNotNull(configSources); // check descending sorting int prevOrdinal = Integer.MAX_VALUE; for (ConfigSource configSource : configSources) { Assert.assertTrue(configSource.getOrdinal() <= prevOrdinal); prevOrdinal = configSource.getOrdinal(); } }
/** * ConfigSources are sorted with descending ordinal. * If 2 ConfigSources have the same ordinal, then they get sorted according to their name, alphabetically. */ private List<ConfigSource> sortDescending(List<ConfigSource> configSources) { configSources.sort( (configSource1, configSource2) -> { int compare = Integer.compare(configSource2.getOrdinal(), configSource1.getOrdinal()); if (compare == 0) { return configSource1.getName().compareTo(configSource2.getName()); } return compare; }); return configSources; }
public Set<String> keySet() { return delegate.getPropertyNames(); }
private String getPropertyValue(String key,String defaultValue){ Config config = ConfigProvider.getConfig(); Iterable<ConfigSource> configSources = config.getConfigSources(); for(ConfigSource configsource:configSources){ if(!configsource.getName().equals(NAME)){ String val = configsource.getValue(key); if(val!=null && !val.isEmpty())return val; } } return defaultValue; }
/** * Gets all property names known to this config source, without evaluating the values. * * For backwards compatibility, there is a default implementation that just returns the keys of {@code getProperties()} * slower ConfigSource implementations should replace this with a more performant implementation * * @return the set of property keys that are known to this ConfigSource */ default Set<String> getPropertyNames() { return getProperties().keySet(); }
@Override protected void performRuntime(OperationContext context, ModelNode operation, ModelNode model) throws OperationFailedException { super.performRuntime(context, operation, model); String name = context.getCurrentAddressValue(); int ordinal = ORDINAL.resolveModelAttribute(context, model).asInt(); ModelNode props = PROPERTIES.resolveModelAttribute(context, model); ModelNode classModel = CLASS.resolveModelAttribute(context, model); ModelNode dirModel = DIR.resolveModelAttribute(context, model); final ConfigSource configSource; if (classModel.isDefined()) { Class configSourceClass = unwrapClass(classModel); try { configSource = ConfigSource.class.cast(configSourceClass.newInstance()); } catch (Exception e) { throw new OperationFailedException(e); } } else if (dirModel.isDefined()) { File dir = new File(dirModel.asString()); configSource = new DirConfigSource(dir, ordinal); } else { Map<String, String> properties = PropertiesAttributeDefinition.unwrapModel(context, props); configSource = new PropertiesConfigSource(properties, name, ordinal); } MicroProfileConfigLogger.ROOT_LOGGER.info("Reading properties from " + configSource.getName()); ConfigSourceService.install(context, name, configSource); } }, new AbstractRemoveStepHandler() {
public String get(final Object key) { return key instanceof String ? delegate.getValue((String) key) : null; }
private JsonObject toJsonObject(ConfigSource source){ return Json.createObjectBuilder().add(String.valueOf(source.getOrdinal()), source.getName()).build(); }
public boolean isEmpty() { // may be cheaper in some cases return delegate.getPropertyNames().isEmpty(); }
private void addConfigSource(ConfigSource source) { LOGGER.finest(() -> "Adding config source: " + source.getName() + " (" + source.getClass() .getName() + "), values: " + source.getProperties()); mpConfigSources.add(source); helidonConfigSources.add(wrapSource(source)); }
private OrdinalConfigSource wrapSource(ConfigSource source) { io.helidon.config.spi.ConfigSource myCs = ConfigSources.create(source.getProperties()).build(); return new OrdinalConfigSource(myCs, source.getOrdinal()); }
@Override public Iterable<String> getPropertyNames() { Set<String> names = new HashSet<>(); for (ConfigSource configSource : configSources) { names.addAll(configSource.getProperties().keySet()); } return names; }