public void PutDocument(ConfigurationDocumentKey documentKey, string content)
 {
     Platform.GetService <IConfigurationService>(
         service => service.SetConfigurationDocument(
             new SetConfigurationDocumentRequest(documentKey, content))
         );
 }
        /// <summary>
        /// Builds the criteria for retrieving a document by key.
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        protected ConfigurationDocumentSearchCriteria BuildDocumentKeyCriteria(ConfigurationDocumentKey key)
        {
            var criteria = new ConfigurationDocumentSearchCriteria();

            criteria.DocumentName.EqualTo(key.DocumentName);

            if (!string.IsNullOrEmpty(key.InstanceKey))
            {
                criteria.InstanceKey.EqualTo(key.InstanceKey);
            }
            else
            {
                criteria.InstanceKey.IsNull();
            }

            if (!string.IsNullOrEmpty(key.User))
            {
                criteria.User.EqualTo(key.User);
            }
            else
            {
                criteria.User.IsNull();
            }
            criteria.DocumentVersionString.EqualTo(VersionUtils.ToPaddedVersionString(key.Version, false, false));
            return(criteria);
        }
        private ConfigurationDocument GetConfigurationDocument(ConfigurationDocumentKey documentKey, bool prior)
        {
            IQueryable <ConfigurationDocument> query = from d in Context.ConfigurationDocuments select d;

            query = !string.IsNullOrEmpty(documentKey.InstanceKey)
                ? query.Where(d => d.InstanceKey == documentKey.InstanceKey)
                : query.Where(d => d.InstanceKey == null);

            query = !string.IsNullOrEmpty(documentKey.User)
                ? query.Where(d => d.User == documentKey.User)
                : query.Where(d => d.User == null);

            query = query.Where(d => d.DocumentName == documentKey.DocumentName);

            var paddedVersionString = VersionUtils.ToPaddedVersionString(documentKey.Version, false, false);

            if (prior)
            {
                query = query.Where(d => d.DocumentVersionString.CompareTo(paddedVersionString) < 0);
                //You want the most recent prior version.
                query = query.OrderByDescending(d => d.DocumentVersionString);
            }
            else
            {
                query = query.Where(d => d.DocumentVersionString == paddedVersionString);
            }

            return(query.FirstOrDefault());
        }
示例#4
0
 private static ConfigurationDocument NewDocument(ConfigurationDocumentKey key)
 {
     return(new ConfigurationDocument(key.DocumentName,
                                      VersionUtils.ToPaddedVersionString(key.Version, false, false),
                                      StringUtilities.NullIfEmpty(key.User),
                                      StringUtilities.NullIfEmpty(key.InstanceKey)));
 }
 public GetConfigurationDocumentResponse(ConfigurationDocumentKey documentKey, DateTime?creationTime, DateTime?modifiedTime, string content)
 {
     DocumentKey  = documentKey;
     CreationTime = creationTime;
     ModifiedTime = modifiedTime;
     Content      = content;
 }
        private static ConfigurationDocumentHeader GetDocumentHeader(ConfigurationDocument document)
        {
            var key = new ConfigurationDocumentKey(
                document.DocumentName,
                VersionUtils.FromPaddedVersionString(document.DocumentVersionString),
                document.User,
                document.InstanceKey);

            return(new ConfigurationDocumentHeader(key, document.CreationTime, document.Body.ModifiedTime));
        }
        private ConfigurationDocument GetConfigurationDocument(ConfigurationDocumentKey documentKey, bool prior)
        {
            var query = prior ? _queryPriorVersion : _queryCurrentVersion;

            return(query(Context,
                         documentKey.DocumentName,
                         !string.IsNullOrEmpty(documentKey.InstanceKey) ? documentKey.InstanceKey : null,
                         !string.IsNullOrEmpty(documentKey.User) ? documentKey.User : null,
                         VersionUtils.ToPaddedVersionString(documentKey.Version, false, false)).FirstOrDefault());
        }
        public void PutSettingsValues(SettingsGroupDescriptor group, string user, string instanceKey,
                                      Dictionary <string, string> dirtyValues)
        {
            using (var context = new DataAccessContext())
            {
                // next we obtain any previously stored configuration document for this settings group
                var documentKey = new ConfigurationDocumentKey(group.Name, group.Version, user, instanceKey);
                var broker      = context.GetConfigurationDocumentBroker();

                var values = new Dictionary <string, string>();
                var parser = new SettingsParser();

                var document = broker.GetConfigurationDocument(documentKey);
                if (document == null)
                {
                    document = new ConfigurationDocument
                    {
                        CreationTime          = Platform.Time,
                        DocumentName          = group.Name,
                        DocumentVersionString = VersionUtils.ToPaddedVersionString(group.Version, false, false),
                        User         = user,
                        DocumentText = string.Empty
                    };
                    broker.AddConfigurationDocument(document);
                }
                else
                {
                    // parse document
                    parser.FromXml(document.DocumentText, values);
                }

                // update the values that have changed
                foreach (var kvp in dirtyValues)
                {
                    values[kvp.Key] = kvp.Value;
                }

                try
                {
                    if (values.Count > 0)
                    {
                        // generate the document, update local cache and server
                        document.DocumentText = parser.ToXml(values);
                        context.Commit();
                    }
                }
                catch (EndpointNotFoundException e)
                {
                    Platform.Log(LogLevel.Debug, e, "Unable to save settings to configuration service.");
                }
            }
        }
