001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.influx;
018
019import okhttp3.OkHttpClient;
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.influxdb.InfluxDB;
023import org.influxdb.impl.InfluxDBImpl;
024
025import org.springframework.beans.factory.ObjectProvider;
026import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
027import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
028import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
029import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
030import org.springframework.boot.context.properties.EnableConfigurationProperties;
031import org.springframework.context.annotation.Bean;
032import org.springframework.context.annotation.Configuration;
033
034/**
035 * {@link EnableAutoConfiguration Auto-configuration} for InfluxDB.
036 *
037 * @author Sergey Kuptsov
038 * @author Stephane Nicoll
039 * @author EddĂș MelĂ©ndez
040 * @since 2.0.0
041 */
042@Configuration
043@ConditionalOnClass(InfluxDB.class)
044@EnableConfigurationProperties(InfluxDbProperties.class)
045public class InfluxDbAutoConfiguration {
046
047        private static final Log logger = LogFactory.getLog(InfluxDbAutoConfiguration.class);
048
049        private final InfluxDbProperties properties;
050
051        private final OkHttpClient.Builder builder;
052
053        public InfluxDbAutoConfiguration(InfluxDbProperties properties,
054                        ObjectProvider<InfluxDbOkHttpClientBuilderProvider> builder,
055                        ObjectProvider<OkHttpClient.Builder> deprecatedBuilder) {
056                this.properties = properties;
057                this.builder = determineBuilder(builder.getIfAvailable(),
058                                deprecatedBuilder.getIfAvailable());
059        }
060
061        @Deprecated
062        private static OkHttpClient.Builder determineBuilder(
063                        InfluxDbOkHttpClientBuilderProvider builder,
064                        OkHttpClient.Builder deprecatedBuilder) {
065                if (builder != null) {
066                        return builder.get();
067                }
068                else if (deprecatedBuilder != null) {
069                        logger.warn(
070                                        "InfluxDB client customizations using a OkHttpClient.Builder is deprecated, register a "
071                                                        + InfluxDbOkHttpClientBuilderProvider.class.getSimpleName()
072                                                        + " bean instead");
073                        return deprecatedBuilder;
074                }
075                return new OkHttpClient.Builder();
076        }
077
078        @Bean
079        @ConditionalOnMissingBean
080        @ConditionalOnProperty("spring.influx.url")
081        public InfluxDB influxDb() {
082                return new InfluxDBImpl(this.properties.getUrl(), this.properties.getUser(),
083                                this.properties.getPassword(), this.builder);
084        }
085
086}