Skip to content

Commit 4651633

Browse files
committed
Merge pull request #9066 from kuptservol:influxdb-autoconfiguration
* pr/9066: Polish "Add influxDB java client auto-configuration" Add influxDB java client auto-configuration
2 parents 9053bad + 297127e commit 4651633

File tree

8 files changed

+253
-0
lines changed

8 files changed

+253
-0
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -641,6 +641,11 @@
641641
<artifactId>aspectjweaver</artifactId>
642642
<optional>true</optional>
643643
</dependency>
644+
<dependency>
645+
<groupId>org.influxdb</groupId>
646+
<artifactId>influxdb-java</artifactId>
647+
<optional>true</optional>
648+
</dependency>
644649
<dependency>
645650
<groupId>org.jooq</groupId>
646651
<artifactId>jooq</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.influx;
18+
19+
import com.google.common.base.Strings;
20+
import org.influxdb.InfluxDB;
21+
import org.influxdb.InfluxDBFactory;
22+
23+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
27+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
31+
/**
32+
* {@link EnableAutoConfiguration Auto-configuration} for InfluxDB.
33+
*
34+
* @author Sergey Kuptsov
35+
* @author Stephane Nicoll
36+
* @since 2.0.0
37+
*/
38+
@Configuration
39+
@ConditionalOnClass(InfluxDB.class)
40+
@EnableConfigurationProperties(InfluxDbProperties.class)
41+
public class InfluxDbAutoConfiguration {
42+
43+
private final InfluxDbProperties properties;
44+
45+
public InfluxDbAutoConfiguration(InfluxDbProperties properties) {
46+
this.properties = properties;
47+
}
48+
49+
@Bean
50+
@ConditionalOnMissingBean
51+
@ConditionalOnProperty("spring.influx.client.url")
52+
public InfluxDB influxDb() {
53+
InfluxDbProperties.Client client = this.properties.getClient();
54+
if (Strings.isNullOrEmpty(client.getUser())) {
55+
return InfluxDBFactory.connect(client.getUrl());
56+
}
57+
else {
58+
return InfluxDBFactory.connect(client.getUrl(), client.getUser(),
59+
client.getPassword());
60+
}
61+
}
62+
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.influx;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties for InfluxDB.
23+
*
24+
* @author Sergey Kuptsov
25+
* @author Stephane Nicoll
26+
* @since 2.0.0
27+
*/
28+
@ConfigurationProperties(prefix = "spring.influx")
29+
public class InfluxDbProperties {
30+
31+
private final Client client = new Client();
32+
33+
public Client getClient() {
34+
return this.client;
35+
}
36+
37+
public static class Client {
38+
39+
/**
40+
* Url of the InfluxDB instance to connect to.
41+
*/
42+
private String url;
43+
44+
/**
45+
* Login user.
46+
*/
47+
private String user;
48+
49+
/**
50+
* Login password.
51+
*/
52+
private String password;
53+
54+
public String getUrl() {
55+
return this.url;
56+
}
57+
58+
public void setUrl(String url) {
59+
this.url = url;
60+
}
61+
62+
public String getUser() {
63+
return this.user;
64+
}
65+
66+
public void setUser(String user) {
67+
this.user = user;
68+
}
69+
70+
public String getPassword() {
71+
return this.password;
72+
}
73+
74+
public void setPassword(String password) {
75+
this.password = password;
76+
}
77+
78+
}
79+
80+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Auto-configuration for InfluxDB.
19+
*/
20+
package org.springframework.boot.autoconfigure.influx;

spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ org.springframework.boot.autoconfigure.hateoas.HypermediaAutoConfiguration,\
6262
org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration,\
6363
org.springframework.boot.autoconfigure.hazelcast.HazelcastJpaDependencyAutoConfiguration,\
6464
org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration,\
65+
org.springframework.boot.autoconfigure.influx.InfluxDbAutoConfiguration,\
6566
org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration,\
6667
org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration,\
6768
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration,\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.influx;
18+
19+
import org.influxdb.InfluxDB;
20+
import org.junit.After;
21+
import org.junit.Test;
22+
23+
import org.springframework.boot.test.util.TestPropertyValues;
24+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link InfluxDbAutoConfiguration}.
30+
*
31+
* @author Sergey Kuptsov
32+
* @author Stephane Nicoll
33+
*/
34+
public class InfluxDbAutoConfigurationTest {
35+
36+
private AnnotationConfigApplicationContext context;
37+
38+
@After
39+
public void tearDown() {
40+
if (this.context != null) {
41+
this.context.close();
42+
}
43+
}
44+
45+
@Test
46+
public void clientRequiresUrl() {
47+
load();
48+
assertThat(this.context.getBeansOfType(InfluxDB.class)).isEmpty();
49+
}
50+
51+
@Test
52+
public void clientCanBeCustomized() {
53+
load("spring.influx.client.url=http://localhost",
54+
"spring.influx.client.password:password",
55+
"spring.influx.client.user:user");
56+
assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1);
57+
}
58+
59+
@Test
60+
public void clientCanBeCreatedWithoutCredentials() {
61+
load("spring.influx.client.url=http://localhost");
62+
assertThat(this.context.getBeansOfType(InfluxDB.class)).hasSize(1);
63+
}
64+
65+
private void load(String... environment) {
66+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
67+
TestPropertyValues.of(environment).applyTo(ctx);
68+
ctx.register(InfluxDbAutoConfiguration.class);
69+
ctx.refresh();
70+
this.context = ctx;
71+
}
72+
73+
}

spring-boot-dependencies/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@
9393
<httpclient.version>4.5.3</httpclient.version>
9494
<httpcore.version>4.4.6</httpcore.version>
9595
<infinispan.version>8.2.6.Final</infinispan.version>
96+
<influxdb-java.version>2.5</influxdb-java.version>
9697
<jackson.version>2.9.0.pr3</jackson.version>
9798
<janino.version>3.0.7</janino.version>
9899
<javassist.version>3.21.0-GA</javassist.version> <!-- Same as Hibernate -->
@@ -1856,6 +1857,11 @@
18561857
</exclusion>
18571858
</exclusions>
18581859
</dependency>
1860+
<dependency>
1861+
<groupId>org.influxdb</groupId>
1862+
<artifactId>influxdb-java</artifactId>
1863+
<version>${influxdb-java.version}</version>
1864+
</dependency>
18591865
<dependency>
18601866
<groupId>org.javassist</groupId>
18611867
<artifactId>javassist</artifactId>

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,11 @@ content into your application; rather pick only the properties that you need.
714714
spring.h2.console.settings.trace=false # Enable trace output.
715715
spring.h2.console.settings.web-allow-others=false # Enable remote access.
716716
717+
# InfluxDB ({sc-spring-boot-autoconfigure}/influx/InfluxProperties.{sc-ext}[InfluxProperties])
718+
spring.influx.client.password= # Login password.
719+
spring.influx.client.url= # Url of the InfluxDB instance to connect to.
720+
spring.influx.client.user= # Login user.
721+
717722
# JOOQ ({sc-spring-boot-autoconfigure}/jooq/JooqAutoConfiguration.{sc-ext}[JooqAutoConfiguration])
718723
spring.jooq.sql-dialect= # Sql dialect to use, auto-detected by default.
719724

0 commit comments

Comments
 (0)