示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Configuration"/> class.
 /// </summary>
 public Configuration(IConfigurationBuilder <IConfiguration> builder)
 {
     properties_   = builder.Properties;
     repositories_ = builder.Repositories;
     providers_    = builder.Providers;
     xml_elements_ = builder.XmlElements;
 }
示例#2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Configuration"/> class
 /// that is empty.
 /// </summary>
 public Configuration()
 {
     properties_   = new DictionaryValue();
     repositories_ = new RepositoriesNode();
     providers_    = new ProvidersNode();
     xml_elements_ = new XmlElementsNode();
 }
示例#3
0
 /// <summary>
 /// Copies the configuration data from the specified
 /// <see cref="Configuration"/> object.
 /// </summary>
 /// <param name="configuration">
 /// A <see cref="Configuration"/> object that contains the
 /// configuration data to be copied.
 /// </param>
 public void CopyFrom(Configuration configuration)
 {
     providers_    = configuration.providers_;
     repositories_ = configuration.repositories_;
     xml_elements_ = configuration.xml_elements_;
     log_level_    = configuration.log_level_;
     properties_   = configuration.properties_;
 }
    /// <summary>
    /// Parses the specified <see cref="XmlElement"/> element into a
    /// <see cref="ProvidersNode"/> object.
    /// </summary>
    /// <param name="element">
    /// A Xml element that contains the providers configuration data.
    /// </param>
    /// <param name="base_directory">
    /// The base directory to use when resolving the providers's location.
    /// </param>
    /// <returns>
    /// A <see cref="ProvidersNode"/> containing the configured providers.
    /// </returns>
    /// <exception cref="ArgumentNullException">
    /// <paramref name="element"/> is a <c>null</c> reference.
    /// </exception>
    /// <exception cref="ConfigurationException">
    /// The <paramref name="element"/> contains invalid configuration data.
    /// </exception>
    public static ProvidersNode Parse(XmlElement element, string base_directory) {
      CheckPreconditions(element, base_directory);
      IList<UnresolvedOptions> unresolved_options_references =
        new List<UnresolvedOptions>();
      List<ReplicasNode> replicas = new List<ReplicasNode>();
      Dictionary<string, IProviderOptions> reference_table =
        new Dictionary<string, IProviderOptions>();
      ProvidersNode providers = new ProvidersNode();
      foreach (XmlNode node in element.ChildNodes) {
        if (node.NodeType == XmlNodeType.Element) {
          if (Strings.AreEquals(node.Name, Strings.kProviderNodeName)) {
            IList<string> references;
            IProviderNode provider =
              ProviderNode
                .Parse((XmlElement) node, base_directory, out references);

            IProvidersNodeGroup providers_node_group;
            if (!providers.GetProvidersNodeGroup(provider.Group,
              out providers_node_group)) {
              providers_node_group = new ProvidersNodeGroup(provider.Group);
              providers.Add(providers_node_group);
            }
            providers_node_group.Add(provider);

            // Associate each alias with the provider object.
            foreach (string alias in provider.Aliases) {
              providers_node_group.Add(alias, provider);
            }

            // Add the provider to the unresolved providers list if it has
            // references to be resolved.
            if (references.Count > 0) {
              unresolved_options_references
                .Add(new UnresolvedOptions(provider.Options, references));
            }
          } else if (Strings.AreEquals(node.Name, Strings.kOptionsNodeName)) {
            ParseReferenceTable((XmlElement) node,
              unresolved_options_references, reference_table);
          } else if (Strings.AreEquals(node.Name, Strings.kReplicasNodeName)) {
            replicas.Add(ReplicasNode.Parse((XmlElement) node));
          }
        }

        if (unresolved_options_references.Count > 0) {
          if (reference_table.Count == 0) {
            throw new ConfigurationException(
              Resources.Resources.Configuration_providers_missing_reference);
          }
          ResolveOptionsReferences(unresolved_options_references,
            reference_table);
        }

        if (replicas.Count > 0) {
          CreateReplicas(replicas, providers);
        }
      }
      return providers;
    }
