/// <summary> /// Load this configuration from xml element. /// </summary> /// <param name="xmlElement"></param> public static GrainTypeConfiguration Load(XmlElement xmlElement) { string fullTypeName = null; bool areDefaults = xmlElement.LocalName == "Defaults"; foreach (XmlAttribute attribute in xmlElement.Attributes) { if (!areDefaults && attribute.LocalName == "Type") { fullTypeName = attribute.Value.Trim(); } else { throw new InvalidOperationException(string.Format("unrecognized attribute {0}", attribute.LocalName)); } } if (!areDefaults) { if (fullTypeName == null) { throw new InvalidOperationException("Type attribute not specified"); } } bool found = false; TimeSpan?collectionAgeLimit = null; foreach (XmlNode node in xmlElement.ChildNodes) { var child = (XmlElement)node; switch (child.LocalName) { default: throw new InvalidOperationException(string.Format("unrecognized XML element {0}", child.LocalName)); case "Deactivation": found = true; collectionAgeLimit = ConfigUtilities.ParseCollectionAgeLimit(child); break; } } if (found) { return(new GrainTypeConfiguration(fullTypeName, collectionAgeLimit)); } throw new InvalidOperationException(string.Format("empty GrainTypeConfiguration for {0}", fullTypeName ?? "defaults")); }
/// <summary> /// Load this configuratin from xml element. /// </summary> /// <param name="xmlElement"></param> /// <param name="logger"></param> public static GrainTypeConfiguration Load(XmlElement xmlElement, TraceLogger logger) { Type type = null; bool areDefaults = xmlElement.LocalName == "Defaults"; foreach (XmlAttribute attribute in xmlElement.Attributes) { if (!areDefaults && attribute.LocalName == "Type") { string fullName = attribute.Value.Trim(); try { type = TypeUtils.ResolveType(fullName); } catch (Exception exception) { logger.Error(ErrorCode.Loader_TypeLoadError, string.Format("Unable to find grain class type specified in configuration ({0}); Ignoring.", fullName), exception); return(null); } } else { throw new InvalidOperationException(string.Format("unrecognized attribute {0}", attribute.LocalName)); } } if (!areDefaults) { if (type == null) { throw new InvalidOperationException("Type attribute not specified"); } // postcondition: returned type must implement IGrain. if (!typeof(IGrain).IsAssignableFrom(type)) { throw new InvalidOperationException(string.Format("Type {0} must implement IGrain to be used in this context", type.FullName)); } // postcondition: returned type must either be an interface or a class. if (!type.IsInterface && !type.IsClass) { throw new InvalidOperationException(string.Format("Type {0} must either be an interface or class.", type.FullName)); } } bool found = false; TimeSpan?collectionAgeLimit = null; foreach (XmlNode node in xmlElement.ChildNodes) { var child = (XmlElement)node; switch (child.LocalName) { default: throw new InvalidOperationException(string.Format("unrecognized XML element {0}", child.LocalName)); case "Deactivation": found = true; collectionAgeLimit = ConfigUtilities.ParseCollectionAgeLimit(child); break; } } if (found) { return new GrainTypeConfiguration(type) { CollectionAgeLimit = collectionAgeLimit, } } ; throw new InvalidOperationException(string.Format("empty GrainTypeConfiguration for {0}", type == null ? "defaults" : type.FullName)); } }