using System; using System.Collections; using System.Threading; using sscs.common; namespace sscs.cache { class KeyChain : IKeychain { //We can have a Arraylist in case we dont need to look up via SecretID // Hashtable has [SecretID(string), Secret(class)] private Hashtable tSecretList = new Hashtable(); private Hashtable SecretList; // = Hashtable.Synchronized(tSecretList); private string keyChainId; private DateTime createdTime; internal DateTime CreatedTime { get { return createdTime; } set { createdTime = value; } } private DateTime accessedTime; internal DateTime AccessedTime { get { return accessedTime; } set { accessedTime = value; } } private DateTime modifiedTime; internal DateTime ModifiedTime { get { return modifiedTime; } set { modifiedTime = value; } } internal KeyChain() { SecretList = Hashtable.Synchronized(tSecretList); } internal KeyChain(string keyChainId) { SecretList = Hashtable.Synchronized(tSecretList); this.keyChainId = keyChainId; } ~KeyChain() { } public void SetKey(string value) { keyChainId = value; } public void AddSecret(Secret mySecret) { try { mySecret.CreatedTime = DateTime.Now; SecretList.Add((mySecret.GetKey()), mySecret); this.ModifiedTime = DateTime.Now; } catch(Exception e) { CSSSLogger.ExpLog(e.ToString()); throw e; } } public void RemoveSecret(String secretID) { // remove all keyvalues first, as to unlink reverse links try { Secret secret = GetSecret(secretID); secret.RemoveAllKeyValuePairs(this); } catch { } SecretList.Remove(secretID); this.ModifiedTime = DateTime.Now; } public void RemoveAllSecrets() { SecretList.Clear(); } public Secret GetSecret(string secretID) { if(SecretList.ContainsKey(secretID)) { Secret secret = (Secret)SecretList[secretID]; secret.AccessedTime = DateTime.Now; return secret; } else throw new SecretNotFoundException(secretID); } internal string GetKey() { return keyChainId; } internal IEnumerator GetAllSecrets() { return SecretList.GetEnumerator(); } internal int GetNumSecrets() { return SecretList.Count; } internal bool CheckIfSecretExists(string id) { if( SecretList.ContainsKey(id) ) return true; else return false; } } }