public IEnumerable <ScopedPolicy <T> > GetScoped <T> () where T : class, IEquatable <T>, new () { if (policies == null) { yield break; } foreach (KeyValuePair <PolicyKey, object> pinfo in policies) { if (pinfo.Key.PolicyType == typeof(T)) { yield return(new ScopedPolicy <T> ((T)pinfo.Value, pinfo.Key.Scope)); } } T pol = Get <T> (); if (pol != null && !PolicyService.IsUndefinedPolicy(pol)) { yield return(new ScopedPolicy <T> (pol, null)); } }
internal void AddSerializedPolicies(StreamReader reader) { if (policies == null) { policies = new PolicyDictionary(); } foreach (ScopedPolicy policyPair in PolicyService.RawDeserializeXml(reader)) { PolicyKey key = new PolicyKey(policyPair.PolicyType, policyPair.Scope); if (policies.ContainsKey(key)) { throw new InvalidOperationException("Cannot add second policy of type '" + key.ToString() + "' to policy set '" + Id + "'"); } policies[key] = policyPair.Policy; if (!policyPair.SupportsDiffSerialize) { externalPolicies.Add(key); } } }
internal void SaveToFile(StreamWriter writer) { XmlWriterSettings xws = new XmlWriterSettings(); xws.Indent = true; XmlConfigurationWriter cw = new XmlConfigurationWriter(); cw.StoreAllInElements = true; cw.StoreInElementExceptions = new String[] { "scope", "inheritsSet", "inheritsScope" }; using (XmlWriter xw = XmlTextWriter.Create(writer, xws)) { xw.WriteStartElement("PolicySet"); if (policies != null) { foreach (KeyValuePair <PolicyKey, object> policyPair in policies) { cw.Write(xw, PolicyService.DiffSerialize(policyPair.Key.PolicyType, policyPair.Value, policyPair.Key.Scope)); } } xw.WriteEndElement(); } }
internal void LoadFromXml(XmlReader reader) { if (policies == null) { policies = new PolicyDictionary(); } else { policies.Clear(); } reader.MoveToContent(); string str = reader.GetAttribute("name"); if (!string.IsNullOrEmpty(str)) { Name = str; } str = reader.GetAttribute("id"); if (!string.IsNullOrEmpty(str)) { Id = str; } reader.MoveToElement(); //note: can't use AddSerializedPolicies as we want diff serialisation foreach (ScopedPolicy policyPair in PolicyService.DiffDeserializeXml(reader)) { PolicyKey key = new PolicyKey(policyPair.PolicyType, policyPair.Scope); if (policies.ContainsKey(key)) { throw new InvalidOperationException( "Cannot add second policy of type '" + key + "' to policy set '" + Id + "'" ); } policies[key] = policyPair.Policy; } }
internal object Get(Type type) { if (policies != null) { object policy; if (policies.TryGetValue(type, null, out policy)) { if (!PolicyService.IsUndefinedPolicy(policy)) { return(policy); } return(GetDefaultPolicy(type)); } } if (IsRoot) { return(GetDefaultPolicy(type)); } else { return(ParentPolicies.Get(type)); } }
/// <summary> /// The Get methods return policies taking into account inheritance. If a policy /// can't be found it may return null, but never an 'undefined' policy. /// </summary> /// <returns> /// The policy of the given type, or null if not found. /// </returns> public T Get <T> () where T : class, IEquatable <T>, new () { if (policies != null) { object policy; if (policies.TryGetValue(typeof(T), null, out policy)) { if (!PolicyService.IsUndefinedPolicy(policy)) { return((T)policy); } return(GetDefaultPolicy <T> ()); } } if (IsRoot) { return(GetDefaultPolicy <T> ()); } else { return(ParentPolicies.Get <T> ()); } }
internal void LoadFromFile(StreamReader reader) { if (policies == null) { policies = new PolicyDictionary(); } else { policies.Clear(); } //note: can't use AddSerializedPolicies as we want diff serialisation foreach (ScopedPolicy policyPair in PolicyService.DiffDeserializeXml(reader)) { PolicyKey key = new PolicyKey(policyPair.PolicyType, policyPair.Scope); if (policies.ContainsKey(key)) { throw new InvalidOperationException("Cannot add second policy of type '" + key.ToString() + "' to policy set '" + Id + "'"); } policies[key] = policyPair.Policy; } }
internal void SaveToXml(XmlWriter xw) { XmlConfigurationWriter cw = new XmlConfigurationWriter(); cw.StoreAllInElements = true; cw.StoreInElementExceptions = new String[] { "scope", "inheritsSet", "inheritsScope" }; xw.WriteStartElement("PolicySet"); if (!string.IsNullOrEmpty(Name)) { xw.WriteAttributeString("name", Name); } if (!string.IsNullOrEmpty(Id)) { xw.WriteAttributeString("id", Id); } if (policies != null) { foreach (KeyValuePair <PolicyKey, object> policyPair in policies) { cw.Write(xw, PolicyService.DiffSerialize(policyPair.Key.PolicyType, policyPair.Value, policyPair.Key.Scope)); } } xw.WriteEndElement(); }
protected virtual object GetDefaultPolicy(Type type, IEnumerable <string> scopes) { return(PolicyService.GetDefaultPolicy(type, scopes)); }
protected virtual object GetDefaultPolicy(Type type) { return(PolicyService.GetDefaultPolicy(type)); }
protected virtual T GetDefaultPolicy <T> (IEnumerable <string> scopes) where T : class, IEquatable <T>, new () { return(PolicyService.GetDefaultPolicy <T> (scopes)); }
protected virtual T GetDefaultPolicy <T> () where T : class, IEquatable <T>, new () { return(PolicyService.GetDefaultPolicy <T> ()); }