using System; using System.Collections.Generic; using System.IO; using Microsoft.Win32; namespace DaoMqPump2 { public class TransportController { List transports = new List(); public string mysqlHost { get; private set; } public string mysqlPort { get; private set; } public string mysqlUser { get; private set; } public string mysqlPassword { get; private set; } public string mqHost { get; private set; } public string mqChannel { get; private set; } public string mqQueueManager { get; private set; } public string logDirectory { get; private set; } protected TransportController() { initialize(); } private void initialize() { Console.WriteLine("TransportController: Loading config"); RegistryKey key = Registry.LocalMachine.CreateSubKey("Software\\DAO\\DaoMqPump2"); //Læser globale MySQL Parametre mysqlHost = (string) key.GetValue("MysqlHost"); if (mysqlHost == null || mysqlHost.Length == 0) throw new System.ArgumentException("MysqlHost cannot be null or empty"); mysqlPort = (string) key.GetValue("MysqlPort"); if (mysqlPort == null || mysqlPort.Length == 0) throw new System.ArgumentException("MysqlPort cannot be null or empty"); mysqlUser = (string) key.GetValue("MysqlUser"); if (mysqlUser == null || mysqlUser.Length == 0) throw new System.ArgumentException("MysqlUser cannot be null or empty"); mysqlPassword = (string) key.GetValue("MysqlPassword"); if (mysqlPassword == null ||mysqlPassword.Length == 0) throw new System.ArgumentException("MysqlPassword cannot be null or empty"); //Læser globale MQ Parametre mqHost = (string)key.GetValue("MQHost"); if (mqHost == null || mqHost.Length == 0) throw new System.ArgumentException("MQHost cannot be null or empty"); mqChannel = (string)key.GetValue("MQChannel"); if (mqChannel == null || mqChannel.Length == 0) throw new System.ArgumentException("MQChannel cannot be null or empty"); mqQueueManager = (string)key.GetValue("MQQueueManager"); if (mqQueueManager == null || mqQueueManager.Length == 0) throw new System.ArgumentException("MQQueueManager cannot be null or empty"); //Læser øvrige parametre logDirectory = (string)key.GetValue("LogDirectory"); if (logDirectory == null || logDirectory.Length == 0) throw new System.ArgumentException("LogDirectory cannot be null or empty"); if (Directory.Exists(logDirectory) == false) { Directory.CreateDirectory(logDirectory); } //Læs alle transport indstillingerne string[] transportNames = key.GetSubKeyNames(); foreach (string name in transportNames) { Console.WriteLine("Reading Transport: " + name); RegistryKey subKey = key.OpenSubKey(name); string direction = (string)subKey.GetValue("Direction"); if (direction == null || direction.Length == 0) throw new System.ArgumentException(name + ".direction cannot be null or empty"); direction = direction.ToLower(); if (direction.Equals(Transport.MQ2SQL) == false && direction.Equals(Transport.SQL2MQ) == false) throw new System.ArgumentException(name + ".direction invalid value (sql2mq OR mq2sql) : " + direction); string queueName = (string)subKey.GetValue("MQQueueName"); if (queueName == null || queueName.Length == 0) throw new System.ArgumentException(name + ".MQQueueName cannot be null or empty"); Object tmpEnabled = subKey.GetValue("Enabled"); if (tmpEnabled == null) throw new System.ArgumentException(name + ".Enabled cannot be null or empty"); bool enabled = ((int)tmpEnabled > 0); string mq2sqlInsertQuery = ""; string sql2mqReadQuery = ""; string sql2mqUpdateQuery = ""; if (direction == "mq2sql") { mq2sqlInsertQuery = (string)subKey.GetValue("Mq2sqlInsertQuery"); if (mq2sqlInsertQuery == null || mq2sqlInsertQuery.Length == 0) throw new System.ArgumentException(name + ".Mq2sqlInsertQuery cannot be null or empty"); } else // sql2mq { sql2mqReadQuery = (string)subKey.GetValue("Sql2mqReadQuery"); if (sql2mqReadQuery == null || sql2mqReadQuery.Length == 0) throw new System.ArgumentException(name + ".Sql2mqReadQuery cannot be null or empty"); sql2mqUpdateQuery = (string)subKey.GetValue("Sql2mqUpdateQuery"); if (sql2mqUpdateQuery == null || sql2mqUpdateQuery.Length == 0) throw new System.ArgumentException(name + ".Sql2mqUpdateQuery cannot be null or empty"); } Transport trans = new Transport(this, name, direction, queueName, mq2sqlInsertQuery, sql2mqReadQuery, sql2mqUpdateQuery, enabled); transports.Add(trans); } } public void transportAllMessages() { Console.WriteLine("transportAllMessages()"); foreach (Transport trans in transports) { trans.transportMessages(); } } public string[] getTransportNames() { List names = new List(); foreach (Transport t in transports) { names.Add(t.name); } return names.ToArray(); } public Transport getTransport(string name) { foreach (Transport t in transports) { if (t.name == name)//found requested Transport object { return t; } } return null; } /* Singleton */ private static TransportController instance = null; public static TransportController getInstance() { if (instance == null) instance = new TransportController(); return instance; } } }