示例#5
0
        /// <summary>
        /// Parses the configuration node using the nohros schema.
        /// </summary>
        /// <param name="element">
        /// A Xml element representing the configuration root node.
        /// </param>
        /// <remarks>
        /// The <paramref name="element"/> does not need to be the nohros
        /// configuration node, but a node with name "nohros" must exists on the
        /// node hierarchy.
        /// </remarks>
        T Parse(XmlElement element)
        {
            XmlElement root_node = GetRootNode(element);

            // the logger is used by some methods above and the level threshold of it
            // could be overloaded by a configuration key. So, we need to do the
            // first logger instantiation here and adjust the threshold level if
            // needed.
            builder.SetLogLevel(GetLogLevel(root_node));

            // parse any internal property
            if (use_dynamic_property_assignment_)
            {
                ParseProperties(element, this);
                ParseProperties(element, builder);
            }

            // parse the know configuration nodes.
            foreach (XmlNode node in root_node.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    string name = node.Name;
                    if (Strings.AreEquals(name, Strings.kRepositoriesNodeName))
                    {
                        builder.SetRepositories(RepositoriesNode.Parse((XmlElement)node,
                                                                       location_));
                    }
                    else if (Strings.AreEquals(name, Strings.kProvidersNodeName))
                    {
                        builder.SetProviders(ProvidersNode.Parse((XmlElement)node,
                                                                 location_));
                    }
                    else if (Strings.AreEquals(name, Strings.kXmlElementsNodeName))
                    {
                        XmlElementsNode xml_elements =
                            XmlElementsNode.Parse((XmlElement)node);

                        // Add the element that was used to configure this class to the
                        // collection of xml elements nodes.
                        xml_elements[Strings.kRootXmlElementName] = element;
                        builder.SetXmlElements(xml_elements);
                    }
                }
            }

            OnParseComplete(this);

            T configuration = CreateConfiguration(builder);

            OnLoadComplete(configuration);

            return(configuration);
        }
        public void ShouldResolveOptionsReferences()
        {
            const string xml_config_file = @"<providers>
    <options name='sql-connection-provider'>
      <option name='connection-string' value='SQL-CONNECTION-STRING'/>
      <option name='schema' value='dbo'/>
    </options>
    <options name=""my-options"">
      <option ref='sql-connection-provider'/>
    </options>

    <provider name=""my-provider"" type="""">
      <options>
        <option name ='my-provider-option'/>
        <option ref='sql-connection-provider'/>
      </options>
    </provider>

    <provider name=""my-provider-two"" type="""">
      <options>
        <option ref='my-options'/>
      </options>
    </provider>
  </providers>";

            var document = new XmlDocument();

            document.LoadXml(xml_config_file);
            var            xml_element    = (XmlElement)document.FirstChild;
            IProvidersNode providers_node = ProvidersNode.Parse(xml_element,
                                                                Path.AbsoluteForApplication(string.Empty));

            Assert.AreEqual(1, providers_node.Count);
            IProviderNode provider = providers_node[string.Empty]["my-provider"];

            Assert.AreEqual(3, provider.Options.Count);
            Assert.AreEqual(true, provider.Options.ContainsKeys("connection-string"));
            Assert.AreEqual(true, provider.Options.ContainsKeys("schema"));
            Assert.AreEqual(true, provider.Options.ContainsKeys("my-provider-option"));

            Assert.AreEqual(1, providers_node.Count);
            provider = providers_node[string.Empty]["my-provider-two"];
            Assert.AreEqual(2, provider.Options.Count);
            Assert.AreEqual(true, provider.Options.ContainsKeys("connection-string"));
            Assert.AreEqual(true, provider.Options.ContainsKeys("schema"));
        }
        public void ShouldThrowExceptionWhenOptionReferenceDoesNotExist()
        {
            const string xml_config_file = @"<providers>
    <options name=""sql-connection-provider"">
      <connection-string>SQL-CONNECTION-STRING</connection-string>
      <schema>dbo</schema>
    </options>
    <options name=""my-options"">
      <option ref=""sql-connection""/>
    </options>
  </providers>";

            var document = new XmlDocument();

            document.LoadXml(xml_config_file);
            var xml_element = (XmlElement)document.FirstChild;

            Assert.Throws <ConfigurationException> (
                () => ProvidersNode.Parse(xml_element,
                                          Path.AbsoluteForApplication(string.Empty)));
        }
示例#8
0
        /// <summary>
        /// Creates a instance of the <see cref="CommonNode"/> object by parsing
        /// the given <paramref name="element"/> Xml element.
        /// </summary>
        /// <param name="element">
        /// A  <see cref="XmlElement"/> that contains the common configuration
        /// data.
        /// </param>
        /// <param name="base_directory">
        /// The path to the directory to use as the base directory when resolving
        /// relative paths.
        /// </param>
        /// <returns>
        /// A <see cref="CommonNode"/> containing the common configuration data.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="element"/> is a <c>null</c> reference.
        /// </exception>
        /// <exception cref="ConfigurationException">
        /// The <paramref name="element"/> contains invalid configuration data.
        /// </exception>
        /// <remarks>
        /// The <see cref="base_directory"/> is used to resolve the relative paths.
        /// </remarks>
        public static CommonNode Parse(XmlElement element, string base_directory)
        {
            CommonNode common = new CommonNode();

            foreach (XmlNode node in element)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    string name = node.Name;
                    if (StringsAreEquals(name, Strings.kRepositoryNodeName))
                    {
                        common.repositories_ = RepositoriesNode.Parse(element,
                                                                      base_directory);
                    }
                    else if (StringsAreEquals(name, Strings.kProvidersNodeName))
                    {
                        common.providers_ = ProvidersNode.Parse(element, base_directory);
                    }
                }
            }
            return(common);
        }
示例#9
0
        static void CreateReplicas(IList <ReplicasNode> replicas,
                                   ProvidersNode providers)
        {
            ProvidersNodeGroup   group  = null;
            List <IProviderNode> clones =
                new List <IProviderNode>(providers.Count * replicas.Count);

            foreach (ReplicasNode replica in replicas)
            {
                if (!providers.GetProvidersNodeGroup(replica.Group, out group))
                {
                    throw new ConfigurationException(string.Format(
                                                         Resources.Resources.
                                                         Configuration_providers_inexistent_replicas_group,
                                                         replica.Group));
                }
                clones.AddRange(Replicate(replica, group));
            }

            if (group != null)
            {
                group.AddRange(clones);
            }
        }
