001package org.eclipse.aether.util; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.ArrayList; 023import java.util.Collection; 024import java.util.Collections; 025import java.util.List; 026import java.util.Map; 027 028import org.eclipse.aether.RepositorySystemSession; 029 030/** 031 * A utility class to read configuration properties from a repository system session. 032 * 033 * @see RepositorySystemSession#getConfigProperties() 034 */ 035public final class ConfigUtils 036{ 037 038 private ConfigUtils() 039 { 040 // hide constructor 041 } 042 043 /** 044 * Gets the specified configuration property. 045 * 046 * @param properties The configuration properties to read, must not be {@code null}. 047 * @param defaultValue The default value to return in case none of the property keys are set, may be {@code null}. 048 * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until 049 * a valid value is found. 050 * @return The property value or {@code null} if none. 051 */ 052 public static Object getObject( Map<?, ?> properties, Object defaultValue, String... keys ) 053 { 054 for ( String key : keys ) 055 { 056 Object value = properties.get( key ); 057 058 if ( value != null ) 059 { 060 return value; 061 } 062 } 063 064 return defaultValue; 065 } 066 067 /** 068 * Gets the specified configuration property. 069 * 070 * @param session The repository system session from which to read the configuration property, must not be 071 * {@code null}. 072 * @param defaultValue The default value to return in case none of the property keys are set, may be {@code null}. 073 * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until 074 * a valid value is found. 075 * @return The property value or {@code null} if none. 076 */ 077 public static Object getObject( RepositorySystemSession session, Object defaultValue, String... keys ) 078 { 079 return getObject( session.getConfigProperties(), defaultValue, keys ); 080 } 081 082 /** 083 * Gets the specified configuration property. 084 * 085 * @param properties The configuration properties to read, must not be {@code null}. 086 * @param defaultValue The default value to return in case none of the property keys is set to a string, may be 087 * {@code null}. 088 * @param keys The property keys to read, must not be {@code null}. The specified keys are read one after one until 089 * a string value is found. 090 * @return The property value or {@code null} if none. 091 */ 092 public static String getString( Map<?, ?> properties, String defaultValue, String... keys ) 093 { 094 for ( String key : keys ) 095 { 096 Object value = properties.get( key ); 097 098 if ( value instanceof String ) 099 { 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}