Esempio n. 1
0
        /// <summary>
        /// Loads virtual server from xml file.
        /// </summary>
        internal void LoadVirtualServers()
        {
            try{
                DateTime dateServers = File.GetLastWriteTime(SCore.PathFix(m_StartupPath + "Settings\\localServers.xml"));

                if (DateTime.Compare(dateServers, m_ServersFileDate) != 0)
                {
                    m_ServersFileDate = dateServers;

                    DataSet ds = new DataSet();
                    ds.Tables.Add("Servers");
                    ds.Tables["Servers"].Columns.Add("ID");
                    ds.Tables["Servers"].Columns.Add("Enabled");
                    ds.Tables["Servers"].Columns.Add("Name");
                    ds.Tables["Servers"].Columns.Add("API_assembly");
                    ds.Tables["Servers"].Columns.Add("API_class");
                    ds.Tables["Servers"].Columns.Add("API_initstring");
                    ds.ReadXml(SCore.PathFix(m_StartupPath + "Settings\\localServers.xml"));

                    if (ds.Tables.Contains("Servers"))
                    {
                        // Delete running virtual servers what has deleted.
                        for (int i = 0; i < m_VirtualServers.Count; i++)
                        {
                            VirtualServer server = m_VirtualServers[i];
                            bool          exists = false;
                            foreach (DataRow dr in ds.Tables["Servers"].Rows)
                            {
                                if (server.ID == dr["ID"].ToString())
                                {
                                    exists = true;
                                    break;
                                }
                            }
                            if (!exists)
                            {
                                server.Stop();
                                m_VirtualServers.Remove(server);
                                i--;
                            }
                        }

                        // Add new added virtual servers what aren't running already.
                        foreach (DataRow dr in ds.Tables["Servers"].Rows)
                        {
                            //--- See if specified server already running, if so, skip it. --//
                            bool exists = false;
                            foreach (VirtualServer server in m_VirtualServers)
                            {
                                if (server.ID == dr["ID"].ToString())
                                {
                                    exists         = true;
                                    server.Enabled = ConvertEx.ToBoolean(dr["Enabled"], true);
                                    break;
                                }
                            }
                            if (exists)
                            {
                                continue;
                            }
                            //--------------------------------------------------------------//

                            string id       = dr["ID"].ToString();
                            string name     = dr["Name"].ToString();
                            string assembly = dr["API_assembly"].ToString();
                            string apiClass = dr["API_class"].ToString();
                            string intiStr  = dr["API_initstring"].ToString();

                            IMailServerApi api           = LoadApi(assembly, apiClass, intiStr);
                            VirtualServer  virtualServer = new VirtualServer(this, id, name, intiStr, api);
                            m_VirtualServers.Add(virtualServer);
                            virtualServer.Enabled = ConvertEx.ToBoolean(dr["Enabled"], true);
                        }
                    }
                }
            }
            catch (Exception x) {
                Error.DumpError(x, new System.Diagnostics.StackTrace());
            }
        }
Esempio n. 2
0
        public void InitializeFromCmdLine(params string[] args)
        {
            args = SplitArgsByDelimiter(args);

            InitializeBy(() =>
            {
                Initialize_Internal();
                if (Properties.Count == 0)
                {
                    return;
                }

                // There aren't any args so just return.
                if (args.Length == 0)
                {
                    return;
                }

                int startIndex = 0;
                if (DefaultProperties != null && DefaultProperties.Length > 0 &&
                    !args[0].StartsWith(Options.ArgumentPrefix))
                {
                    string[] argValues = GetArgValues(args);
                    if (DefaultProperties.Length == 1)
                    {
                        DefaultProperties[0].Value = argValues;
                    }
                    else
                    {
                        for (int i = 0; i < argValues.Length && i < DefaultProperties.Length; i++)
                        {
                            DefaultProperties[i].Value = argValues[i];
                        }
                    }
                    startIndex = argValues.Length;
                    // We already took care of the first set of arguments so start with the next.
                }

                for (int i = startIndex; i < args.Length; i++)
                {
                    if (!args[i].StartsWith(Options.ArgumentPrefix))
                    {
                        continue;
                    }

                    string argName = args[i].Substring(Options.ArgumentPrefix.Length);
                    if (argName == "")
                    {
                        continue;
                    }

                    CmdLineProperty prop = Properties[argName.TrimEnd('-')];
                    if (prop == null)
                    {
                        continue;
                    }

                    string[] argValues;
                    argValues = GetArgValues(args.Shrink(i + 1));
                    i         = i + argValues.Length;                                  // Move to the next valid argument.

                    if (prop.PropertyType == typeof(bool))
                    {
                        // Boolean arguments don't require an argument value (/A and /A- can be used for true/false).
                        if (argName.EndsWith("-"))
                        {
                            prop.Value = false;
                        }
                        else
                        {
                            bool bVal = true;

                            if (argValues.Length > 0)
                            {
                                bVal = ConvertEx.ToBoolean(argValues[0]);
                                try
                                {
                                    bVal = ConvertEx.ToBoolean(argValues[0]);
                                }
                                catch (Exception)
                                {
                                    // The command-line argument value couldn't be converted to a
                                    // bool so assume it wasn't intended to be.
                                }
                            }
                            prop.Value = bVal;
                        }
                    }
                    else if (prop.PropertyType.IsArray)
                    {
                        try
                        {
                            var splitValues = argValues.Join(Options.ArraySeparator).Split(new[] { Options.ArraySeparator }, StringSplitOptions.RemoveEmptyEntries);
                            prop.Value      = splitValues.Convert(prop.PropertyType.GetElementType());
                        }
                        catch (Exception)
                        {
                            prop.Value = argValues;
                        }
                    }
                    else if (argValues.Length > 0)
                    {
                        prop.Value = argValues;
                    }
                }
            });
        }