示例#9
0
        public void TestCacheConfigurationDocument()
        {
            var cache = new TestCacheClient();

            cache.ClearCache();

            var documentKey = new ConfigurationDocumentKey("Test", new Version(1, 0), null, "");
            var cacheKey    = ((IDefinesCacheKey)documentKey).GetCacheKey();

            var    service    = new TestConfigurationService();
            object request    = new GetConfigurationDocumentRequest(documentKey);
            object response   = new GetConfigurationDocumentResponse(documentKey, DateTime.Now, DateTime.Now, "Test");
            var    invocation = new TestInvocation
            {
                Target     = service,
                Method     = typeof(IApplicationConfigurationReadService).GetMethod("GetConfigurationDocument", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public),
                TargetType = typeof(IApplicationConfigurationReadService),
                Request    = request,
                Response   = response
            };

            var directive = new ResponseCachingDirective(true, TimeSpan.FromMinutes(1), ResponseCachingSite.Server);
            var advice    = new ConcreteResponseCachingAdvice(directive);

            advice.Intercept(invocation);

            var cacheEntry = cache.Get(cacheKey, new CacheGetOptions(""));

            Assert.IsNotNull(cacheEntry);
            Assert.AreEqual(response, cacheEntry);

            request  = new SetConfigurationDocumentRequest(documentKey, "Test");
            response = new SetConfigurationDocumentResponse();

            invocation = new TestInvocation
            {
                Target     = service,
                Method     = typeof(IConfigurationService).GetMethod("SetConfigurationDocument", BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public),
                TargetType = typeof(IConfigurationService),
                Request    = request,
                Response   = response
            };

            advice = new ConcreteResponseCachingAdvice(null);
            advice.Intercept(invocation);

            cacheEntry = cache.Get(cacheKey, new CacheGetOptions(""));
            Assert.IsNull(cacheEntry);
        }
示例#10
0
        public void RemoveUserSettings(SettingsGroupDescriptor group, string user, string instanceKey)
        {
            Platform.CheckForNullReference(user, "user");

            var documentKey = new ConfigurationDocumentKey(group.Name, group.Version, user, instanceKey);

            // remove from offline cache
            using (var offlineCacheClient = _offlineCache.CreateClient())
            {
                offlineCacheClient.Remove(documentKey);
            }

            // remove from server
            Platform.GetService((IConfigurationService service) => service.RemoveConfigurationDocument(new RemoveConfigurationDocumentRequest(documentKey)));
        }
        /// <summary>
        /// Gets the specified configuration document.
        /// </summary>
        /// <param name="documentKey"></param>
        /// <returns></returns>
        protected GetConfigurationDocumentResponse GetConfigurationDocumentHelper(ConfigurationDocumentKey documentKey)
        {
            CheckReadAccess(documentKey);

            var broker    = PersistenceContext.GetBroker <IConfigurationDocumentBroker>();
            var criteria  = BuildDocumentKeyCriteria(documentKey);
            var documents = broker.Find(criteria, new SearchResultPage(0, 1), new EntityFindOptions {
                Cache = true
            });

            var document = CollectionUtils.FirstElement(documents);

            return(document == null
                                        ? new GetConfigurationDocumentResponse(documentKey, null, null, null)
                                        : new GetConfigurationDocumentResponse(documentKey, document.CreationTime, document.Body.ModifiedTime, document.Body.DocumentText));
        }
