using System;
using System.Collections.Generic;
using System.Text;
namespace ScrewTurn.Wiki.AclEngine {
///
/// Implements a base class for an ACL Storer.
///
public abstract class AclStorerBase : IDisposable {
///
/// Indicates whether the object was disposed.
///
protected bool disposed = false;
///
/// The instance of the ACL Manager to handle.
///
protected IAclManager aclManager;
///
/// The event handler for the event.
///
protected EventHandler aclChangedHandler;
///
/// Initializes a new instance of the abstract class.
///
/// The instance of the ACL Manager to handle.
/// If is null.
public AclStorerBase(IAclManager aclManager) {
if(aclManager == null) throw new ArgumentNullException("aclManager");
this.aclManager = aclManager;
aclChangedHandler = new EventHandler(aclManager_AclChanged);
this.aclManager.AclChanged += aclChangedHandler;
}
///
/// Handles the event.
///
/// The sender.
/// The event arguments.
private void aclManager_AclChanged(object sender, AclChangedEventArgs e) {
if(e.Change == Change.EntryDeleted) DeleteEntries(e.Entries);
else if(e.Change == Change.EntryStored) StoreEntries(e.Entries);
else throw new NotSupportedException("Change type not supported");
}
///
/// Loads data from storage.
///
/// The loaded ACL entries.
protected abstract AclEntry[] LoadDataInternal();
///
/// Deletes some entries.
///
/// The entries to delete.
protected abstract void DeleteEntries(AclEntry[] entries);
///
/// Stores some entries.
///
/// The entries to store.
protected abstract void StoreEntries(AclEntry[] entries);
///
/// Loads the data and injects it in the instance of .
///
public void LoadData() {
lock(this) {
AclEntry[] entries = LoadDataInternal();
aclManager.InitializeData(entries);
}
}
///
/// Gets the instance of the ACL Manager.
///
public IAclManager AclManager {
get {
lock(this) {
return aclManager;
}
}
}
///
/// Disposes the current object.
///
public void Dispose() {
lock(this) {
if(!disposed) {
disposed = true;
aclManager.AclChanged -= aclChangedHandler;
}
}
}
}
}