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 * @since 2.0 049 */ 050public class CombinedLocationStrategy implements FileLocationStrategy 051{ 052 /** A collection with all sub strategies managed by this object. */ 053 private final Collection<FileLocationStrategy> subStrategies; 054 055 /** 056 * Creates a new instance of {@code CombinedLocationStrategy} and 057 * initializes it with the provided sub strategies. The passed in collection 058 * must not be <b>null</b> or contain <b>null</b> elements. 059 * 060 * @param subs the collection with sub strategies 061 * @throws IllegalArgumentException if the collection is <b>null</b> or has 062 * <b>null</b> elements 063 */ 064 public CombinedLocationStrategy( 065 final Collection<? extends FileLocationStrategy> subs) 066 { 067 if (subs == null) 068 { 069 throw new IllegalArgumentException( 070 "Collection with sub strategies must not be null!"); 071 } 072 subStrategies = 073 Collections 074 .unmodifiableCollection(new ArrayList<>( 075 subs)); 076 if (subStrategies.contains(null)) 077 { 078 throw new IllegalArgumentException( 079 "Collection with sub strategies contains null entry!"); 080 } 081 } 082 083 /** 084 * Returns a (unmodifiable) collection with the sub strategies managed by 085 * this object. 086 * 087 * @return the sub {@code FileLocationStrategy} objects 088 */ 089 public Collection<FileLocationStrategy> getSubStrategies() 090 { 091 return subStrategies; 092 } 093 094 /** 095 * {@inheritDoc} This implementation tries to locate the file by delegating 096 * to the managed sub strategies. 097 */ 098 @Override 099 public URL locate(final FileSystem fileSystem, final FileLocator locator) 100 { 101 for (final FileLocationStrategy sub : getSubStrategies()) 102 { 103 final URL url = sub.locate(fileSystem, locator); 104 if (url != null) 105 { 106 return url; 107 } 108 } 109 110 return null; 111 } 112}