@Override public String parseProperty(String key, String value, Properties properties) { return ConfigResolver.getPropertyValue(key); } }
@Override protected String getPropertyValue(FeatureState featureState, FeatureUser user, String name) { return ConfigResolver.getProjectStageAwarePropertyValue(name); } }
private List<ConfigEntry> calculateConfigEntries() { Map<String, String> allProperties = ConfigResolver.getAllProperties(); List<ConfigEntry> configEntries = new ArrayList<ConfigEntry>(allProperties.size()); ConfigSource[] configSources = ConfigResolver.getConfigSources(); for (Map.Entry<String, String> configEntry : allProperties.entrySet()) { String key = configEntry.getKey(); String value = ConfigResolver.filterConfigValueForLog(key, ConfigResolver.getProjectStageAwarePropertyValue(key)); String fromConfigSource = getFromConfigSource(configSources, key); configEntries.add(new ConfigEntry(key, value, fromConfigSource)); } return configEntries; }
/** * {@link #getProjectStageAwarePropertyValue(String)} which returns the defaultValue * if the property is <code>null</code> or empty. * @param key * @param defaultValue * @return the configured value or if non found the defaultValue * */ public static String getProjectStageAwarePropertyValue(String key, String defaultValue) { String value = getProjectStageAwarePropertyValue(key); return fallbackToDefaultIfEmpty(key, value, defaultValue); }
/** * Resolve the property value by going through the list of configured {@link ConfigSource}s * and use the one with the highest priority. If no configured value has been found that * way we will use the defaultValue. * * @param key the property key. * @param defaultValue will be used if no configured value for the key could be found. * @return the configured property value from the {@link ConfigSource} with the highest ordinal or * the defaultValue if there is no value explicitly configured. */ public static String getPropertyValue(String key, String defaultValue) { String value = getPropertyValue(key); return fallbackToDefaultIfEmpty(key, value, defaultValue); }
@Test public void testConfigFilter() { ConfigFilter configFilter = new TestConfigFilter(); Assert.assertEquals("shouldGetDecrypted: value", configFilter.filterValue("somekey.encrypted", "value")); Assert.assertEquals("**********", configFilter.filterValueForLog("somekey.password", "value")); ConfigResolver.addConfigFilter(configFilter); Assert.assertEquals("shouldGetDecrypted: value", ConfigResolver.getPropertyValue("testkey4.encrypted")); Assert.assertEquals("shouldGetDecrypted: value", ConfigResolver.getProjectStageAwarePropertyValue("testkey4.encrypted")); Assert.assertEquals("shouldGetDecrypted: value", ConfigResolver.getProjectStageAwarePropertyValue("testkey4.encrypted", null)); Assert.assertEquals("shouldGetDecrypted: value", ConfigResolver.getPropertyAwarePropertyValue("testkey4.encrypted", "dbvendor")); Assert.assertEquals("shouldGetDecrypted: value", ConfigResolver.getPropertyAwarePropertyValue("testkey4.encrypted", "dbvendor", null)); List<String> allPropertyValues = ConfigResolver.getAllPropertyValues("testkey4.encrypted"); Assert.assertNotNull(allPropertyValues); Assert.assertEquals(1, allPropertyValues.size()); Assert.assertEquals("shouldGetDecrypted: value", allPropertyValues.get(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()); }
public void init() { Set<String> ruleConfigKeys = new HashSet<String>(); // first we collect all the rule property names for (String propertyName : ConfigResolver.getAllProperties().keySet()) { if (propertyName.startsWith(CoreBaseConfig.InterDynCustomization.INTERDYN_RULE_PREFIX) && propertyName.contains(".match")) { ruleConfigKeys.add(propertyName.substring(0, propertyName.indexOf(".match"))); } } for (String ruleConfigKey : ruleConfigKeys) { String match = ConfigResolver.getPropertyValue(ruleConfigKey + ".match"); String annotationClassName = ConfigResolver.getPropertyValue(ruleConfigKey + ".annotation"); if (match != null && annotationClassName != null && match.length() > 0 && annotationClassName.length() > 0) { Annotation anno = getAnnotationImplementation(annotationClassName); boolean requiresProxy = anno.annotationType().getAnnotation(InterceptorBinding.class) != null; interceptorRules.add(new AnnotationRule(match, anno, requiresProxy)); } } if (interceptorRules.isEmpty()) { enabled = false; } }
/** * This method triggers freeing of the ConfigSources. */ @SuppressWarnings("UnusedDeclaration") public void freeConfigSources(@Observes BeforeShutdown bs) { String appName = ConfigResolver.getPropertyValue(ConfigResolver.DELTASPIKE_APP_NAME_CONFIG); unRegisterConfigMBean(appName); ConfigResolver.freeConfigSources(); detectedParentPropertyFileConfigs.remove(ClassUtils.getClassLoader(null)); }
@Test public void testConfigVariableRecursiveDeclaration() { String url = ConfigResolver.getPropertyValue("deltaspike.test.recursive.variable1", "", true); Assert.assertEquals("pre-crazy-post/ohgosh/crazy", url); ConfigResolver.TypedResolver<String> tr = ConfigResolver.resolve("deltaspike.test.recursive.variable1") .evaluateVariables(true).logChanges(true); Assert.assertEquals("pre-crazy-post/ohgosh/crazy", tr.getValue()); }
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()); } }
/** * Load additional configuration from the Configuration system * and overload the basic settings with that info. * * The key is deltaspike.persistence.config.${persistenceUnitName}.${originalKey} * * @see #CONFIG_PREFIX * @since 1.8.0 */ protected Properties addConfigProperties(Properties unitProperties, String persistenceUnitName) { // we start with a copy of the original properties Properties mergedConfig = new Properties(); mergedConfig.putAll(unitProperties); Set<String> allConfigKeys = ConfigResolver.getAllProperties().keySet(); String unitPrefix = CONFIG_PREFIX + persistenceUnitName + "."; for (String configKey : allConfigKeys) { if (configKey.startsWith(unitPrefix)) { mergedConfig.put(configKey.substring(unitPrefix.length()), ConfigResolver.getProjectStageAwarePropertyValue(configKey)); } } return mergedConfig; } }
/** * 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; }
@Override public void contextInitialized(ServletContextEvent sce) { ConfigSource[] configSources = ConfigResolver.getConfigSources(); for (ConfigSource configSource : configSources) { if (configSource instanceof ServletConfigSource) { setServletConfig((ServletConfigSource) configSource, sce); return; } } }
/** * 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; }
for (ConfigSource configSource : ConfigResolver.getConfigSources()) ConfigResolver.addConfigSources(Arrays.<ConfigSource>asList(testConfigSource));
protected void initContainerConfig() { containerConfig = new HashMap<String, String>(); for (Map.Entry<String, String> entry : ConfigResolver.getAllProperties().entrySet()) { if (entry.getKey().startsWith("org.apache.myfaces.") || entry.getKey().startsWith("javax.faces.") || entry.getKey().startsWith("facelets.")) { containerConfig.put(entry.getKey(), entry.getValue()); } } }
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); }
/** * This method can be used for programmatically adding {@link ConfigSource}s. * It is not needed for normal 'usage' by end users, but only for Extension Developers! * * @param configSourcesToAdd the ConfigSources to add */ public static synchronized void addConfigSources(List<ConfigSource> configSourcesToAdd) { // we first pickup all pre-configured ConfigSources... getConfigSources(); // and now we can easily add our own ClassLoader currentClassLoader = ClassUtils.getClassLoader(null); ConfigSource[] configuredConfigSources = configSources.get(currentClassLoader); List<ConfigSource> allConfigSources = new ArrayList<ConfigSource>(); allConfigSources.addAll(Arrays.asList(configuredConfigSources)); allConfigSources.addAll(configSourcesToAdd); // finally put all the configSources back into the map configSources.put(currentClassLoader, sortDescending(allConfigSources)); }
public void validateConfiguration(@Observes AfterDeploymentValidation adv) { List<ConfigSource> configSources = new ArrayList<ConfigSource>(cdiSources.size()); for (final Bean bean : cdiSources) { configSources.add(BeanProvider.getContextualReference(ConfigSource.class, bean)); } ConfigResolver.addConfigSources(configSources); for (final Bean bean : cdiFilters) { ConfigResolver.addConfigFilter(BeanProvider.getContextualReference(ConfigFilter.class, bean)); } processConfigurationValidation(adv); }