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.jdbc.metadata;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Collections;
022import java.util.List;
023
024import javax.sql.DataSource;
025
026/**
027 * A {@link DataSourcePoolMetadataProvider} implementation that returns the first
028 * {@link DataSourcePoolMetadata} that is found by one of its delegate.
029 *
030 * @author Stephane Nicoll
031 * @since 2.0.0
032 */
033public class CompositeDataSourcePoolMetadataProvider
034                implements DataSourcePoolMetadataProvider {
035
036        private final List<DataSourcePoolMetadataProvider> providers;
037
038        /**
039         * Create a {@link CompositeDataSourcePoolMetadataProvider} instance with an initial
040         * collection of delegates to use.
041         * @param providers the data source pool metadata providers
042         */
043        public CompositeDataSourcePoolMetadataProvider(
044                        Collection<? extends DataSourcePoolMetadataProvider> providers) {
045                this.providers = (providers != null)
046                                ? Collections.unmodifiableList(new ArrayList<>(providers))
047                                : Collections.emptyList();
048        }
049
050        @Override
051        public DataSourcePoolMetadata getDataSourcePoolMetadata(DataSource dataSource) {
052                for (DataSourcePoolMetadataProvider provider : this.providers) {
053                        DataSourcePoolMetadata metadata = provider
054                                        .getDataSourcePoolMetadata(dataSource);
055                        if (metadata != null) {
056                                return metadata;
057                        }
058                }
059                return null;
060        }
061
062}