예제 #1
0
        /// <summary>
        /// Read the settings in RoleEnvironment, insert into the accounts dictionary
        /// </summary>
        /// <returns></returns>
        public int LoadConfigration()
        {
            // init member vars
            _usernum = 0;
            _accounts.Clear();

            string       filename = StorageProviderConfiguration.FtpAccount;
            StreamReader sr       = new StreamReader(File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite));

            //string[] accountInfo = StorageProviderConfiguration.FtpAccount.Split(":".ToCharArray());

            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine().Trim();
                if (line.Length == 0)
                {
                    continue;
                }
                if (line.StartsWith("#"))
                {
                    continue;
                }

                string[] accountInfo = line.Split(":".ToCharArray());

                for (int i = 0; i < accountInfo.Length; i++)
                {
                    string   oneAccount = accountInfo[i];
                    string[] parts      = oneAccount.Split(separator);

                    if (parts.Length < 3)
                    {
                        Trace.WriteLine(string.Format("Invalid account data. Must be userid;pwd;folder", oneAccount), "Warnning");
                        continue;
                    }

                    // get the username substr
                    string username = parts[0].ToLowerInvariant();

                    // check the username whether conform to the naming rules
                    if (!CheckUsername(username))
                    {
                        Trace.WriteLine(string.Format("Invalid username in account data", oneAccount), "Warnning");
                        continue;
                    }

                    // check whether the username already exists
                    if (_accounts.ContainsKey(username))
                    {
                        continue;
                    }

                    // get the password substr
                    string password = parts[1];
                    // simple check, password can not be empty
                    if (password.Length == 0)
                    {
                        Trace.WriteLine(string.Format("Invalid password in account data", oneAccount), "Warnning");
                        continue;
                    }

                    // get the password substr
                    string rootFolder = parts[2];
                    // simple check, password can not be empty
                    if (rootFolder.Length == 0)
                    {
                        Trace.WriteLine(string.Format("Invalid folder in account data", oneAccount), "Warnning");
                        continue;
                    }

                    FtpAccount account = new FtpAccount();
                    account.userid      = username;
                    account.password    = password;
                    account.rootFolder  = rootFolder;
                    account.readAccess  = true;
                    account.writeAccess = true;
                    _accounts.Add(username, account);
                    _usernum++;
                }
            }
            sr.Close();


            Trace.WriteLine(string.Format("Load {0} accounts.", _usernum), "Information");

            return(_usernum);
        }