public static Map<String, String> getAllProperties() { List<ConfigSource> appConfigSources = sortAscending(new ArrayList<ConfigSource>(Arrays.asList(getConfigSources()))); Map<String, String> result = new HashMap<String, String>(); for (ConfigSource configSource : appConfigSources) { if (configSource.isScannable()) { result.putAll(configSource.getProperties()); } } return Collections.unmodifiableMap(result); }
private String getFromConfigSource(ConfigSource[] configSources, String key) { for (ConfigSource configSource : configSources) { if (configSource.getPropertyValue(key) != null) { return configSource.getConfigName(); } } return null; }
/** * {@inheritDoc} */ @Override public int compare(ConfigSource configSource1, ConfigSource configSource2) { int o1 = configSource1.getOrdinal(); int o2 = configSource2.getOrdinal(); if (o1 == o2) { return configSource1.getConfigName().compareTo(configSource2.getConfigName()); } return (o1 > o2) ? -1 : 1; } });
/** * Resolve all values for the given key, from all registered ConfigSources ordered by their * ordinal value in ascending ways. If more {@link ConfigSource}s have the same ordinal, their * order is undefined. * * @param key under which configuration is stored * @return List with all found property values, sorted in ascending order of their ordinal. * @see org.apache.deltaspike.core.spi.config.ConfigSource#getOrdinal() */ public static List<String> getAllPropertyValues(String key) { List<ConfigSource> appConfigSources = sortAscending(new ArrayList<ConfigSource>(Arrays.asList(getConfigSources()))); List<String> result = new ArrayList<String>(); String value; for (ConfigSource configSource : appConfigSources) { value = configSource.getPropertyValue(key); if (value != null) { value = filterConfigValue(key, value); if (!result.contains(value)) { result.add(value); } } } return result; }
private void setTestConfigSourceValue(String key, String value) { ConfigSource[] configSources = ConfigResolver.getConfigSources(); for (ConfigSource configSource : configSources) { if (configSource instanceof TestConfigSource) { if (value == null) { configSource.getProperties().remove(key); } else { configSource.getProperties().put(key, value); } break; } } }
/** * {@inheritDoc} */ @Override public int compare(ConfigSource configSource1, ConfigSource configSource2) { return (configSource1.getOrdinal() > configSource2.getOrdinal()) ? 1 : -1; } });
@Override public void addConfigSources(List<ConfigSource> configSourcesToAdd) { if (configSourcesToAdd == null || configSourcesToAdd.isEmpty()) { return; } List<ConfigSource> allConfigSources = new ArrayList<>(); // start with all existing ConfigSources if (this.configSources != null) { for (ConfigSource configSource : this.configSources) { allConfigSources.add(configSource); } } for (ConfigSource configSourceToAdd : configSourcesToAdd) { configSourceToAdd.setOnAttributeChange(this::onAttributeChange); allConfigSources.add(configSourceToAdd); } this.configSources = sortDescending(allConfigSources); }
/** * Resolve all values for the given key. * * @param key * * @return a List of all found property values, sorted by their ordinal in ascending order * * @see org.apache.deltaspike.core.spi.config.ConfigSource#getOrdinal() */ public static List<String> getAllPropertyValues(String key) { ConfigSource[] configSources = getConfigProvider().getConfig().getConfigSources(); List<String> result = new ArrayList<String>(); for (int i = configSources.length; i > 0; i--) { String value = configSources[i - 1].getPropertyValue(key); if (value != null) { value = filterConfigValue(key, value); if (!result.contains(value)) { result.add(value); } } } return result; }
/** * {@inheritDoc} */ @Override public int compare(ConfigSource configSource1, ConfigSource configSource2) { return (configSource1.getOrdinal() > configSource2.getOrdinal()) ? -1 : 1; } });
@Override public String[] getConfigSourcesAsString() { ClassLoader originalCl = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(appConfigClassLoader); ConfigSource[] configSources = ConfigResolver.getConfigSources(); List<String> configSourceInfo = new ArrayList<String>(); for (ConfigSource configSource : configSources) { configSourceInfo.add(Integer.toString(configSource.getOrdinal()) + " - " + configSource.getConfigName()); } return configSourceInfo.toArray(new String[configSourceInfo.size()]); } finally { // set back the original TCCL Thread.currentThread().setContextClassLoader(originalCl); } }
/** * Resolve the property value by going through the list of configured {@link ConfigSource}s * and use the one with the highest priority. * * @param key the property key. * @return the configured property value from the {@link ConfigSource} with the highest ordinal or * null if there is no configured value for it. */ public static String getPropertyValue(String key) { ConfigSource[] appConfigSources = getConfigSources(); String value; for (ConfigSource configSource : appConfigSources) { value = configSource.getPropertyValue(key); if (value != null) { LOG.log(Level.FINE, "found value {0} for key {1} in ConfigSource {2}.", new Object[]{filterConfigValueForLog(key, value), key, configSource.getConfigName()}); return filterConfigValue(key, value); } LOG.log(Level.FINER, "NO value found for key {0} in ConfigSource {1}.", new Object[]{key, configSource.getConfigName()}); } return null; }
/** * Returns a Map of all properties from all scannable config sources. The values of the properties reflect the * values that would be obtained by a call to {@link #getPropertyValue(java.lang.String)}, that is, the value of the * property from the ConfigSource with the highest ordinal. * * @see ConfigSource#isScannable() */ public static Map<String, String> getAllProperties() { ConfigSource[] configSources = getConfigProvider().getConfig().getConfigSources(); Map<String, String> result = new HashMap<String, String>(); for (int i = configSources.length; i > 0; i--) { ConfigSource configSource = configSources[i - 1]; if (configSource.isScannable()) { result.putAll(configSource.getProperties()); } } return Collections.unmodifiableMap(result); }
/** * Resolve all values for the given key. * * @param key * * @return a List of all found property values, sorted by their ordinal in ascending order * * @see org.apache.deltaspike.core.spi.config.ConfigSource#getOrdinal() */ public static List<String> getAllPropertyValues(String key) { ConfigSource[] configSources = getConfigProvider().getConfig().getConfigSources(); List<String> result = new ArrayList<String>(); for (int i = configSources.length; i > 0; i--) { String value = configSources[i - 1].getPropertyValue(key); if (value != null) { value = filterConfigValue(key, value); if (!result.contains(value)) { result.add(value); } } } return result; }
private static synchronized ConfigSource[] getConfigSources() { ClassLoader currentClassLoader = ClassUtils.getClassLoader(null); ConfigSource[] appConfigSources = configSources.get(currentClassLoader); if (appConfigSources == null) { appConfigSources = sortDescending(resolveConfigSources()); if (LOG.isLoggable(Level.FINE)) { for (ConfigSource cs : appConfigSources) { LOG.log(Level.FINE, "Adding ordinal {0} ConfigSource {1}", new Object[]{cs.getOrdinal(), cs.getConfigName()}); } } configSources.put(currentClassLoader, appConfigSources); } return appConfigSources; }
private String getPropertyValue(String key) { String value; for (ConfigSource configSource : config.getConfigSources()) { value = configSource.getPropertyValue(key); if (value != null) { if (LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "found value {0} for key {1} in ConfigSource {2}.", new Object[]{config.filterConfigValue(key, value, true), key, configSource.getConfigName()}); } if (this.evaluateVariables) { value = resolveVariables(value); } return config.filterConfigValue(key, value, false); } if (LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINER, "NO value found for key {0} in ConfigSource {1}.", new Object[]{key, configSource.getConfigName()}); } } return null; }
/** * Returns a Map of all properties from all scannable config sources. The values of the properties reflect the * values that would be obtained by a call to {@link #getPropertyValue(java.lang.String)}, that is, the value of the * property from the ConfigSource with the highest ordinal. * * @see ConfigSource#isScannable() */ public static Map<String, String> getAllProperties() { ConfigSource[] configSources = getConfigProvider().getConfig().getConfigSources(); Map<String, String> result = new HashMap<String, String>(); for (int i = configSources.length; i > 0; i--) { ConfigSource configSource = configSources[i - 1]; if (configSource.isScannable()) { result.putAll(configSource.getProperties()); } } return Collections.unmodifiableMap(result); }
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()); } }
new Object[]{configSource.getOrdinal(), configSource.getConfigName()}));
/** * Performs all the initialisation of the default * ConfigSources, ConfigFilters, etc */ void init() { List<ConfigSource> appConfigSources = ServiceUtils.loadServiceImplementations(ConfigSource.class, false, classLoader); List<ConfigSourceProvider> configSourceProviderServiceLoader = ServiceUtils.loadServiceImplementations(ConfigSourceProvider.class, false, classLoader); for (ConfigSourceProvider configSourceProvider : configSourceProviderServiceLoader) { appConfigSources.addAll(configSourceProvider.getConfigSources()); } addConfigSources(appConfigSources); if (LOG.isLoggable(Level.FINE)) { for (ConfigSource cs : appConfigSources) { LOG.log(Level.FINE, "Adding ordinal {0} ConfigSource {1}", new Object[]{cs.getOrdinal(), cs.getConfigName()}); } } List<ConfigFilter> configFilters = ServiceUtils.loadServiceImplementations(ConfigFilter.class, false, classLoader); this.configFilters = new CopyOnWriteArrayList<>(configFilters); }