001/*
002 * Copyright 2002-2016 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 *      https://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.jdbc.datasource.init;
018
019import java.sql.Connection;
020import java.sql.SQLException;
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.Collection;
024import java.util.List;
025
026/**
027 * Composite {@link DatabasePopulator} that delegates to a list of given
028 * {@code DatabasePopulator} implementations, executing all scripts.
029 *
030 * @author Dave Syer
031 * @author Juergen Hoeller
032 * @author Sam Brannen
033 * @author Kazuki Shimizu
034 * @since 3.1
035 */
036public class CompositeDatabasePopulator implements DatabasePopulator {
037
038        private final List<DatabasePopulator> populators = new ArrayList<DatabasePopulator>(4);
039
040
041        /**
042         * Create an empty {@code CompositeDatabasePopulator}.
043         * @see #setPopulators
044         * @see #addPopulators
045         */
046        public CompositeDatabasePopulator() {
047        }
048
049        /**
050         * Create a {@code CompositeDatabasePopulator} with the given populators.
051         * @param populators one or more populators to delegate to
052         * @since 4.3
053         */
054        public CompositeDatabasePopulator(Collection<DatabasePopulator> populators) {
055                this.populators.addAll(populators);
056        }
057
058        /**
059         * Create a {@code CompositeDatabasePopulator} with the given populators.
060         * @param populators one or more populators to delegate to
061         * @since 4.3
062         */
063        public CompositeDatabasePopulator(DatabasePopulator... populators) {
064                this.populators.addAll(Arrays.asList(populators));
065        }
066
067
068        /**
069         * Specify one or more populators to delegate to.
070         */
071        public void setPopulators(DatabasePopulator... populators) {
072                this.populators.clear();
073                this.populators.addAll(Arrays.asList(populators));
074        }
075
076        /**
077         * Add one or more populators to the list of delegates.
078         */
079        public void addPopulators(DatabasePopulator... populators) {
080                this.populators.addAll(Arrays.asList(populators));
081        }
082
083
084        @Override
085        public void populate(Connection connection) throws SQLException, ScriptException {
086                for (DatabasePopulator populator : this.populators) {
087                        populator.populate(connection);
088                }
089        }
090
091}