diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index 1b7893a057..48c9cde59a 100755 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -641,6 +641,11 @@ aspectjweaver true + + org.influxdb + influxdb-java + true + org.jooq jooq diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java new file mode 100644 index 0000000000..057684c849 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfiguration.java @@ -0,0 +1,63 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.influx; + +import com.google.common.base.Strings; +import org.influxdb.InfluxDB; +import org.influxdb.InfluxDBFactory; + +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for InfluxDB. + * + * @author Sergey Kuptsov + * @author Stephane Nicoll + * @since 2.0.0 + */ +@Configuration +@ConditionalOnClass(InfluxDB.class) +@EnableConfigurationProperties(InfluxDbProperties.class) +public class InfluxDbAutoConfiguration { + + private final InfluxDbProperties properties; + + public InfluxDbAutoConfiguration(InfluxDbProperties properties) { + this.properties = properties; + } + + @Bean + @ConditionalOnMissingBean + @ConditionalOnProperty("spring.influx.client.url") + public InfluxDB influxDb() { + InfluxDbProperties.Client client = this.properties.getClient(); + if (Strings.isNullOrEmpty(client.getUser())) { + return InfluxDBFactory.connect(client.getUrl()); + } + else { + return InfluxDBFactory.connect(client.getUrl(), client.getUser(), + client.getPassword()); + } + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbProperties.java new file mode 100644 index 0000000000..b3881fac87 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDbProperties.java @@ -0,0 +1,80 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.influx; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +/** + * Configuration properties for InfluxDB. + * + * @author Sergey Kuptsov + * @author Stephane Nicoll + * @since 2.0.0 + */ +@ConfigurationProperties(prefix = "spring.influx") +public class InfluxDbProperties { + + private final Client client = new Client(); + + public Client getClient() { + return this.client; + } + + public static class Client { + + /** + * Url of the InfluxDB instance to connect to. + */ + private String url; + + /** + * Login user. + */ + private String user; + + /** + * Login password. + */ + private String password; + + public String getUrl() { + return this.url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getUser() { + return this.user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getPassword() { + return this.password; + } + + public void setPassword(String password) { + this.password = password; + } + + } + +} diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/package-info.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/package-info.java new file mode 100644 index 0000000000..8eb73aee36 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright 2012-2015 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Auto-configuration for InfluxDB. + */ +package org.springframework.boot.autoconfigure.influx; diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 8d3ba7d5a8..81a43a9aa2 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -62,6 +62,7 @@ org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\ org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\ org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\ org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\ +org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\ org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\ org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\ org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\ diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTest.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTest.java new file mode 100644 index 0000000000..59207aae8c --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDbAutoConfigurationTest.java @@ -0,0 +1,73 @@ +/* + * Copyright 2012-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.autoconfigure.influx; + +import org.influxdb.InfluxDB; +import org.junit.After; +import org.junit.Test; + +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link InfluxDbAutoConfiguration}. + * + * @author Sergey Kuptsov + * @author Stephane Nicoll + */ +public class InfluxDbAutoConfigurationTest { + + private AnnotationConfigApplicationContext context; + + @After + public void tearDown() { + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void clientRequiresUrl() { + load(); + assertThat(this.context.getBeansOfType(InfluxDB.class)).isEmpty(); + } + + @Test + public void clientCanBeCustomized() { + load("spring.influx.client.url=http://localhost", + "spring.influx.client.password:password", + "spring.influx.client.user:user"); + assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); + } + + @Test + public void clientCanBeCreatedWithoutCredentials() { + load("spring.influx.client.url=http://localhost"); + assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1); + } + + private void load(String... environment) { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + TestPropertyValues.of(environment).applyTo(ctx); + ctx.register(InfluxDbAutoConfiguration.class); + ctx.refresh(); + this.context = ctx; + } + +} diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 43fadf802d..38828932d6 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -93,6 +93,7 @@ 4.5.3 4.4.6 8.2.6.Final + 2.5 2.9.0.pr3 3.0.7 3.21.0-GA @@ -1856,6 +1857,11 @@ + + org.influxdb + influxdb-java + ${influxdb-java.version} + org.javassist javassist diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 96bad8ed71..dbc41d5d17 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -714,6 +714,11 @@ content into your application; rather pick only the properties that you need. spring.h2.console.settings.trace=false # Enable trace output. spring.h2.console.settings.web-allow-others=false # Enable remote access. + # InfluxDB ({sc-spring-boot-autoconfigure}/influx/InfluxProperties.{sc-ext}[InfluxProperties]) + spring.influx.client.password= # Login password. + spring.influx.client.url= # Url of the InfluxDB instance to connect to. + spring.influx.client.user= # Login user. + # JOOQ ({sc-spring-boot-autoconfigure}/jooq/JooqAutoConfiguration.{sc-ext}[JooqAutoConfiguration]) spring.jooq.sql-dialect= # Sql dialect to use, auto-detected by default.