|
| 1 | +/* |
| 2 | + * Copyright 2025 ABSA Group Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.apache.commons.configuration2 |
| 18 | + |
| 19 | +import org.apache.commons.lang3.StringUtils.{isBlank, isNotBlank} |
| 20 | + |
| 21 | +import scala.jdk.CollectionConverters._ |
| 22 | +import scala.reflect.runtime.universe.{TypeTag, typeOf} |
| 23 | +import scala.util.Try |
| 24 | + |
| 25 | + |
| 26 | +/** |
| 27 | + * The object contains extension methods for the [[org.apache.commons.configuration2.Configuration Configuration]] interface. |
| 28 | + */ |
| 29 | +//noinspection ScalaUnusedSymbol |
| 30 | +object ConfigurationImplicits { |
| 31 | + |
| 32 | + /** |
| 33 | + * The class wraps the [[org.apache.commons.configuration2.Configuration Configuration]] interface in order to provide extension methods. |
| 34 | + * |
| 35 | + * @param conf A configuration instance |
| 36 | + * @tparam T A specific type implementing the [[org.apache.commons.configuration2.Configuration Configuration]] interface |
| 37 | + */ |
| 38 | + implicit class ConfigurationRequiredWrapper[T <: Configuration](val conf: T) extends AnyVal { |
| 39 | + |
| 40 | + /** |
| 41 | + * Gets a value of an object configuration property and checks whether property exists. |
| 42 | + * |
| 43 | + * @return A value of an object configuration property if exists, otherwise throws an exception. |
| 44 | + */ |
| 45 | + def getRequiredObject[A <: AnyRef]: String => A = getRequired[AnyRef](conf.getProperty, null.!=)(_).asInstanceOf[A] |
| 46 | + |
| 47 | + /** |
| 48 | + * Gets a value of string configuration property and checks whether property exists. |
| 49 | + * |
| 50 | + * @return A value of string configuration property if exists, otherwise throws an exception. |
| 51 | + */ |
| 52 | + def getRequiredString: String => String = getRequired(conf.getString, isNotBlank) |
| 53 | + |
| 54 | + /** |
| 55 | + * Gets a value of string array configuration property and checks whether the array is not empty. |
| 56 | + * |
| 57 | + * @return A value of string array configuration property if not empty, otherwise throws an exception. |
| 58 | + */ |
| 59 | + def getRequiredStringArray: String => Array[String] = getRequired(conf.getStringArray, (arr: Array[String]) => !arr.forall(isBlank)) |
| 60 | + |
| 61 | + /** |
| 62 | + * Gets a value of boolean configuration property and checks whether property exists. |
| 63 | + * |
| 64 | + * @return A value of boolean configuration property if exists, otherwise throws an exception. |
| 65 | + */ |
| 66 | + def getRequiredBoolean: String => Boolean = getRequired(conf.getBoolean(_, null), null.!=) //NOSONAR |
| 67 | + |
| 68 | + /** |
| 69 | + * Gets a value of big decimal configuration property and checks whether property exists. |
| 70 | + * |
| 71 | + * @return A value of big decimal configuration property if exists, otherwise throws an exception. |
| 72 | + */ |
| 73 | + def getRequiredBigDecimal: String => BigDecimal = getRequired(conf.getBigDecimal(_, null), null.!=) //NOSONAR |
| 74 | + |
| 75 | + /** |
| 76 | + * Gets a value of byte configuration property and checks whether property exists. |
| 77 | + * |
| 78 | + * @return A value of byte configuration property if exists, otherwise throws an exception. |
| 79 | + */ |
| 80 | + def getRequiredByte: String => Byte = getRequired(conf.getByte(_, null), null.!=) //NOSONAR |
| 81 | + |
| 82 | + /** |
| 83 | + * Gets a value of short configuration property and checks whether property exists. |
| 84 | + * |
| 85 | + * @return A value of short configuration property if exists, otherwise throws an exception. |
| 86 | + */ |
| 87 | + def getRequiredShort: String => Short = getRequired(conf.getShort(_, null), null.!=) //NOSONAR |
| 88 | + |
| 89 | + /** |
| 90 | + * Gets a value of int configuration property and checks whether property exists. |
| 91 | + * |
| 92 | + * @return A value of int configuration property if exists, otherwise throws an exception. |
| 93 | + */ |
| 94 | + def getRequiredInt: String => Int = getRequired(conf.getInteger(_, null), null.!=) //NOSONAR |
| 95 | + |
| 96 | + /** |
| 97 | + * Gets a value of long configuration property and checks whether property exists. |
| 98 | + * |
| 99 | + * @return A value of long configuration property if exists, otherwise throws an exception. |
| 100 | + */ |
| 101 | + def getRequiredLong: String => Long = getRequired(conf.getLong(_, null), null.!=) //NOSONAR |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets a value of float configuration property and checks whether property exists. |
| 105 | + * |
| 106 | + * @return A value of float configuration property if exists, otherwise throws an exception. |
| 107 | + */ |
| 108 | + def getRequiredFloat: String => Float = getRequired(conf.getFloat(_, null), null.!=) //NOSONAR |
| 109 | + |
| 110 | + /** |
| 111 | + * Gets a value of double configuration property and checks whether property exists. |
| 112 | + * |
| 113 | + * @return A value of double configuration property if exists, otherwise throws an exception. |
| 114 | + */ |
| 115 | + def getRequiredDouble: String => Double = getRequired(conf.getDouble(_, null), null.!=) //NOSONAR |
| 116 | + |
| 117 | + private def getRequired[V](get: String => V, check: V => Boolean)(key: String): V = { |
| 118 | + Try(get(key)) |
| 119 | + .filter(check) |
| 120 | + .recover { |
| 121 | + case _: NoSuchElementException => |
| 122 | + // rewrite exception message for clarity |
| 123 | + throw new NoSuchElementException(s"Missing configuration property ${getFullPropName(key)}") |
| 124 | + case e: Exception => |
| 125 | + throw new RuntimeException(s"Error in retrieving configuration property ${getFullPropName(key)}", e) |
| 126 | + } |
| 127 | + .get |
| 128 | + } |
| 129 | + |
| 130 | + private def getFullPropName(key: String) = conf match { |
| 131 | + case sc: SubsetConfiguration => sc.getParentKey(key) |
| 132 | + case _ => key |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * The class wraps the [[org.apache.commons.configuration2.Configuration Configuration]] interface in order to provide extension methods. |
| 138 | + * |
| 139 | + * @param conf A configuration instance |
| 140 | + * @tparam T A specific type implementing the [[org.apache.commons.configuration2.Configuration Configuration]] interface |
| 141 | + */ |
| 142 | + implicit class ConfigurationOptionalWrapper[T <: Configuration](val conf: T) extends AnyVal { |
| 143 | + |
| 144 | + /** |
| 145 | + * Gets a value of an object configuration property. |
| 146 | + * |
| 147 | + * @return A Some wrapped value of object configuration property if exists, otherwise None. |
| 148 | + */ |
| 149 | + def getOptionalObject[A <: AnyRef]: String => Option[A] = getOptional[AnyRef](conf.getProperty)(_).map(_.asInstanceOf[A]) |
| 150 | + |
| 151 | + /** |
| 152 | + * Gets a value of string configuration property. |
| 153 | + * |
| 154 | + * @return A Some wrapped value of string configuration property if exists, otherwise None. |
| 155 | + */ |
| 156 | + def getOptionalString: String => Option[String] = getOptional(conf.getString)(_).filter(isNotBlank) |
| 157 | + |
| 158 | + |
| 159 | + /** |
| 160 | + * Gets a value of string array configuration property and checks whether the array is not empty. |
| 161 | + * |
| 162 | + * @return A Some wrapped value of string array configuration property if not empty, otherwise None. |
| 163 | + */ |
| 164 | + def getOptionalStringArray: String => Option[Array[String]] = getOptional(conf.getStringArray)(_).filter(_.nonEmpty) |
| 165 | + |
| 166 | + /** |
| 167 | + * Gets a value of boolean configuration property. |
| 168 | + * |
| 169 | + * @return A Some wrapped value of boolean configuration property if exists, otherwise None. |
| 170 | + */ |
| 171 | + def getOptionalBoolean: String => Option[Boolean] = getOptional(conf.getBoolean) |
| 172 | + |
| 173 | + /** |
| 174 | + * Gets a value of big decimal configuration property. |
| 175 | + * |
| 176 | + * @return A Some wrapped value of big decimal configuration property if exists, otherwise None. |
| 177 | + */ |
| 178 | + //noinspection ConvertibleToMethodValue |
| 179 | + def getOptionalBigDecimal: String => Option[BigDecimal] = getOptional(conf.getBigDecimal(_)) |
| 180 | + |
| 181 | + /** |
| 182 | + * Gets a value of byte configuration property. |
| 183 | + * |
| 184 | + * @return A Some wrapped value of byte configuration property if exists, otherwise None. |
| 185 | + */ |
| 186 | + def getOptionalByte: String => Option[Byte] = getOptional(conf.getByte) |
| 187 | + |
| 188 | + /** |
| 189 | + * Gets a value of short configuration property. |
| 190 | + * |
| 191 | + * @return A Some wrapped value of short configuration property if exists, otherwise None. |
| 192 | + */ |
| 193 | + def getOptionalShort: String => Option[Short] = getOptional(conf.getShort) |
| 194 | + |
| 195 | + /** |
| 196 | + * Gets a value of int configuration property. |
| 197 | + * |
| 198 | + * @return A Some wrapped value of int configuration property if exists, otherwise None. |
| 199 | + */ |
| 200 | + def getOptionalInt: String => Option[Int] = getOptional(conf.getInt) |
| 201 | + |
| 202 | + /** |
| 203 | + * Gets a value of long configuration property. |
| 204 | + * |
| 205 | + * @return A Some wrapped value of long configuration property if exists, otherwise None. |
| 206 | + */ |
| 207 | + def getOptionalLong: String => Option[Long] = getOptional(conf.getLong) |
| 208 | + |
| 209 | + /** |
| 210 | + * Gets a value of float configuration property. |
| 211 | + * |
| 212 | + * @return A Some wrapped value of float configuration property if exists, otherwise None. |
| 213 | + */ |
| 214 | + def getOptionalFloat: String => Option[Float] = getOptional(conf.getFloat) |
| 215 | + |
| 216 | + /** |
| 217 | + * Gets a value of double configuration property. |
| 218 | + * |
| 219 | + * @return A Some wrapped value of double configuration property if exists, otherwise None. |
| 220 | + */ |
| 221 | + def getOptionalDouble: String => Option[Double] = getOptional(conf.getDouble) |
| 222 | + |
| 223 | + private def getOptional[V](get: String => V)(key: String): Option[V] = { |
| 224 | + if (conf.containsKey(key)) |
| 225 | + Option(get(key)) |
| 226 | + else |
| 227 | + None |
| 228 | + } |
| 229 | + |
| 230 | + } |
| 231 | + |
| 232 | + implicit class ConfigurationMapWrapper(val conf: Configuration) extends AnyVal { |
| 233 | + |
| 234 | + /** |
| 235 | + * Converts the configuration into map where keys are Strings and values are converted to U type. |
| 236 | + * When the value key is not of proper type it throws. |
| 237 | + * |
| 238 | + * @tparam U type of values in returned map |
| 239 | + * @return map representation of the configuration |
| 240 | + */ |
| 241 | + def toMap[U: TypeTag]: Map[String, U] = |
| 242 | + ConfigurationImplicits.toMap[U](conf) |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * This method needs to be defined outside of the Value class since Scala has issues with TypeTag in Value classes |
| 247 | + */ |
| 248 | + private def toMap[U: TypeTag](conf: Configuration): Map[String, U] = { |
| 249 | + val fun = typeOf[U] match { |
| 250 | + case t if t =:= typeOf[String] => (c: Configuration, k: String) => c.getRequiredString(k) |
| 251 | + case t if t =:= typeOf[Boolean] => (c: Configuration, k: String) => c.getRequiredBoolean(k) |
| 252 | + case t if t =:= typeOf[BigDecimal] => (c: Configuration, k: String) => c.getRequiredBigDecimal(k) |
| 253 | + case t if t =:= typeOf[Byte] => (c: Configuration, k: String) => c.getRequiredByte(k) |
| 254 | + case t if t =:= typeOf[Short] => (c: Configuration, k: String) => c.getRequiredShort(k) |
| 255 | + case t if t =:= typeOf[Int] => (c: Configuration, k: String) => c.getRequiredInt(k) |
| 256 | + case t if t =:= typeOf[Long] => (c: Configuration, k: String) => c.getRequiredLong(k) |
| 257 | + case t if t =:= typeOf[Float] => (c: Configuration, k: String) => c.getRequiredFloat(k) |
| 258 | + case t if t =:= typeOf[Double] => (c: Configuration, k: String) => c.getRequiredDouble(k) |
| 259 | + case t if t =:= typeOf[AnyRef] => (c: Configuration, k: String) => c.getProperty(k) |
| 260 | + case t if t =:= typeOf[Array[String]] => (c: Configuration, k: String) => c.getRequiredStringArray(k) |
| 261 | + case t => throw new UnsupportedOperationException(s"Type $t not supported") |
| 262 | + } |
| 263 | + |
| 264 | + conf |
| 265 | + .getKeys.asScala |
| 266 | + .map(k => k -> fun(conf, k).asInstanceOf[U]) |
| 267 | + .toMap |
| 268 | + } |
| 269 | + |
| 270 | + |
| 271 | +} |
0 commit comments