示例#10
0
    static void CreateReplicas(IList<ReplicasNode> replicas,
      ProvidersNode providers) {
      ProvidersNodeGroup group = null;
      List<IProviderNode> clones =
        new List<IProviderNode>(providers.Count*replicas.Count);
      foreach (ReplicasNode replica in replicas) {
        if (!providers.GetProvidersNodeGroup(replica.Group, out group)) {
          throw new ConfigurationException(string.Format(
            Resources.Resources.
              Configuration_providers_inexistent_replicas_group,
            replica.Group));
        }
        clones.AddRange(Replicate(replica, group));
      }

      if (group != null) {
        group.AddRange(clones);
      }
    }
示例#11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Configuration"/> class.
 /// </summary>
 public Configuration(IConfigurationBuilder<IConfiguration> builder) {
   properties_ = builder.Properties;
   repositories_ = builder.Repositories;
   providers_ = builder.Providers;
   xml_elements_ = builder.XmlElements;
 }
示例#12
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Configuration"/> class
 /// that is empty.
 /// </summary>
 public Configuration() {
   properties_ = new DictionaryValue();
   repositories_ = new RepositoriesNode();
   providers_ = new ProvidersNode();
   xml_elements_ = new XmlElementsNode();
 }
示例#13
0
 /// <summary>
 /// Copies the configuration data from the specified
 /// <see cref="Configuration"/> object.
 /// </summary>
 /// <param name="configuration">
 /// A <see cref="Configuration"/> object that contains the
 /// configuration data to be copied.
 /// </param>
 public void CopyFrom(Configuration configuration) {
   providers_ = configuration.providers_;
   repositories_ = configuration.repositories_;
   xml_elements_ = configuration.xml_elements_;
   log_level_ = configuration.log_level_;
   properties_ = configuration.properties_;
 }
示例#14
0
        /// <summary>
        /// Parses the specified <see cref="XmlElement"/> element into a
        /// <see cref="ProvidersNode"/> object.
        /// </summary>
        /// <param name="element">
        /// A Xml element that contains the providers configuration data.
        /// </param>
        /// <param name="base_directory">
        /// The base directory to use when resolving the providers's location.
        /// </param>
        /// <returns>
        /// A <see cref="ProvidersNode"/> containing the configured providers.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="element"/> is a <c>null</c> reference.
        /// </exception>
        /// <exception cref="ConfigurationException">
        /// The <paramref name="element"/> contains invalid configuration data.
        /// </exception>
        public static ProvidersNode Parse(XmlElement element, string base_directory)
        {
            CheckPreconditions(element, base_directory);
            IList <UnresolvedOptions> unresolved_options_references =
                new List <UnresolvedOptions>();
            List <ReplicasNode> replicas = new List <ReplicasNode>();
            Dictionary <string, IProviderOptions> reference_table =
                new Dictionary <string, IProviderOptions>();
            ProvidersNode providers = new ProvidersNode();

            foreach (XmlNode node in element.ChildNodes)
            {
                if (node.NodeType == XmlNodeType.Element)
                {
                    if (Strings.AreEquals(node.Name, Strings.kProviderNodeName))
                    {
                        IList <string> references;
                        IProviderNode  provider =
                            ProviderNode
                            .Parse((XmlElement)node, base_directory, out references);

                        IProvidersNodeGroup providers_node_group;
                        if (!providers.GetProvidersNodeGroup(provider.Group,
                                                             out providers_node_group))
                        {
                            providers_node_group = new ProvidersNodeGroup(provider.Group);
                            providers.Add(providers_node_group);
                        }
                        providers_node_group.Add(provider);

                        // Associate each alias with the provider object.
                        foreach (string alias in provider.Aliases)
                        {
                            providers_node_group.Add(alias, provider);
                        }

                        // Add the provider to the unresolved providers list if it has
                        // references to be resolved.
                        if (references.Count > 0)
                        {
                            unresolved_options_references
                            .Add(new UnresolvedOptions(provider.Options, references));
                        }
                    }
                    else if (Strings.AreEquals(node.Name, Strings.kOptionsNodeName))
                    {
                        ParseReferenceTable((XmlElement)node,
                                            unresolved_options_references, reference_table);
                    }
                    else if (Strings.AreEquals(node.Name, Strings.kReplicasNodeName))
                    {
                        replicas.Add(ReplicasNode.Parse((XmlElement)node));
                    }
                }

                if (unresolved_options_references.Count > 0)
                {
                    if (reference_table.Count == 0)
                    {
                        throw new ConfigurationException(
                                  Resources.Resources.Configuration_providers_missing_reference);
                    }
                    ResolveOptionsReferences(unresolved_options_references,
                                             reference_table);
                }

                if (replicas.Count > 0)
                {
                    CreateReplicas(replicas, providers);
                }
            }
            return(providers);
        }