/** * Resolves any {@link KafkaPropertyUtils#isNullValue(Object) null} property values in the {@code Map} using the * supplied {@code propertyResolver} and {@code Environment}. * * @param props * @param propertyResolver * @param env */ public static void resolveProperties(Map<String, Object> props, PropertySourcesPropertyResolver propertyResolver, Environment env) { props.entrySet().stream() .filter(entry -> isNullValue(entry.getValue()) || entry.getValue().toString().contains("${")) .forEach(entry -> { String resolvedValue = null; if (isNullValue(entry.getValue())) { resolvedValue = env.getProperty(entry.getKey()); LOG.debug("Resolved kafka null property key [{}] to [{}]", entry.getKey(), resolvedValue); } else { resolvedValue = entry.getValue().toString(); } if (resolvedValue != null && resolvedValue.contains("${")) { String placeholder = resolvedValue; resolvedValue = propertyResolver.resolvePlaceholders(resolvedValue); resolvedValue = env.resolvePlaceholders(resolvedValue); LOG.debug("Resolved kafka property key [{}] with placeholder [{}] to [{}]", entry.getKey(), placeholder, resolvedValue); } props.put(entry.getKey(), resolvedValue); }); }
private void processNonEnumerablePropertySource(PropertySource<?> source, PropertySourcesPropertyResolver resolver) { // We can only do exact matches for non-enumerable property names, but // that's better than nothing... if (this.nonEnumerableFallbackNames == null) { return; } for (String propertyName : this.nonEnumerableFallbackNames) { if (!source.containsProperty(propertyName)) { continue; } Object value = null; try { value = resolver.getProperty(propertyName, Object.class); } catch (RuntimeException ex) { // Probably could not convert to Object, weird, but ignorable } if (value == null) { value = source.getProperty(propertyName.toUpperCase()); } putIfAbsent(propertyName, value, source); } }
/** * Creates a property resolver that pulls from multiple property resource * locations. The first "built-in" location must be successfully loaded, but * all other "custom" locations must load only if {code}allowMissing{code} is * false. * * @param allowMissing true to allow custom resource locations to fail to load * @param builtInResourceLocation lowest precedence, required resource * location for properties * @param customResourceLocations additional resource locations for * properties, in increasing order of precedence * @return new property resolver * @throws IOException if the built-in resource location could not be loaded, * or if {code}allowMissing{code} is false and any custom resource location * fails to load * @throws NullPointerException if any resource location is null */ public static PropertyResolver newMultiResourcePropertyResolver(boolean allowMissing, String builtInResourceLocation, String... customResourceLocations) throws IOException { MutablePropertySources sources = new MutablePropertySources(); checkNotNull(builtInResourceLocation, "builtInResourceLocation is null"); sources.addLast(buildPropertySource(BUILT_IN_NAME, builtInResourceLocation, false)); String lastname = BUILT_IN_NAME; int customCtr = 1; for (String loc : customResourceLocations) { checkNotNull(loc, "customResourceLocations[" + (customCtr - 1) + "] is null"); String thisname = CUSTOM_NAME_PREFIX + customCtr++; PropertySource source = buildPropertySource(thisname, loc, allowMissing); if (source != null) { sources.addBefore(lastname, source); lastname = thisname; } } return new PropertySourcesPropertyResolver(sources); }
private void processNonEnumerablePropertySource(PropertySource<?> source, PropertySourcesPropertyResolver resolver) { // We can only do exact matches for non-enumerable property names, but // that's better than nothing... if (this.nonEnumerableFallbackNames == null) { return; } for (String propertyName : this.nonEnumerableFallbackNames) { if (!source.containsProperty(propertyName)) { continue; } Object value = null; try { value = resolver.getProperty(propertyName, Object.class); } catch (RuntimeException ex) { // Probably could not convert to Object, weird, but ignoreable } if (value == null) { value = source.getProperty(propertyName.toUpperCase()); } putIfAbsent(propertyName, value, source); } }
@Override public void postProcessBeanFactory(final ConfigurableListableBeanFactory beanFactory) { if (propertySources == null) { propertySources = new MutablePropertySources(); if (environment != null) { propertySources.addLast( new PropertySource<Environment>(PropertySourcesPlaceholderConfigurer.ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, environment) { @Override public String getProperty(final String key) { return source.getProperty(key); } } ); } ConfigPropertySource configPropertySource = new ConfigPropertySource(CONFIG_PROPERTY_SOURCE_NAME, config); if (localOverride) { propertySources.addFirst(configPropertySource); } else { propertySources.addLast(configPropertySource); } } processProperties(beanFactory, new PropertySourcesPropertyResolver(propertySources)); appliedPropertySources = propertySources; }
/** * {@inheritDoc} * <p>Processing occurs by replacing ${...} placeholders in bean definitions by resolving each * against this configurer's set of {@link PropertySources}, which includes: * <ul> * <li>all {@linkplain org.springframework.core.env.ConfigurableEnvironment#getPropertySources * environment property sources}, if an {@code Environment} {@linkplain #setEnvironment is present} * <li>{@linkplain #mergeProperties merged local properties}, if {@linkplain #setLocation any} * {@linkplain #setLocations have} {@linkplain #setProperties been} * {@linkplain #setPropertiesArray specified} * <li>any property sources set by calling {@link #setPropertySources} * </ul> * <p>If {@link #setPropertySources} is called, <strong>environment and local properties will be * ignored</strong>. This method is designed to give the user fine-grained control over property * sources, and once set, the configurer makes no assumptions about adding additional sources. */ @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (this.propertySources == null) { this.propertySources = new MutablePropertySources(); if (this.environment != null) { this.propertySources.addLast( new PropertySource<Environment>(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, this.environment) { @Override public String getProperty(String key) { return this.source.getProperty(key); } } ); } try { PropertySource<?> localPropertySource = new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergeProperties()); if (this.localOverride) { this.propertySources.addFirst(localPropertySource); } else { this.propertySources.addLast(localPropertySource); } } catch (IOException ex) { throw new BeanInitializationException("Could not load properties", ex); } } processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources)); this.appliedPropertySources = this.propertySources; }
Map<String, Object> getConfigurationProperties() { Map<String, Object> props = asMap(sources, prefix, strip); PropertySourcesPropertyResolver propertyResolver = new PropertySourcesPropertyResolver(sources); resolveProperties(props, propertyResolver, env); return props; }
private PropertyResolver getTitleResolver(Class<?> sourceClass) { MutablePropertySources sources = new MutablePropertySources(); String applicationTitle = getApplicationTitle(sourceClass); Map<String, Object> titleMap = Collections.<String, Object>singletonMap( "application.title", (applicationTitle == null ? "" : applicationTitle)); sources.addFirst(new MapPropertySource("title", titleMap)); return new PropertySourcesPropertyResolver(sources); }
/** * Create a new PropertyValues from the given PropertySources. * @param propertySources a PropertySources instance * @param nonEnumerableFallbackNames the property names to try in lieu of an * {@link EnumerablePropertySource}. * @param includes the property name patterns to include */ PropertySourcesPropertyValues(PropertySources propertySources, Collection<String> nonEnumerableFallbackNames, PropertyNamePatternsMatcher includes) { Assert.notNull(propertySources, "PropertySources must not be null"); Assert.notNull(includes, "Includes must not be null"); this.propertySources = propertySources; this.nonEnumerableFallbackNames = nonEnumerableFallbackNames; this.includes = includes; PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver( propertySources); for (PropertySource<?> source : propertySources) { processPropertySource(source, resolver); } }
private void processPropertySource(PropertySource<?> source, PropertySourcesPropertyResolver resolver) { if (source instanceof CompositePropertySource) { processCompositePropertySource((CompositePropertySource) source, resolver); } else if (source instanceof EnumerablePropertySource) { processEnumerablePropertySource((EnumerablePropertySource<?>) source, resolver, this.includes); } else { processNonEnumerablePropertySource(source, resolver); } }
private void processEnumerablePropertySource(EnumerablePropertySource<?> source, PropertySourcesPropertyResolver resolver, PropertyNamePatternsMatcher includes) { if (source.getPropertyNames().length > 0) { for (String propertyName : source.getPropertyNames()) { if (includes.matches(propertyName)) { Object value = getEnumerableProperty(source, resolver, propertyName); putIfAbsent(propertyName, value, source); } } } }
private Object getEnumerableProperty(EnumerablePropertySource<?> source, PropertySourcesPropertyResolver resolver, String propertyName) { try { return resolver.getProperty(propertyName, Object.class); } catch (RuntimeException ex) { // Probably could not resolve placeholders, ignore it here return source.getProperty(propertyName); } }
private PropertyResolver getPropertyResolver(String file, String path) { Map<String, Object> properties = new LinkedHashMap<String, Object>(); properties.put("logging.file", file); properties.put("logging.path", path); PropertySource<?> propertySource = new MapPropertySource("properties", properties); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(propertySource); return new PropertySourcesPropertyResolver(propertySources); }
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (this.propertySources == null) { this.propertySources = new MutablePropertySources(); if (this.environment != null) { this.propertySources.addLast( new PropertySource<Environment>(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, this.environment) { @Override public String getProperty(String key) { return this.source.getProperty(key); } }); } PropertySource<?> localPropertySource = new PropertySource<Configuration>( PropertySourcesPlaceholderConfigurer.LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, configuration) { @Override public Object getProperty(String name) { if (name != null && name.indexOf("?:") > 0) { name = name.substring(0, name.indexOf("?:")); } return this.source.getProperty(name); } }; propertySources.addFirst(localPropertySource); } processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources)); this.appliedPropertySources = this.propertySources; }
/** * Creates a new property resolver that gets properties from the given map. * * @param m property map * @return new property resolver * @throws NullPointerException if the map is null */ public static PropertyResolver newMapPropertyResolver(Map<String, String> m) { checkNotNull(m, "map is null"); MutablePropertySources sources = new MutablePropertySources(); sources.addFirst(new MapPropertySource("map", ImmutableMap.<String, Object>copyOf(m))); return new PropertySourcesPropertyResolver(sources); }
@Override public void contextInitialized(ServletContextEvent contextEvent) { ServletContext servletContext = contextEvent.getServletContext(); WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext); PropertySourcesPlaceholderConfigurer bean = webApplicationContext.getBean(PropertySourcesPlaceholderConfigurer.class); PropertySourcesPropertyResolver resolver = new PropertySourcesPropertyResolver(bean.getAppliedPropertySources()); servletContext.setAttribute("gps.enabled", Boolean.valueOf(resolver.resolvePlaceholders("${gps.enabled:false}"))); servletContext.setAttribute("appCache.disabled", Boolean.valueOf(resolver.resolvePlaceholders("${appCache.disabled:false}"))); servletContext.setAttribute("javascript.useSource", Boolean.valueOf(resolver.resolvePlaceholders("${javascript.useSource:false}"))); servletContext.setAttribute("routeAlgorithm.useRoutes", Boolean.valueOf(resolver.resolvePlaceholders("${routeAlgorithm.useRoutes:false}"))); servletContext.setAttribute("alert.message", resolver.resolvePlaceholders("${alert.message:}")); servletContext.setAttribute("app.name", resolver.resolvePlaceholders("${app.name:Tren Urbano App}")); servletContext.setAttribute("report.phone", resolver.resolvePlaceholders("${report.phone:311}")); servletContext.setAttribute("map.center.lat", resolver.resolvePlaceholders("${map.center.lat:18.430189}")); servletContext.setAttribute("map.center.lng", resolver.resolvePlaceholders("${map.center.lng:-66.060061}")); servletContext.setAttribute("map.bounds.southwest.lat", resolver.resolvePlaceholders("${map.bounds.southwest.lat:18.352687}")); servletContext.setAttribute("map.bounds.southwest.lng", resolver.resolvePlaceholders("${map.bounds.southwest.lng:-66.179752}")); servletContext.setAttribute("map.bounds.northeast.lat", resolver.resolvePlaceholders("${map.bounds.northeast.lat:18.477284}")); servletContext.setAttribute("map.bounds.northeast.lng", resolver.resolvePlaceholders("${map.bounds.northeast.lng:-65.928097}")); servletContext.setAttribute("links.ios", resolver.resolvePlaceholders("${links.ios:http://itunes.apple.com/us/app/tren-urbano-app/id484781635}")); servletContext.setAttribute("links.android", resolver.resolvePlaceholders("${links.android:https://market.android.com/details?id=com.trenurbanoapp}")); servletContext.setAttribute("logo.16", resolver.resolvePlaceholders("${logo.16:/images/trenurbano_icon16.png}")); servletContext.setAttribute("logo.48", resolver.resolvePlaceholders("${logo.48:/images/trenurbano_icon48.png}")); servletContext.setAttribute("logo.57", resolver.resolvePlaceholders("${logo.57:/images/trenurbano_icon57.png}")); servletContext.setAttribute("banner.231", resolver.resolvePlaceholders("${banner.231:/images/trenurbano_icon_banner231.png}")); servletContext.setAttribute("banner.468", resolver.resolvePlaceholders("${banner.468:/images/trenurbano_icon_banner468.png}")); servletContext.setAttribute("content.aboutus", resolver.resolvePlaceholders("${content.aboutus:/html/about_us.html}")); }
/** * {@inheritDoc} * <p>Processing occurs by replacing ${...} placeholders in bean definitions by resolving each * against this configurer's set of {@link PropertySources}, which includes: * <ul> * <li>all {@linkplain org.springframework.core.env.ConfigurableEnvironment#getPropertySources * environment property sources}, if an {@code Environment} {@linkplain #setEnvironment is present} * <li>{@linkplain #mergeProperties merged local properties}, if {@linkplain #setLocation any} * {@linkplain #setLocations have} {@linkplain #setProperties been} * {@linkplain #setPropertiesArray specified} * <li>any property sources set by calling {@link #setPropertySources} * </ul> * <p>If {@link #setPropertySources} is called, <strong>environment and local properties will be * ignored</strong>. This method is designed to give the user fine-grained control over property * sources, and once set, the configurer makes no assumptions about adding additional sources. */ @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { if (this.propertySources == null) { this.propertySources = new MutablePropertySources(); if (this.environment != null) { this.propertySources.addLast( new PropertySource<Environment>(ENVIRONMENT_PROPERTIES_PROPERTY_SOURCE_NAME, this.environment) { @Override public String getProperty(String key) { return this.source.getProperty(key); } } ); } try { PropertySource<?> localPropertySource = new PropertiesPropertySource(LOCAL_PROPERTIES_PROPERTY_SOURCE_NAME, mergeProperties()); if (this.localOverride) { this.propertySources.addFirst(localPropertySource); } else { this.propertySources.addLast(localPropertySource); } } catch (IOException ex) { throw new BeanInitializationException("Could not load properties", ex); } } processProperties(beanFactory, new PropertySourcesPropertyResolver(this.propertySources)); }
private PropertyResolver getPatternsResolver(Environment environment) { if (environment == null) { return new PropertySourcesPropertyResolver(null); } return new RelaxedPropertyResolver(environment, "logging.pattern."); }
private PropertyResolver getVersionResolver(Class<?> sourceClass) { MutablePropertySources propertySources = new MutablePropertySources(); propertySources .addLast(new MapPropertySource("version", getVersionsMap(sourceClass))); return new PropertySourcesPropertyResolver(propertySources); }
private PropertyResolver getAnsiResolver() { MutablePropertySources sources = new MutablePropertySources(); sources.addFirst(new AnsiPropertySource("ansi", true)); return new PropertySourcesPropertyResolver(sources); }
private void processCompositePropertySource(CompositePropertySource source, PropertySourcesPropertyResolver resolver) { for (PropertySource<?> nested : source.getPropertySources()) { processPropertySource(nested, resolver); } }
private PropertySourcesPropertyResolver getPropertyResolverBeforeSpring4(final PropertySourcesPlaceholderConfigurer placeholderConfigurer) throws IllegalArgumentException, SecurityException, IllegalAccessException, NoSuchFieldException { Field field = placeholderConfigurer.getClass().getDeclaredField("propertySources"); field.setAccessible(true); return new PropertySourcesPropertyResolver((PropertySources) field.get(placeholderConfigurer)); }
private PropertySourcesPropertyResolver getPropertyResolverBeforeSpring4(final PropertySourcesPlaceholderConfigurer placeholderConfigurer) throws ReflectiveOperationException { return new PropertySourcesPropertyResolver((PropertySources) PropertySourcesPlaceholderConfigurer.class.getField("propertySources").get(placeholderConfigurer)); }