|
1 | 1 | /* |
2 | | - Copyright (c) 2012, 2020, Anatole Tresch, Werner Keil and others by the @author tag. |
| 2 | + Copyright (c) 2012, 2020, Werner Keil and others by the @author tag. |
3 | 3 |
|
4 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); you may not |
5 | 5 | use this file except in compliance with the License. You may obtain a copy of |
|
15 | 15 | */ |
16 | 16 | package org.javamoney.moneta.spi; |
17 | 17 |
|
18 | | -import javax.money.spi.Bootstrap; |
| 18 | +import java.io.IOException; |
| 19 | +import java.net.URL; |
19 | 20 | import java.util.*; |
| 21 | +import java.util.logging.Level; |
20 | 22 | import java.util.logging.Logger; |
21 | 23 |
|
22 | 24 | /** |
23 | 25 | * Loader for the Java Money JSR configuration. |
24 | 26 | * |
25 | 27 | * @author Anatole Tresch |
26 | | - * @deprecated Will be removed from the SPI. Implement and register an instance of {@link MonetaryConfigProvider} |
27 | | - * instead of. |
28 | | - * @see MonetaryConfigProvider |
| 28 | + * @author Werner Keil |
29 | 29 | */ |
30 | | -@Deprecated |
31 | 30 | public final class MonetaryConfig { |
32 | 31 |
|
33 | 32 | private static final Logger LOG = Logger |
34 | 33 | .getLogger(MonetaryConfig.class.getName()); |
35 | 34 |
|
36 | 35 | private static final MonetaryConfig INSTANCE = new MonetaryConfig(); |
37 | 36 |
|
| 37 | + private final Map<String, String> config = new HashMap<>(); |
| 38 | + private final Map<String, Integer> priorities = new HashMap<>(); |
| 39 | + |
38 | 40 | private MonetaryConfig() { |
| 41 | + try { |
| 42 | + Enumeration<URL> urls = getClass().getClassLoader().getResources( |
| 43 | + "javamoney.properties"); |
| 44 | + while (urls.hasMoreElements()) { |
| 45 | + URL url = urls.nextElement(); |
| 46 | + try { |
| 47 | + Properties props = new Properties(); |
| 48 | + props.load(url.openStream()); |
| 49 | + updateConfig(props); |
| 50 | + } catch (Exception e) { |
| 51 | + LOG.log(Level.SEVERE, |
| 52 | + "Error loading javamoney.properties, ignoring " |
| 53 | + + url, e); |
| 54 | + } |
| 55 | + } |
| 56 | + } catch (IOException e) { |
| 57 | + LOG.log(Level.SEVERE, "Error loading javamoney.properties.", e); |
| 58 | + } |
39 | 59 | } |
40 | 60 |
|
41 | | - /** |
42 | | - * Sets a new config value. Note that when a custom {@link MonetaryConfigProvider} is registered, writing |
43 | | - * of configuration values is not supported. In this case a debug log (fine) is written. |
44 | | - * |
45 | | - * @param key the key to be set, not null. |
46 | | - * @param value the new value, or null. |
47 | | - * @return the previous value. |
48 | | - */ |
49 | | - public static String setValue(String key, String value){ |
50 | | - if(Bootstrap.getService(MonetaryConfigProvider.class) instanceof DefaultConfigProvider) { |
51 | | - DefaultConfigProvider defaultConfigProvider = (DefaultConfigProvider)Bootstrap.getService(MonetaryConfigProvider.class); |
52 | | - if (value == null) { |
53 | | - return defaultConfigProvider.config.remove(key); |
| 61 | + private void updateConfig(Properties props) { |
| 62 | + for (Map.Entry<Object, Object> en : props.entrySet()) { |
| 63 | + String key = en.getKey().toString(); |
| 64 | + String value = en.getValue().toString(); |
| 65 | + int prio = 0; |
| 66 | + if (key.startsWith("{")) { |
| 67 | + int index = key.indexOf('}'); |
| 68 | + if (index > 0) { |
| 69 | + String prioString = key.substring(1, index); |
| 70 | + try { |
| 71 | + prio = Integer.parseInt(prioString); |
| 72 | + key = key.substring(index + 1); |
| 73 | + } catch (NumberFormatException e) { |
| 74 | + LOG.warning("Invalid config key in javamoney.properties: " + key); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + Integer existingPrio = priorities.get(key); |
| 79 | + if (Objects.isNull(existingPrio)) { |
| 80 | + priorities.put(key, prio); |
| 81 | + config.put(key, value); |
| 82 | + } else if (existingPrio < prio) { |
| 83 | + priorities.put(key, prio); |
| 84 | + config.put(key, value); |
| 85 | + } else if (existingPrio == prio) { |
| 86 | + throw new IllegalStateException( |
| 87 | + "AmbiguousConfiguration detected for '" + key + "'."); |
54 | 88 | } |
55 | | - return defaultConfigProvider.config.put(key, value); |
| 89 | + // else ignore entry with lower prio! |
56 | 90 | } |
57 | | - LOG.fine("MonetaryConfig does not support deprecated write of " + key + "=" + value); |
58 | | - return null; |
| 91 | + } |
| 92 | + |
| 93 | + public static String setValue(String key, String value){ |
| 94 | + if(value==null){ |
| 95 | + return INSTANCE.config.remove(key); |
| 96 | + } |
| 97 | + return INSTANCE.config.put(key, value); |
59 | 98 | } |
60 | 99 |
|
61 | 100 | public static Optional<String> getString(String key){ |
62 | | - return Optional.ofNullable(Bootstrap.getService(MonetaryConfigProvider.class).getProperty(key)); |
| 101 | + return Optional.ofNullable(INSTANCE.config.get(key)); |
63 | 102 | } |
64 | 103 |
|
65 | 104 | public static Optional<Boolean> getBoolean(String key){ |
66 | | - String val = Bootstrap.getService(MonetaryConfigProvider.class).getProperty(key); |
| 105 | + String val = INSTANCE.config.get(key); |
67 | 106 | if(val != null){ |
68 | 107 | return Optional.ofNullable(Boolean.parseBoolean(val)); |
69 | 108 | } |
70 | 109 | return Optional.empty(); |
71 | 110 | } |
72 | 111 |
|
73 | 112 | public static Optional<Integer> getInteger(String key){ |
74 | | - String val = Bootstrap.getService(MonetaryConfigProvider.class).getProperty(key); |
| 113 | + String val = INSTANCE.config.get(key); |
75 | 114 | if(val != null){ |
76 | 115 | return Optional.ofNullable(Integer.parseInt(val)); |
77 | 116 | } |
78 | 117 | return Optional.empty(); |
79 | 118 | } |
80 | 119 |
|
81 | 120 | public static Optional<Long> getLong(String key){ |
82 | | - String val = Bootstrap.getService(MonetaryConfigProvider.class).getProperty(key); |
| 121 | + String val = INSTANCE.config.get(key); |
83 | 122 | if(val != null){ |
84 | 123 | return Optional.ofNullable(Long.parseLong(val)); |
85 | 124 | } |
86 | 125 | return Optional.empty(); |
87 | 126 | } |
88 | 127 |
|
89 | 128 | public static Optional<Float> getFloat(String key){ |
90 | | - String val = Bootstrap.getService(MonetaryConfigProvider.class).getProperty(key); |
| 129 | + String val = INSTANCE.config.get(key); |
91 | 130 | if(val != null){ |
92 | 131 | return Optional.ofNullable(Float.parseFloat(val)); |
93 | 132 | } |
94 | 133 | return Optional.empty(); |
95 | 134 | } |
96 | 135 |
|
97 | 136 | public static Optional<Double> getDouble(String key){ |
98 | | - String val = Bootstrap.getService(MonetaryConfigProvider.class).getProperty(key); |
| 137 | + String val = INSTANCE.config.get(key); |
99 | 138 | if(val != null){ |
100 | 139 | return Optional.ofNullable(Double.parseDouble(val)); |
101 | 140 | } |
102 | 141 | return Optional.empty(); |
103 | 142 | } |
104 | 143 |
|
105 | 144 | public static Map<String, String> getConfig() { |
106 | | - return Collections.unmodifiableMap(Bootstrap.getService(MonetaryConfigProvider.class).getProperties()); |
| 145 | + return Collections.unmodifiableMap(INSTANCE.config); |
107 | 146 | } |
108 | | - |
109 | | - |
110 | 147 | } |
0 commit comments