View Javadoc
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.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  /**
31   * A utility class to read configuration properties from a repository system session.
32   * 
33   * @see RepositorySystemSession#getConfigProperties()
34   */
35  public final class ConfigUtils
36  {
37  
38      private ConfigUtils()
39      {
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      {
54          for ( String key : keys )
55          {
56              Object value = properties.get( key );
57  
58              if ( value != null )
59              {
60                  return value;
61              }
62          }
63  
64          return defaultValue;
65      }
66  
67      /**
68       * Gets the specified configuration property.
69       * 
70       * @param session The repository system session from which to read the configuration property, must not be
71       *            {@code null}.
72       * @param defaultValue The default value to return in case none of the property keys are set, may be {@code null}.
73       * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
74       *            a valid value is found.
75       * @return The property value or {@code null} if none.
76       */
77      public static Object getObject( RepositorySystemSession session, Object defaultValue, String... keys )
78      {
79          return getObject( session.getConfigProperties(), defaultValue, keys );
80      }
81  
82      /**
83       * Gets the specified configuration property.
84       * 
85       * @param properties The configuration properties to read, must not be {@code null}.
86       * @param defaultValue The default value to return in case none of the property keys is set to a string, may be
87       *            {@code null}.
88       * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
89       *            a string value is found.
90       * @return The property value or {@code null} if none.
91       */
92      public static String getString( Map<?, ?> properties, String defaultValue, String... keys )
93      {
94          for ( String key : keys )
95          {
96              Object value = properties.get( key );
97  
98              if ( value instanceof String )
99              {
100                 return (String) value;
101             }
102         }
103 
104         return defaultValue;
105     }
106 
107     /**
108      * Gets the specified configuration property.
109      * 
110      * @param session The repository system session from which to read the configuration property, must not be
111      *            {@code null}.
112      * @param defaultValue The default value to return in case none of the property keys is set to a string, may be
113      *            {@code null}.
114      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
115      *            a string value is found.
116      * @return The property value or {@code null} if none.
117      */
118     public static String getString( RepositorySystemSession session, String defaultValue, String... keys )
119     {
120         return getString( session.getConfigProperties(), defaultValue, keys );
121     }
122 
123     /**
124      * Gets the specified configuration property.
125      * 
126      * @param properties The configuration properties to read, must not be {@code null}.
127      * @param defaultValue The default value to return in case none of the property keys is set to a number.
128      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
129      *            a {@link Number} or a string representation of an {@link Integer} is found.
130      * @return The property value.
131      */
132     public static int getInteger( Map<?, ?> properties, int defaultValue, String... keys )
133     {
134         for ( String key : keys )
135         {
136             Object value = properties.get( key );
137 
138             if ( value instanceof Number )
139             {
140                 return ( (Number) value ).intValue();
141             }
142             else if ( value instanceof String )
143             {
144                 try
145                 {
146                     return Integer.parseInt( (String) value );
147                 }
148                 catch ( NumberFormatException e )
149                 {
150                     // try next key
151                 }
152             }
153         }
154 
155         return defaultValue;
156     }
157 
158     /**
159      * Gets the specified configuration property.
160      * 
161      * @param session The repository system session from which to read the configuration property, must not be
162      *            {@code null}.
163      * @param defaultValue The default value to return in case none of the property keys is set to a number.
164      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
165      *            a {@link Number} or a string representation of an {@link Integer} is found.
166      * @return The property value.
167      */
168     public static int getInteger( RepositorySystemSession session, int defaultValue, String... keys )
169     {
170         return getInteger( session.getConfigProperties(), defaultValue, keys );
171     }
172 
173     /**
174      * Gets the specified configuration property.
175      * 
176      * @param properties The configuration properties to read, must not be {@code null}.
177      * @param defaultValue The default value to return in case none of the property keys is set to a number.
178      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
179      *            a {@link Number} or a string representation of a {@link Long} is found.
180      * @return The property value.
181      */
182     public static long getLong( Map<?, ?> properties, long defaultValue, String... keys )
183     {
184         for ( String key : keys )
185         {
186             Object value = properties.get( key );
187 
188             if ( value instanceof Number )
189             {
190                 return ( (Number) value ).longValue();
191             }
192             else if ( value instanceof String )
193             {
194                 try
195                 {
196                     return Long.parseLong( (String) value );
197                 }
198                 catch ( NumberFormatException e )
199                 {
200                     // try next key
201                 }
202             }
203         }
204 
205         return defaultValue;
206     }
207 
208     /**
209      * Gets the specified configuration property.
210      * 
211      * @param session The repository system session from which to read the configuration property, must not be
212      *            {@code null}.
213      * @param defaultValue The default value to return in case none of the property keys is set to a number.
214      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
215      *            a {@link Number} or a string representation of a {@link Long} is found.
216      * @return The property value.
217      */
218     public static long getLong( RepositorySystemSession session, long defaultValue, String... keys )
219     {
220         return getLong( session.getConfigProperties(), defaultValue, keys );
221     }
222 
223     /**
224      * Gets the specified configuration property.
225      * 
226      * @param properties The configuration properties to read, must not be {@code null}.
227      * @param defaultValue The default value to return in case none of the property keys is set to a number.
228      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
229      *            a {@link Number} or a string representation of a {@link Float} is found.
230      * @return The property value.
231      */
232     public static float getFloat( Map<?, ?> properties, float defaultValue, String... keys )
233     {
234         for ( String key : keys )
235         {
236             Object value = properties.get( key );
237 
238             if ( value instanceof Number )
239             {
240                 return ( (Number) value ).floatValue();
241             }
242             else if ( value instanceof String )
243             {
244                 try
245                 {
246                     return Float.parseFloat( (String) value );
247                 }
248                 catch ( NumberFormatException e )
249                 {
250                     // try next key
251                 }
252             }
253         }
254 
255         return defaultValue;
256     }
257 
258     /**
259      * Gets the specified configuration property.
260      * 
261      * @param session The repository system session from which to read the configuration property, must not be
262      *            {@code null}.
263      * @param defaultValue The default value to return in case none of the property keys is set to a number.
264      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
265      *            a {@link Number} or a string representation of a {@link Float} is found.
266      * @return The property value.
267      */
268     public static float getFloat( RepositorySystemSession session, float defaultValue, String... keys )
269     {
270         return getFloat( session.getConfigProperties(), defaultValue, keys );
271     }
272 
273     /**
274      * Gets the specified configuration property.
275      * 
276      * @param properties The configuration properties to read, must not be {@code null}.
277      * @param defaultValue The default value to return in case none of the property keys is set to a boolean.
278      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
279      *            a {@link Boolean} or a string (to be {@link Boolean#parseBoolean(String) parsed as boolean}) is found.
280      * @return The property value.
281      */
282     public static boolean getBoolean( Map<?, ?> properties, boolean defaultValue, String... keys )
283     {
284         for ( String key : keys )
285         {
286             Object value = properties.get( key );
287 
288             if ( value instanceof Boolean )
289             {
290                 return (Boolean) value;
291             }
292             else if ( value instanceof String )
293             {
294                 return Boolean.parseBoolean( (String) value );
295             }
296         }
297 
298         return defaultValue;
299     }
300 
301     /**
302      * Gets the specified configuration property.
303      * 
304      * @param session The repository system session from which to read the configuration property, must not be
305      *            {@code null}.
306      * @param defaultValue The default value to return in case none of the property keys is set to a boolean.
307      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
308      *            a {@link Boolean} or a string (to be {@link Boolean#parseBoolean(String) parsed as boolean}) is found.
309      * @return The property value.
310      */
311     public static boolean getBoolean( RepositorySystemSession session, boolean defaultValue, String... keys )
312     {
313         return getBoolean( session.getConfigProperties(), defaultValue, keys );
314     }
315 
316     /**
317      * Gets the specified configuration property.
318      * 
319      * @param properties The configuration properties to read, must not be {@code null}.
320      * @param defaultValue The default value to return in case none of the property keys is set to a collection.
321      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
322      *            a collection is found.
323      * @return The property value or {@code null} if none.
324      */
325     public static List<?> getList( Map<?, ?> properties, List<?> defaultValue, String... keys )
326     {
327         for ( String key : keys )
328         {
329             Object value = properties.get( key );
330 
331             if ( value instanceof List )
332             {
333                 return (List<?>) value;
334             }
335             else if ( value instanceof Collection )
336             {
337                 return Collections.unmodifiableList( new ArrayList<>( (Collection<?>) value ) );
338             }
339         }
340 
341         return defaultValue;
342     }
343 
344     /**
345      * Gets the specified configuration property.
346      * 
347      * @param session The repository system session from which to read the configuration property, must not be
348      *            {@code null}.
349      * @param defaultValue The default value to return in case none of the property keys is set to a collection.
350      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
351      *            a collection is found.
352      * @return The property value or {@code null} if none.
353      */
354     public static List<?> getList( RepositorySystemSession session, List<?> defaultValue, String... keys )
355     {
356         return getList( session.getConfigProperties(), defaultValue, keys );
357     }
358 
359     /**
360      * Gets the specified configuration property.
361      * 
362      * @param properties The configuration properties to read, must not be {@code null}.
363      * @param defaultValue The default value to return in case none of the property keys is set to a map.
364      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
365      *            a map is found.
366      * @return The property value or {@code null} if none.
367      */
368     public static Map<?, ?> getMap( Map<?, ?> properties, Map<?, ?> defaultValue, String... keys )
369     {
370         for ( String key : keys )
371         {
372             Object value = properties.get( key );
373 
374             if ( value instanceof Map )
375             {
376                 return (Map<?, ?>) value;
377             }
378         }
379 
380         return defaultValue;
381     }
382 
383     /**
384      * Gets the specified configuration property.
385      * 
386      * @param session The repository system session from which to read the configuration property, must not be
387      *            {@code null}.
388      * @param defaultValue The default value to return in case none of the property keys is set to a map.
389      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
390      *            a map is found.
391      * @return The property value or {@code null} if none.
392      */
393     public static Map<?, ?> getMap( RepositorySystemSession session, Map<?, ?> defaultValue, String... keys )
394     {
395         return getMap( session.getConfigProperties(), defaultValue, keys );
396     }
397 
398 }