216 lines
7.0 KiB
JavaScript
216 lines
7.0 KiB
JavaScript
/*
|
|
* Main features
|
|
*/
|
|
|
|
Options = function (authorAccess, namespace, lifetime, __system__) {
|
|
this.namespace = namespace;
|
|
this.authorAccess = authorAccess;
|
|
this.optionsStorage = this.authorAccess.getOptionsStorage();
|
|
this.lifetime = lifetime;
|
|
this.expiration = Options.getExpiration(lifetime);
|
|
|
|
// Consider namespaces as a SYSTEM option
|
|
if ( __system__ !== true ) {
|
|
this.systemOptions = new Options(authorAccess, Options.SYSTEM, null, true);
|
|
this.__cleanup__();
|
|
var namespaces = JSON.parse(this.systemOptions.getOption(Options.NAMESPACES, '{}'));
|
|
namespaces[ this.namespace] = this.expiration;
|
|
Packages.java.lang.System.out.println('Namespaces update for ' + this.namespace );
|
|
this.systemOptions.setOption(Options.NAMESPACES, JSON.stringify(namespaces));
|
|
}
|
|
}
|
|
|
|
Options.SYSTEM = '__system__';
|
|
Options.NAMESPACES = '__namespaces__';
|
|
Options.HOUSEKEEPING = '__housekeeping__';
|
|
Options.HOUSEKEEPING_PERIOD = 1;
|
|
|
|
Options.getExpiration = function (lifetime) {
|
|
if (lifetime == undefined) {
|
|
return null;
|
|
} else {
|
|
var d = new Date();
|
|
d.setDate(d.getDate() + lifetime);
|
|
return d.toISOString();
|
|
}
|
|
}
|
|
|
|
|
|
Options.prototype.getOption = function (key, defaultValue) {
|
|
this.optionsStorage.setOptionsDoctypePrefix(this.namespace);
|
|
return this.optionsStorage.getOption(key, defaultValue);
|
|
}
|
|
|
|
Options.prototype.setOption = function (key, value) {
|
|
this.optionsStorage.setOptionsDoctypePrefix(Options.SYSTEM);
|
|
var options = JSON.parse(this.optionsStorage.getOption(this.namespace, '{}'));
|
|
options[key] = this.expiration;
|
|
this.optionsStorage.setOption(this.namespace, JSON.stringify(options));
|
|
this.optionsStorage.setOptionsDoctypePrefix(this.namespace);
|
|
this.optionsStorage.setOption(key, value);
|
|
}
|
|
|
|
Options.prototype.deleteOption = function (key) {
|
|
this.optionsStorage.setOptionsDoctypePrefix(Options.SYSTEM);
|
|
var options = JSON.parse(this.optionsStorage.getOption(this.namespace, '{}'));
|
|
delete options[key];
|
|
if (Object.keys(options).length > 0 ){
|
|
this.optionsStorage.setOption(this.namespace, JSON.stringify(options));
|
|
} else {
|
|
this.optionsStorage.setOption(this.namespace, null);
|
|
}
|
|
this.optionsStorage.setOptionsDoctypePrefix(this.namespace);
|
|
this.optionsStorage.setOption(key, null);
|
|
}
|
|
|
|
Options.prototype.getOptionKeys = function () {
|
|
this.optionsStorage.setOptionsDoctypePrefix(Options.SYSTEM);
|
|
return JSON.parse(this.optionsStorage.getOption(this.namespace, '{}'));
|
|
}
|
|
|
|
Options.prototype.getOptions = function () {
|
|
var results = {
|
|
};
|
|
var options = this.getOptionKeys();
|
|
for (var key in options) {
|
|
results[key] = this.getOption(key, '');
|
|
}
|
|
return results;
|
|
}
|
|
|
|
/*
|
|
* Per file extensions
|
|
*
|
|
* Setting a systemID creates a new namespace that concatenates the current namespace
|
|
* and the SHA1 of the document ID.
|
|
*
|
|
* Access to the options of this namespace is done through specific methods.
|
|
*
|
|
* The reverse mapping (SHA1 to system ID) is stored using the __systemIds__ namespace.
|
|
*
|
|
*/
|
|
|
|
Options.SYSTEMIDS = '__systemIds__';
|
|
|
|
Options.prototype.setSystemId = function (systemID, lifetime) {
|
|
if (systemID === undefined) {
|
|
this.systemId = String(this.authorAccess.getDocumentController().getAuthorDocumentNode().getSystemID());
|
|
} else {
|
|
this.systemId = String(systemID);
|
|
}
|
|
if (lifetime === undefined) {
|
|
this.systemIdLifetime = this.lifetime;
|
|
} else {
|
|
this.systemIdLifetime = lifetime;
|
|
}
|
|
var md = java.security.MessageDigest.getInstance("SHA-1");
|
|
var digestBytes = md.digest(java.lang.String(this.systemId).getBytes(java.nio.charset.Charset.forName("UTF-8")));
|
|
this.systemIdDigest = javax.xml.bind.DatatypeConverter.printHexBinary(digestBytes);
|
|
var systemIds = JSON.parse(this.systemOptions.getOption(Options.SYSTEMIDS, '{}'));
|
|
systemIds[ this.systemIdDigest] = {
|
|
date: (new Date()).toISOString(),
|
|
systemId: this.systemId
|
|
}
|
|
this.systemOptions.setOption(Options.SYSTEMIDS, JSON.stringify(systemIds));
|
|
this.systemIdOptions = new Options(this.authorAccess, this.namespace + '.' + this.systemIdDigest, this.systemIdLifetime);
|
|
}
|
|
|
|
Options.prototype.setSystemIdOption = function (key, value) {
|
|
if (this.systemIdOptions == undefined) {
|
|
this.setSystemId();
|
|
}
|
|
this.systemIdOptions.setOption(key, value);
|
|
}
|
|
|
|
Options.prototype.deleteSystemIdOption = function (key) {
|
|
if (this.systemIdOptions == undefined) {
|
|
this.setSystemId();
|
|
}
|
|
this.systemIdOptions.deleteOption(key);
|
|
}
|
|
|
|
Options.prototype.getSystemIdOption = function (key, defaultValue) {
|
|
if (this.systemIdOptions == undefined) {
|
|
this.setSystemId();
|
|
}
|
|
return this.systemIdOptions.getOption(key, defaultValue);
|
|
}
|
|
|
|
Options.prototype.getSystemIdOptions = function () {
|
|
if (this.systemIdOptions == undefined) {
|
|
this.setSystemId();
|
|
}
|
|
return this.systemIdOptions.getOptions();
|
|
}
|
|
|
|
Options.prototype.getSystemOptionKeys = function () {
|
|
if (this.systemIdOptions == undefined) {
|
|
this.setSystemId();
|
|
}
|
|
return this.systemIdOptions.getSystemOptionKeys();
|
|
}
|
|
|
|
Options.prototype.getSystemOptions = function () {
|
|
if (this.systemIdOptions == undefined) {
|
|
this.setSystemId();
|
|
}
|
|
return this.systemIdOptions.getSystemOptions();
|
|
}
|
|
|
|
/*
|
|
*
|
|
* Housekeeping
|
|
*
|
|
*/
|
|
|
|
|
|
Options.prototype.__cleanup__ = function () {
|
|
// TODO: delete systemIds
|
|
|
|
var nextCleanup = this.systemOptions.getOption(Options.HOUSEKEEPING, null);
|
|
Packages.java.lang.System.out.println('nextCleanup: ' + nextCleanup);
|
|
if (nextCleanup == null) {
|
|
this.systemOptions.setOption(Options.HOUSEKEEPING, Options.getExpiration(Options.HOUSEKEEPING_PERIOD));
|
|
} else {
|
|
this.currentDate = (new Date()).toISOString();
|
|
//this.currentDate = Options.getExpiration(10);
|
|
Packages.java.lang.System.out.println('currentDate: ' + this.currentDate);
|
|
if (nextCleanup <= this.currentDate) {
|
|
Packages.java.lang.System.out.println('Time to clean !');
|
|
var namespaces = JSON.parse(this.systemOptions.getOption(Options.NAMESPACES, '{}'));
|
|
for (var name in namespaces) {
|
|
if ( ! this.__cleanupNamespace__(name, namespaces[name])) {
|
|
delete namespaces[name];
|
|
Packages.java.lang.System.out.println('deleting empty namespace ' + name);
|
|
};
|
|
}
|
|
this.systemOptions.setOption(Options.NAMESPACES, JSON.stringify(namespaces));
|
|
this.systemOptions.setOption(Options.HOUSEKEEPING, Options.getExpiration(Options.HOUSEKEEPING_PERIOD));
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
*
|
|
* Cleanup a namespace, return true if there are still keys in the namespace
|
|
*
|
|
*/
|
|
Options.prototype.__cleanupNamespace__ = function (name, namespaceExpiration, currentDate) {
|
|
Packages.java.lang.System.out.println('Cleanup for namespace ' + name + ' / ' + namespaceExpiration);
|
|
var namespaceExpired = namespaceExpiration != null && namespaceExpiration <= this.currentDate;
|
|
var options = new Options(this.authorAccess, name, undefined, true);
|
|
var nsOptions = options.getOptionKeys();
|
|
for (var key in nsOptions) {
|
|
var keyExpiration = nsOptions[key];
|
|
Packages.java.lang.System.out.println(' Cleanup for option ' + key + ' / ' + keyExpiration);
|
|
if ( namespaceExpired || (keyExpiration != null && keyExpiration <= this.currentDate) ) {
|
|
options.deleteOption(key);
|
|
Packages.java.lang.System.out.println(' deleting key ' + key);
|
|
}
|
|
}
|
|
nsOptions = options.getOptionKeys();
|
|
return Object.keys(nsOptions).length > 0;
|
|
|
|
|
|
}
|