View Javadoc
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    *     https://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  
19  import java.io.IOException;
20  import java.net.URL;
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.List;
25  import java.util.Set;
26  import java.util.regex.Pattern;
27  
28  /**
29   * A specialized implementation of a {@code FileLocationStrategy} which encapsulates an arbitrary number of
30   * {@code FileLocationStrategy} objects.
31   * <p>
32   * A collection with the wrapped {@code FileLocationStrategy} objects is passed at construction time. During a
33   * [{@code locate()} operation the wrapped strategies are called one after the other until one returns a non <strong>null</strong>
34   * URL. This URL is returned. If none of the wrapped strategies is able to resolve the passed in {@link FileLocator},
35   * result is <strong>null</strong>. This is similar to the <em>chain of responsibility</em> design pattern.
36   * </p>
37   * <p>
38   * This class, together with the provided concrete {@code FileLocationStrategy} implementations, offers a convenient way
39   * to customize the lookup for configuration files: Just add the desired concrete strategies to a
40   * {@code CombinedLocationStrategy} object. If necessary, custom strategies can be implemented if there are specific
41   * requirements. Note that the order in which strategies are added to a {@code CombinedLocationStrategy} matters: sub
42   * strategies are queried in the same order as they appear in the collection passed to the constructor.
43   * </p>
44   * <p>
45   * See {@link AbstractFileLocationStrategy} learn how to grant an deny URL schemes and hosts.
46   * </p>
47   *
48   * @see AbstractFileLocationStrategy
49   * @since 2.0
50   */
51  public class CombinedLocationStrategy extends AbstractFileLocationStrategy {
52  
53      /**
54       * Builds new instances of {@link CombinedLocationStrategy}.
55       *
56       * @since 2.15.0
57       */
58      public static class Builder extends AbstractBuilder<CombinedLocationStrategy, Builder> {
59  
60          /** A collection with all sub strategies managed by this object. */
61          private Collection<? extends FileLocationStrategy> subStrategies;
62  
63          /**
64           * Constructs a new instance.
65           */
66          public Builder() {
67              // empty
68          }
69  
70          @Override
71          public CombinedLocationStrategy get() throws IOException {
72              return new CombinedLocationStrategy(this);
73          }
74  
75          /**
76           * Propagates properties of the parent builder scheme and host to subStrategies.
77           *
78           * @return {@code this} instance.
79           */
80          public Builder propagate() {
81              if (subStrategies != null) {
82                  subStrategies.forEach(e -> {
83                      if (e instanceof AbstractFileLocationStrategy) {
84                          final AbstractFileLocationStrategy afls = (AbstractFileLocationStrategy) e;
85                          final Set<String> schemes = afls.getSchemes();
86                          schemes.clear();
87                          schemes.addAll(getSchemes());
88                          final Set<Pattern> hosts = afls.getHosts();
89                          hosts.clear();
90                          hosts.addAll(getHosts());
91                      }
92                  });
93              }
94              return asThis();
95          }
96  
97          /**
98           * Sets the collection with sub strategies.
99           *
100          * @param subStrategies the collection with sub strategies.
101          * @return {@code this} instance.
102          */
103         public Builder setSubStrategies(final Collection<FileLocationStrategy> subStrategies) {
104             this.subStrategies = subStrategies;
105             return asThis();
106         }
107 
108     }
109 
110     /** A collection with all sub strategies managed by this object. */
111     private final Collection<FileLocationStrategy> subStrategies;
112 
113     /**
114      * Constructs a new instance.
115      *
116      * @param builder How to build the instance.
117      */
118     private CombinedLocationStrategy(final Builder builder) {
119         super(builder);
120         if (builder.subStrategies == null) {
121             throw new IllegalArgumentException("Collection with sub strategies must not be null.");
122         }
123         List<FileLocationStrategy> subStrategiesCopy = new ArrayList<>(builder.subStrategies);
124         if (subStrategiesCopy.contains(null)) {
125             throw new IllegalArgumentException("Collection with sub strategies contains null entry.");
126         }
127         subStrategies = Collections.unmodifiableCollection(subStrategiesCopy);
128     }
129 
130     /**
131      * Creates a new instance of {@code CombinedLocationStrategy} and initializes it with the provided sub strategies. The
132      * passed in collection must not be <strong>null</strong> or contain <strong>null</strong> elements.
133      *
134      * @param subs the collection with sub strategies.
135      * @throws IllegalArgumentException if the collection is <strong>null</strong> or has <strong>null</strong> elements.
136      */
137     public CombinedLocationStrategy(final Collection<FileLocationStrategy> subs) {
138         this(new Builder().setSubStrategies(subs));
139     }
140 
141     /**
142      * Gets a (unmodifiable) collection with the sub strategies managed by this object.
143      *
144      * @return the sub {@code FileLocationStrategy} objects
145      */
146     public Collection<FileLocationStrategy> getSubStrategies() {
147         return subStrategies;
148     }
149 
150     /**
151      * {@inheritDoc} This implementation tries to locate the file by delegating to the managed sub strategies.
152      */
153     @Override
154     public URL locate(final FileSystem fileSystem, final FileLocator locator) {
155         for (final FileLocationStrategy sub : getSubStrategies()) {
156             final URL url = sub.locate(fileSystem, locator);
157             if (url != null) {
158                 return check(url);
159             }
160         }
161         return null;
162     }
163 }