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 
143             try
144             {
145                 return Integer.valueOf( (String) value );
146             }
147             catch ( Exception e )
148             {
149                 // try next key
150             }
151         }
152 
153         return defaultValue;
154     }
155 
156     /**
157      * Gets the specified configuration property.
158      * 
159      * @param session The repository system session from which to read the configuration property, must not be
160      *            {@code null}.
161      * @param defaultValue The default value to return in case none of the property keys is set to a number.
162      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
163      *            a {@link Number} or a string representation of an {@link Integer} is found.
164      * @return The property value.
165      */
166     public static int getInteger( RepositorySystemSession session, int defaultValue, String... keys )
167     {
168         return getInteger( session.getConfigProperties(), defaultValue, keys );
169     }
170 
171     /**
172      * Gets the specified configuration property.
173      * 
174      * @param properties The configuration properties to read, must not be {@code null}.
175      * @param defaultValue The default value to return in case none of the property keys is set to a number.
176      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
177      *            a {@link Number} or a string representation of a {@link Long} is found.
178      * @return The property value.
179      */
180     public static long getLong( Map<?, ?> properties, long defaultValue, String... keys )
181     {
182         for ( String key : keys )
183         {
184             Object value = properties.get( key );
185 
186             if ( value instanceof Number )
187             {
188                 return ( (Number) value ).longValue();
189             }
190 
191             try
192             {
193                 return Long.valueOf( (String) value );
194             }
195             catch ( Exception e )
196             {
197                 // try next key
198             }
199         }
200 
201         return defaultValue;
202     }
203 
204     /**
205      * Gets the specified configuration property.
206      * 
207      * @param session The repository system session from which to read the configuration property, must not be
208      *            {@code null}.
209      * @param defaultValue The default value to return in case none of the property keys is set to a number.
210      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
211      *            a {@link Number} or a string representation of a {@link Long} is found.
212      * @return The property value.
213      */
214     public static long getLong( RepositorySystemSession session, long defaultValue, String... keys )
215     {
216         return getLong( session.getConfigProperties(), defaultValue, keys );
217     }
218 
219     /**
220      * Gets the specified configuration property.
221      * 
222      * @param properties The configuration properties to read, must not be {@code null}.
223      * @param defaultValue The default value to return in case none of the property keys is set to a number.
224      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
225      *            a {@link Number} or a string representation of a {@link Float} is found.
226      * @return The property value.
227      */
228     public static float getFloat( Map<?, ?> properties, float defaultValue, String... keys )
229     {
230         for ( String key : keys )
231         {
232             Object value = properties.get( key );
233 
234             if ( value instanceof Number )
235             {
236                 return ( (Number) value ).floatValue();
237             }
238 
239             try
240             {
241                 return Float.valueOf( (String) value );
242             }
243             catch ( Exception e )
244             {
245                 // try next key
246             }
247         }
248 
249         return defaultValue;
250     }
251 
252     /**
253      * Gets the specified configuration property.
254      * 
255      * @param session The repository system session from which to read the configuration property, must not be
256      *            {@code null}.
257      * @param defaultValue The default value to return in case none of the property keys is set to a number.
258      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
259      *            a {@link Number} or a string representation of a {@link Float} is found.
260      * @return The property value.
261      */
262     public static float getFloat( RepositorySystemSession session, float defaultValue, String... keys )
263     {
264         return getFloat( session.getConfigProperties(), defaultValue, keys );
265     }
266 
267     /**
268      * Gets the specified configuration property.
269      * 
270      * @param properties The configuration properties to read, must not be {@code null}.
271      * @param defaultValue The default value to return in case none of the property keys is set to a boolean.
272      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
273      *            a {@link Boolean} or a string (to be {@link Boolean#parseBoolean(String) parsed as boolean}) is found.
274      * @return The property value.
275      */
276     public static boolean getBoolean( Map<?, ?> properties, boolean defaultValue, String... keys )
277     {
278         for ( String key : keys )
279         {
280             Object value = properties.get( key );
281 
282             if ( value instanceof Boolean )
283             {
284                 return (Boolean) value;
285             }
286             else if ( value instanceof String )
287             {
288                 return Boolean.parseBoolean( (String) value );
289             }
290         }
291 
292         return defaultValue;
293     }
294 
295     /**
296      * Gets the specified configuration property.
297      * 
298      * @param session The repository system session from which to read the configuration property, must not be
299      *            {@code null}.
300      * @param defaultValue The default value to return in case none of the property keys is set to a boolean.
301      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
302      *            a {@link Boolean} or a string (to be {@link Boolean#parseBoolean(String) parsed as boolean}) is found.
303      * @return The property value.
304      */
305     public static boolean getBoolean( RepositorySystemSession session, boolean defaultValue, String... keys )
306     {
307         return getBoolean( session.getConfigProperties(), defaultValue, keys );
308     }
309 
310     /**
311      * Gets the specified configuration property.
312      * 
313      * @param properties The configuration properties to read, must not be {@code null}.
314      * @param defaultValue The default value to return in case none of the property keys is set to a collection.
315      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
316      *            a collection is found.
317      * @return The property value or {@code null} if none.
318      */
319     public static List<?> getList( Map<?, ?> properties, List<?> defaultValue, String... keys )
320     {
321         for ( String key : keys )
322         {
323             Object value = properties.get( key );
324 
325             if ( value instanceof List )
326             {
327                 return (List<?>) value;
328             }
329             else if ( value instanceof Collection )
330             {
331                 return Collections.unmodifiableList( new ArrayList<Object>( (Collection<?>) value ) );
332             }
333         }
334 
335         return defaultValue;
336     }
337 
338     /**
339      * Gets the specified configuration property.
340      * 
341      * @param session The repository system session from which to read the configuration property, must not be
342      *            {@code null}.
343      * @param defaultValue The default value to return in case none of the property keys is set to a collection.
344      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
345      *            a collection is found.
346      * @return The property value or {@code null} if none.
347      */
348     public static List<?> getList( RepositorySystemSession session, List<?> defaultValue, String... keys )
349     {
350         return getList( session.getConfigProperties(), defaultValue, keys );
351     }
352 
353     /**
354      * Gets the specified configuration property.
355      * 
356      * @param properties The configuration properties to read, must not be {@code null}.
357      * @param defaultValue The default value to return in case none of the property keys is set to a map.
358      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
359      *            a map is found.
360      * @return The property value or {@code null} if none.
361      */
362     public static Map<?, ?> getMap( Map<?, ?> properties, Map<?, ?> defaultValue, String... keys )
363     {
364         for ( String key : keys )
365         {
366             Object value = properties.get( key );
367 
368             if ( value instanceof Map )
369             {
370                 return (Map<?, ?>) value;
371             }
372         }
373 
374         return defaultValue;
375     }
376 
377     /**
378      * Gets the specified configuration property.
379      * 
380      * @param session The repository system session from which to read the configuration property, must not be
381      *            {@code null}.
382      * @param defaultValue The default value to return in case none of the property keys is set to a map.
383      * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until
384      *            a map is found.
385      * @return The property value or {@code null} if none.
386      */
387     public static Map<?, ?> getMap( RepositorySystemSession session, Map<?, ?> defaultValue, String... keys )
388     {
389         return getMap( session.getConfigProperties(), defaultValue, keys );
390     }
391 
392 }