示例#12
0
        public Dictionary <string, string> GetSettingsValues(SettingsGroupDescriptor group, string user, string instanceKey)
        {
            var serviceContract = !String.IsNullOrEmpty(user) ?
                                  typeof(IConfigurationService) : typeof(IApplicationConfigurationReadService);
            var service = (IApplicationConfigurationReadService)Platform.GetService(serviceContract);

            using (service as IDisposable)
            {
                var documentKey = new ConfigurationDocumentKey(group.Name, group.Version, user, instanceKey);
                var document    = GetConfigurationDocument(service, documentKey);

                var values = new Dictionary <string, string>();
                var parser = new SettingsParser();
                parser.FromXml(document, values);
                return(values);
            }
        }
        protected static void CheckReadAccess(ConfigurationDocumentKey key)
        {
            var user = key.User;

            if (string.IsNullOrEmpty(user))
            {
                // all users can read application configuration docs
            }
            else
            {
                // user can only read their own configuration docs
                if (user != Thread.CurrentPrincipal.Identity.Name)
                {
                    ThrowNotAuthorized();
                }
            }
        }
        /// <summary>
        /// Gets the settings group that immediately precedes the one provided.
        /// </summary>
        public SettingsGroupDescriptor GetPreviousSettingsGroup(SettingsGroupDescriptor group)
        {
            using (var context = new DataAccessContext())
            {
                var documentKey = new ConfigurationDocumentKey(group.Name, group.Version, null, null);
                var broker      = context.GetConfigurationDocumentBroker();

                var document = broker.GetPriorConfigurationDocument(documentKey);

                if (document != null)
                {
                    return(new SettingsGroupDescriptor(document.DocumentName, VersionUtils.FromPaddedVersionString(document.DocumentVersionString), string.Empty, group.AssemblyQualifiedTypeName, group.HasUserScopedSettings));
                }

                return(null);
            }
        }
        public IConfigurationDocument GetDocument(ConfigurationDocumentKey documentKey)
        {
            // choose the anonymous-access service if possible
            var serviceContract = string.IsNullOrEmpty(documentKey.User) ?
                                  typeof(IApplicationConfigurationReadService) : typeof(IConfigurationService);

            var service = (IApplicationConfigurationReadService)Platform.GetService(serviceContract);

            using (service as IDisposable)
            {
                var response = service.GetConfigurationDocument(new GetConfigurationDocumentRequest(documentKey));
                if (response.Content == null)
                {
                    throw new ConfigurationDocumentNotFoundException(documentKey);
                }

                return(new Document(response));
            }
        }
        public Dictionary <string, string> GetSettingsValues(SettingsGroupDescriptor group, string user,
                                                             string instanceKey)
        {
            using (var context = new DataAccessContext())
            {
                var documentKey = new ConfigurationDocumentKey(group.Name, group.Version, user, instanceKey);
                var broker      = context.GetConfigurationDocumentBroker();

                var document = broker.GetConfigurationDocument(documentKey);

                var values = new Dictionary <string, string>();
                if (document != null)
                {
                    var parser = new SettingsParser();
                    parser.FromXml(document.DocumentText, values);
                }

                return(values);
            }
        }
        protected static void CheckWriteAccess(ConfigurationDocumentKey key)
        {
            var user = key.User;

            if (string.IsNullOrEmpty(user))
            {
                // this is an application configuration doc - need admin permission
                if (!Thread.CurrentPrincipal.IsInRole(AuthorityTokens.Admin.System.Configuration))
                {
                    ThrowNotAuthorized();
                }
            }
            else
            {
                // user can only save their own configuration docs
                if (user != Thread.CurrentPrincipal.Identity.Name)
                {
                    ThrowNotAuthorized();
                }
            }
        }
