001/* 002 * JGrapes Event Driven Framework 003 * Copyright (C) 2017-2018 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 com.electronwill.nightconfig.core.file.FileConfig; 022import java.io.File; 023import java.io.IOException; 024import org.jgrapes.core.Channel; 025import org.jgrapes.core.EventPipeline; 026import org.jgrapes.core.events.Start; 027import org.jgrapes.util.events.ConfigurationUpdate; 028import org.jgrapes.util.events.FileChanged; 029import org.jgrapes.util.events.InitialPreferences; 030 031/** 032 * This component provides a store for an application's configuration 033 * backed by a YAML file. The YAML object described by this file 034 * represents the root directory. If an entry does not start with a 035 * slash, it represents the key of a key value pair. If it does 036 * starts with a slash, the value is another JSON object that 037 * describes the respective subdirectory. 038 * 039 * The component reads the initial values from {@link File} passed 040 * to the constructor. During application bootstrap, it 041 * intercepts the {@link Start} event using a handler with priority 042 * 999999. When receiving this event, it fires all known preferences 043 * values on the channels of the start event as a 044 * {@link InitialPreferences} event, using a new {@link EventPipeline} 045 * and waiting for its completion. Then, allows the intercepted 046 * {@link Start} event to continue. 047 * 048 * Components that depend on configuration values define handlers 049 * for {@link ConfigurationUpdate} events and adapt themselves to the values 050 * received. Note that due to the intercepted {@link Start} event, the initial 051 * preferences values are received before the {@link Start} event, so 052 * components' configurations can be rearranged before they actually 053 * start doing something. 054 * 055 * Besides initially publishing the stored preferences values, 056 * the component also listens for {@link ConfigurationUpdate} events 057 * on its channel and updates the YAML file (may be suppressed). 058 */ 059public class YamlConfigurationStore extends NightConfigStore { 060 061 /** 062 * Creates a new component with its channel set to the given 063 * channel and the given file. The component handles 064 * {@link ConfigurationUpdate} events and {@link FileChanged} 065 * events for the configuration file (see 066 * @link #NightConfigStore(Channel, File, boolean, boolean)} 067 * 068 * @param componentChannel the channel 069 * @param file the file used to store the configuration 070 * @throws IOException 071 */ 072 public YamlConfigurationStore(Channel componentChannel, File file) 073 throws IOException { 074 this(componentChannel, file, true); 075 } 076 077 /** 078 * Creates a new component with its channel set to the given 079 * channel and the given file. The component handles 080 * {@link FileChanged} events for the configuration file (see 081 * @link #NightConfigStore(Channel, File, boolean, boolean)} 082 * 083 * If `update` is `true`, the configuration file is updated 084 * when {@link ConfigurationUpdate} events are received. 085 * 086 * @param componentChannel the channel 087 * @param file the file used to store the configuration 088 * @param update if the configuration file is to be updated 089 * @throws IOException Signals that an I/O exception has occurred. 090 */ 091 public YamlConfigurationStore(Channel componentChannel, File file, 092 boolean update) throws IOException { 093 this(componentChannel, file, update, true); 094 } 095 096 /** 097 * Creates a new component with its channel set to the given 098 * channel and the given file. 099 * 100 * If `update` is `true`, the configuration file is updated 101 * when {@link ConfigurationUpdate} events are received. 102 * 103 * If `watch` is `true`, {@link FileChanged} events are processed 104 * and the configuration file is reloaded when it changes. Note 105 * that the generation of the {@link FileChanged} events must 106 * be configured independently (see {@link FileSystemWatcher}). 107 * 108 * @param componentChannel the channel 109 * @param file the file used to store the configuration 110 * @param update if the configuration file is to be updated 111 * @param watch if {@link FileChanged} events are to be processed 112 * @throws IOException Signals that an I/O exception has occurred. 113 */ 114 public YamlConfigurationStore(Channel componentChannel, File file, 115 boolean update, boolean watch) throws IOException { 116 super(componentChannel, file, update, watch); 117 // Force load of YamlFormat and its registration. Required for OSGi. 118 com.electronwill.nightconfig.yaml.YamlFormat.defaultInstance(); 119 config = FileConfig.builder(file.getAbsolutePath()).sync().concurrent() 120 .build(); 121 config.load(); 122 } 123 124}