|
29 | 29 | */ |
30 | 30 | final class ConfigurationSystemImpl implements ConfigurationSystem { |
31 | 31 |
|
32 | | - private volatile MutableConfiguration configuration; |
| 32 | + private Lazy<MutableConfiguration> configuration = |
| 33 | + Lazy.of(new MutableConfigurationSupplier()); |
33 | 34 |
|
34 | 35 | ConfigurationSystemImpl() { |
35 | 36 | // nothing here |
36 | 37 | } |
37 | 38 |
|
38 | 39 | @Override |
39 | 40 | public Configuration getConfiguration() { |
40 | | - if (configuration == null) { |
41 | | - configuration = getMutableConfiguration(); |
42 | | - } |
43 | | - return configuration; |
| 41 | + return configuration.get(); |
44 | 42 | } |
45 | 43 |
|
46 | | - /** |
47 | | - * Configures an Eid library programmatically. |
48 | | - * |
49 | | - * @param configurator a configurator to use to configure Eid library |
50 | | - * @return a reference to a configurator that can be used to restore |
51 | | - * previous configuration |
52 | | - */ |
53 | 44 | @Override |
54 | 45 | public Configurator configure(Configurator configurator) { |
55 | | - // ensure system configuration are loaded |
56 | | - getConfiguration(); |
57 | | - MutableConfiguration configuredSettings = configuration; |
58 | | - configuration = new ConfigurationImpl(configuredSettings); |
59 | | - configurator.configure(configuration); |
60 | | - return new RestoreConfigurator(configuredSettings); |
| 46 | + MutableConfiguration configured = configuration.get(); |
| 47 | + MutableConfiguration mutable = new ConfigurationImpl(configured); |
| 48 | + configurator.configure(mutable); |
| 49 | + configuration = Lazy.of(mutable); |
| 50 | + return new RestoreConfigurator(configured); |
61 | 51 | } |
62 | 52 |
|
63 | | - private synchronized MutableConfiguration getMutableConfiguration() { |
64 | | - if (configuration == null) { |
| 53 | + private static final class MutableConfigurationSupplier |
| 54 | + implements Supplier<MutableConfiguration> { |
| 55 | + |
| 56 | + @Override |
| 57 | + public MutableConfiguration get() { |
65 | 58 | return loadConfiguration(); |
66 | 59 | } |
67 | | - return configuration; |
68 | | - } |
69 | 60 |
|
70 | | - private static MutableConfiguration loadConfiguration() { |
71 | | - MutableConfiguration mutableConfiguration = new ConfigurationImpl(); |
72 | | - new DefaultConfigurator().configure(mutableConfiguration); |
73 | | - ServiceLoader<Configurator> configurators = |
74 | | - ServiceLoader.load(Configurator.class); |
| 61 | + private static MutableConfiguration loadConfiguration() { |
| 62 | + MutableConfiguration mutableConfiguration = new ConfigurationImpl(); |
| 63 | + new DefaultConfigurator().configure(mutableConfiguration); |
| 64 | + ServiceLoader<Configurator> configurators = |
| 65 | + ServiceLoader.load(Configurator.class); |
75 | 66 |
|
76 | | - for (Configurator configurator : configurators) { |
77 | | - configurator.configure(mutableConfiguration); |
| 67 | + for (Configurator configurator : configurators) { |
| 68 | + configurator.configure(mutableConfiguration); |
| 69 | + } |
| 70 | + return mutableConfiguration; |
78 | 71 | } |
79 | | - return mutableConfiguration; |
80 | 72 | } |
81 | 73 |
|
82 | 74 | private static final class RestoreConfigurator |
|
0 commit comments