示例#18
0
        private string GetConfigurationDocument(IApplicationConfigurationReadService service, ConfigurationDocumentKey documentKey)
        {
            using (var offlineCacheClient = _offlineCache.CreateClient())
            {
                try
                {
                    var document = service.GetConfigurationDocument(new GetConfigurationDocumentRequest(documentKey)).Content;

                    // keep offline cache up to date
                    offlineCacheClient.Put(documentKey, document);

                    return(document);
                }
                catch (EndpointNotFoundException e)
                {
                    Platform.Log(LogLevel.Debug, e, "Unable to retreive settings from configuration service.");

                    // get most recent version from offline cache
                    return(offlineCacheClient.Get(documentKey));
                }
            }
        }
 public ConfigurationDocumentRequestBase(ConfigurationDocumentKey documentKey)
 {
     DocumentKey = documentKey;
 }
示例#20
0
 public GetConfigurationDocumentRequest(ConfigurationDocumentKey documentKey)
     : base(documentKey)
 {
 }
示例#21
0
 public RemoveConfigurationDocumentRequest(ConfigurationDocumentKey documentKey)
     : base(documentKey)
 {
 }
 public void PutDocument(ConfigurationDocumentKey documentKey, TextReader content)
 {
     PutDocument(documentKey, content.ReadToEnd());
 }
示例#23
0
 protected static string GetDocumentCacheKey(ConfigurationDocumentKey key)
 {
     return(((IDefinesCacheKey)key).GetCacheKey());
 }
示例#24
0
        public void PutSettingsValues(SettingsGroupDescriptor group, string user, string instanceKey, Dictionary <string, string> dirtyValues)
        {
            // note: if user == null, we are saving shared settings, if user is valued, we are saving user settings
            // but both are never edited as a single operation

            // the approach taken here is to create an XML document that represents a diff between
            // the default settings (as specified by the settings group meta-data) and the modified settings,
            // and store that document in the configuration store

            var service = Platform.GetService <IConfigurationService>();

            using (service as IDisposable)
                using (var offlineCacheClient = _offlineCache.CreateClient())
                {
                    // first obtain the meta-data for the settings group properties
                    var properties = ListSettingsProperties(group, service);

                    // next we obtain any previously stored configuration document for this settings group
                    var documentKey = new ConfigurationDocumentKey(group.Name, group.Version, user, instanceKey);
                    var document    = GetConfigurationDocument(service, documentKey);

                    // parse document
                    var parser = new SettingsParser();
                    var values = new Dictionary <string, string>();
                    parser.FromXml(document, values);

                    // update the values that have changed
                    foreach (var kvp in dirtyValues)
                    {
                        values[kvp.Key] = kvp.Value;
                    }

                    // now remove any values that are identical to the default values
                    foreach (var property in properties)
                    {
                        string value;
                        if (values.TryGetValue(property.Name, out value) && Equals(value, property.DefaultValue))
                        {
                            values.Remove(property.Name);
                        }
                    }

                    try
                    {
                        if (values.Count > 0)
                        {
                            // generate the document, update local cache and server
                            document = parser.ToXml(values);
                            offlineCacheClient.Put(documentKey, document);
                            service.SetConfigurationDocument(new SetConfigurationDocumentRequest(documentKey, document));
                        }
                        else
                        {
                            // every value is the same as the default, so the document can be removed
                            // update local cache and server
                            offlineCacheClient.Remove(documentKey);
                            service.RemoveConfigurationDocument(new RemoveConfigurationDocumentRequest(documentKey));
                        }
                    }
                    catch (EndpointNotFoundException e)
                    {
                        Platform.Log(LogLevel.Debug, e, "Unable to save settings to configuration service.");
                    }
                }
        }
 public SetConfigurationDocumentRequest(ConfigurationDocumentKey documentKey, string content)
     : base(documentKey)
 {
     Content = content;
 }
 /// <summary>
 /// Get the specified ConfigurationDocument or null if not found
 /// </summary>
 /// <param name="documentKey"></param>
 /// <returns></returns>
 public ConfigurationDocument GetConfigurationDocument(ConfigurationDocumentKey documentKey)
 {
     return(GetConfigurationDocument(documentKey, false));
 }
