Exemple #1
0
        private WebAppSettingStore GetSettingStore()
        {
            rrLock.EnterUpgradeableReadLock();

            try
            {
                if (_settingStore == null || (DateTime.Now.Subtract(lastLoad).TotalSeconds) > cacheInterval)
                {
                    //Need to exist so don't deadlock.

                    rrLock.EnterWriteLock();
                    try
                    {
                        //make sure first another thread didn't already load...
                        if (_settingStore == null || (DateTime.Now.Subtract(lastLoad).TotalSeconds) > cacheInterval)
                        {
                            _settingStore = WebAppSettingStore.Load(this.webApplication);
                            lastLoad      = DateTime.Now;
                        }
                    }
                    finally
                    {
                        rrLock.ExitWriteLock();
                    }
                }

                return(_settingStore);
            }
            finally
            {
                rrLock.ExitUpgradeableReadLock();
            }
        }
Exemple #2
0
        public void Remove(string key)
        {
            var logger = SharePointServiceLocator.GetCurrent().GetInstance <ILogger>();

            rrLock.EnterWriteLock();

            try
            {
                //intentionally nested, both cases getting a write lock, is safe and ensures no race.
                //Force the reload of the persisted object, which minimizes chances of a failure due
                // to a concurrency failure.

                Reset();
                WebAppSettingStore store = GetWriteSettingStore();

                if (store.Settings.ContainsKey(key))
                {
                    store.Settings.Remove(key);
                    store.Update();
                }
            }
            catch (SPUpdatedConcurrencyException)
            {
                Reset();
                throw;
            }
            finally
            {
                rrLock.ExitWriteLock();
            }
        }
Exemple #3
0
        private WebAppSettingStore GetWriteSettingStore()
        {
            rrLock.EnterUpgradeableReadLock();

            try
            {
                GetSettingStore();

                if (_settingStore == null) // setting store not yet created
                {
                    rrLock.EnterWriteLock();

                    try
                    {
                        _settingStore = WebAppSettingStore.Create(this.webApplication);
                    }
                    finally
                    {
                        rrLock.ExitWriteLock();
                    }
                }
                return(_settingStore);
            }
            finally
            {
                rrLock.ExitUpgradeableReadLock();
            }
        }
Exemple #4
0
        public void AddAndRemove()
        {
            //Arrange
            SPWebAppPropertyBag.ClearCache();
            MSPPersistedObject webPO;
            WebAppSettingStore wss;
            string             key = "flintstone";

            var webApp = new BSPConfiguredWebApp();

            wss = new WebAppSettingStore();
            wss.Settings[key] = "fred";
            webPO             = new MSPPersistedObject((SPPersistedObject)webApp.Instance);
            webPO.GetChildString <WebAppSettingStore>((s) => wss);
            var wssPO = new MSPPersistedObject(wss);

            wssPO.Update = () =>
            {
            };

            // Act
            var target = new SPWebAppPropertyBag(webApp);
            var containsBeforeCondition = target.Contains(key);

            target.Remove(key);
            var result = target.Contains(key);

            // Assert
            Assert.IsFalse(result);
            Assert.IsTrue(containsBeforeCondition);
        }
Exemple #5
0
        public void AddAndContains()
        {
            //Arrange
            SPWebAppPropertyBag.ClearCache();
            MSPPersistedObject webPO;
            WebAppSettingStore wss;
            var webApp = new BSPConfiguredWebApp();

            wss   = new WebAppSettingStore();
            webPO = new MSPPersistedObject((SPPersistedObject)webApp.Instance);
            webPO.GetChildString <WebAppSettingStore>(
                (s) =>
            {
                return(wss);
            });
            var wssPO = new MSPPersistedObject(wss);

            wssPO.Update = () =>
            {
            };


            string key    = "key";
            string value  = "value";
            var    target = new SPWebAppPropertyBag(webApp);

            IPropertyBagTest.AddContains(target, key, value);
        }
Exemple #6
0
        public void Execute_ContainsKeyReturnsFalse_WithValidKeyNotSetAtWebAppLevel()
        {
            //arrange
            var    args = new ContainsKeyDataArgs();
            string key  = ConfigManager.PnPKeyNamespace + "." + TestsConstants.TestGuidName;

            args.Key    = key;
            args.Level  = (int)ConfigLevel.CurrentSPWebApplication;
            args.SiteId = TestsConstants.TestGuid;
            var proxyOp = new ContainsKeyOperation();

            var webApp             = new BSPWebApplication();
            WebAppSettingStore wss = new WebAppSettingStore();
            var webPO = new MSPPersistedObject((SPPersistedObject)webApp.Instance);

            MSPSite.ConstructorGuid = (instance, guid) =>
            {
                var site = new MSPSite(instance)
                {
                    WebApplicationGet = () =>
                    {
                        webPO.GetChildString <WebAppSettingStore>((s) => wss);
                        return(webApp);
                    },
                    Dispose = () => { }
                };
            };

            //Act
            var target = proxyOp.Execute(args);

            //Assert
            Assert.IsInstanceOfType(target, typeof(bool));
            Assert.IsFalse((bool)target);
        }
