-
Notifications
You must be signed in to change notification settings - Fork 16
/
SettingsService.java
462 lines (424 loc) · 13.3 KB
/
SettingsService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* ==================================================================
* SettingsService.java - Mar 12, 2012 4:58:14 PM
*
* Copyright 2007-2012 SolarNetwork.net Dev Team
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
* ==================================================================
*/
package net.solarnetwork.node.settings;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.core.io.Resource;
import net.solarnetwork.node.Constants;
import net.solarnetwork.node.domain.Setting;
import net.solarnetwork.settings.FactorySettingSpecifierProvider;
import net.solarnetwork.settings.SettingSpecifier;
import net.solarnetwork.settings.SettingSpecifierProvider;
import net.solarnetwork.settings.SettingSpecifierProviderFactory;
import net.solarnetwork.util.SearchFilter;
/**
* Service API for settings.
*
* @author matt
* @version 2.3
* @since 2.0
*/
public interface SettingsService {
/**
* The system property for the setting resource directory.
*
* @since 1.4
*/
String SYSTEM_PROP_SETTING_RESOURCE_DIR = "sn.rsrc";
/**
* The default setting resource directory, if
* {@link #SYSTEM_PROP_SETTING_RESOURCE_DIR} is not defined.
*
* @since 1.4
*/
String DEFAULT_SETTING_RESOURCE_DIR = "conf/rsrc";
/**
* Get the path to the setting resource directory.
*
* <p>
* This is the directory where external setting resources can be persisted.
* If the system property {@link #SYSTEM_PROP_SETTING_RESOURCE_DIR} is
* defined as an absolute path, that path is used directly. Otherwise
* {@link #SYSTEM_PROP_SETTING_RESOURCE_DIR} is treated as a path relative
* to {@link Constants#solarNodeHome()}, defaulting to
* {@link #DEFAULT_SETTING_RESOURCE_DIR} if not defined.
* </p>
*
* @return the setting resource directory, never {@literal null}
* @since 1.4
*/
static Path settingResourceDirectory() {
Path rsrcDir = Paths
.get(System.getProperty(SYSTEM_PROP_SETTING_RESOURCE_DIR, DEFAULT_SETTING_RESOURCE_DIR));
if ( rsrcDir.isAbsolute() ) {
return rsrcDir;
}
Path dir = Paths.get(Constants.solarNodeHome());
return dir.resolve(rsrcDir);
}
/**
* The instruction topic for a request to update (create or change) a
* setting value.
*
* @since 1.3
*/
String TOPIC_UPDATE_SETTING = "UpdateSetting";
/**
* The instruction parameter for setting key.
*
* @since 1.3
*/
String PARAM_UPDATE_SETTING_KEY = "key";
/**
* The instruction parameter for setting type.
*
* @since 1.3
*/
String PARAM_UPDATE_SETTING_TYPE = "type";
/**
* The instruction parameter for setting value.
*
* @since 1.3
*/
String PARAM_UPDATE_SETTING_VALUE = "value";
/**
* The instruction parameter for setting flags.
*
* @since 1.3
*/
String PARAM_UPDATE_SETTING_FLAGS = "flags";
/**
* Get a list of all possible non-factory setting providers.
*
* @return list of setting providers (never {@literal null})
*/
List<SettingSpecifierProvider> getProviders();
/**
* Get a list of all possible non-factory setting providers.
*
* @param filter
* the search filter; the filter is applied to the factory service
* properties
* @return list of setting providers (never {@literal null})
* @since 1.6
*/
List<SettingSpecifierProvider> getProviders(SearchFilter filter);
/**
* Get a list of all possible setting provider factories.
*
* @return list of setting provider factories (never {@literal null})
*/
List<SettingSpecifierProviderFactory> getProviderFactories();
/**
* Get a filtered list of all possible setting provider factories.
*
* @param filter
* the search filter; the filter is applied to the factory service
* properties
* @return list of setting provider factories (never {@literal null})
* @since 1.6
*/
List<SettingSpecifierProviderFactory> getProviderFactories(SearchFilter filter);
/**
* Get a specific factory for a given UID.
*
* @param factoryUid
* the factory UID to get the providers for
*
* @return the factory, or {@literal null} if not available
*/
SettingSpecifierProviderFactory getProviderFactory(String factoryUid);
/**
* Enable a provider factory instance.
*
* @param factoryUid
* the factory UID to enable
* @param instanceUid
* the instance UID to enable
* @since 2.1
*/
void enableProviderFactoryInstance(String factoryUid, String instanceUid);
/**
* Disable a provider factory instance.
*
* @param factoryUid
* the factory UID to disable
* @param instanceUid
* the instance UID to disable
* @since 2.1
*/
void disableProviderFactoryInstance(String factoryUid, String instanceUid);
/**
* Add a new factory instance, and return the new instance ID.
*
* @param factoryUid
* the factory UID to create the new instance for
* @return the new instance ID
*/
String addProviderFactoryInstance(String factoryUid);
/**
* Add a new factory instance, and return the new instance ID.
*
* <p>
* If {@code instanceUid} is provided and an instance already exists for
* that ID, this will have the same effect as if
* {@link #enableProviderFactoryInstance(String, String)} was called.
* </p>
*
* @param factoryUid
* the factory UID to create the new instance for
* @param instanceUid
* the instance UID to create the new instance for, or
* {@literal null} to automatically assign one
* @return the new instance ID
* @since 2.1
*/
default String addProviderFactoryInstance(String factoryUid, String instanceUid) {
return addProviderFactoryInstance(factoryUid);
}
/**
* Delete an existing factory instance.
*
* @param factoryUid
* the factory UID to create the new instance for
* @param instanceUid
* the instance UID to create the new instance for
*/
void deleteProviderFactoryInstance(String factoryUid, String instanceUid);
/**
* Reset an existing factory instance to its default values.
*
* @param factoryUid
* the factory UID to reset
* @param instanceUid
* the instance UID to reset
*/
void resetProviderFactoryInstance(String factoryUid, String instanceUid);
/**
* Reset and delete factory instances.
*
* @param factoryUid
* the factory UID of the instances to remove
* @param instanceUids
* the instance UID to remove
* @since 2.2
*/
default void removeProviderFactoryInstances(String factoryUid, Set<String> instanceUids) {
for ( String instanceUid : instanceUids ) {
resetProviderFactoryInstance(factoryUid, instanceUid);
deleteProviderFactoryInstance(factoryUid, instanceUid);
}
}
/**
* Get all possible setting providers for a specific factory UID, grouped by
* instance ID.
*
* @param factoryUid
* the factory UID to get the providers for
*
* @return mapping of instance IDs to associated setting providers (never
* {@literal null})
*/
Map<String, FactorySettingSpecifierProvider> getProvidersForFactory(String factoryUid);
/**
* Get the current value of a setting.
*
* @param provider
* the provider of the setting
* @param setting
* the setting
* @return the current setting value
*/
Object getSettingValue(SettingSpecifierProvider provider, SettingSpecifier setting);
/**
* Update setting values.
*
* @param command
* the update command
*/
void updateSettings(SettingsCommand command);
/**
* Get a list of all available setting resource handlers.
*
* @return the handlers, never {@literal null}
* @since 2.1
*/
List<SettingResourceHandler> getSettingResourceHandlers();
/**
* Get a setting resource handler.
*
* @param handlerKey
* the ID if the {@link SettingResourceHandler} to get
* @param instanceKey
* if {@code handlerKey} is a factory, the ID of the instance to
* import the resources for, otherwise {@literal null}
* @return the resource handler, or {@literal null} if not available
* @since 1.4
*/
SettingResourceHandler getSettingResourceHandler(String handlerKey, String instanceKey);
/**
* Get current setting resources.
*
* @param handlerKey
* the ID if the {@link SettingResourceHandler} to get the resources
* from
* @param instanceKey
* if {@code handlerKey} is a factory, the ID of the instance to
* import the resources for, otherwise {@literal null}
* @param settingKey
* the setting ID to get the resources for
* @return the resources (never {@literal null})
* @throws IOException
* if any IO error occurs
* @since 1.4
*/
Iterable<Resource> getSettingResources(String handlerKey, String instanceKey, String settingKey)
throws IOException;
/**
* Import setting resources.
*
* <p>
* The setting resources will be persisted by this service, and should then
* also be passed to the appropriate {@link SettingResourceHandler} for the
* given {@code handlerKey} and {@code instanceKey} values by invoking
* {@link SettingResourceHandler#applySettingResources(String, Iterable)}
* with {@code settingKey} and the persisted resources.
* </p>
*
* @param handlerKey
* the ID if the {@link SettingResourceHandler} to import to
* @param instanceKey
* if {@code handlerKey} is a factory, the ID of the instance to
* import the resources for, otherwise {@literal null}
* @param settingKey
* the setting ID to import the resources for
* @param resources
* the resources to import
* @throws IOException
* if any IO error occurs
* @since 1.4
*/
void importSettingResources(String handlerKey, String instanceKey, String settingKey,
Iterable<Resource> resources) throws IOException;
/**
* Remove setting resources.
*
* <p>
* The persisted setting resources will be removed by this service.
* </p>
*
* @param handlerKey
* the ID if the {@link SettingResourceHandler} to remove from
* @param instanceKey
* if {@code handlerKey} is a factory, the ID of the instance to
* remove the resources for, otherwise {@literal null}
* @param settingKey
* the setting ID to remove the resources for
* @param resources
* the resources to remove
* @throws IOException
* if any IO error occurs
* @since 2.3
*/
void removeSettingResources(String handlerKey, String instanceKey, String settingKey,
Iterable<Resource> resources) throws IOException;
/**
* Export all settings as CSV formatted text.
*
* @param out
* the output stream
* @throws IOException
* if any IO error occurs
*/
void exportSettingsCSV(Writer out) throws IOException;
/**
* Get a list of all available settings for a given factory and/or instance
* ID.
*
* @param factoryUid
* the UID of the factory to get, or {@literal null} for a
* non-factory component
* @param instanceUid
* if UID of the instance
* @return the available settings, never {@literal null}
* @throws IllegalArgumentException
* if both arguments are {@literal null}
* @since 2.1
*/
List<Setting> getSettings(String factoryUid, String instanceUid);
/**
* Import all settings from a CSV formatted text stream.
*
* @param in
* the input stream
* @throws IOException
* if any IO error occurs
*/
void importSettingsCSV(Reader in) throws IOException;
/**
* Import all settings from a CSV formatted text stream, with options.
*
* @param in
* The input stream to import.
* @param options
* The import options.
* @throws IOException
* if any IO error occurs
* @since 1.2
*/
void importSettingsCSV(Reader in, SettingsImportOptions options) throws IOException;
/**
* Create a backup of all settings, and return a backup object if the backup
* was performed.
*
* <p>
* A new backup need not be created if the settings are unchanged. In that
* case, or if this method does not create a backup for any reason, this
* method should return {@literal null}.
* </p>
*
* @return the backup object, or {@literal null} if no backup created
*/
SettingsBackup backupSettings();
/**
* Get a collection of all known settings backups.
*
* @return the backups, never {@literal null}
*/
Collection<SettingsBackup> getAvailableBackups();
/**
* Get a {@link Reader} to the backup data for a given SettingsBackup
* object.
*
* @param backup
* the backup to get the Reader for
* @return the Reader, or {@literal null} if the backup cannot be found
*/
Reader getReaderForBackup(SettingsBackup backup);
}