/*********************************************************************/ /* */ /* AgentService Framework */ /* www.agentservice.it */ /* */ /* Copyright (C) 2007 l.i.d.o. - DIST, University of Genoa, Italy */ /* GNU Lesser General Public License */ /* */ /* This library is free software; you can redistribute it and/or */ /* modify it under the terms of the GNU Lesser General Public */ /* License as published by the Free Software Foundation, */ /* version 2.1 of the License. */ /* */ /* This library is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ /* Lesser General Public License for more details. */ /* */ /* You should have received a copy of the GNU Lesser General Public */ /* License along with this library; if not, write to the */ /* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */ /* Boston, MA 02111-1307, USA. */ /* */ /*********************************************************************/ #region XML /// ///
/// /// 07/13/2004 /// 1.0 /// /// /// /// /// Reads and writes a Configuration object by using an xml file as storage /// /// /// /// /// /// /// ///
/// /// #endregion using System; using System.Xml; using System.Xml.XPath; using AgentService.Platform.Modules; using AgentService.Install.XmlDataModel; namespace AgentService.Platform.Configuration { /// /// Class XmlConfigurationManager. Reads and Writes an XML configuration file. /// public class XmlConfigurationManager : ConfigurationManager { /// /// configuration file /// private string file; /// /// Gets the configuration file. /// public override string File { get { return this.file; } } /// /// Constructa a ConfigurationReader object and binds it to /// the given configuration file. /// /// configuration file public XmlConfigurationManager(string file) { if (file == null) throw new ArgumentNullException("file","configuration file cannot be null"); if (System.IO.File.Exists(file) == false) throw new ArgumentException("configuration file does not exists", "file"); this.file = file; } /// /// Reads the configuration file and returns the /// corresponding configuration object /// /// Configuration object read from the file public override Configuration Read() { AgentService.Platform.Configuration.Configuration conf = new Configuration(); conf.BeginUpdate(); try { #region OLD CODE /* System.Reflection.Assembly assembly = System.Reflection.Assembly.GetAssembly(typeof(AgentService.Platform.Configuration.XmlInstallManager)); System.IO.Stream stream = assembly.GetManifestResourceStream("xmlconf.config.xsd"); System.IO.StreamReader streamReader = new System.IO.StreamReader(stream); XmlTextReader schemaReader = new XmlTextReader(streamReader.BaseStream); XmlValidatingReader reader = new XmlValidatingReader(new XmlTextReader(this.file)); reader.ValidationType = ValidationType.Schema; reader.Schemas.Add("config.xsd",schemaReader); System.Console.WriteLine("\n validating {0} ..\n", this.file); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(reader); // xmlDoc.Load(this.file); XmlElement root = xmlDoc.DocumentElement; conf.IsFirstStart = (root.Attributes["first-start"].Value == "true" ? true : false); conf.SkipFailedModules = (root.Attributes["skip-failed-modules"].Value == "true" ? true : false); XmlNode node = root.GetElementsByTagName("base-dir")[0]; conf.BaseDirPath = (string) node.Attributes["path"].Value; node = root.GetElementsByTagName("repository")[0]; conf.RepositoryFile = (string) node.Attributes["file"].Value; node = root.GetElementsByTagName("description")[0]; conf.PlatformDescription = new PlatformDescription((string) node.Attributes["name"].Value, (node.Attributes["mobile"].Value == "true" ? true : false), (node.Attributes["dynamic"].Value == "true" ? true : false), (string) node.Attributes["transport-profile"].Value); node = root.GetElementsByTagName("core")[0]; conf.CoreModulesBinPath = node.Attributes["bin-path"].Value; conf.CoreModulesConfPath = node.Attributes["conf-path"].Value; node = root.GetElementsByTagName("storage")[0]; conf.StorageModule = new ModuleInfo((string) node.Attributes["full-type-name"].Value, (string) node.Attributes["assembly-name"].Value, (string) node.Attributes["config-file"].Value, (string) node.Attributes["key"].Value, ModuleInfoState.Installed); node = root.GetElementsByTagName("persistence")[0]; conf.PersistenceModule = new ModuleInfo((string) node.Attributes["full-type-name"].Value, (string) node.Attributes["assembly-name"].Value, (string) node.Attributes["config-file"].Value, (string) node.Attributes["key"].Value, ModuleInfoState.Installed); node = root.GetElementsByTagName("messaging")[0]; conf.MessagingModule = new ModuleInfo((string) node.Attributes["full-type-name"].Value, (string) node.Attributes["assembly-name"].Value, (string) node.Attributes["config-file"].Value, (string) node.Attributes["key"].Value, ModuleInfoState.Installed); node = root.GetElementsByTagName("logging")[0]; conf.LoggingModule = new ModuleInfo((string) node.Attributes["full-type-name"].Value, (string) node.Attributes["assembly-name"].Value, (string) node.Attributes["config-file"].Value, (string) node.Attributes["key"].Value, ModuleInfoState.Installed); node = root.GetElementsByTagName("additional")[0]; conf.AdditionalModulesBinPath = node.Attributes["bin-path"].Value; conf.AdditionalModulesConfPath = node.Attributes["conf-path"].Value; XmlNodeList nodeList = root.GetElementsByTagName("module"); conf.AdditionalModules = new ModuleInfo[nodeList.Count]; int ndx = 0; foreach(XmlNode n in nodeList) { string fullTypeName = n.Attributes["full-type-name"].Value as String; string assemblyName = n.Attributes["assembly-name"].Value as String; string configFile = n.Attributes["config-file"].Value as String; string key = n.Attributes["key"].Value as String; conf.AdditionalModules[ndx++] = new ModuleInfo(fullTypeName, assemblyName, configFile, key, ModuleInfoState.Installed); } reader.Close(); schemaReader.Close(); */ #endregion XmlPlatformConfigurationInfo info = XmlPlatformConfigurationInfo.FromFile(this.file); conf.IsFirstStart = info.IsFirstStart; conf.SkipFailedModules = info.SkipFailedModules; conf.PlatformDescription = new PlatformDescription(info.Description.Name, info.Description.Dynamic, info.Description.Mobile, info.Description.TransportProfile); conf.StorageModule = new ModuleInfo(info.Core.Storage.FullTypeName, info.Core.Storage.AssemblyFile, info.Core.Storage.ConfigFile, info.Core.Storage.Key); conf.PersistenceModule = new ModuleInfo(info.Core.Persistence.FullTypeName, info.Core.Persistence.AssemblyFile, info.Core.Persistence.ConfigFile, info.Core.Persistence.Key); conf.MessagingModule = new ModuleInfo(info.Core.Messaging.FullTypeName, info.Core.Messaging.AssemblyFile, info.Core.Messaging.ConfigFile, info.Core.Messaging.Key); conf.LoggingModule = new ModuleInfo(info.Core.Logging.FullTypeName, info.Core.Logging.AssemblyFile, info.Core.Logging.ConfigFile, info.Core.Logging.Key); string coreDir = System.IO.Path.Combine(Platform.ModuleSubDir, Platform.CoreSubDir); conf.CoreModulesBinPath = (info.Core.BinPath == null ? System.IO.Path.Combine(coreDir,Platform.BinDir) : info.Core.BinPath); conf.CoreModulesConfPath = (info.Core.ConfPath == null ? System.IO.Path.Combine(coreDir,Platform.ConfDir) : info.Core.ConfPath); conf.BaseDirPath = info.BaseDir; conf.RepositoryFile = info.Repository; int nMax = info.Additional.Count; conf.AdditionalModules = new ModuleInfo[nMax]; string additionalDir = System.IO.Path.Combine(Platform.ModuleSubDir, Platform.AdditionalSubDir); conf.AdditionalModulesBinPath = (info.Additional.BinPath == null ? System.IO.Path.Combine(additionalDir, Platform.BinDir) : info.Additional.BinPath); conf.AdditionalModulesConfPath = (info.Additional.ConfPath == null ? System.IO.Path.Combine(additionalDir, Platform.ConfDir) : info.Additional.ConfPath); for (int ndx = 0; ndx < nMax; ndx++) { XmlModuleInfo infoModule = info.Additional[ndx]; conf.AdditionalModules[ndx] = new ModuleInfo(infoModule.FullTypeName, infoModule.AssemblyFile, infoModule.ConfigFile, infoModule.Key); } conf.Properties = new System.Collections.Specialized.StringDictionary(); foreach (XmlPropertyInfo property in info.Properties) { conf.Properties.Add(property.Name, property.Value); } } catch(System.Xml.Schema.XmlSchemaException ex) { conf.SetError(); conf.ErrorMessage = "error in reading xml schema: " + ex.Message; } catch(System.Xml.XPath.XPathException ex) { conf.SetError(); conf.ErrorMessage = "error processing configuration file: " + ex.Message; } catch(System.Xml.XmlException ex) { conf.SetError(); conf.ErrorMessage = "error processing configuration file: " + ex.Message; } conf.EndUpdate(); return conf; } /// /// Writes the configuration object into an Xml file /// /// Configuration object public override void Write(Configuration conf) { #region OLD CODE /* System.IO.TextWriter txtWriter = new System.IO.StreamWriter(this.file,false); System.Xml.XmlWriter writer = new XmlTextWriter(txtWriter); writer.WriteStartDocument(true); // opens the platfom tag writer.WriteWhitespace("\n"); writer.WriteStartElement("platform"); // writes platform attributes string strFirstStart = (conf.IsFirstStart ? "true" : "false"); writer.WriteAttributeString("first-start",strFirstStart); if (conf.SkipFailedModules) writer.WriteAttributeString("skip-failed-modules", "true"); else writer.WriteAttributeString("skip-failed-modules", "false"); // writes description tag writer.WriteWhitespace("\n"); writer.WriteStartElement("base-dir"); writer.WriteAttributeString("path", conf.BaseDirPath); writer.WriteEndElement(); writer.WriteWhitespace("\n"); writer.WriteStartElement("repository"); writer.WriteAttributeString("file", conf.RepositoryFile); writer.WriteEndElement(); writer.WriteWhitespace("\n"); writer.WriteStartElement("description"); writer.WriteAttributeString("name", conf.PlatformDescription.Name); writer.WriteAttributeString("transport-profile", conf.PlatformDescription.TransportProfile); if (conf.PlatformDescription.Mobile) writer.WriteAttributeString("mobile", "true"); else writer.WriteAttributeString("mobile", "false"); if (conf.PlatformDescription.Dynamic) writer.WriteAttributeString("dynamic", "true"); else writer.WriteAttributeString("dynamic", "false"); // closes description tag writer.WriteEndElement(); // opens modules tag writer.WriteStartElement("modules"); // opens core tag writer.WriteStartElement("core"); writer.WriteAttributeString("bin-path", conf.CoreModulesBinPath); writer.WriteAttributeString("conf-path", conf.CoreModulesConfPath); // writes the storage tag writer.WriteStartElement("storage"); writer.WriteAttributeString("key", conf.StorageModule.Key); writer.WriteAttributeString("full-type-name", conf.StorageModule.FullTypeName); writer.WriteAttributeString("assembly-name", conf.StorageModule.AssemblyName); writer.WriteAttributeString("config-file", conf.StorageModule.ConfigFile); writer.WriteEndElement(); writer.WriteWhitespace("\n"); // writes the messaging tag writer.WriteStartElement("messaging"); writer.WriteAttributeString("key", conf.MessagingModule.Key); writer.WriteAttributeString("full-type-name", conf.MessagingModule.FullTypeName); writer.WriteAttributeString("assembly-name", conf.MessagingModule.AssemblyName); writer.WriteAttributeString("config-file", conf.MessagingModule.ConfigFile); writer.WriteEndElement(); writer.WriteWhitespace("\n"); // writes the persistence tag writer.WriteStartElement("persistence"); writer.WriteAttributeString("key", conf.PersistenceModule.Key); writer.WriteAttributeString("full-type-name", conf.PersistenceModule.FullTypeName); writer.WriteAttributeString("assembly-name", conf.PersistenceModule.AssemblyName); writer.WriteAttributeString("config-file", conf.PersistenceModule.ConfigFile); writer.WriteEndElement(); writer.WriteWhitespace("\n"); // writes the logging tag writer.WriteStartElement("logging"); writer.WriteAttributeString("key", conf.LoggingModule.Key); writer.WriteAttributeString("full-type-name", conf.LoggingModule.FullTypeName); writer.WriteAttributeString("assembly-name", conf.LoggingModule.AssemblyName); writer.WriteAttributeString("config-file", conf.LoggingModule.ConfigFile); writer.WriteEndElement(); writer.WriteWhitespace("\n"); // closes the core tag writer.WriteEndElement(); writer.WriteWhitespace("\n"); // opens the additional tag writer.WriteStartElement("additional"); writer.WriteAttributeString("bin-path",conf.AdditionalModulesBinPath); writer.WriteAttributeString("conf-path",conf.AdditionalModulesConfPath); // writes module tags foreach(ModuleInfo mi in conf.AdditionalModules) { if (mi.State != ModuleInfoState.Removed) { writer.WriteWhitespace("\n"); writer.WriteStartElement("module"); writer.WriteAttributeString("key", mi.Key); writer.WriteAttributeString("full-type-name", mi.FullTypeName); writer.WriteAttributeString("assembly-name", mi.AssemblyName); writer.WriteAttributeString("config-file", mi.ConfigFile); writer.WriteEndElement(); writer.WriteWhitespace("\n"); } } // closes the additional tag writer.WriteEndElement(); writer.WriteWhitespace("\n"); // closes the modules tag writer.WriteEndElement(); writer.WriteWhitespace("\n"); // closes the platform tag writer.WriteEndElement(); writer.WriteWhitespace("\n"); // closes the document writer.WriteEndDocument(); writer.Close(); */ #endregion XmlPlatformConfigurationInfo info = new XmlPlatformConfigurationInfo(); info.IsFirstStart = conf.IsFirstStart; info.SkipFailedModules = conf.SkipFailedModules; info.BaseDir = conf.BaseDirPath; info.Repository = conf.RepositoryFile; info.Description.Name = conf.PlatformDescription.Name; info.Description.Mobile = conf.PlatformDescription.Mobile; info.Description.Dynamic = conf.PlatformDescription.Dynamic; info.Description.TransportProfile = conf.PlatformDescription.TransportProfile; info.Core.BinPath = conf.CoreModulesBinPath; info.Core.ConfPath = conf.CoreModulesConfPath; info.Core.Storage.Key = conf.StorageModule.Key; info.Core.Storage.FullTypeName = conf.StorageModule.FullTypeName; info.Core.Storage.AssemblyFile = conf.StorageModule.AssemblyName; info.Core.Storage.ConfigFile = conf.StorageModule.ConfigFile; info.Core.Persistence.Key = conf.PersistenceModule.Key; info.Core.Persistence.FullTypeName = conf.PersistenceModule.FullTypeName; info.Core.Persistence.AssemblyFile = conf.PersistenceModule.AssemblyName; info.Core.Persistence.ConfigFile = conf.PersistenceModule.ConfigFile; info.Core.Messaging.Key = conf.MessagingModule.Key; info.Core.Messaging.FullTypeName = conf.MessagingModule.FullTypeName; info.Core.Messaging.AssemblyFile = conf.MessagingModule.AssemblyName; info.Core.Messaging.ConfigFile = conf.MessagingModule.ConfigFile; info.Core.Logging.Key = conf.LoggingModule.Key; info.Core.Logging.FullTypeName = conf.LoggingModule.FullTypeName; info.Core.Logging.AssemblyFile = conf.LoggingModule.AssemblyName; info.Core.Logging.ConfigFile = conf.LoggingModule.ConfigFile; for (int ndx = 0; ndx < conf.AdditionalModules.Length; ndx++) { XmlModuleInfo additional = new XmlModuleInfo(); additional.Key = conf.AdditionalModules[ndx].Key; additional.FullTypeName = conf.AdditionalModules[ndx].FullTypeName; additional.AssemblyFile = conf.AdditionalModules[ndx].AssemblyName; additional.ConfigFile = conf.AdditionalModules[ndx].ConfigFile; info.Additional.Add(additional); } foreach (string key in conf.Properties.Keys) { XmlPropertyInfo property = new XmlPropertyInfo(); property.Name = key; property.Value = conf.Properties[key]; info.Properties.Add(property); } XmlPlatformConfigurationInfo.ToFile(info, this.file); } } }