Exemple #7
0
        /// <summary>
        /// Gets or sets a value based on the key. If the value is not defined in this PropertyBag, it will look in it's
        /// parent property bag.
        /// </summary>
        /// <value></value>
        /// <returns>The config value defined in the property bag. </returns>
        public string this[string key]
        {
            [SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
            [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
            get
            {
                WebAppSettingStore store = GetSettingStore();

                if (store == null)      //store doesn't exist, so setting doesn't exist...
                {
                    return(null);
                }

                rrLock.EnterReadLock();

                try
                {
                    if (!store.Settings.ContainsKey(key))
                    {
                        return(null);
                    }

                    return(store.Settings[key]);
                }
                finally
                {
                    rrLock.ExitReadLock();
                }
            }
            [SharePointPermission(SecurityAction.InheritanceDemand, ObjectModel = true)]
            [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
            set
            {
                rrLock.EnterWriteLock();

                try
                {
                    //intentionally nested, Reset is also getting a write lock, is safe and ensures no race.
                    //Force the reload of the persisted object, which minimizes chances of a failure due
                    // to a concurrency failure.
                    Reset();
                    WebAppSettingStore store = GetWriteSettingStore();

                    store.Settings[key] = value;
                    store.Update();
                }
                catch (SPUpdatedConcurrencyException)
                {
                    Reset();

                    throw;
                }
                finally
                {
                    rrLock.ExitWriteLock();
                }
            }
        }
Exemple #8
0
        /// <summary>
        /// Forces the property bag to reload settings on next access.
        /// </summary>
        public void Reset()
        {
            rrLock.EnterWriteLock();

            try
            {
                _settingStore = null;
            }
            finally
            {
                rrLock.ExitWriteLock();
            }
        }
Exemple #9
0
        public bool Contains(string key)
        {
            WebAppSettingStore store = GetSettingStore();

            if (store == null)      //store doesn't exist, so setting doesn't exist...
            {
                return(false);
            }

            rrLock.EnterReadLock();

            try
            {
                return(store.Settings.ContainsKey(key));
            }
            finally
            {
                rrLock.ExitReadLock();
            }
        }
        public void Execute_ReturnsKeyData_WithValidKeyAtWebAppLevel()
        {
            //arrange
            SPWebAppPropertyBag.ClearCache();
            var    args = new ReadConfigArgs();
            string key  = ConfigManager.PnPKeyNamespace + "." + TestsConstants.TestGuidName;

            args.Key    = key;
            args.Level  = (int)ConfigLevel.CurrentSPWebApplication;
            args.SiteId = TestsConstants.TestGuid;
            var    proxyOp      = new ReadConfigurationOperation();
            string expectedData = "{92700BB6-B144-434F-A97B-5F696068A425}";

            MSPPersistedObject webPO;
            WebAppSettingStore wss;

            MSPSite.ConstructorGuid = (instance, guid) =>
            {
                var site = new MSPSite(instance)
                {
                    WebApplicationGet = () =>
                    {
                        var webApp = new BSPConfiguredWebApp();
                        wss = new WebAppSettingStore();
                        wss.Settings[key] = expectedData;
                        webPO             = new MSPPersistedObject((SPPersistedObject)webApp.Instance);
                        webPO.GetChildString <WebAppSettingStore>((s) => wss);
                        return(webApp);
                    },
                    Dispose = () => { }
                };
            };

            //Act
            object target = proxyOp.Execute(args);

            //Assert .
            Assert.IsInstanceOfType(target, typeof(string));
            Assert.AreEqual(expectedData, (string)target);
        }
        public void Execute_ReturnsNull_WithValidKeyNotSetAtWebAppLevel()
        {
            //arrange
            SPWebAppPropertyBag.ClearCache();
            var    args = new ReadConfigArgs();
            string key  = ConfigManager.PnPKeyNamespace + "." + TestsConstants.TestGuidName;

            args.Key    = key;
            args.Level  = (int)ConfigLevel.CurrentSPWebApplication;
            args.SiteId = TestsConstants.TestGuid;
            var proxyOp = new ReadConfigurationOperation();
            MSPPersistedObject webPO;
            WebAppSettingStore wss;

            MSPSite.ConstructorGuid = (instance, guid) =>
            {
                var site = new MSPSite(instance)
                {
                    WebApplicationGet = () =>
                    {
                        var webApp = new BSPConfiguredWebApp();
                        wss   = new WebAppSettingStore();
                        webPO = new MSPPersistedObject((SPPersistedObject)webApp.Instance);
                        webPO.GetChildString <WebAppSettingStore>((s) => wss);
                        return(webApp);
                    },
                    Dispose = () => { }
                };
            };

            //Act
            object target = proxyOp.Execute(args);

            //Assert
            Assert.IsNull(target);
        }
        public BSPConfiguredWebApp()
        {
            BSPWebApplication webApp = new BSPWebApplication();

            wss = new WebAppSettingStore();
        }
Exemple #13
0
 /// <summary>
 /// For internal use, clears the cached setting store data
 /// </summary>
 public static void ClearCache()
 {
     _settingStore = null;
 }