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 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}