Example #1
0
        internal PolicyKey[] AddSerializedPolicies(StreamReader reader)
        {
            if (policies == null)
            {
                policies = new PolicyDictionary();
            }
            var keys = new List <PolicyKey> ();

            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 + "'");
                }
                keys.Add(key);
                policies[key] = policyPair.Policy;
                if (!policyPair.SupportsDiffSerialize)
                {
                    externalPolicies.Add(key);
                }
            }
            return(keys.ToArray());
        }
Example #2
0
        void ICustomDataItem.Deserialize(ITypeSerializer handler, DataCollection data)
        {
            if (data.Count == 0)
            {
                return;
            }

            policies = new PolicyDictionary();
            foreach (DataNode node in data)
            {
                try {
                    if (!(node is DataItem))
                    {
                        continue;
                    }
                    ScopedPolicy val = PolicyService.DiffDeserialize((DataItem)node);
                    policies.Add(val);
                } catch (Exception ex) {
                    if (handler.SerializationContext.ProgressMonitor != null)
                    {
                        handler.SerializationContext.ProgressMonitor.ReportError(ex.Message, ex);
                    }
                    else
                    {
                        LoggingService.LogError(ex.Message, ex);
                    }
                }
            }
        }
Example #3
0
        /// <summary>
        /// Import the policies defined by another policy container
        /// </summary>
        /// <param name='source'>
        /// The policy container to be imported
        /// </param>
        /// <param name='includeParentPolicies'>
        /// If <c>true</c>, policies defined by all ancestors of polContainer will also
        /// be imported
        /// </param>
        /// <remarks>
        /// This method adds or replaces policies defined in the source container into
        /// this container. Policies in this container which are not defined in the source container
        /// are not modified or removed.
        /// </remarks>
        public void Import(PolicyContainer source, bool includeParentPolicies)
        {
            if (includeParentPolicies && source.ParentPolicies != null)
            {
                Import(source.ParentPolicies, true);
            }

            if (source.policies == null && policies == null)
            {
                return;
            }

            // Add and update policies

            if (source.policies != null)
            {
                foreach (KeyValuePair <PolicyKey, object> p in source.policies)
                {
                    object oldVal;
                    if (policies == null || !policies.TryGetValue(p.Key, out oldVal) || oldVal == null || !oldVal.Equals(p.Value))
                    {
                        if (policies == null)
                        {
                            policies = new PolicyDictionary();
                        }
                        policies [p.Key] = p.Value;
                        OnPolicyChanged(p.Key.PolicyType, p.Key.Scope);
                    }
                }
            }
        }
Example #4
0
        /// <summary>
        /// Removes all policies defined in this container
        /// </summary>
        public void Clear()
        {
            PolicyDictionary oldPolicies = policies;

            policies = null;
            foreach (PolicyKey pk in oldPolicies.Keys)
            {
                OnPolicyChanged(pk.PolicyType, pk.Scope);
            }
        }
Example #5
0
        internal void InternalSet(Type t, string scope, object ob)
        {
            PolicyKey key = new PolicyKey(t, scope);

            if (policies == null)
            {
                policies = new PolicyDictionary();
            }
            policies[key] = ob;
            OnPolicyChanged(key.PolicyType, key.Scope);
        }
Example #6
0
 internal void RemoveAll(PolicyKey[] keys)
 {
     if (policies != null)
     {
         foreach (var k in keys)
         {
             policies.Remove(k);
             OnPolicyChanged(k.PolicyType, k.Scope);
         }
         if (policies.Count == 0)
         {
             policies = null;
         }
     }
 }
Example #7
0
 internal bool InternalRemove(Type type, string scope)
 {
     if (policies != null)
     {
         if (policies.Remove(new PolicyKey(type, scope)))
         {
             OnPolicyChanged(type, scope);
             if (policies.Count == 0)
             {
                 policies = null;
             }
             return(true);
         }
     }
     return(false);
 }
Example #8
0
 internal void RemoveAll(Type type)
 {
     if (policies != null)
     {
         foreach (var p in policies.ToArray())
         {
             if (p.Key.PolicyType == type)
             {
                 policies.Remove(p.Key);
                 OnPolicyChanged(type, p.Key.Scope);
             }
         }
         if (policies.Count == 0)
         {
             policies = null;
         }
     }
 }
Example #9
0
        /// <summary>
        /// Copies the policies defined in another container
        /// </summary>
        /// <param name='other'>
        /// A policy container from which to copy the policies
        /// </param>
        /// <remarks>
        /// Policies of this container are removed or replaced by policies defined in the
        /// provided container.
        /// </remarks>
        public void CopyFrom(PolicyContainer other)
        {
            if (other.policies == null && policies == null)
            {
                return;
            }

            // Add and update policies

            if (other.policies != null)
            {
                foreach (KeyValuePair <PolicyKey, object> p in other.policies)
                {
                    object oldVal;
                    if (policies == null || !policies.TryGetValue(p.Key, out oldVal) || oldVal == null || !oldVal.Equals(p.Value))
                    {
                        if (policies == null)
                        {
                            policies = new PolicyDictionary();
                        }
                        policies [p.Key] = p.Value;
                        OnPolicyChanged(p.Key.PolicyType, p.Key.Scope);
                    }
                }
            }

            // Remove policies

            if (policies != null)
            {
                foreach (PolicyKey k in policies.Keys.ToArray())
                {
                    if (other.policies == null || !other.policies.ContainsKey(k))
                    {
                        policies.Remove(k);
                        OnPolicyChanged(k.PolicyType, k.Scope);
                    }
                }
            }
        }
Example #10
0
        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;
            }
        }
Example #11
0
        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;
            }
        }
Example #12
0
        public void Set <T> (T value, string scope) where T : class, IEquatable <T>, new ()
        {
            CheckReadOnly();
            PolicyKey key = new PolicyKey(typeof(T), scope);

            System.Diagnostics.Debug.Assert(key.Scope == scope);

            if (policies == null)
            {
                policies = new PolicyDictionary();
            }
            else if (value != null)
            {
                object oldVal = null;
                policies.TryGetValue(key, out oldVal);
                if (oldVal != null && ((IEquatable <T>)oldVal).Equals(value))
                {
                    return;
                }
            }

            policies[key] = value;
            OnPolicyChanged(key.PolicyType, key.Scope);
        }