/// <summary> /// Creates a new Resource Operation exception /// </summary> /// <param name="resource"></param> /// <param name="operations"></param> /// <param name="rule"></param> /// <param name="bk"></param> public ResourceOperationException(string resource, string operations, ConflictRule rule, BookKeeper bk) : base(String.Format("The operation sequence {1} on the resource \"{0}\" violates the {2}", resource, operations, rule.ToString())) { _resource = resource; _operations = operations; _rule = rule; _bookkeeper = bk; }
private void procAssembly(Assembly asm) { foreach (Object ca in asm.GetCustomAttributes(typeof(ConflictRuleAttribute), true)) { ConflictRuleAttribute cae = (ConflictRuleAttribute)ca; string resname; if ("*".Equals(cae.Resource)) { resname = "*"; } else { short ord; ResourceType rt = BookKeeper.getResourceType(cae.Resource, out ord); if (rt == ResourceType.Custom) { resname = cae.Resource.ToLower(); } else if (rt == ResourceType.ArgumentEntry && ord > -1) { resname = BookKeeper.resourceTypeAsString(rt) + ord; } else { resname = BookKeeper.resourceTypeAsString(rt); } } bool add = true; foreach (ConflictRule cr in rules) { if (cr.Constraint == cae.Constraint && cr.Resource.Equals(resname) && cr.Pattern.Equals(cae.Pattern)) { add = false; break; } } if (add) { try { ConflictRule cr = new ConflictRule(cae.Pattern, resname, cae.Constraint, cae.Message); rules.Add(cr); } catch (Exception e) { #if DEBUG Console.Error.WriteLine("<ResourceValidator> Unable to create conflict rule with pattern {0} for resource {1}", cae.Pattern, resname); Console.Error.WriteLine("<ResourceValidator> " + e.Message); Console.Error.WriteLine("<ResourceValidator> " + e.StackTrace); #endif } } } }
/// <summary> /// Validate the resource operation /// </summary> /// <param name="resource">the resource name</param> /// <param name="altres">alternative name for the resource (used for arg vs arg0)</param> /// <param name="operations"></param> /// <param name="bk"></param> /// <exception cref="ResourceOperationException">Thrown when a rule is violated.</exception> public void validate(string resource, string altres, IList <string> operations, BookKeeper bk) { if (operations.Count == 0) { return; } foreach (ConflictRule rule in rules) { if ("*".Equals(rule.Resource) || resource.Equals(rule.Resource) || (!String.IsNullOrEmpty(altres) && altres.Equals(rule.Resource))) { #if DEBUG Console.Error.WriteLine("<ResourceValidator> Validating resource {0} with rule {1}", resource, rule.ToString()); #endif if (rule.violatesRule(operations)) { StringBuilder sb = new StringBuilder(); sb.Append("["); foreach (String s in operations) { if (sb.Length > 1) { sb.Append(", "); } sb.Append(s); } sb.Append("]"); throw new ResourceOperationException(resource, sb.ToString(), rule, bk); } } } }