@Bean public static PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean(); yaml.setResources(new ClassPathResource("default.yml")); propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject()); return propertySourcesPlaceholderConfigurer; }
private static Properties loadProperties(Resource resource) { YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); yamlFactory.setResources(resource); yamlFactory.afterPropertiesSet(); return yamlFactory.getObject(); }
@Override public void afterPropertiesSet() { if (isSingleton()) { this.properties = createProperties(); } }
static Function<String, Properties> yamlParserGenerator( final String[] profiles) { return s -> { YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); yamlFactory.setDocumentMatchers(properties -> { String profileProperty = properties.getProperty("spring.profiles"); if (profileProperty != null && profileProperty.length() > 0) { return asList(profiles).contains(profileProperty) ? FOUND : NOT_FOUND; } else { return ABSTAIN; } }); yamlFactory.setResources(new ByteArrayResource(s.getBytes())); return yamlFactory.getObject(); }; } }
@Test public void testLoadNonExistentResource() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResolutionMethod(ResolutionMethod.OVERRIDE_AND_IGNORE); factory.setResources(new ClassPathResource("no-such-file.yml")); Properties properties = factory.getObject(); assertThat(properties.size(), equalTo(0)); }
/** * Load yaml properties map. * * @param resource the resource * @return the map */ public static Map loadYamlProperties(final Resource... resource) { val factory = new YamlPropertiesFactoryBean(); factory.setResolutionMethod(YamlProcessor.ResolutionMethod.OVERRIDE); factory.setResources(resource); factory.setSingleton(true); factory.afterPropertiesSet(); return factory.getObject(); } }
factoryBean = new YamlPropertiesFactoryBean(); factoryBean.setSingleton(true); // optional depends on your use-case factoryBean.setResources(resource); properties = factoryBean.getObject();
/** * Get a singleton instance of {@link YamlPropertiesFactoryBean}. * * @param resourceLoader The loader to use to find the yaml file. * @return an instance of the {@link YamlPropertiesFactoryBean}. */ public static YamlPropertiesFactoryBean get(ResourceLoader resourceLoader) { if (INSTANCE == null) { for (String searchLocation : SEARCH_LOCATIONS) { Resource resource = resourceLoader.getResource(searchLocation + ALIEN_CONFIGURATION_YAML); if (resource != null && resource.exists()) { log.info("Loading Alien 4 Cloud configuration from {}", resource.getDescription()); INSTANCE = new YamlPropertiesFactoryBean(); INSTANCE.setResources(new Resource[] { resource }); return INSTANCE; } } } return INSTANCE; } }
@Bean(name = {"alienconfig", "elasticsearchConfig"}) public static Properties alienConfig(BeanFactory beans, ResourceLoader resourceLoader) throws IOException { ManagementContext mgmt = null; if (beans.containsBean("brooklynManagementContext")) { mgmt = beans.getBean("brooklynManagementContext", ManagementContext.class); } return AlienBrooklynYamlPropertiesFactoryBeanFactory.get(mgmt, resourceLoader).getObject(); }
@Bean public static PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); propertySourcesPlaceholderConfigurer.setProperties(new YamlPropertiesFactoryBean().getObject()); return propertySourcesPlaceholderConfigurer; } }
@Override @Nullable public Properties getObject() { return (this.properties != null ? this.properties : createProperties()); }
@Test public void testLoadResourceWithSelectedDocuments() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource( "foo: bar\nspam: baz\n---\nfoo: bag\nspam: bad".getBytes())); factory.setDocumentMatchers(properties -> ("bag".equals(properties.getProperty("foo")) ? MatchStatus.FOUND : MatchStatus.NOT_FOUND)); Properties properties = factory.getObject(); assertThat(properties.getProperty("foo"), equalTo("bag")); assertThat(properties.getProperty("spam"), equalTo("bad")); }
/** * Get a singleton instance of {@link YamlPropertiesFactoryBean}. * @param mgmt * * @param resourceLoader The loader to use to find the yaml file. * @return an instance of the {@link YamlPropertiesFactoryBean} or null if one could not be loaded. */ public static YamlPropertiesFactoryBean get(ManagementContext mgmt, ResourceLoader resourceLoader) { if (INSTANCE == null) { String configFile = System.getProperty(ALIEN_CONFIG_FILE.getName()); if (configFile==null && mgmt!=null) { configFile = mgmt.getConfig().getConfig(ALIEN_CONFIG_FILE); } if (Strings.isNonBlank(configFile)) { log.info("Loading A4C config from "+configFile); final byte[] resourceArr = new ResourceUtils(AlienBrooklynYamlPropertiesFactoryBeanFactory.class) .getResourceAsString(configFile) .getBytes(Charsets.UTF_8); Resource resource = new ByteArrayResource(resourceArr, configFile); INSTANCE = new YamlPropertiesFactoryBean(); INSTANCE.setResources(resource); } else { INSTANCE = AlienYamlPropertiesFactoryBeanFactory.get(resourceLoader); } } return INSTANCE; } }
@Override public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { YamlPropertiesFactoryBean propertiesFactoryBean = AlienYamlPropertiesFactoryBeanFactory.get(context.getResourceLoader()); Object ldapEnabled = propertiesFactoryBean.getObject().get("ldap.enabled"); if (ldapEnabled != null && ldapEnabled instanceof Boolean) { return ((Boolean) ldapEnabled).booleanValue(); } return false; } }
@Override @Nullable public Properties getObject() { return (this.properties != null ? this.properties : createProperties()); }
@Test(expected = DuplicateKeyException.class) public void testLoadResourcesWithInternalOverride() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(new ByteArrayResource( "foo: bar\nspam:\n foo: baz\nfoo: bucket".getBytes())); factory.getObject(); }
private static void contributeDefaults(Map<String, Object> defaults, Resource resource) { if (resource.exists()) { YamlPropertiesFactoryBean yamlPropertiesFactoryBean = new YamlPropertiesFactoryBean(); yamlPropertiesFactoryBean.setResources(resource); yamlPropertiesFactoryBean.afterPropertiesSet(); Properties p = yamlPropertiesFactoryBean.getObject(); for (Object k : p.keySet()) { String key = k.toString(); defaults.put(key, p.get(key)); } } }
@Test public void testLoadResourceWithDefaultMatchSkippingMissedMatch() { YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setMatchDefault(true); factory.setResources(new ByteArrayResource( "one: two\n---\nfoo: bag\nspam: bad\n---\nfoo: bar\nspam: baz".getBytes())); factory.setDocumentMatchers(properties -> { if (!properties.containsKey("foo")) { return MatchStatus.ABSTAIN; } return ("bag".equals(properties.getProperty("foo")) ? MatchStatus.FOUND : MatchStatus.NOT_FOUND); }); Properties properties = factory.getObject(); assertThat(properties.getProperty("foo"), equalTo("bag")); assertThat(properties.getProperty("spam"), equalTo("bad")); assertThat(properties.getProperty("one"), equalTo("two")); }
@Bean public static PropertySourcesPlaceholderConfigurer properties(ResourceLoader resourceLoader) { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yaml = AlienYamlPropertiesFactoryBeanFactory.get(resourceLoader); propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject()); return propertySourcesPlaceholderConfigurer; }
@Override public void afterPropertiesSet() { if (isSingleton()) { this.properties = createProperties(); } }