CombinedLocationStrategy.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.io;

  18. import java.net.URL;
  19. import java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Collections;

  22. /**
  23.  * <p>
  24.  * A specialized implementation of a {@code FileLocationStrategy} which encapsulates an arbitrary number of
  25.  * {@code FileLocationStrategy} objects.
  26.  * </p>
  27.  * <p>
  28.  * A collection with the wrapped {@code FileLocationStrategy} objects is passed at construction time. During a
  29.  * [{@code locate()} operation the wrapped strategies are called one after the other until one returns a non <strong>null</strong>
  30.  * URL. This URL is returned. If none of the wrapped strategies is able to resolve the passed in {@link FileLocator},
  31.  * result is <strong>null</strong>. This is similar to the <em>chain of responsibility</em> design pattern.
  32.  * </p>
  33.  * <p>
  34.  * This class, together with the provided concrete {@code FileLocationStrategy} implementations, offers a convenient way
  35.  * to customize the lookup for configuration files: Just add the desired concrete strategies to a
  36.  * {@code CombinedLocationStrategy} object. If necessary, custom strategies can be implemented if there are specific
  37.  * requirements. Note that the order in which strategies are added to a {@code CombinedLocationStrategy} matters: sub
  38.  * strategies are queried in the same order as they appear in the collection passed to the constructor.
  39.  * </p>
  40.  *
  41.  * @since 2.0
  42.  */
  43. public class CombinedLocationStrategy implements FileLocationStrategy {
  44.     /** A collection with all sub strategies managed by this object. */
  45.     private final Collection<FileLocationStrategy> subStrategies;

  46.     /**
  47.      * Creates a new instance of {@code CombinedLocationStrategy} and initializes it with the provided sub strategies. The
  48.      * passed in collection must not be <strong>null</strong> or contain <strong>null</strong> elements.
  49.      *
  50.      * @param subs the collection with sub strategies
  51.      * @throws IllegalArgumentException if the collection is <strong>null</strong> or has <strong>null</strong> elements
  52.      */
  53.     public CombinedLocationStrategy(final Collection<? extends FileLocationStrategy> subs) {
  54.         if (subs == null) {
  55.             throw new IllegalArgumentException("Collection with sub strategies must not be null!");
  56.         }
  57.         subStrategies = Collections.unmodifiableCollection(new ArrayList<>(subs));
  58.         if (subStrategies.contains(null)) {
  59.             throw new IllegalArgumentException("Collection with sub strategies contains null entry!");
  60.         }
  61.     }

  62.     /**
  63.      * Gets a (unmodifiable) collection with the sub strategies managed by this object.
  64.      *
  65.      * @return the sub {@code FileLocationStrategy} objects
  66.      */
  67.     public Collection<FileLocationStrategy> getSubStrategies() {
  68.         return subStrategies;
  69.     }

  70.     /**
  71.      * {@inheritDoc} This implementation tries to locate the file by delegating to the managed sub strategies.
  72.      */
  73.     @Override
  74.     public URL locate(final FileSystem fileSystem, final FileLocator locator) {
  75.         for (final FileLocationStrategy sub : getSubStrategies()) {
  76.             final URL url = sub.locate(fileSystem, locator);
  77.             if (url != null) {
  78.                 return url;
  79.             }
  80.         }

  81.         return null;
  82.     }
  83. }