    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 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 <b>null</b>
    032 * URL. This URL is returned. If none of the wrapped strategies is able to resolve the passed in {@link FileLocator},
    033 * result is <b>null</b>. 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    /** A collection with all sub strategies managed by this object. */
    047    private final Collection<FileLocationStrategy> subStrategies;
    048
    049    /**
    050     * Creates a new instance of {@code CombinedLocationStrategy} and initializes it with the provided sub strategies. The
    051     * passed in collection must not be <b>null</b> or contain <b>null</b> elements.
    052     *
    053     * @param subs the collection with sub strategies
    054     * @throws IllegalArgumentException if the collection is <b>null</b> or has <b>null</b> elements
    055     */
    056    public CombinedLocationStrategy(final Collection<? extends FileLocationStrategy> subs) {
    057        if (subs == null) {
    058            throw new IllegalArgumentException("Collection with sub strategies must not be null!");
    059        }
    060        subStrategies = Collections.unmodifiableCollection(new ArrayList<>(subs));
    061        if (subStrategies.contains(null)) {
    062            throw new IllegalArgumentException("Collection with sub strategies contains null entry!");
    063        }
    064    }
    065
    066    /**
    067     * Returns a (unmodifiable) collection with the sub strategies managed by this object.
    068     *
    069     * @return the sub {@code FileLocationStrategy} objects
    070     */
    071    public Collection<FileLocationStrategy> getSubStrategies() {
    072        return subStrategies;
    073    }
    074
    075    /**
    076     * {@inheritDoc} This implementation tries to locate the file by delegating to the managed sub strategies.
    077     */
    078    @Override
    079    public URL locate(final FileSystem fileSystem, final FileLocator locator) {
    080        for (final FileLocationStrategy sub : getSubStrategies()) {
    081            final URL url = sub.locate(fileSystem, locator);
    082            if (url != null) {
    083                return url;
    084            }
    085        }
    086
    087        return null;
    088    }
    089}