|
| 1 | +packageio.quarkus.runtime; |
| 2 | + |
| 3 | +importjava.util.Map; |
| 4 | +importjava.util.Objects; |
| 5 | +importjava.util.concurrent.ConcurrentHashMap; |
| 6 | + |
| 7 | +/** |
| 8 | + * Provides a low-level API for registering and accessing runtime values generated by Quarkus or Quarkus |
| 9 | + * extensions. Typically, such values describe a fully configured application, known only at application startup, |
| 10 | + * such as the HTTP port. |
| 11 | + * <p> |
| 12 | + * These values should not be exposed by the Config mechanism directly, as that would require mutating the Config |
| 13 | + * system, which should be immutable, and cause hard-to-debug issues. Instead, RuntimeValues should be preferred for |
| 14 | + * exposing such values. |
| 15 | + */ |
| 16 | +publicclassRuntimeValues { |
| 17 | +privatestaticfinalMap<String,Object>values =newConcurrentHashMap<>(); |
| 18 | + |
| 19 | +/** |
| 20 | + * Registers a value with a specified key. |
| 21 | + * |
| 22 | + * @param key key with which the specified value is to be associated |
| 23 | + * @param value value to be associated with the specified key |
| 24 | + * @return the value associated with the specified key |
| 25 | + * @throws IllegalArgumentException if the specified key already has an associated value |
| 26 | + */ |
| 27 | +@SuppressWarnings("unchecked") |
| 28 | +publicstatic <T>Tregister(finalRuntimeKey<T>key,finalTvalue) { |
| 29 | +ObjectmapValue =values.putIfAbsent(key.key(),value); |
| 30 | +if (mapValue !=null && !mapValue.equals(value)) { |
| 31 | +thrownewIllegalArgumentException("Key already registered " +key.key() +" with value " +mapValue); |
| 32 | + } |
| 33 | +return (T)mapValue; |
| 34 | + } |
| 35 | + |
| 36 | +/** |
| 37 | + * Returns the value to which the specified key is mapped. |
| 38 | + * |
| 39 | + * @param key the key whose associated value is to be returned |
| 40 | + * @return the value to which the specified key is mapped |
| 41 | + * @throws java.lang.IllegalArgumentException if the specified key has no associated value |
| 42 | + */ |
| 43 | +@SuppressWarnings("unchecked") |
| 44 | +publicstatic <T>Tget(finalRuntimeKey<T>key) { |
| 45 | +Tt = (T)values.get(key.key()); |
| 46 | +if (t ==null) { |
| 47 | +thrownewIllegalArgumentException("Key " +key.key() +" not found"); |
| 48 | + } |
| 49 | +returnt; |
| 50 | + } |
| 51 | + |
| 52 | +/** |
| 53 | + * Returns the value to which the specified key is mapped. |
| 54 | + * |
| 55 | + * @param key the key whose associated value is to be returned |
| 56 | + * @param defaultValue the default mapping of the key |
| 57 | + * @return the value to which the specified key is mapped, or {@code defaultValue} if the key has no value |
| 58 | + */ |
| 59 | +@SuppressWarnings("unchecked") |
| 60 | +publicstatic <T>Tget(finalRuntimeKey<T>key,finalTdefaultValue) { |
| 61 | +return (T)values.getOrDefault(key.key(),defaultValue); |
| 62 | + } |
| 63 | + |
| 64 | +staticStringgetValue(finalStringpropertyName) { |
| 65 | +Objectvalue =values.get(propertyName); |
| 66 | +// TODO - We may require to convert this to the expected config string |
| 67 | +returnvalue !=null ?value.toString() :null; |
| 68 | + } |
| 69 | + |
| 70 | +publicstaticfinalclassRuntimeKey<T> { |
| 71 | +privatefinalStringkey; |
| 72 | + |
| 73 | +RuntimeKey(Stringkey) { |
| 74 | +this.key =key; |
| 75 | + } |
| 76 | + |
| 77 | +publicStringkey() { |
| 78 | +returnkey; |
| 79 | + } |
| 80 | + |
| 81 | +@Override |
| 82 | +publicbooleanequals(Objecto) { |
| 83 | +if (!(oinstanceofRuntimeKey<?>that)) |
| 84 | +returnfalse; |
| 85 | +returnObjects.equals(key,that.key); |
| 86 | + } |
| 87 | + |
| 88 | +@Override |
| 89 | +publicinthashCode() { |
| 90 | +returnObjects.hashCode(key); |
| 91 | + } |
| 92 | + |
| 93 | +publicstaticRuntimeKey<String>key(finalStringkey) { |
| 94 | +returnnewRuntimeKey<>(key); |
| 95 | + } |
| 96 | + |
| 97 | +publicstaticRuntimeKey<Integer>intKey(finalStringkey) { |
| 98 | +returnnewRuntimeKey<>(key); |
| 99 | + } |
| 100 | + } |
| 101 | +} |