Configuration change events can be listened for using the following methods:
- {@link oaj.config.Config}
- {@link oaj.config.Config#addListener(ConfigEventListener) addListener(ConfigEventListener)}
- {@link oaj.config.Config#removeListener(ConfigEventListener) removeListener(ConfigEventListener)}
The {@link oaj.config.event.ConfigEventListener} interface consists of the following method:
- {@link oaj.config.event.ConfigEventListener}
- {@link oaj.config.event.ConfigEventListener#onConfigChange(ConfigEvents) onConfigChange(ConfigEvents)}
The {@link oaj.config.event.ConfigEvent} class provides access to all the information about the updated entry:
- {@link oaj.config.event.ConfigEvent}
- {@link oaj.config.event.ConfigEvent#getType() getType()}
- {@link oaj.config.event.ConfigEvent#getSection() getSection()}
- {@link oaj.config.event.ConfigEvent#getKey() getKey()}
- {@link oaj.config.event.ConfigEvent#getValue() getValue()}
- {@link oaj.config.event.ConfigEvent#getModifiers() getModifiers()}
- {@link oaj.config.event.ConfigEvent#getComment() getComment()}
- {@link oaj.config.event.ConfigEvent#getPreLines() getPreLines}
The listener method is triggered:
- After {@link oaj.config.Config#commit()} is called.
- When the file changes on the file system.
In both cases, the listener is triggered after the changes have been committed.
| final Config config = Config.create("MyConfig.cfg").build();
|
| // Add a listener for changes to MySection/key1
| config.addListener(
| new ConfigEventListener() {
| @Override
| public void onConfigChange(ConfigEvents events) {
| for (ConfigEvent event : events) {
| if (event.getType() == SET_ENTRY) {
|
| String section = event.getSection();
| String key = event.getKey();
|
| if (section.equals("MySection") && key.equals("key1")) {
|
| // Get the new value from the event.
| String newVal = event.getValue();
|
| // Or get the new value from the config (since the change has already been committed).
| newVal = config.getString("MySection/key1");
| }
| }
| }
| }
| }
| )