001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2017-2022 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.util;
020
021import java.io.IOException;
022import java.util.HashMap;
023import java.util.Map;
024import java.util.Optional;
025import java.util.prefs.BackingStoreException;
026import java.util.prefs.Preferences;
027import java.util.stream.Collectors;
028import org.jgrapes.core.Channel;
029import org.jgrapes.core.EventPipeline;
030import org.jgrapes.core.annotation.Handler;
031import org.jgrapes.core.events.Start;
032import org.jgrapes.util.events.ConfigurationUpdate;
033import org.jgrapes.util.events.InitialPreferences;
034
035/**
036 * This component provides a store for an application's configuration
037 * backed by the Java {@link Preferences}. Preferences
038 * are maps of key value pairs that are associated with a path. A common
039 * base path is passed to the component on creation. The application's
040 * configuration information is stored using paths relative to that 
041 * base path.
042 * 
043 * The component reads the initial values from the Java {@link Preferences}
044 * tree denoted by the base path. During application bootstrap, it 
045 * intercepts the {@link Start} event using a handler with  priority 
046 * 999999. When receiving this event, it fires all known preferences 
047 * values on the channels of the start event as a 
048 * {@link InitialPreferences} event, using a new {@link EventPipeline}
049 * and waiting for its completion. Then, allows the intercepted 
050 * {@link Start} event to continue. 
051 * 
052 * Components that depend on configuration values define handlers
053 * for {@link ConfigurationUpdate} events and adapt themselves to the values 
054 * received. Note that due to the intercepted {@link Start} event, the initial
055 * preferences values are received before the {@link Start} event, so
056 * components' configurations can be rearranged before they actually
057 * start doing something.
058 *
059 * Besides initially publishing the stored preferences values,
060 * the component also listens for {@link ConfigurationUpdate} events
061 * on its channel and updates the preferences store (may be suppressed).
062 */
063public class PreferencesStore extends ConfigurationStore {
064
065    private Preferences preferences;
066
067    /**
068     * Creates a new component with its channel set to the given 
069     * channel and a base path derived from the given class.
070     * 
071     * @param componentChannel the channel 
072     * @param appClass the application class; the base path
073     * is formed by replacing each dot in the class's package's full 
074     * name with a slash, prepending a slash, and appending 
075     * "`/PreferencesStore`".
076     */
077    public PreferencesStore(Channel componentChannel, Class<?> appClass) {
078        this(componentChannel, appClass, true);
079    }
080
081    /**
082     * Allows the creation of a "read-only" store.
083     * 
084     * @param componentChannel the channel 
085     * @param appClass the application class; the base path
086     * is formed by replacing each dot in the class's package's full 
087     * name with a slash, prepending a slash, and appending 
088     * "`/PreferencesStore`".
089     * @param update whether to update the store when 
090     * {@link ConfigurationUpdate} events are received
091     * 
092     * @see #PreferencesStore(Channel, Class)
093     */
094    @SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
095    public PreferencesStore(
096            Channel componentChannel, Class<?> appClass, boolean update) {
097        super(componentChannel);
098        if (update) {
099            Handler.Evaluator.add(this, "onConfigurationUpdate",
100                channel().defaultCriterion());
101        }
102        preferences = Preferences.userNodeForPackage(appClass)
103            .node("PreferencesStore");
104    }
105
106    /**
107     * Intercepts the {@link Start} event and fires a
108     * {@link ConfigurationUpdate} event.
109     *
110     * @param event the event
111     * @throws BackingStoreException the backing store exception
112     * @throws InterruptedException the interrupted exception
113     */
114    @Handler(priority = 999_999, channels = Channel.class)
115    public void onStart(Start event)
116            throws BackingStoreException, InterruptedException {
117        InitialPreferences updEvt
118            = new InitialPreferences(preferences.parent().absolutePath());
119        addPrefs(updEvt, preferences.absolutePath(), preferences);
120        newEventPipeline().fire(updEvt, event.channels()).get();
121    }
122
123    private void addPrefs(
124            InitialPreferences updEvt, String rootPath, Preferences node)
125            throws BackingStoreException {
126        String nodePath = node.absolutePath();
127        String relPath = "/" + nodePath.substring(Math.min(
128            rootPath.length() + 1, nodePath.length()));
129        var props = new HashMap<String, String>();
130        for (String key : node.keys()) {
131            props.put(key, node.get(key, null));
132        }
133        updEvt.set(relPath, structure(props));
134        for (String child : node.childrenNames()) {
135            addPrefs(updEvt, rootPath, node.node(child));
136        }
137    }
138
139    /**
140     * Merges and saves configuration updates.
141     *
142     * @param event the event
143     * @throws IOException Signals that an I/O exception has occurred.
144     */
145    @Handler(dynamic = true)
146    @SuppressWarnings("PMD.AvoidReassigningLoopVariables")
147    public void onConfigurationUpdate(ConfigurationUpdate event)
148            throws BackingStoreException {
149        if (event instanceof InitialPreferences) {
150            return;
151        }
152        for (String path : event.paths()) {
153            Optional<Map<String, String>> prefs = event.values(path);
154            path = path.substring(1); // Remove leading slash
155            if (!prefs.isPresent()) {
156                preferences.node(path).removeNode();
157                continue;
158            }
159            for (Map.Entry<String, String> e : prefs.get().entrySet()) {
160                preferences.node(path).put(e.getKey(), e.getValue());
161            }
162        }
163        preferences.flush();
164    }
165
166    @Override
167    public Optional<Map<String, String>> values(String path) {
168        return nodeValues(path).map(m -> m.entrySet().stream()).map(
169            s -> s.collect(Collectors.toMap(e -> e.getKey().replace("\"", ""),
170                Map.Entry::getValue)));
171    }
172
173    @Override
174    public Optional<Map<String, Object>> structured(String path) {
175        return nodeValues(path).map(ConfigurationStore::structure);
176    }
177
178    private Optional<Map<String, String>> nodeValues(String path) {
179        if (!path.startsWith("/")) {
180            throw new IllegalArgumentException("Path must start with \"/\".");
181        }
182        try {
183            var relPath = path.substring(1);
184            if (!preferences.nodeExists(relPath)) {
185                return Optional.empty();
186            }
187            var node = preferences.node(relPath);
188            var result = new HashMap<String, String>();
189            for (String key : node.keys()) {
190                result.put(key, node.get(key, null));
191            }
192            return Optional.of(result);
193        } catch (BackingStoreException e) {
194            throw new IllegalStateException(e);
195        }
196    }
197
198}