/// <summary> /// Gets the first plugin section. /// </summary> /// <typeparam name="TSection">The type of the section.</typeparam> /// <param name="manager">The manager.</param> /// <param name="sectionName">Name of the section.</param> /// <returns></returns> public static TSection GetFirstPluginSection <TSection>(this DeepConfigManager manager, string sectionName) where TSection : ConfigurationSection { var settings = PluginManagerConfiguration.GetSettings(); return(manager.GetFirstWebSetting <TSection, TSection>(sectionName, x => x, settings.PluginsPath)); }
/// <summary> /// Gets the first configuration setting matching <paramref name="sectionName"/> by calling <see cref="DeepConfigManager.GetWebSettings{TSection,TOut}(string,Func{TSection,TOut},string,string,string)"/>. /// See that method for an example. The relative plugin base path in <see cref="PluginManagerConfiguration.PluginsPath"/> is mapped to a physical path using the <see cref="System.Web.HttpContextWrapper"/> supplied to this instance /// <paramref name="manager"/> in its constructor. /// </summary> /// <typeparam name="TSection">The type of the section.</typeparam> /// <typeparam name="TOut">The type outputted by the expression in <paramref name="deferred"/>.</typeparam> /// <param name="manager">The manager.</param> /// <param name="sectionName">Name of the section in the configuration files.</param> /// <param name="deferred">The deffered expression.</param> /// <returns></returns> public static TOut GetFirstPluginSetting <TSection, TOut>(this DeepConfigManager manager, string sectionName, Expression <Func <TSection, TOut> > deferred) where TSection : ConfigurationSection where TOut : class { var settings = PluginManagerConfiguration.GetSettings(); return(manager.GetFirstWebSetting(sectionName, deferred, settings.PluginsPath)); }
/// <summary> /// Gets the plugin settings. /// </summary> /// <typeparam name="TSection">The type of the section.</typeparam> /// <typeparam name="TOut">The type of the out.</typeparam> /// <param name="manager">The manager.</param> /// <param name="sectionName">Name of the section.</param> /// <param name="deferred">The deferred.</param> /// <returns></returns> public static IEnumerable <TOut> GetPluginSettings <TSection, TOut>(this DeepConfigManager manager, string sectionName, Expression <Func <TSection, TOut> > deferred) where TSection : ConfigurationSection where TOut : class { var settings = PluginManagerConfiguration.GetSettings(); var pluginBasePath = settings != null ? settings.PluginsPath : "~/Config/"; return(manager.GetWebSettings(sectionName, deferred, pluginBasePath)); }
public void NHibernateBootstrapper_Configure_Application() { //Arrange //create a new web.config file in /plugins for the providers to write to var http = new FakeHttpContextFactory("~/test"); var configFile = new FileInfo(Path.Combine(Environment.CurrentDirectory, "nhibernate.config")); var configMgr = new DeepConfigManager(http.HttpContext); var configXml = DeepConfigManager.CreateNewConfigFile(configFile, true); var installModel = new DatabaseInstallModel() { DatabaseType = DatabaseServerType.MSSQL, DatabaseName = "test", Server = "testserver", Username = "******", Password = "******" }; var boot = new ProviderBootstrapper(null, null, new FakeFrameworkContext()); //Act boot.ConfigureApplication("rw-test", "test", configXml, new BendyObject(installModel)); //Assert Assert.AreEqual(@"<configuration> <configSections> <sectionGroup name=""rebel""> <sectionGroup name=""persistenceProviderSettings""> <section name=""test"" type=""Rebel.Framework.Persistence.NHibernate.Config.ProviderConfigurationSection, Rebel.Framework.Persistence.NHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"" /> </sectionGroup> </sectionGroup> </configSections> <connectionStrings> <add name=""test.ConnString"" connectionString=""Data Source=testserver; Initial Catalog=test;User Id=testuser;Password=testpass"" providerName=""System.Data.SqlClient"" /> </connectionStrings> <appSettings /> <rebel> <persistenceProviderSettings> <test connectionStringKey=""test.ConnString"" sessionContext=""web"" driver=""MsSql2008"" /> </persistenceProviderSettings> </rebel> </configuration>", configXml.ToString()); }
public void DeepConfigManager_Serialize_Provider_Config_Section() { //Arrange var file = new FileInfo(Path.Combine(Environment.CurrentDirectory, "web.config")); var config = DeepConfigManager.CreateNewConfigFile(file, true); var section = new TestConfigSection { Address = "address1", Name = "name1", Age = 123 }; //Act DeepConfigManager.SerializeProviderConfigSection(config, section, "rebel/persistenceProviderSettings/nhibernate", true); //Assert Assert.AreEqual("configuration", config.Root.Name.LocalName); Assert.AreEqual("configSections", config.Root.Elements().First().Name.LocalName); Assert.AreEqual("sectionGroup", config.Root.Elements().First().Elements().First().Name.LocalName); Assert.AreEqual("name", config.Root.Elements().First().Elements().First().Attributes().First().Name.LocalName); Assert.AreEqual("rebel", config.Root.Elements().First().Elements().First().Attributes().First().Value); Assert.AreEqual("sectionGroup", config.Root.Elements().First().Elements().First().Elements().First().Name.LocalName); Assert.AreEqual("name", config.Root.Elements().First().Elements().First().Elements().First().Attributes().First().Name.LocalName); Assert.AreEqual("persistenceProviderSettings", config.Root.Elements().First().Elements().First().Elements().First().Attributes().First().Value); Assert.AreEqual("section", config.Root.Elements().First().Elements().First().Elements().First().Elements().First().Name.LocalName); Assert.AreEqual("name", config.Root.Elements().First().Elements().First().Elements().First().Elements().First().Attributes().First().Name.LocalName); Assert.AreEqual("nhibernate", config.Root.Elements().First().Elements().First().Elements().First().Elements().First().Attributes().First().Value); Assert.AreEqual("requirePermission", config.Root.Elements().First().Elements().First().Elements().First().Elements().First().Attributes().Last().Name.LocalName); Assert.AreEqual(section.GetType().AssemblyQualifiedName, config.Root.Descendants("section").Where(x => x.Attributes("type").Any()).FirstOrDefault().Attribute("type").Value); Assert.AreEqual("rebel", config.Root.Elements().Last().Name.LocalName); Assert.AreEqual("persistenceProviderSettings", config.Root.Elements().Last().Elements().First().Name.LocalName); Assert.AreEqual("nhibernate", config.Root.Elements().Last().Elements().First().Elements().First().Name.LocalName); Assert.AreEqual("name1", config.Root.Elements().Last().Elements().First().Elements().First().Attribute("name").Value); Assert.AreEqual("address1", config.Root.Elements().Last().Elements().First().Elements().First().Attribute("address").Value); Assert.AreEqual(123, (int)config.Root.Elements().Last().Elements().First().Elements().First().Attribute("age")); }
/// <summary> /// Creates any necessary configuration files/transforms for the provider to operate /// </summary> /// <param name="providerKey">The provider key for the provider that is being configured</param> /// <param name="configXml">The configuration xml file that needs to be written to</param> /// <param name="installParams">TODO: This is only a temporary way of passing arbitrary parameters to a provider to create its configuration, /// we need to allow hive providers to return a model for which we display a form/installer for and then pass in that /// model to the installParams</param> public override void ConfigureApplication(string providerKey, XDocument configXml, BendyObject installParams) { dynamic dynamicParams = installParams; string dbType = dynamicParams.DatabaseType.ToString(); var connectionString = ""; var providerName = ""; var nhDriver = ""; //we need to create the connection strings if it's not custom switch (dbType) { case "MSSQL": connectionString = string.Format("Data Source={0}; Initial Catalog={1};User Id={2};Password={3}", dynamicParams.Server, dynamicParams.DatabaseName, dynamicParams.Username, dynamicParams.Password); providerName = "System.Data.SqlClient"; nhDriver = "MsSql2008"; break; case "MySQL": connectionString = string.Format("Server={0}; Database={1};Uid={2};Pwd={3}", dynamicParams.Server, dynamicParams.DatabaseName, dynamicParams.Username, dynamicParams.Password); providerName = "MySql.Data.MySQLClient"; nhDriver = "MySql"; break; case "SQLCE": connectionString = "Data Source=|DataDirectory|Umbraco.sdf"; providerName = "System.Data.SqlServerCe.4.0"; nhDriver = "MsSqlCe4"; break; case "Custom": //limiting to MS SQL atm connectionString = dynamicParams.ConnectionString; providerName = "System.Data.SqlClient"; nhDriver = "MsSql2008"; break; } var connstringKey = ""; var hiveElement = new ProviderConfigurationSection() { ConnectionStringKey = "{0}.ConnString", Driver = SupportedNHDrivers.MsSqlCe4, SessionContext = "web" }; var elementName = providerKey; hiveElement.DriverAsString = nhDriver; connstringKey = string.Format(hiveElement.ConnectionStringKey, providerKey); hiveElement.ConnectionStringKey = connstringKey; DeepConfigManager.SerializeProviderConfigSection(configXml, hiveElement, "umbraco/persistenceProviderSettings/" + elementName, true); //add the connection strings var connStrings = new ConnectionStringsSection(); connStrings.ConnectionStrings.Add(new ConnectionStringSettings(connstringKey, connectionString, providerName)); //now serialize the connection strings to the config var connectionStringElement = DeepConfigManager.SerializeProviderConfigSection(configXml, connStrings, "connectionStrings", false); var newConnString = new XElement("add"); DeepConfigManager.AddPropertiesToElement(connStrings.ConnectionStrings[0], newConnString); connectionStringElement.Add(newConnString); // The following is superceded by the above to support multiple "add" references: DeepConfigManager.SerializeProviderConfigSection(configXml, connStrings.ConnectionStrings[0], "connectionStrings/add", false); }
public void NHibernateBootstrapper_Configure_Application() { //Arrange //create a new web.config file in /plugins for the providers to write to var http = new FakeHttpContextFactory("~/test"); var configFile = new FileInfo(Path.Combine(Environment.CurrentDirectory, "nhibernate.config")); var configMgr = new DeepConfigManager(http.HttpContext); var configXml = DeepConfigManager.CreateNewConfigFile(configFile, true); var installModel = new DatabaseInstallModel() { DatabaseType = DatabaseServerType.MSSQL, DatabaseName = "test", Server = "testserver", Username = "******", Password = "******" }; var boot = new ProviderBootstrapper(null, null, new FakeFrameworkContext()); //Act boot.ConfigureApplication("rw-test", "test", configXml, new BendyObject(installModel)); //Assert Assert.AreEqual(@"<configuration> <configSections> <sectionGroup name=""umbraco""> <sectionGroup name=""persistenceProviderSettings""> <section name=""test"" type=""Umbraco.Framework.Persistence.NHibernate.Config.ProviderConfigurationSection, Umbraco.Framework.Persistence.NHibernate, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"" /> </sectionGroup> </sectionGroup> </configSections> <connectionStrings> <add name=""test.ConnString"" connectionString=""Data Source=testserver; Initial Catalog=test;User Id=testuser;Password=testpass"" providerName=""System.Data.SqlClient"" /> </connectionStrings> <appSettings /> <umbraco> <persistenceProviderSettings> <test connectionStringKey=""test.ConnString"" sessionContext=""web"" driver=""MsSql2008"" /> </persistenceProviderSettings> </umbraco> </configuration>", configXml.ToString()); }
/// <summary> /// Returns a collection of all connection string registered via deep config /// </summary> /// <param name="manager"></param> /// <returns></returns> public static IEnumerable <ConnectionStringSettings> GetConnectionStrings(this DeepConfigManager manager) { var settings = PluginManagerConfiguration.GetSettings(); return(manager.GetConnectionStrings(settings.PluginsPath)); }
public ActionResult DatabaseForm(DatabaseInstallModel model) { if (!ModelState.IsValid) { return(View(model)); } //create a new web.config file in /plugins for the providers to write to var configFile = new FileInfo( Path.Combine(HttpContext.Server.MapPath("~/App_Data/Rebel/HiveConfig"), "web.config")); Directory.CreateDirectory(configFile.Directory.FullName); var configXml = DeepConfigManager.CreateNewConfigFile(configFile, true); //for each hive provider, get them to serialize their config to our xml file foreach (var hive in _requestContext.Application.Hive.GetAllReadWriteProviders()) { var installStatus = hive.Bootstrapper.GetInstallStatus(); //even if it's not completed, then overwrite if (installStatus.StatusType == InstallStatusType.RequiresConfiguration || installStatus.StatusType == InstallStatusType.Pending || installStatus.StatusType == InstallStatusType.TriedAndFailed) { //TODO: This will currently only work for the nhibernate provider since we're just passing in the DatabaseInstallmodel as the install params hive.Bootstrapper.ConfigureApplication(hive.ProviderMetadata.Alias, configXml, new BendyObject(model)); } } // Test connection strings if (model.DatabaseType != DatabaseServerType.SQLCE) { // We don't test SQL CE connection string as we will just create the DB if it doesn't exist try { foreach ( var connStringElement in configXml.Element("configuration").Element("connectionStrings").Descendants("add")) { var connString = connStringElement.Attribute("connectionString").Value; var providerName = connStringElement.Attribute("providerName").Value; var factory = DbProviderFactories.GetFactory(providerName); using (var conn = factory.CreateConnection()) { conn.ConnectionString = connString; conn.Open(); if (model.DatabaseType == DatabaseServerType.Custom || model.DatabaseType == DatabaseServerType.MSSQL) { var cmd = conn.CreateCommand(); cmd.CommandText = "SELECT SERVERPROPERTY('productversion')"; var reader = cmd.ExecuteReader(); while (reader.Read()) { var versionString = reader[0].ToString(); var version = Convert.ToInt32(versionString.Replace(".", "").PadRight(10, '0')); if (version < 1000160000) // SQL 2008 RTM { throw new ApplicationException(string.Format("Unsupported version of SQL Server. SQL 2008 RTM (10.00.1600) or greater is required, but found {0}.", versionString)); } } } } } } catch (global::System.Exception ex) { ModelState.AddModelError("", "An error occured while testing connection string: " + ex.Message); return(View(model)); } } //now save the new config file configXml.Save(configFile.FullName); return(RedirectToAction("DatabaseInstall")); }