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; } });
@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); } }
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()); } }
/** * 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; }
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; }
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); }