-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
810 additions
and
784 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
71 changes: 20 additions & 51 deletions
71
src/Sponge.Client/Configuration/ClientConfigurationManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,39 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Xml; | ||
using System.Linq; | ||
using Sponge.Client.ConfigServiceReference; | ||
|
||
namespace Sponge.Client.Configuration | ||
{ | ||
public class ClientConfigurationManager: IDisposable | ||
public class ClientConfigurationManager | ||
{ | ||
private ConfigService _svc; | ||
|
||
public ClientConfigurationManager(string url) | ||
public static Configuration GetOnline(string spongeUrl, string appName, bool central) | ||
{ | ||
if (!url.EndsWith("/")) | ||
url += "/"; | ||
|
||
url += "_layouts/Sponge/ConfigService.asmx"; | ||
|
||
_svc = new ConfigService(); | ||
_svc.Url = url; | ||
_svc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; | ||
} | ||
|
||
public T Get<T>(string app, string key) | ||
{ | ||
object result = _svc.Get(app, key); | ||
return (T)result; | ||
using (var svc = GetConfigService(spongeUrl)) | ||
{ | ||
var cfg = central ? svc.GetCentral(appName) : svc.GetRelative(spongeUrl, appName); | ||
return GetConfig(cfg); | ||
} | ||
} | ||
|
||
public object Get(string app, string key) | ||
private static Configuration GetConfig(ConfigServiceReference.Configuration cfg) | ||
{ | ||
return _svc.Get(app, key); | ||
} | ||
var items = (from i in cfg.ConfigurationItems | ||
select new ConfigurationItem { Key = i.Key, Value = i.Value }).ToList(); | ||
|
||
public Dictionary<string, string> GetAll(string app) | ||
{ | ||
return FromXml(_svc.GetAll(app)); | ||
return new Configuration(cfg.Name, cfg.SpongeUrl, cfg.IsOnline, items); | ||
} | ||
|
||
public void Set(string app, string key, string value) | ||
private static ConfigService GetConfigService(string url) | ||
{ | ||
_svc.Set(app, key, value); | ||
} | ||
|
||
public void CreateApplication(string appName) | ||
{ | ||
_svc.CreateApplication(appName); | ||
} | ||
|
||
public bool ApplicationExists(string appName) | ||
{ | ||
return _svc.ApplicationExists(appName); | ||
} | ||
if (!url.EndsWith("/")) | ||
url = url += "/"; | ||
|
||
public void Dispose() | ||
{ | ||
if (_svc != null) | ||
_svc.Dispose(); | ||
} | ||
url = url + "_layouts/Sponge/ConfigService.asmx"; | ||
|
||
private Dictionary<string, string> FromXml(XmlNode doc) | ||
{ | ||
var dict = new Dictionary<string, string>(); | ||
foreach (XmlNode el in doc.ChildNodes) | ||
dict.Add(el.Name, el.InnerText); | ||
var svc = new ConfigService(); | ||
svc.Url = url; | ||
svc.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; | ||
|
||
return dict; | ||
return svc; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Xml.Serialization; | ||
|
||
namespace Sponge.Client.Configuration | ||
{ | ||
[XmlRoot("Configuration")] | ||
public class Configuration | ||
{ | ||
[XmlElement("Name")] | ||
public string Name { get; set; } | ||
|
||
[XmlElement("SpongeUrl")] | ||
public string SpongeUrl { get; set; } | ||
|
||
[XmlElement("IsOnline")] | ||
public bool IsOnline { get; set; } | ||
|
||
[XmlArray("ConfigurationItems")] | ||
[XmlArrayItem("ConfigurationItem")] | ||
public List<ConfigurationItem> Items; | ||
|
||
public Configuration(string name, string spongeUrl, bool isOnline, IEnumerable<ConfigurationItem> items) | ||
{ | ||
Name = name; | ||
SpongeUrl = spongeUrl; | ||
IsOnline = IsOnline; | ||
Items = items.ToList(); | ||
} | ||
|
||
public Configuration() | ||
{ | ||
} | ||
|
||
public T Get<T>(string key) | ||
{ | ||
if (Items != null && Items.Count > 0) | ||
{ | ||
var result = Items.Single(i => i.Key.Equals(key)); | ||
|
||
if (result == null) | ||
throw new Exception(string.Format("No Item with Key '{0}' found", key)); | ||
|
||
return (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFrom(result.Value); | ||
} | ||
else | ||
throw new Exception("Configuration does not contain any items"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Xml.Serialization; | ||
|
||
namespace Sponge.Client.Configuration | ||
{ | ||
[XmlType("Item")] | ||
public class ConfigurationItem | ||
{ | ||
[XmlElement("Key")] | ||
public string Key { get; set; } | ||
|
||
[XmlElement("Value")] | ||
public object Value { get; set; } | ||
|
||
public ConfigurationItem() | ||
{ | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.