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