    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 *     http://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
    027 * encapsulates an arbitrary number of {@code FileLocationStrategy} objects.
    028 * </p>
    029 * <p>
    030 * A collection with the wrapped {@code FileLocationStrategy} objects is passed
    031 * at construction time. During a [{@code locate()} operation the wrapped
    032 * strategies are called one after the other until one returns a non <b>null</b>
    033 * URL. This URL is returned. If none of the wrapped strategies is able to
    034 * resolve the passed in {@link FileLocator}, result is <b>null</b>. This is
    035 * similar to the <em>chain of responsibility</em> design pattern.
    036 * </p>
    037 * <p>
    038 * This class, together with the provided concrete {@code FileLocationStrategy}
    039 * implementations, offers a convenient way to customize the lookup for
    040 * configuration files: Just add the desired concrete strategies to a
    041 * {@code CombinedLocationStrategy} object. If necessary, custom strategies can
    042 * be implemented if there are specific requirements. Note that the order in
    043 * which strategies are added to a {@code CombinedLocationStrategy} matters: sub
    044 * strategies are queried in the same order as they appear in the collection
    045 * passed to the constructor.
    046 * </p>
    047 *
    048 * @version $Id: CombinedLocationStrategy.java 1842194 2018-09-27 22:24:23Z ggregory $
    049 * @since 2.0
    050 */
    051public class CombinedLocationStrategy implements FileLocationStrategy
    052{
    053    /** A collection with all sub strategies managed by this object. */
    054    private final Collection<FileLocationStrategy> subStrategies;
    055
    056    /**
    057     * Creates a new instance of {@code CombinedLocationStrategy} and
    058     * initializes it with the provided sub strategies. The passed in collection
    059     * must not be <b>null</b> or contain <b>null</b> elements.
    060     *
    061     * @param subs the collection with sub strategies
    062     * @throws IllegalArgumentException if the collection is <b>null</b> or has
    063     *         <b>null</b> elements
    064     */
    065    public CombinedLocationStrategy(
    066            final Collection<? extends FileLocationStrategy> subs)
    067    {
    068        if (subs == null)
    069        {
    070            throw new IllegalArgumentException(
    071                    "Collection with sub strategies must not be null!");
    072        }
    073        subStrategies =
    074                Collections
    075                        .unmodifiableCollection(new ArrayList<>(
    076                                subs));
    077        if (subStrategies.contains(null))
    078        {
    079            throw new IllegalArgumentException(
    080                    "Collection with sub strategies contains null entry!");
    081        }
    082    }
    083
    084    /**
    085     * Returns a (unmodifiable) collection with the sub strategies managed by
    086     * this object.
    087     *
    088     * @return the sub {@code FileLocationStrategy} objects
    089     */
    090    public Collection<FileLocationStrategy> getSubStrategies()
    091    {
    092        return subStrategies;
    093    }
    094
    095    /**
    096     * {@inheritDoc} This implementation tries to locate the file by delegating
    097     * to the managed sub strategies.
    098     */
    099    @Override
    100    public URL locate(final FileSystem fileSystem, final FileLocator locator)
    101    {
    102        for (final FileLocationStrategy sub : getSubStrategies())
    103        {
    104            final URL url = sub.locate(fileSystem, locator);
    105            if (url != null)
    106            {
    107                return url;
    108            }
    109        }
    110
    111        return null;
    112    }
    113}