js-options: implementing housekeeping tasks (WIP)

This commit is contained in:
Eric van der Vlist 2021-04-30 21:13:26 +02:00
parent 832ca857b2
commit 429f44f57b
2 changed files with 18 additions and 9 deletions

View File

@ -3,7 +3,7 @@ hello = function () {
// Instanciation // Instanciation
var options = new Options(authorAccess, 'hello', 10); var options = new Options(authorAccess, 'hello', 10);
/*
// Set up a couple of options // Set up a couple of options
options.setOption('Hello', 'World'); options.setOption('Hello', 'World');
options.setOption('See you later', 'aligator'); options.setOption('See you later', 'aligator');
@ -28,4 +28,4 @@ hello = function () {
// Display some file specific properties // Display some file specific properties
Packages.java.lang.System.out.println('options.systemId: ' + options.systemId); Packages.java.lang.System.out.println('options.systemId: ' + options.systemId);
Packages.java.lang.System.out.println('options.systemIdDigest: ' + options.systemIdDigest); Packages.java.lang.System.out.println('options.systemIdDigest: ' + options.systemIdDigest);
} */}

View File

@ -2,7 +2,7 @@
* Main features * Main features
*/ */
Options = function (authorAccess, namespace, lifetime) { Options = function (authorAccess, namespace, lifetime, __system__) {
this.namespace = namespace; this.namespace = namespace;
this.authorAccess = authorAccess; this.authorAccess = authorAccess;
this.optionsStorage = this.authorAccess.getOptionsStorage(); this.optionsStorage = this.authorAccess.getOptionsStorage();
@ -10,10 +10,11 @@ Options = function (authorAccess, namespace, lifetime) {
this.expiration = Options.getExpiration(lifetime); this.expiration = Options.getExpiration(lifetime);
// Consider namespaces as a SYSTEM option // Consider namespaces as a SYSTEM option
if (namespace != Options.SYSTEM) { if ( __system__ !== true ) {
this.systemOptions = new Options(authorAccess, Options.SYSTEM); this.systemOptions = new Options(authorAccess, Options.SYSTEM, null, true);
var namespaces = JSON.parse(this.systemOptions.getOption(Options.NAMESPACES, '{}')); var namespaces = JSON.parse(this.systemOptions.getOption(Options.NAMESPACES, '{}'));
namespaces[ this.namespace] = this.expiration; namespaces[ this.namespace] = this.expiration;
Packages.java.lang.System.out.println('Namespaces update for ' + this.namespace );
this.systemOptions.setOption(Options.NAMESPACES, JSON.stringify(namespaces)); this.systemOptions.setOption(Options.NAMESPACES, JSON.stringify(namespaces));
this.__cleanup__(); this.__cleanup__();
} }
@ -49,7 +50,7 @@ Options.prototype.setOption = function (key, value) {
this.optionsStorage.setOption(key, value); this.optionsStorage.setOption(key, value);
} }
Options.prototype.getOptionsKeys = function () { Options.prototype.getOptionKeys = function () {
this.optionsStorage.setOptionsDoctypePrefix(Options.SYSTEM); this.optionsStorage.setOptionsDoctypePrefix(Options.SYSTEM);
return JSON.parse(this.optionsStorage.getOption(this.namespace, '{}')); return JSON.parse(this.optionsStorage.getOption(this.namespace, '{}'));
} }
@ -57,7 +58,7 @@ Options.prototype.getOptionsKeys = function () {
Options.prototype.getOptions = function () { Options.prototype.getOptions = function () {
var results = { var results = {
}; };
var options = this.getOptionsKeys(); var options = this.getOptionKeys();
for (var key in options) { for (var key in options) {
results[key] = this.getOption(key, ''); results[key] = this.getOption(key, '');
} }
@ -155,12 +156,20 @@ Options.prototype.__cleanup__ = function () {
Packages.java.lang.System.out.println('Time to clean !'); Packages.java.lang.System.out.println('Time to clean !');
var namespaces = JSON.parse(this.systemOptions.getOption(Options.NAMESPACES, '{}')); var namespaces = JSON.parse(this.systemOptions.getOption(Options.NAMESPACES, '{}'));
for (var name in namespaces) { for (var name in namespaces) {
this.__cleanupNamespace__(name, namespaces[name], currentDate); this.__cleanupNamespace__(name, namespaces[name], currentDate); // TODO: return value to say if the NS needsto be deleted
} }
} }
} }
} }
Options.prototype.__cleanupNamespace__ = function (name, expiration, currentDate) { Options.prototype.__cleanupNamespace__ = function (name, expiration, currentDate) {
Packages.java.lang.System.out.println('Cleanup for ' + name) Packages.java.lang.System.out.println('Cleanup for namespace ' + name + ' / ' + expiration);
var options = new Options(this.authorAccess, name, undefined, true);
var nsOptions = options.getOptionKeys();
for (var key in nsOptions) {
var value = nsOptions[key];
Packages.java.lang.System.out.println(' Cleanup for option ' + key + ' / ' + value);
}
} }