View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.util;
20  
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.List;
26  import java.util.Map;
27  
28  import org.eclipse.aether.RepositorySystemSession;
29  
30  import static java.util.stream.Collectors.toList;
31  
32  /**
33   * A utility class to read configuration properties from a repository system session.
34   *
35   * @see RepositorySystemSession#getConfigProperties()
36   */
37  public final class ConfigUtils {
38  
39      private ConfigUtils() {
40          // hide constructor
41      }
42  
43      /**
44       * Gets the specified configuration property.
45       *
46       * @param properties The configuration properties to read, must not be {@code null}.
47       * @param defaultValue The default value to return in case none of the property keys are set, may be {@code null}.
48       * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
49       *            a valid value is found.
50       * @return The property value or {@code null} if none.
51       */
52      public static Object getObject(Map<?, ?> properties, Object defaultValue, String... keys) {
53          for (String key : keys) {
54              Object value = properties.get(key);
55  
56              if (value != null) {
57                  return value;
58              }
59          }
60  
61          return defaultValue;
62      }
63  
64      /**
65       * Gets the specified configuration property.
66       *
67       * @param session The repository system session from which to read the configuration property, must not be
68       *            {@code null}.
69       * @param defaultValue The default value to return in case none of the property keys are set, may be {@code null}.
70       * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
71       *            a valid value is found.
72       * @return The property value or {@code null} if none.
73       */
74      public static Object getObject(RepositorySystemSession session, Object defaultValue, String... keys) {
75          return getObject(session.getConfigProperties(), defaultValue, keys);
76      }
77  
78      /**
79       * Gets the specified configuration property.
80       *
81       * @param properties The configuration properties to read, must not be {@code null}.
82       * @param defaultValue The default value to return in case none of the property keys is set to a string, may be
83       *            {@code null}.
84       * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
85       *            a string value is found.
86       * @return The property value or {@code null} if none.
87       */
88      public static String getString(Map<?, ?> properties, String defaultValue, String... keys) {
89          for (String key : keys) {
90              Object value = properties.get(key);
91  
92              if (value instanceof String) {
93                  return (String) value;
94              }
95          }
96  
97          return defaultValue;
98      }
99  
100     /**
101      * Gets the specified configuration property.
102      *
103      * @param session The repository system session from which to read the configuration property, must not be
104      *            {@code null}.
105      * @param defaultValue The default value to return in case none of the property keys is set to a string, may be
106      *            {@code null}.
107      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
108      *            a string value is found.
109      * @return The property value or {@code null} if none.
110      */
111     public static String getString(RepositorySystemSession session, String defaultValue, String... keys) {
112         return getString(session.getConfigProperties(), defaultValue, keys);
113     }
114 
115     /**
116      * Gets the specified configuration property.
117      *
118      * @param properties The configuration properties to read, must not be {@code null}.
119      * @param defaultValue The default value to return in case none of the property keys is set to a number.
120      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
121      *            a {@link Number} or a string representation of an {@link Integer} is found.
122      * @return The property value.
123      */
124     public static int getInteger(Map<?, ?> properties, int defaultValue, String... keys) {
125         for (String key : keys) {
126             Object value = properties.get(key);
127 
128             if (value instanceof Number) {
129                 return ((Number) value).intValue();
130             } else if (value instanceof String) {
131                 try {
132                     return Integer.parseInt((String) value);
133                 } catch (NumberFormatException e) {
134                     // try next key
135                 }
136             }
137         }
138 
139         return defaultValue;
140     }
141 
142     /**
143      * Gets the specified configuration property.
144      *
145      * @param session The repository system session from which to read the configuration property, must not be
146      *            {@code null}.
147      * @param defaultValue The default value to return in case none of the property keys is set to a number.
148      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
149      *            a {@link Number} or a string representation of an {@link Integer} is found.
150      * @return The property value.
151      */
152     public static int getInteger(RepositorySystemSession session, int defaultValue, String... keys) {
153         return getInteger(session.getConfigProperties(), defaultValue, keys);
154     }
155 
156     /**
157      * Gets the specified configuration property.
158      *
159      * @param properties The configuration properties to read, must not be {@code null}.
160      * @param defaultValue The default value to return in case none of the property keys is set to a number.
161      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
162      *            a {@link Number} or a string representation of a {@link Long} is found.
163      * @return The property value.
164      */
165     public static long getLong(Map<?, ?> properties, long defaultValue, String... keys) {
166         for (String key : keys) {
167             Object value = properties.get(key);
168 
169             if (value instanceof Number) {
170                 return ((Number) value).longValue();
171             } else if (value instanceof String) {
172                 try {
173                     return Long.parseLong((String) value);
174                 } catch (NumberFormatException e) {
175                     // try next key
176                 }
177             }
178         }
179 
180         return defaultValue;
181     }
182 
183     /**
184      * Gets the specified configuration property.
185      *
186      * @param session The repository system session from which to read the configuration property, must not be
187      *            {@code null}.
188      * @param defaultValue The default value to return in case none of the property keys is set to a number.
189      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
190      *            a {@link Number} or a string representation of a {@link Long} is found.
191      * @return The property value.
192      */
193     public static long getLong(RepositorySystemSession session, long defaultValue, String... keys) {
194         return getLong(session.getConfigProperties(), defaultValue, keys);
195     }
196 
197     /**
198      * Gets the specified configuration property.
199      *
200      * @param properties The configuration properties to read, must not be {@code null}.
201      * @param defaultValue The default value to return in case none of the property keys is set to a number.
202      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
203      *            a {@link Number} or a string representation of a {@link Float} is found.
204      * @return The property value.
205      */
206     public static float getFloat(Map<?, ?> properties, float defaultValue, String... keys) {
207         for (String key : keys) {
208             Object value = properties.get(key);
209 
210             if (value instanceof Number) {
211                 return ((Number) value).floatValue();
212             } else if (value instanceof String) {
213                 try {
214                     return Float.parseFloat((String) value);
215                 } catch (NumberFormatException e) {
216                     // try next key
217                 }
218             }
219         }
220 
221         return defaultValue;
222     }
223 
224     /**
225      * Gets the specified configuration property.
226      *
227      * @param session The repository system session from which to read the configuration property, must not be
228      *            {@code null}.
229      * @param defaultValue The default value to return in case none of the property keys is set to a number.
230      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
231      *            a {@link Number} or a string representation of a {@link Float} is found.
232      * @return The property value.
233      */
234     public static float getFloat(RepositorySystemSession session, float defaultValue, String... keys) {
235         return getFloat(session.getConfigProperties(), defaultValue, keys);
236     }
237 
238     /**
239      * Gets the specified configuration property.
240      *
241      * @param properties The configuration properties to read, must not be {@code null}.
242      * @param defaultValue The default value to return in case none of the property keys is set to a boolean.
243      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
244      *            a {@link Boolean} or a string (to be {@link Boolean#parseBoolean(String) parsed as boolean}) is found.
245      * @return The property value.
246      */
247     public static boolean getBoolean(Map<?, ?> properties, boolean defaultValue, String... keys) {
248         for (String key : keys) {
249             Object value = properties.get(key);
250 
251             if (value instanceof Boolean) {
252                 return (Boolean) value;
253             } else if (value instanceof String) {
254                 return Boolean.parseBoolean((String) value);
255             }
256         }
257 
258         return defaultValue;
259     }
260 
261     /**
262      * Gets the specified configuration property.
263      *
264      * @param session The repository system session from which to read the configuration property, must not be
265      *            {@code null}.
266      * @param defaultValue The default value to return in case none of the property keys is set to a boolean.
267      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
268      *            a {@link Boolean} or a string (to be {@link Boolean#parseBoolean(String) parsed as boolean}) is found.
269      * @return The property value.
270      */
271     public static boolean getBoolean(RepositorySystemSession session, boolean defaultValue, String... keys) {
272         return getBoolean(session.getConfigProperties(), defaultValue, keys);
273     }
274 
275     /**
276      * Gets the specified configuration property.
277      *
278      * @param properties The configuration properties to read, must not be {@code null}.
279      * @param defaultValue The default value to return in case none of the property keys is set to a collection.
280      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
281      *            a collection is found.
282      * @return The property value or {@code null} if none.
283      */
284     public static List<?> getList(Map<?, ?> properties, List<?> defaultValue, String... keys) {
285         for (String key : keys) {
286             Object value = properties.get(key);
287 
288             if (value instanceof List) {
289                 return (List<?>) value;
290             } else if (value instanceof Collection) {
291                 return Collections.unmodifiableList(new ArrayList<>((Collection<?>) value));
292             }
293         }
294 
295         return defaultValue;
296     }
297 
298     /**
299      * Gets the specified configuration property.
300      *
301      * @param session The repository system session from which to read the configuration property, must not be
302      *            {@code null}.
303      * @param defaultValue The default value to return in case none of the property keys is set to a collection.
304      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
305      *            a collection is found.
306      * @return The property value or {@code null} if none.
307      */
308     public static List<?> getList(RepositorySystemSession session, List<?> defaultValue, String... keys) {
309         return getList(session.getConfigProperties(), defaultValue, keys);
310     }
311 
312     /**
313      * Gets the specified configuration property.
314      *
315      * @param properties The configuration properties to read, must not be {@code null}.
316      * @param defaultValue The default value to return in case none of the property keys is set to a map.
317      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
318      *            a map is found.
319      * @return The property value or {@code null} if none.
320      */
321     public static Map<?, ?> getMap(Map<?, ?> properties, Map<?, ?> defaultValue, String... keys) {
322         for (String key : keys) {
323             Object value = properties.get(key);
324 
325             if (value instanceof Map) {
326                 return (Map<?, ?>) value;
327             }
328         }
329 
330         return defaultValue;
331     }
332 
333     /**
334      * Gets the specified configuration property.
335      *
336      * @param session The repository system session from which to read the configuration property, must not be
337      *            {@code null}.
338      * @param defaultValue The default value to return in case none of the property keys is set to a map.
339      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
340      *            a map is found.
341      * @return The property value or {@code null} if none.
342      */
343     public static Map<?, ?> getMap(RepositorySystemSession session, Map<?, ?> defaultValue, String... keys) {
344         return getMap(session.getConfigProperties(), defaultValue, keys);
345     }
346 
347     /**
348      * Utility method to parse configuration string that contains comma separated list of names into
349      * {@code List<String>}, never returns {@code null}.
350      *
351      * @since 1.9.0
352      */
353     public static List<String> parseCommaSeparatedNames(String commaSeparatedNames) {
354         if (commaSeparatedNames == null || commaSeparatedNames.trim().isEmpty()) {
355             return Collections.emptyList();
356         }
357         return Arrays.stream(commaSeparatedNames.split(","))
358                 .filter(s -> s != null && !s.trim().isEmpty())
359                 .collect(toList());
360     }
361 
362     /**
363      * Utility method to parse configuration string that contains comma separated list of names into
364      * {@code List<String>} with unique elements (duplicates, if any, are discarded), never returns {@code null}.
365      *
366      * @since 1.9.0
367      */
368     public static List<String> parseCommaSeparatedUniqueNames(String commaSeparatedNames) {
369         return parseCommaSeparatedNames(commaSeparatedNames).stream().distinct().collect(toList());
370     }
371 }