001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.configuration2.io;
018
019import java.net.URL;
020import java.util.ArrayList;
021import java.util.Collection;
022import java.util.Collections;
023
024/**
025 * <p>
026 * A specialized implementation of a {@code FileLocationStrategy} which encapsulates an arbitrary number of
027 * {@code FileLocationStrategy} objects.
028 * </p>
029 * <p>
030 * A collection with the wrapped {@code FileLocationStrategy} objects is passed at construction time. During a
031 * [{@code locate()} operation the wrapped strategies are called one after the other until one returns a non <strong>null</strong>
032 * URL. This URL is returned. If none of the wrapped strategies is able to resolve the passed in {@link FileLocator},
033 * result is <strong>null</strong>. This is similar to the <em>chain of responsibility</em> design pattern.
034 * </p>
035 * <p>
036 * This class, together with the provided concrete {@code FileLocationStrategy} implementations, offers a convenient way
037 * to customize the lookup for configuration files: Just add the desired concrete strategies to a
038 * {@code CombinedLocationStrategy} object. If necessary, custom strategies can be implemented if there are specific
039 * requirements. Note that the order in which strategies are added to a {@code CombinedLocationStrategy} matters: sub
040 * strategies are queried in the same order as they appear in the collection passed to the constructor.
041 * </p>
042 *
043 * @since 2.0
044 */
045public class CombinedLocationStrategy implements FileLocationStrategy {
046
047    /** A collection with all sub strategies managed by this object. */
048    private final Collection<FileLocationStrategy> subStrategies;
049
050    /**
051     * Creates a new instance of {@code CombinedLocationStrategy} and initializes it with the provided sub strategies. The
052     * passed in collection must not be <strong>null</strong> or contain <strong>null</strong> elements.
053     *
054     * @param subs the collection with sub strategies
055     * @throws IllegalArgumentException if the collection is <strong>null</strong> or has <strong>null</strong> elements
056     */
057    public CombinedLocationStrategy(final Collection<? extends FileLocationStrategy> subs) {
058        if (subs == null) {
059            throw new IllegalArgumentException("Collection with sub strategies must not be null!");
060        }
061        subStrategies = Collections.unmodifiableCollection(new ArrayList<>(subs));
062        if (subStrategies.contains(null)) {
063            throw new IllegalArgumentException("Collection with sub strategies contains null entry!");
064        }
065    }
066
067    /**
068     * Gets a (unmodifiable) collection with the sub strategies managed by this object.
069     *
070     * @return the sub {@code FileLocationStrategy} objects
071     */
072    public Collection<FileLocationStrategy> getSubStrategies() {
073        return subStrategies;
074    }
075
076    /**
077     * {@inheritDoc} This implementation tries to locate the file by delegating to the managed sub strategies.
078     */
079    @Override
080    public URL locate(final FileSystem fileSystem, final FileLocator locator) {
081        for (final FileLocationStrategy sub : getSubStrategies()) {
082            final URL url = sub.locate(fileSystem, locator);
083            if (url != null) {
084                return url;
085            }
086        }
087
088        return null;
089    }
090}