@Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { ConfigurableEnvironment environment = (ConfigurableEnvironment) context .getEnvironment(); ResourceProperties properties = new ResourceProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(properties, "spring.resources"); binder.bind(new PropertySourcesPropertyValues(environment.getPropertySources())); Boolean match = properties.getChain().getEnabled(); if (match == null) { boolean webJarsLocatorPresent = ClassUtils.isPresent(WEBJAR_ASSERT_LOCATOR, getClass().getClassLoader()); return new ConditionOutcome(webJarsLocatorPresent, "Webjars locator (" + WEBJAR_ASSERT_LOCATOR + ") is " + (webJarsLocatorPresent ? "present" : "absent")); } return new ConditionOutcome(match, "Resource chain is " + (match ? "enabled" : "disabled")); }
@Test public void testBindingSsh() { ShellProperties props = new ShellProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); binder.setConversionService(new DefaultConversionService()); Map<String, String> map = new HashMap<String, String>(); map.put("shell.ssh.enabled", "true"); map.put("shell.ssh.port", "2222"); map.put("shell.ssh.key_path", "~/.ssh/test.pem"); binder.bind(new MutablePropertyValues(map)); assertFalse(binder.getBindingResult().hasErrors()); Properties p = props.asCrshShellConfig(); assertEquals("2222", p.get("crash.ssh.port")); assertEquals("~/.ssh/test.pem", p.get("crash.ssh.keypath")); }
@Test public void testBindingSshIgnored() { ShellProperties props = new ShellProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); binder.setConversionService(new DefaultConversionService()); Map<String, String> map = new HashMap<String, String>(); map.put("shell.ssh.enabled", "false"); map.put("shell.ssh.port", "2222"); map.put("shell.ssh.key_path", "~/.ssh/test.pem"); binder.bind(new MutablePropertyValues(map)); assertFalse(binder.getBindingResult().hasErrors()); Properties p = props.asCrshShellConfig(); assertNull(p.get("crash.ssh.port")); assertNull(p.get("crash.ssh.keypath")); }
@Test public void testBindingSimple() { SimpleAuthenticationProperties props = new SimpleAuthenticationProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell.auth.simple"); binder.setConversionService(new DefaultConversionService()); Map<String, String> map = new HashMap<String, String>(); map.put("shell.auth.simple.user.name", "username123"); map.put("shell.auth.simple.user.password", "password123"); binder.bind(new MutablePropertyValues(map)); assertFalse(binder.getBindingResult().hasErrors()); Properties p = new Properties(); props.applyToCrshShellConfig(p); assertEquals("username123", p.get("crash.auth.simple.username")); assertEquals("password123", p.get("crash.auth.simple.password")); }
@Bean(name = "commonDataSource") @Primary public DataSource druidDataSource() { DruidDataSource dataSource = new DruidDataSource(); MutablePropertyValues properties = new MutablePropertyValues(beautyDruidDataProperties()); new RelaxedDataBinder(dataSource).bind(properties); return dataSource; }
private void bindXaProperties(XADataSource target, DataSourceProperties properties) { MutablePropertyValues values = new MutablePropertyValues(); values.add("user", this.properties.determineUsername()); values.add("password", this.properties.determinePassword()); values.add("url", this.properties.determineUrl()); values.addPropertyValues(properties.getXa().getProperties()); new RelaxedDataBinder(target).withAlias("user", "username").bind(values); }
private List<String> getExcludeAutoConfigurationsProperty() { if (getEnvironment() instanceof ConfigurableEnvironment) { Excludes excludes = new Excludes(); RelaxedDataBinder binder = new RelaxedDataBinder(excludes, "spring.autoconfigure."); binder.bind(new PropertySourcesPropertyValues( ((ConfigurableEnvironment) getEnvironment()).getPropertySources())); return excludes.getExclude(); } RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(getEnvironment(), "spring.autoconfigure."); String[] exclude = resolver.getProperty("exclude", String[].class); return (Arrays.asList(exclude == null ? new String[0] : exclude)); }
@Override public void setEnvironment(Environment environment) { this.environment = (ConfigurableEnvironment) environment; this.target = new HashMap<String, Object>(); new RelaxedDataBinder(this.target).bind( new PropertySourcesPropertyValues(this.environment.getPropertySources())); this.propertyResolver = new RelaxedPropertyResolver(environment); }
@Test public void testAddressBinding() throws Exception { RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server"); binder.bind(new MutablePropertyValues( Collections.singletonMap("server.address", "127.0.0.1"))); assertThat(binder.getBindingResult().hasErrors()).isFalse(); assertThat(this.properties.getAddress()) .isEqualTo(InetAddress.getByName("127.0.0.1")); }
@Test public void testServerHeader() throws Exception { RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server"); binder.bind(new MutablePropertyValues( Collections.singletonMap("server.server-header", "Custom Server"))); assertThat(this.properties.getServerHeader()).isEqualTo("Custom Server"); }
@Test public void testServletPathAsMapping() throws Exception { RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server"); binder.bind(new MutablePropertyValues( Collections.singletonMap("server.servletPath", "/foo/*"))); assertThat(binder.getBindingResult().hasErrors()).isFalse(); assertThat(this.properties.getServletMapping()).isEqualTo("/foo/*"); assertThat(this.properties.getServletPrefix()).isEqualTo("/foo"); }
@Test public void testServletPathAsPrefix() throws Exception { RelaxedDataBinder binder = new RelaxedDataBinder(this.properties, "server"); binder.bind(new MutablePropertyValues( Collections.singletonMap("server.servletPath", "/foo"))); assertThat(binder.getBindingResult().hasErrors()).isFalse(); assertThat(this.properties.getServletMapping()).isEqualTo("/foo/*"); assertThat(this.properties.getServletPrefix()).isEqualTo("/foo"); }
private void bind(DataSource result) { MutablePropertyValues properties = new MutablePropertyValues(this.properties); new RelaxedDataBinder(result) .withAlias("url", "jdbcUrl") .withAlias("username", "user") .bind(properties); }
@Override public void preInit(SpringProcessEngineConfiguration springProcessEngineConfiguration) { GenericProperties genericProperties = camundaBpmProperties.getGenericProperties(); final Map<String, Object> properties = genericProperties.getProperties(); if (!CollectionUtils.isEmpty(properties)) { RelaxedDataBinder relaxedDataBinder = new RelaxedDataBinder(springProcessEngineConfiguration); relaxedDataBinder.setIgnoreInvalidFields(genericProperties.isIgnoreInvalidFields()); relaxedDataBinder.setIgnoreUnknownFields(genericProperties.isIgnoreUnknownFields()); relaxedDataBinder.bind(getPropertyValues(properties)); logger.debug("properties bound to configuration: {}", genericProperties); } }
@Test public void testBindingAuth() { ShellProperties props = new ShellProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); binder.bind(new MutablePropertyValues( Collections.singletonMap("shell.auth", "spring"))); assertFalse(binder.getBindingResult().hasErrors()); assertEquals("spring", props.getAuth()); }
@Test public void testBindingAuthIfEmpty() { ShellProperties props = new ShellProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); binder.bind( new MutablePropertyValues(Collections.singletonMap("shell.auth", ""))); assertTrue(binder.getBindingResult().hasErrors()); assertEquals("simple", props.getAuth()); }
@Test public void testBindingCommandRefreshInterval() { ShellProperties props = new ShellProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); binder.setConversionService(new DefaultConversionService()); binder.bind(new MutablePropertyValues( Collections.singletonMap("shell.command_refresh_interval", "1"))); assertFalse(binder.getBindingResult().hasErrors()); assertEquals(1, props.getCommandRefreshInterval()); }
@Test public void testBindingCommandPathPatterns() { ShellProperties props = new ShellProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); binder.setConversionService(new DefaultConversionService()); binder.bind(new MutablePropertyValues(Collections .singletonMap("shell.command_path_patterns", "pattern1, pattern2"))); assertFalse(binder.getBindingResult().hasErrors()); assertEquals(2, props.getCommandPathPatterns().length); Assert.assertArrayEquals(new String[] { "pattern1", "pattern2" }, props.getCommandPathPatterns()); }
@Test public void testBindingConfigPathPatterns() { ShellProperties props = new ShellProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); binder.setConversionService(new DefaultConversionService()); binder.bind(new MutablePropertyValues(Collections .singletonMap("shell.config_path_patterns", "pattern1, pattern2"))); assertFalse(binder.getBindingResult().hasErrors()); assertEquals(2, props.getConfigPathPatterns().length); Assert.assertArrayEquals(new String[] { "pattern1", "pattern2" }, props.getConfigPathPatterns()); }
@Test public void testBindingDisabledPlugins() { ShellProperties props = new ShellProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); binder.setConversionService(new DefaultConversionService()); binder.bind(new MutablePropertyValues(Collections .singletonMap("shell.disabled_plugins", "pattern1, pattern2"))); assertFalse(binder.getBindingResult().hasErrors()); assertEquals(2, props.getDisabledPlugins().length); assertArrayEquals(new String[] { "pattern1", "pattern2" }, props.getDisabledPlugins()); }
@Test public void testBindingDisabledCommands() { ShellProperties props = new ShellProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); binder.setConversionService(new DefaultConversionService()); binder.bind(new MutablePropertyValues(Collections .singletonMap("shell.disabled_commands", "pattern1, pattern2"))); assertFalse(binder.getBindingResult().hasErrors()); assertEquals(2, props.getDisabledCommands().length); assertArrayEquals(new String[] { "pattern1", "pattern2" }, props.getDisabledCommands()); }
@Test public void testBindingTelnet() { ShellProperties props = new ShellProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); binder.setConversionService(new DefaultConversionService()); Map<String, String> map = new HashMap<String, String>(); map.put("shell.telnet.enabled", "true"); map.put("shell.telnet.port", "2222"); binder.bind(new MutablePropertyValues(map)); assertFalse(binder.getBindingResult().hasErrors()); Properties p = props.asCrshShellConfig(); assertEquals("2222", p.get("crash.telnet.port")); }
@Test public void testBindingTelnetIgnored() { ShellProperties props = new ShellProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell"); binder.setConversionService(new DefaultConversionService()); Map<String, String> map = new HashMap<String, String>(); map.put("shell.telnet.enabled", "false"); map.put("shell.telnet.port", "2222"); binder.bind(new MutablePropertyValues(map)); assertFalse(binder.getBindingResult().hasErrors()); Properties p = props.asCrshShellConfig(); assertNull(p.get("crash.telnet.port")); }
@Test public void testBindingJaas() { JaasAuthenticationProperties props = new JaasAuthenticationProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell.auth.jaas"); binder.setConversionService(new DefaultConversionService()); Map<String, String> map = new HashMap<String, String>(); map.put("shell.auth.jaas.domain", "my-test-domain"); binder.bind(new MutablePropertyValues(map)); assertFalse(binder.getBindingResult().hasErrors()); Properties p = new Properties(); props.applyToCrshShellConfig(p); assertEquals("my-test-domain", p.get("crash.auth.jaas.domain")); }
@Test public void testBindingKey() { KeyAuthenticationProperties props = new KeyAuthenticationProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell.auth.key"); binder.setConversionService(new DefaultConversionService()); Map<String, String> map = new HashMap<String, String>(); map.put("shell.auth.key.path", "~/.ssh/test.pem"); binder.bind(new MutablePropertyValues(map)); assertFalse(binder.getBindingResult().hasErrors()); Properties p = new Properties(); props.applyToCrshShellConfig(p); assertEquals("~/.ssh/test.pem", p.get("crash.auth.key.path")); }
@Test public void testBindingKeyIgnored() { KeyAuthenticationProperties props = new KeyAuthenticationProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell.auth.key"); binder.setConversionService(new DefaultConversionService()); Map<String, String> map = new HashMap<String, String>(); binder.bind(new MutablePropertyValues(map)); assertFalse(binder.getBindingResult().hasErrors()); Properties p = new Properties(); props.applyToCrshShellConfig(p); assertNull(p.get("crash.auth.key.path")); }
@Test public void testDefaultPasswordAutogeneratedIfUnresolovedPlaceholder() { SimpleAuthenticationProperties security = new SimpleAuthenticationProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); binder.bind(new MutablePropertyValues(Collections .singletonMap("shell.auth.simple.user.password", "${ADMIN_PASSWORD}"))); assertFalse(binder.getBindingResult().hasErrors()); assertTrue(security.getUser().isDefaultPassword()); }
@Test public void testDefaultPasswordAutogeneratedIfEmpty() { SimpleAuthenticationProperties security = new SimpleAuthenticationProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(security, "security"); binder.bind(new MutablePropertyValues( Collections.singletonMap("shell.auth.simple.user.password", ""))); assertFalse(binder.getBindingResult().hasErrors()); assertTrue(security.getUser().isDefaultPassword()); }
@Test public void testBindingSpring() { SpringAuthenticationProperties props = new SpringAuthenticationProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(props, "shell.auth.spring"); binder.bind(new MutablePropertyValues( Collections.singletonMap("shell.auth.spring.roles", "role1, role2"))); assertFalse(binder.getBindingResult().hasErrors()); Properties p = new Properties(); props.applyToCrshShellConfig(p); assertEquals("role1, role2", p.get("crash.auth.spring.roles")); }
@Override public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { ConfigurableEnvironment environment = (ConfigurableEnvironment) context .getEnvironment(); ResourceProperties properties = new ResourceProperties(); RelaxedDataBinder binder = new RelaxedDataBinder(properties, "spring.resources"); binder.bind(new PropertySourcesPropertyValues(environment.getPropertySources())); Boolean match = properties.getChain().getEnabled(); return new ConditionOutcome(match, "Resource chain is " + (match ? "enabled" : "disabled")); }