/// <summary> /// A method to set Instruction Guid IDs to Empty without Deserializing the collection if possible /// </summary> public void ClearInstructionIDs() { lock (this) { if (_Instructions != null) { foreach (Instruction i in _Instructions) { i.ID = Guid.Empty; } } if (_InstructionDocument != null) { foreach (XElement n in _InstructionDocument.Root.Elements()) { n.Elements().FirstOrDefault(i => i.Name.LocalName == "ID").Value = Guid.Empty.ToString(); } } if (_SerializationSourceInstructionDocument != null) { _InstructionDocument = null; StemStr str = new StemStr(_SerializationSourceInstructionDocument, _SerializationSourceInstructionDocument.Length); int index = 0; while ((index = str.IndexOf(VDClose, index)) > -1) { index += VDClose.Length; int i = str.IndexOf(IDOpen, index); if (i == -1) { continue; } index = i; index += IDOpen.Length; str.Overwrite(Guid.Empty.ToString(), index); } _SerializationSourceInstructionDocument = str.ToString(); } } }
/// <summary> /// A method to randomize Instruction Guid IDs without Deserializing the collection if possible /// </summary> public void RandomizeInstructionIDs() { lock (this) { List <Guid> ids = new List <Guid>(); if (_Instructions != null) { foreach (Instruction i in _Instructions) { i.ID = Guid.NewGuid(); ids.Add(i.ID); } } if (_InstructionDocument != null) { int x = 0; foreach (XElement n in _InstructionDocument.Root.Elements()) { if (ids.Count < x + 1) { ids.Add(Guid.NewGuid()); } n.Elements().FirstOrDefault(i => i.Name.LocalName == "ID").Value = ids[x].ToString(); x++; } } if (_SerializationSourceInstructionDocument != null) { _InstructionDocument = null; StemStr str = new StemStr(_SerializationSourceInstructionDocument, _SerializationSourceInstructionDocument.Length); int index = 0; int x = 0; while ((index = str.IndexOf(VDClose, index)) > -1) { index += VDClose.Length; int i = str.IndexOf(IDOpen, index); if (i == -1) { continue; } index = i; index += IDOpen.Length; if (ids.Count < x + 1) { ids.Add(Guid.NewGuid()); } str.Overwrite(ids[x].ToString(), index); x++; } _SerializationSourceInstructionDocument = str.ToString(); } } }
public override string Serialize() { StemStr str = null; if (_ReuseQueue.Count > 0) { lock (_ReuseQueue) if (_ReuseQueue.Count > 0) { str = _ReuseQueue.Dequeue(); } } if (str == null) { str = new StemStr("", 10000); } try { str.Reset("<InstructionSet xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>"); str.Append(VersionDescriptor.Serialize()); str.Append("\r\n <ID>" + ID.ToString() + "</ID>"); str.Append("\r\n <BranchIP>" + System.Security.SecurityElement.Escape(BranchIP) + "</BranchIP>"); str.Append("\r\n <Assigned>" + XmlConvert.ToString(Assigned, XmlDateTimeSerializationMode.Utc) + "</Assigned>"); str.Append("\r\n <Received>" + XmlConvert.ToString(Received, XmlDateTimeSerializationMode.Utc) + "</Received>"); str.Append("\r\n <Started>" + XmlConvert.ToString(Started, XmlDateTimeSerializationMode.Utc) + "</Started>"); str.Append("\r\n <Completed>" + XmlConvert.ToString(Completed, XmlDateTimeSerializationMode.Utc) + "</Completed>"); str.Append("\r\n <DeploymentControllerID>" + DeploymentControllerID + "</DeploymentControllerID>"); str.Append("\r\n <DeploymentManagerIP>" + DeploymentManagerIP + "</DeploymentManagerIP>"); str.Append("\r\n <DeploymentController>" + DeploymentController + "</DeploymentController>"); str.Append("\r\n <InstructionSetTemplate>" + System.Security.SecurityElement.Escape(InstructionSetTemplate) + "</InstructionSetTemplate>"); str.Append("\r\n <InitiationSource>" + System.Security.SecurityElement.Escape(InitiationSource) + "</InitiationSource>"); str.Append("\r\n <ProcessName>" + System.Security.SecurityElement.Escape(ProcessName) + "</ProcessName>"); str.Append("\r\n <ContinuousExecution>" + XmlConvert.ToString(ContinuousExecution) + "</ContinuousExecution>"); str.Append("\r\n <ContinuousExecutionInterval>" + XmlConvert.ToString(ContinuousExecutionInterval) + "</ContinuousExecutionInterval>"); str.Append("\r\n <ExecuteStaticInSandboxes>" + XmlConvert.ToString(ExecuteStaticInSandboxes) + "</ExecuteStaticInSandboxes>"); str.Append("\r\n <CachePostMortem>" + XmlConvert.ToString(CachePostMortem) + "</CachePostMortem>"); str.Append("\r\n <DeploymentManagerRelease>" + System.Security.SecurityElement.Escape(DeploymentManagerRelease) + "</DeploymentManagerRelease>"); if (PostMortemMetaData != null && PostMortemMetaData.Count > 0) { XDocument d = XDocument.Load(new System.Xml.XmlTextReader(new System.IO.StringReader(STEM.Sys.Serializable.Serialize(PostMortemMetaData)))); d.Root.Name = "PostMortemMetaData"; str.Append("\r\n " + d.ToString()); } else { str.Append("\r\n <PostMortemMetaData />"); } str.Append("\r\n <InstructionsXml>" + SerializationSourceInstructionDocument + "</InstructionsXml>"); str.Append("\r\n</InstructionSet>"); return(str.ToString()); } finally { if (_ReuseQueue.Count < 1000) { lock (_ReuseQueue) _ReuseQueue.Enqueue(str); } } }
/// <summary> /// /// </summary> /// <param name="target"></param> /// <param name="kvp"></param> /// <param name="escapeForXml"></param> /// <returns></returns> public static string ApplyKVP(string target, System.Collections.Generic.Dictionary <string, string> kvp, bool escapeForXml, StemStr reuseStemStr = null) { if (target == null) { throw new ArgumentNullException(nameof(target)); } if (kvp == null) { throw new ArgumentNullException(nameof(kvp)); } IEnumerable <string> keysWithTokensInValues = kvp.Keys.Where(i => kvp[i] != null && kvp[i].Contains("[") && kvp[i].Contains("]")); foreach (string key in keysWithTokensInValues) { if (kvp[key].IndexOf(key, StringComparison.InvariantCultureIgnoreCase) >= 0) { throw new Exception("Map error - key in value (" + key + ")"); } } // Check for cross collision foreach (string key in keysWithTokensInValues) { foreach (string k in keysWithTokensInValues) { if (kvp[key].IndexOf(k, StringComparison.InvariantCultureIgnoreCase) >= 0 && kvp[k].IndexOf(key, StringComparison.InvariantCultureIgnoreCase) >= 0) { throw new Exception("Map error - cross key reference (" + key + ", " + k + ")"); } } } DateTime now = DateTime.UtcNow; List <string> keysPresent = new List <string>(); StemStr ss = reuseStemStr; if (ss == null) { ss = new StemStr(target, target.Length * 2); } else { ss.Reset(target); } List <string> keys = new List <string>(); while (true) { bool matchFound = false; int masterIndex = 0; keys.Clear(); while ((masterIndex = ss.IndexOf("[", masterIndex)) >= 0) { if (ss.IndexOf("]", masterIndex) < 0) { break; } string k = ss.Substring(masterIndex, ss.IndexOf("]", masterIndex) - masterIndex + 1); if (k.Length == 0) { masterIndex++; continue; } masterIndex += k.Length; if (keys.Contains(k)) { continue; } keys.Add(k); string v = null; if (kvp.ContainsKey(k)) { v = kvp[k]; } else { v = kvp.Where(i => i.Key.Equals(k, StringComparison.InvariantCultureIgnoreCase)).Select(i => i.Value).FirstOrDefault(); } if (v == null) { continue; } v = v.Trim(); if (k.Equals("[NewGuid]", StringComparison.InvariantCultureIgnoreCase)) { matchFound = true; if (target == k) { return(Guid.NewGuid().ToString()); } string[] parts = target.Split(new string[] { k }, StringSplitOptions.None); string tmp = parts[0]; for (int i = 1; i < parts.Length; i++) { tmp = tmp + Guid.NewGuid().ToString() + parts[i]; } ss.Reset(tmp); } else { if (v.Equals("Reserved", StringComparison.InvariantCultureIgnoreCase)) { continue; } if (v.ToUpper().Contains(k.ToUpper())) { throw new Exception("Key in value! (" + k + ", " + v + ")"); } if (k.Equals("[UtcNow]", StringComparison.InvariantCultureIgnoreCase)) { v = now.ToString(v, System.Globalization.CultureInfo.CurrentCulture); } if (escapeForXml) { v = System.Security.SecurityElement.Escape(v); } matchFound = true; ss.Replace(k, v, 0); } } if (!matchFound) { return(ss.ToString()); } } }
/// <summary> /// Customize a target string by applying the TemplateKVP map to the target /// </summary> /// <param name="target">The string to which the map is to be applied</param> /// <param name="map">The TemplateKVP map used to modify the target</param> /// <param name="cloneMap">Should the map be cloned as it will be modified in this method</param> /// <returns>The modified target</returns> public virtual string ApplyKVP(string target, System.Collections.Generic.Dictionary <string, string> map, bool cloneMap = true) { if (map == null) { throw new ArgumentNullException(nameof(map)); } if (String.IsNullOrEmpty(target)) { throw new ArgumentNullException(nameof(target)); } System.Collections.Generic.Dictionary <string, string> kvp = map; if (cloneMap) { kvp = new System.Collections.Generic.Dictionary <string, string>(map); } kvp["[NewGuid]"] = "Reserved"; if (!kvp.ContainsKey("[UtcNow]")) { kvp["[UtcNow]"] = TemplateKVP["[UtcNow]"]; } kvp["[DeploymentControllerName]"] = DeploymentControllerName; kvp["[DeploymentControllerID]"] = DeploymentControllerID; kvp["[ControllerVersionNumber]"] = VersionNumber; kvp["[SwitchboardRowID]"] = SwitchboardRowID; kvp["[DeploymentManagerIP]"] = STEM.Sys.IO.Net.MachineIP(); if (String.IsNullOrEmpty(OverrideInstructionSetProcessName)) { kvp["[InstructionSetProcessName]"] = STEM.Sys.IO.Path.GetFileNameWithoutExtension(InstructionSetTemplate); } else { kvp["[InstructionSetProcessName]"] = OverrideInstructionSetProcessName; } kvp["[PollerSourceString]"] = PollerSourceString; kvp["[PollerFileFilter]"] = PollerFileFilter; kvp["[PollerDirectoryFilter]"] = PollerDirectoryFilter; kvp["[PollerRecurseSetting]"] = PollerRecurseSetting.ToString(System.Globalization.CultureInfo.CurrentCulture); kvp["[PingableSourceSetting]"] = PollerRecurseSetting.ToString(System.Globalization.CultureInfo.CurrentCulture); if (_ReuseStemStr == null) { lock (this) if (_ReuseStemStr == null) { _ReuseStemStr = new StemStr(target, target.Length * 2); } } string xml = null; lock (_ReuseStemStr) xml = KVPMapUtils.ApplyKVP(target, kvp, true, _ReuseStemStr); return(xml); }