// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.IO; using System.Configuration; namespace Spludlow { public class Config { public static string ProgramData { get { return Spludlow.Config.Get("Spludlow.ProgramData"); } } public static bool StandAlone { get { return Spludlow.Config.Get("Spludlow.ProgramData", true) == null; } } static Config() { } public static string ConnectionString(string key) { return Get("Spludlow.ConnectionString." + key); } public static string HostAddress(string key) { return HostAddress(key, false); } public static string HostAddress(string key, bool clean) { key = Host(key); string address = Get("Spludlow.Address." + key); if (clean == true && address.StartsWith("@") == true) address = address.Substring(1); return address; } public static string Host(string key) { return Host(key, false); } public static string Host(string key, bool optional) { return Get("Spludlow.Host." + key, optional); } public static string RemotingAddress(string key) { return Get("Spludlow.Remotings." + key); } public static bool GetBoolean(string key) { return GetBoolean(key, false); } public static bool GetBoolean(string key, bool optional) { string data = Get(key, optional); return Boolean.Parse(data); } public static string Get(string key) { return Get(key, false); } public static string Get(string key, bool optional) { string data = ConfigurationManager.AppSettings[key]; if (optional == false && data == null) throw new ApplicationException("Config; Item not found:\t" + key); return data; } public static string NetworkId(string hostId) { foreach (string networkId in Networks()) { foreach (string host in Spludlow.Text.Split(Get("Spludlow.NetworkHosts." + networkId), ',')) { if (host == hostId) return networkId; } } throw new ApplicationException("Config; NetworkId for host not found:\t" + hostId); } public static string OrganizationId(string hostId) { string networkId = NetworkId(hostId); foreach (string organizationId in Organizations()) { foreach (string matchNetworkId in Spludlow.Text.Split(Get("Spludlow.OrganizationNetworks." + organizationId), ',')) { if (networkId == matchNetworkId) return organizationId; } } throw new ApplicationException("Config; OrganizationId for host not found:\t" + hostId); } public static string[] Hosts(string networkId) { return Spludlow.Text.Split(Get("Spludlow.NetworkHosts." + networkId), ','); } public static string[] Hosts() { return Spludlow.Text.Split(Get("Spludlow.Hosts"), ','); } public static string[] Networks(string organizationId) { return Spludlow.Text.Split(Get("Spludlow.OrganizationNetworks." + organizationId), ','); } public static string[] Networks() { return Spludlow.Text.Split(Get("Spludlow.Networks"), ','); } public static string[] Organizations() { return Spludlow.Text.Split(Get("Spludlow.Organizations"), ','); } public static string[] DevelopSourceHosts() { return Spludlow.Text.Split(Get("Spludlow.DevelopSources"), ','); } public static string Router(string networkId) { return Get("Spludlow.NetworkRouter." + networkId); } public static string ApplicationDirectory(string applicationId) { return ApplicationDirectory(applicationId, false); } public static string ApplicationDirectory(string applicationId, bool optional) { DataTable table = Applications().Tables[0]; DataRow row = table.Rows.Find(applicationId); if (row == null) { if (optional == false) throw new ApplicationException("Config, applicationId not found:\t" + applicationId); else return null; } return (string)row["Directory"]; } public static DataTable Applications(string host) { DataSet dataSet = (DataSet)Spludlow.Call.Now(host, "Spludlow", "Spludlow.Config", "Applications"); return dataSet.Tables[0]; } public static DataRow Application(string applicationId) { return Application(applicationId, false); } public static DataRow Application(string applicationId, bool optional) { DataTable table = Applications().Tables[0]; DataRow row = table.Rows.Find(applicationId); if (row == null && optional == false) throw new ApplicationException("Application not found in config file, ApplicationId: " + applicationId + ", host: " + Environment.MachineName); return row; } public static DataSet Applications() { return ApplicationsAltProgData(Spludlow.Config.ProgramData); } public static DataSet ApplicationsAltProgData(string programData) { string filename = programData + @"\Config\Applications.txt"; if (File.Exists(filename) == false) throw new ApplicationException("Applications.txt file does not exist: " + filename); DataTable table = Spludlow.Data.TextTable.ReadFile(filename, true); table.Columns.Add("PortNumber", typeof(int)); foreach (DataRow row in table.Rows) { if (row.IsNull("Hosted") == true) continue; string hosted = (string)row["Hosted"]; int index = hosted.IndexOf(":"); if (index != -1) { row["PortNumber"] = Int32.Parse(hosted.Substring(index + 1)); row["Hosted"] = hosted.Substring(0, index); } } DataSet dataSet = new DataSet(); dataSet.Tables.Add(table); return dataSet; } public static string[][] WCFServerAddresses(string host) { return (string[][])Spludlow.Call.Now(host, "Spludlow", "Spludlow.Config", "WCFServerAddresses"); } public static string[][] WCFServerAddresses() { return WCFServerAddressesAltProgData(Spludlow.Config.ProgramData); } public static string[][] WCFServerAddressesAltProgData(string programData) { List bindings = new List(); DataTable applicationsTable = Spludlow.Config.ApplicationsAltProgData(programData).Tables[0]; string host = Environment.MachineName.ToLower(); foreach (DataRow applicationRow in applicationsTable.Rows) { if (applicationRow.IsNull("PortNumber") == true) continue; string applicationId = (string)applicationRow["ApplicationId"]; int portNumber = (int)applicationRow["PortNumber"]; string remoteAddress = Spludlow.ServiceModels.Address(host, "Spludlow.Service", portNumber); bindings.Add(new string[] { applicationId, remoteAddress }); DataTable serversTable = Spludlow.Service.LoadServiceConfigAltProgData(applicationId, programData).Tables["Servers"]; foreach (DataRow serverRow in serversTable.Rows) { string key = (string)serverRow["Key"]; string type = (string)serverRow["Type"]; portNumber = (int)serverRow["Port"]; remoteAddress = Spludlow.ServiceModels.Address(host, type, portNumber); bindings.Add(new string[] { key, remoteAddress }); } } return bindings.ToArray(); } public static DataTable DevelopSources(string host) { if (host == null) return DevelopSourcesEmpty().Tables[0]; DataSet dataSet = (DataSet)Spludlow.Call.Now(host, "Spludlow", "Spludlow.Config", "DevelopSources"); return dataSet.Tables[0]; } public static DataSet DevelopSources() { string filename = ProgramData + @"\Config\DevelopSources.txt"; DataTable table; if (File.Exists(filename) == false) { table = DevelopSourcesEmpty().Tables[0]; Spludlow.Data.TextTable.Write(filename, table); Spludlow.Log.Warning("Config, New default DevelopementSources file created:\t" + filename); } table = Spludlow.Data.TextTable.ReadFile(filename, true); DataSet dataSet = new DataSet(); dataSet.Tables.Add(table); return dataSet; } public static DataSet DevelopSourcesEmpty() { DataTable table = Spludlow.Data.TextTable.ReadText(new string[] { "DevelopSourceId Directory", "String* String", }); return Spludlow.Data.ADO.WireDataSet(table); } public static void MergeDevelopSources(DataTable table) { string filename = ProgramData + @"\Config\DevelopSources.txt"; if (File.Exists(filename) == true) Spludlow.Log.Warning("MergeDevelopSources; Copy of existing DevelopSources.txt", File.ReadAllText(filename)); DataTable existingTable = DevelopSources().Tables[0]; DataTable beforeTable = existingTable.Copy(); foreach (DataRow row in table.Rows) { string applicationId = (string)row["DevelopSourceId"]; DataRow existingRow = existingTable.Rows.Find(applicationId); if (existingRow != null) existingRow.Delete(); existingTable.ImportRow(row); } Spludlow.Data.TextTable.Write(filename, existingTable); beforeTable.TableName = "Before"; existingTable.TableName = "After"; Spludlow.Log.Report("MergeDevelopSources; Import count: " + table.Rows.Count, beforeTable, existingTable); } } }