示例#27
0
        public void TestAddAndGetPrior()
        {
            DeleteAllDocuments();

            string documentName = "test";
            string instanceKey  = null;
            string user         = null;

            var oldestKey = new ConfigurationDocumentKey(documentName, new Version(3, 5, 21685, 22177), user, instanceKey);
            var oldest    = new ConfigurationDocument
            {
                CreationTime          = DateTime.Now,
                DocumentName          = oldestKey.DocumentName,
                DocumentVersionString = VersionUtils.ToPaddedVersionString(oldestKey.Version, false, false),
                User         = oldestKey.User,
                InstanceKey  = oldestKey.InstanceKey,
                DocumentText = "oldest"
            };

            var previousKey = new ConfigurationDocumentKey(documentName, new Version(4, 4, 21685, 22177), user, instanceKey);
            var previous    = new ConfigurationDocument
            {
                CreationTime          = DateTime.Now,
                DocumentName          = previousKey.DocumentName,
                DocumentVersionString = VersionUtils.ToPaddedVersionString(previousKey.Version, false, false),
                User         = previousKey.User,
                InstanceKey  = previousKey.InstanceKey,
                DocumentText = "previous"
            };

            var newestKey = new ConfigurationDocumentKey(documentName, new Version(5, 1, 21685, 22177), user, instanceKey);
            var newest    = new ConfigurationDocument
            {
                CreationTime          = DateTime.Now,
                DocumentName          = newestKey.DocumentName,
                DocumentVersionString = VersionUtils.ToPaddedVersionString(newestKey.Version, false, false),
                User         = newestKey.User,
                InstanceKey  = newestKey.InstanceKey,
                DocumentText = "newest"
            };

            using (var context = new DataAccessContext())
            {
                var broker = context.GetConfigurationDocumentBroker();
                broker.AddConfigurationDocument(oldest);
                broker.AddConfigurationDocument(previous);
                broker.AddConfigurationDocument(newest);
                context.Commit();
            }

            using (var context = new DataAccessContext())
            {
                var broker          = context.GetConfigurationDocumentBroker();
                var oldestRetrieved = broker.GetConfigurationDocument(oldestKey);
                Assert.AreEqual(oldestRetrieved.DocumentName, oldest.DocumentName);
                Assert.AreEqual(oldestRetrieved.DocumentVersionString, oldest.DocumentVersionString);

                var previousRetrieved = broker.GetConfigurationDocument(previousKey);
                Assert.AreEqual(previousRetrieved.DocumentName, previous.DocumentName);
                Assert.AreEqual(previousRetrieved.DocumentVersionString, previous.DocumentVersionString);

                var newestRetrieved = broker.GetConfigurationDocument(newestKey);
                Assert.AreEqual(newestRetrieved.DocumentName, newest.DocumentName);
                Assert.AreEqual(newestRetrieved.DocumentVersionString, newest.DocumentVersionString);

                var previousOfNewest = broker.GetPriorConfigurationDocument(newestKey);
                Assert.AreEqual(previousOfNewest.DocumentName, previous.DocumentName);
                Assert.AreEqual(previousOfNewest.DocumentVersionString, previous.DocumentVersionString);
            }
        }
 /// <summary>
 /// Get the specified ConfigurationDocument or null if not found
 /// </summary>
 /// <param name="documentKey"></param>
 /// <returns></returns>
 public ConfigurationDocument GetPriorConfigurationDocument(ConfigurationDocumentKey documentKey)
 {
     return(GetConfigurationDocument(documentKey, true));
 }