Class CombinedLocationStrategy
- java.lang.Object
-
- org.apache.commons.configuration2.io.CombinedLocationStrategy
-
- All Implemented Interfaces:
FileLocationStrategy
public class CombinedLocationStrategy extends java.lang.Object implements FileLocationStrategy
A specialized implementation of a
FileLocationStrategy
which encapsulates an arbitrary number ofFileLocationStrategy
objects.A collection with the wrapped
FileLocationStrategy
objects is passed at construction time. During a [locate()
operation the wrapped strategies are called one after the other until one returns a non null URL. This URL is returned. If none of the wrapped strategies is able to resolve the passed inFileLocator
, result is null. This is similar to the chain of responsibility design pattern.This class, together with the provided concrete
FileLocationStrategy
implementations, offers a convenient way to customize the lookup for configuration files: Just add the desired concrete strategies to aCombinedLocationStrategy
object. If necessary, custom strategies can be implemented if there are specific requirements. Note that the order in which strategies are added to aCombinedLocationStrategy
matters: sub strategies are queried in the same order as they appear in the collection passed to the constructor.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Collection<FileLocationStrategy>
subStrategies
A collection with all sub strategies managed by this object.
-
Constructor Summary
Constructors Constructor Description CombinedLocationStrategy(java.util.Collection<? extends FileLocationStrategy> subs)
Creates a new instance ofCombinedLocationStrategy
and initializes it with the provided sub strategies.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.util.Collection<FileLocationStrategy>
getSubStrategies()
Gets a (unmodifiable) collection with the sub strategies managed by this object.java.net.URL
locate(FileSystem fileSystem, FileLocator locator)
Tries to locate the specified file.
-
-
-
Field Detail
-
subStrategies
private final java.util.Collection<FileLocationStrategy> subStrategies
A collection with all sub strategies managed by this object.
-
-
Constructor Detail
-
CombinedLocationStrategy
public CombinedLocationStrategy(java.util.Collection<? extends FileLocationStrategy> subs)
Creates a new instance ofCombinedLocationStrategy
and initializes it with the provided sub strategies. The passed in collection must not be null or contain null elements.- Parameters:
subs
- the collection with sub strategies- Throws:
java.lang.IllegalArgumentException
- if the collection is null or has null elements
-
-
Method Detail
-
getSubStrategies
public java.util.Collection<FileLocationStrategy> getSubStrategies()
Gets a (unmodifiable) collection with the sub strategies managed by this object.- Returns:
- the sub
FileLocationStrategy
objects
-
locate
public java.net.URL locate(FileSystem fileSystem, FileLocator locator)
Tries to locate the specified file. The method also expects theFileSystem
to be used. Note that theFileLocator
object may also contain aFileSystem
, but this is optional. The passed inFileSystem
should be used, and callers must not pass a null reference for this argument. A concrete implementation has to evaluate the properties stored in theFileLocator
object and try to match them to an existing file. If this can be done, a corresponding URL is returned. Otherwise, result is null. Implementations should not throw an exception (unless parameters are null) as there might be alternative strategies which can find the file in question. This implementation tries to locate the file by delegating to the managed sub strategies.- Specified by:
locate
in interfaceFileLocationStrategy
- Parameters:
fileSystem
- theFileSystem
to be used for this operationlocator
- the object describing the file to be located- Returns:
- a URL pointing to the referenced file if location was successful; null if the file could not be resolved
-
-