From 8e85f6b89c004fba3bf8f89d3231522bc47e07a7 Mon Sep 17 00:00:00 2001 From: Sergey Kuptsov Date: Tue, 2 May 2017 20:25:32 +0300 Subject: [PATCH] Add influxDB java client auto-configuration --- spring-boot-autoconfigure/pom.xml | 5 ++ .../influx/InfluxDBAutoConfiguration.java | 62 +++++++++++++++++ .../influx/InfluxDBProperties.java | 67 +++++++++++++++++++ .../autoconfigure/influx/package-info.java | 20 ++++++ .../main/resources/META-INF/spring.factories | 3 +- .../influx/InfluxDBAutoConfigurationTest.java | 66 ++++++++++++++++++ spring-boot-dependencies/pom.xml | 6 ++ 7 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDBAutoConfiguration.java create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDBProperties.java create mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/package-info.java create mode 100644 spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDBAutoConfigurationTest.java diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index 6bbd95a1ded1..3f4e941d424b 100755 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -651,6 +651,11 @@ narayana-jts-integration true + + org.influxdb + influxdb-java + true + org.springframework.boot 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 000000000000..dbdb6dee8fbb --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDBAutoConfiguration.java @@ -0,0 +1,62 @@ +/* + * 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.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * {@link EnableAutoConfiguration Auto-configuration} for InfluxDB. + * + * @author Sergey Kuptsov + */ +@Configuration +@ConditionalOnClass(InfluxDB.class) +@EnableConfigurationProperties(InfluxDBProperties.class) +public class InfluxDBAutoConfiguration { + + private final InfluxDBProperties properties; + + public InfluxDBAutoConfiguration(InfluxDBProperties properties) { + this.properties = properties; + } + + @Bean + @ConditionalOnMissingBean + public InfluxDB influxDB() { + if (Strings.isNullOrEmpty(this.properties.getUser())) { + return InfluxDBFactory.connect( + this.properties.getUrl() + ); + } + else { + return InfluxDBFactory.connect( + this.properties.getUrl(), + this.properties.getUser(), + this.properties.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 000000000000..0a313b6b7024 --- /dev/null +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/influx/InfluxDBProperties.java @@ -0,0 +1,67 @@ +/* + * 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 + */ +@ConfigurationProperties(prefix = "spring.data.influx") +public class InfluxDBProperties { + + /** + * The url to connect to. + */ + private String url; + + /** + * The username which is used to authorize against the influxDB instance. + */ + private String user; + + /** + * The password for the username which is used to authorize against the influxDB. + */ + 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 000000000000..8eb73aee3616 --- /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 8b5e9eed2023..3635f9d416bc 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -119,7 +119,8 @@ org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\ org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.WebSocketAutoConfiguration,\ org.springframework.boot.autoconfigure.websocket.WebSocketMessagingAutoConfiguration,\ -org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration +org.springframework.boot.autoconfigure.webservices.WebServicesAutoConfiguration,\ +org.springframework.boot.autoconfigure.influx.InfluxDBAutoConfiguration # Failure analyzers org.springframework.boot.diagnostics.FailureAnalyzer=\ 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 000000000000..67c07d5d6ef7 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/influx/InfluxDBAutoConfigurationTest.java @@ -0,0 +1,66 @@ +/* + * 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.assertj.core.api.Java6Assertions; +import org.influxdb.InfluxDB; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import org.springframework.boot.test.util.EnvironmentTestUtils; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +/** + * Tests for {@link InfluxDBAutoConfiguration}. + * + * @author Sergey Kuptsov + */ +public class InfluxDBAutoConfigurationTest { + + private AnnotationConfigApplicationContext context; + + @Before + public void setUp() { + this.context = new AnnotationConfigApplicationContext(); + } + + @After + public void tearDown() { + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void canEnableConfiguration() { + this.context.register(InfluxDBAutoConfiguration.class); + EnvironmentTestUtils.addEnvironment(this.context, "spring.data.influx.url=http://localhost"); + EnvironmentTestUtils.addEnvironment(this.context, "spring.data.influx.password:password"); + EnvironmentTestUtils.addEnvironment(this.context, "spring.data.influx.user:user"); + this.context.refresh(); + Java6Assertions.assertThat(this.context.getBeansOfType(InfluxDB.class)).isNotEmpty(); + } + + @Test + public void canEnableWithEmptyUserConfiguration() { + this.context.register(InfluxDBAutoConfiguration.class); + EnvironmentTestUtils.addEnvironment(this.context, "spring.data.influx.url=http://localhost"); + this.context.refresh(); + Java6Assertions.assertThat(this.context.getBeansOfType(InfluxDB.class)).isNotEmpty(); + } +} diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 589ed7e9842a..6e0ea396aee2 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -192,6 +192,7 @@ 0.32-1 1.6.3 1.4.01 + 2.5 1.10 1.5.0 @@ -2347,6 +2348,11 @@ xml-apis ${xml-apis.version} + + org.influxdb + influxdb-java + ${influxdb-java.version} +