Пример #1
0
 ILoginModule CreateLoginModule(IConfiguration settings,
                                IProviderNode provider)
 {
     return(RuntimeTypeFactory <ILoginModuleFactory>
            .CreateInstanceFallback(provider, settings)
            .CreateLoginModule(provider.Options.ToDictionary()));
 }
Пример #2
0
        /// <summary>
        /// Creates an instance of the <see cref="IConnectionProvider"/> class.
        /// <paramref name="key"/>.
        /// </summary>
        /// <param name="key">
        /// The name of the provider that should be created
        /// </param>
        /// <returns>
        /// The newly created <see cref="IConnectionProvider"/> object.
        /// </returns>
        /// <exception cref="KeyNotFoundException"></exception>
        /// <remarks>
        /// This method used the <paramref name="key"/> as a index into the list
        /// of providers that was specified in constructor and them uses the found
        /// <see cref=" IProviderNode"/> to construct an instance of the
        /// <see cref="IConnectionProvider"/> class, if a provider with name
        /// <paramref name="key"/> is not found, this method raises a
        /// <see cref="KeyNotFoundException"/> exception.
        /// </remarks>
        public override IConnectionProvider Load(string key)
        {
            IProviderNode provider = providers_[key];

            return(ProviderFactory <IConnectionProviderFactory>
                   .CreateProviderFactory(provider)
                   .CreateProvider(provider.Options.ToDictionary()));
        }
Пример #3
0
        /// <summary>
        /// Gets the <see cref="Type"/> of a provider, using the specified provider
        /// node.
        /// </summary>
        /// <param name="node">A <see cref="IProviderNode"/> object that contains
        /// information about the type <typeparamref name="T"/></param>
        /// <returns>The type instance that represents the exact runtime type of
        /// the specified provider or null if the type could not be loaded.
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="node"/> is a
        /// null reference.</exception>
        /// <remarks>
        /// If the location of the assemlby is specified on the configuration node
        /// we will try load the assembly using this location and them get the type
        /// from the loaded assembly.
        /// </remarks>
        public static Type GetProviderFactoryType(IProviderNode node)
        {
            if (node == null)
            {
                throw new ArgumentNullException("node");
            }

            // Try to get the type from the loaded assemblies.
            Type type = Type.GetType(node.Type);

            // attempt to load .NET type of the provider. If the location of the
            // assemlby is specified we need to load the assembly and try to get the
            // type from the loaded assembly. The name of the assembly will be
            // extracted from the provider type.
            if (type == null)
            {
                string assembly_name = node.Type;
                int    num           = assembly_name.IndexOf(',');
                if (num == -1)
                {
                    throw new ProviderException(
                              string.Format(
                                  Resources.Resources.Provider_AssemblyNotSpecified, assembly_name));
                }

                assembly_name = assembly_name.Substring(num + 1).Trim();
                int num2 = assembly_name.IndexOfAny(new char[] { ' ', ',' });
                if (num2 != -1)
                {
                    assembly_name = assembly_name.Substring(0, num2);
                }

                if (!assembly_name.EndsWith(".dll"))
                {
                    assembly_name = assembly_name + ".dll";
                }

                string assembly_path =
                    Path.Combine(node.Location, assembly_name);

                if (!File.Exists(assembly_path))
                {
                    throw new ProviderException(
                              string.Format(
                                  Resources.Resources.Provider_LoadAssembly, assembly_path));
                }

                try {
                    Assembly assembly = Assembly.LoadFrom(assembly_path);
                    type = assembly.GetType(node.Type.Substring(0, num));
                } catch (Exception ex) {
                    throw new ProviderException(
                              string.Format(
                                  Resources.Resources.Provider_LoadAssembly, assembly_path), ex);
                }
            }
            return(type);
        }
Пример #4
0
 /// <summary>
 /// Gets a <see cref="IProviderNode"/> whose anme is <paramref name="name"/>
 /// and belongs to the unamed providers group.
 /// </summary>
 /// <param name="providers">
 /// A <see cref="IProvidersNode"/> taht contains a group whose name is
 /// <see cref="string.Empty"/>.
 /// </param>
 /// <param name="name">
 /// The name of the provider to get.
 /// </param>
 /// <returns></returns>
 public static bool GetProviderNode(this IProvidersNode providers,
   string name, out IProviderNode provider) {
   IProvidersNodeGroup group;
   if (providers.GetProvidersNodeGroup(string.Empty, out group)) {
     return group.GetProviderNode(name, out provider);
   }
   provider = default(IProviderNode);
   return false;
 }
Пример #5
0
        IMetricsDao CreateMetricsRepository()
        {
            IProviderNode provider = settings_.Providers
                                     .GetProviderNode(R.kMetricsDataProviderName);

            return(RuntimeTypeFactory <IMetricsRepositoryFactory>
                   .CreateInstanceFallback(provider, settings_)
                   .CreateMetricsDataProvider(provider.Options.ToDictionary()));
        }
Пример #6
0
 /// <summary>
 /// Adds the given <see cref="IProvidersNode"/> object to a empty
 /// <see cref="IProvidersNodeGroup"/> inside the given
 /// <see cref="IProvidersNode"/> object using the given provider name.
 /// </summary>
 /// <param name="nodes">
 /// A <see cref="IProvidersNode"/> object to add the <paramref name="node"/>
 /// </param>
 /// <param name="node">
 /// The <see cref="IProvidersNode"/> object to be added to the
 /// <paramref name="nodes"/>.
 /// </param>
 /// <param name="name">
 /// The name to be associated with the provider node.
 /// </param>
 public static void Add(this IProvidersNode nodes, string name,
   IProviderNode node) {
   IProvidersNodeGroup empty_provider_group;
   if (!nodes.GetProvidersNodeGroup(string.Empty, out empty_provider_group)) {
     empty_provider_group = new ProvidersNodeGroup();
     nodes.Add(empty_provider_group);
   }
   empty_provider_group.Add(name, node);
 }
Пример #7
0
        ICacheProvider GetCacheProvider()
        {
            IProviderNode provider =
                settings_.Providers.GetProviderNode(Strings.kCacheProviderName);

            return(RuntimeTypeFactory <ICacheProviderFactory>
                   .CreateInstanceFallback(provider, settings_)
                   .CreateCacheProvider(provider.Options.ToDictionary()));
        }
Пример #8
0
        /// <summary>
        /// Gets a <see cref="IProviderNode"/> object whose name is
        /// <paramref name="name"/> and is not associated with any group.
        /// </summary>
        /// <param name="name">
        /// The name of the <see cref="IProviderNode"/> to retrieve.
        /// </param>
        /// <remarks>
        /// This methos is a shortcut of
        /// the most common variation of the
        /// <see cref="IProvidersNodeGroup.GetProviderNode(string)"/> method.
        /// </remarks>
        public bool GetProviderNode(string name, out IProviderNode node)
        {
            IProvidersNodeGroup group;

            if (GetProvidersNodeGroup(string.Empty, out group))
            {
                return(group.GetProviderNode(name, out node));
            }
            node = default(IProviderNode);
            return(false);
        }
Пример #9
0
        /// <summary>
        /// Adds the given <see cref="IProvidersNode"/> object to a empty
        /// <see cref="IProvidersNodeGroup"/> inside the given
        /// <see cref="IProvidersNode"/> object using the given provider name.
        /// </summary>
        /// <param name="nodes">
        /// A <see cref="IProvidersNode"/> object to add the <paramref name="node"/>
        /// </param>
        /// <param name="node">
        /// The <see cref="IProvidersNode"/> object to be added to the
        /// <paramref name="nodes"/>.
        /// </param>
        /// <param name="name">
        /// The name to be associated with the provider node.
        /// </param>
        public static void Add(this IProvidersNode nodes, string name,
                               IProviderNode node)
        {
            IProvidersNodeGroup empty_provider_group;

            if (!nodes.GetProvidersNodeGroup(string.Empty, out empty_provider_group))
            {
                empty_provider_group = new ProvidersNodeGroup();
                nodes.Add(empty_provider_group);
            }
            empty_provider_group.Add(name, node);
        }
Пример #10
0
        /// <summary>
        /// Gets a <see cref="IProviderNode"/> whose anme is <paramref name="name"/>
        /// and belongs to the unamed providers group.
        /// </summary>
        /// <param name="providers">
        /// A <see cref="IProvidersNode"/> taht contains a group whose name is
        /// <see cref="string.Empty"/>.
        /// </param>
        /// <param name="name">
        /// The name of the provider to get.
        /// </param>
        /// <returns></returns>
        public static bool GetProviderNode(this IProvidersNode providers,
                                           string name, out IProviderNode provider)
        {
            IProvidersNodeGroup group;

            if (providers.GetProvidersNodeGroup(string.Empty, out group))
            {
                return(group.GetProviderNode(name, out provider));
            }
            provider = default(IProviderNode);
            return(false);
        }
Пример #11
0
        /// <summary>
        /// Creates an instance of the <see cref="QueryResolver"/> object using the
        /// specified cache provider, common data provider and query settings.
        /// </summary>
        /// <returns>
        /// The created <see cref="QueryResolver"/> object.
        /// </returns>
        public QueryResolver CreateQueryResolver()
        {
            IProviderNode provider = settings_
                                     .Providers
                                     .GetProviderNode(Strings.kCacheProviderName);
            ICacheProvider cache_provider =
                RuntimeTypeFactory <ICacheProviderFactory>
                .CreateInstanceFallback(provider, settings_)
                .CreateCacheProvider(provider.Options.ToDictionary());

            return(CreateQueryResolver(cache_provider));
        }
Пример #12
0
        static T CreateProviderFactory(IProviderNode node, bool fallback,
                                       params object[] args)
        {
            Exception inner_exception = null;

            // A try/catch block is used here because this method should throw only
            // a ProviderException, any other exception throwed should be packed
            // into a ProviderException.
            try {
                Type type = GetProviderFactoryType(node);
                if (type != null)
                {
                    // create a new object instance using a public or non-public
                    // constructor.
                    const BindingFlags kFlags =
                        BindingFlags.CreateInstance | BindingFlags.Public |
                        BindingFlags.Instance | BindingFlags.NonPublic;

                    T new_obj = null;

                    // A try catch block is used here because we need to fallback, if
                    // desired, to the default constructor if the first CreateInstance
                    // fails because of a missing constructor.
                    try {
                        new_obj =
                            Activator.CreateInstance(type, kFlags, null, args, null) as T;
                    } catch (MissingMethodException) {
                        if (fallback)
                        {
                            new_obj =
                                Activator.CreateInstance(type, kFlags, null, null, null) as T;
                        }
                    }

                    if (new_obj != null)
                    {
                        return(new_obj);
                    }
                }
            } catch (ProviderException) {
                throw;
            } catch (Exception ex) {
                // minimizing code duplication.
                inner_exception = ex;
            }

            // the provider could not be created and we need to pack the exception
            // into a new ProviderException exception.
            throw new ProviderException(
                      string.Format(Resources.Resources.TypeLoad_CreateInstance,
                                    typeof(T)), inner_exception);
        }
Пример #13
0
 /// <summary>
 /// Creates a new instance of the <typeparamref name="T"/> class by using
 /// the type that is defined on the configuration node.
 /// </summary>
 /// <param name="node">A <see cref="IProviderNode"/> object that contains
 /// information about the type <typeparamref name="T"/></param>
 /// <param name="args">An array of arguments that match in number, order,
 /// and type the parameters of the constructor to invoke. If args is an
 /// empty array or null, the constructor that takes no parameters(the
 /// default constructor) is invoked.</param>
 /// <returns>An instance of the <typeparamref name="T"/> class.
 /// or null if a class of the type <typeparamref name="T"/> could not be
 /// created.</returns>
 /// <remarks>
 /// A exception is never raised by this method. If a exception is raised
 /// by the object constructor it will be catched and <c>null</c> will be
 /// returned. If you need to know about the exception use the method
 /// <see cref="CreateProviderFactory"/>.
 /// </remarks>
 /// <seealso cref="CreateProviderFactory"/>
 /// <seealso cref="IProviderNode"/>
 public static T CreateProviderFactoryNoException(IProviderNode node,
                                                  params object[] args)
 {
     // A try/catch block is used here because this method should not throw
     // any exception.
     try {
         return(CreateProviderFactory(node, args));
     } catch (ProviderException) {
         // TODO: Add meaing to the exception.
         MustLogger.ForCurrentProcess.Error("");
     }
     return(null);
 }
Пример #14
0
        /// <summary>
        /// Configures the logger that should be used by the application.
        /// </summary>
        public void ConfigureLogger()
        {
            try {
                IProviderNode provider = settings_.Providers
                                         .GetProviderNode(Strings.kLoggingProviderName);

                ILogger logger = RuntimeTypeFactory <ILoggerFactory>
                                 .CreateInstanceFallback(provider, settings_)
                                 .CreateLogger(provider.Options.ToDictionary());

                HttpQueryLogger.ForCurrentProcess.Logger = logger;
            } catch {
                // fails silently.
            }
        }
Пример #15
0
        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"));
        }
Пример #16
0
 /// <summary>
 /// Creates a new instance of the <typeparamref name="T"/> class by using
 /// the type that is defined on the configuration node.
 /// </summary>
 /// <param name="node">
 /// A <see cref="IProviderNode"/> object that contains information about
 /// the type <typeparamref name="T"/>.
 /// </param>
 /// <param name="args">
 /// An array of arguments that match in number, order, and type the
 /// parameters of the constructor to invoke. If args is an empty array or
 /// null, the constructor that takes no parameters(the default constructor)
 /// is invoked.
 /// </param>
 /// <returns>
 /// An instance of the <typeparamref name="T"/> class.
 /// </returns>
 /// <exception cref="ProviderException">
 /// A instance of the specified type could not be created.
 /// </exception>
 public static T CreateProviderFactory(IProviderNode node,
                                       params object[] args)
 {
     return(CreateProviderFactory(node, false, args));
 }
Пример #17
0
 /// <summary>
 /// Creates a new instance of the <typeparamref name="T"/> class by using
 /// the type defined by the <paramref name="node"/> and the specified
 /// arguments, falling back to the default constructor.
 /// </summary>
 /// <param name="node">
 /// A <see cref="IProviderNode"/> object that contains information about
 /// the type <typeparamref name="T"/>
 /// </param>
 /// <param name="args">
 /// An array of arguments that match in number, order,
 /// and type the parameters of the constructor to invoke. If args is an
 /// empty array or null, the constructor that takes no parameters(the
 /// default constructor) is invoked.
 /// </param>
 /// <returns>
 /// An instance of the <typeparamref name="T"/> class.
 /// </returns>
 /// <remarks>
 /// If a constructor that match in number, order and type the specified
 /// array of arguments is not found, this method try to create an instance
 /// of the type <typeparamref name="T"/> using the default constructor.
 /// </remarks>
 /// <seealso cref="CreateProviderFactory"/>
 /// <seealso cref="IProviderNode"/>
 public static T CreateProviderFactoryFallback(IProviderNode node,
                                               params object[] args)
 {
     return(CreateProviderFactory(node, true, args));
 }
Пример #18
0
 /// <inheritdoc/>
 public bool GetProviderNode(string name, out IProviderNode provider)
 {
     return(GetChildNode(name, out provider));
 }
Пример #19
0
 /// <inheritdoc/>
 public void Add(string name, IProviderNode node)
 {
     AddChildNode(name, node);
 }
Пример #20
0
 /// <inheritdoc/>
 public void Add(IProviderNode node)
 {
     AddChildNode(node);
 }
Пример #21
0
 /// <summary>
 /// Adds the given <see cref="IProvidersNode"/> object to a empty
 /// <see cref="IProvidersNodeGroup"/> inside the given
 /// <see cref="IProvidersNode"/> object.
 /// </summary>
 /// <param name="nodes">
 /// A <see cref="IProvidersNode"/> object to add the <paramref name="node"/>
 /// </param>
 /// <param name="node">
 /// The <see cref="IProvidersNode"/> object to be added to the
 /// <paramref name="nodes"/>.
 /// </param>
 public static void Add(this IProvidersNode nodes, IProviderNode node)
 {
     nodes.Add(node.Name, node);
 }
Пример #22
0
 /// <inheritdoc/>
 public bool GetProviderNode(string name, out IProviderNode provider) {
   return GetChildNode(name, out provider);
 }
Пример #23
0
 /// <inheritdoc/>
 public void Add(string name, IProviderNode node) {
   AddChildNode(name, node);
 }
Пример #24
0
 /// <inheritdoc/>
 public void Add(IProviderNode node) {
   AddChildNode(node);
 }
Пример #25
0
 /// <summary>
 /// Adds the given <see cref="IProvidersNode"/> object to a empty
 /// <see cref="IProvidersNodeGroup"/> inside the given
 /// <see cref="IProvidersNode"/> object.
 /// </summary>
 /// <param name="nodes">
 /// A <see cref="IProvidersNode"/> object to add the <paramref name="node"/>
 /// </param>
 /// <param name="node">
 /// The <see cref="IProvidersNode"/> object to be added to the
 /// <paramref name="nodes"/>.
 /// </param>
 public static void Add(this IProvidersNode nodes, IProviderNode node) {
   nodes.Add(node.Name, node);
 }
Пример #26
0
 ILoginModule CreateLoginModule(IConfiguration settings,
   IProviderNode provider) {
   return RuntimeTypeFactory<ILoginModuleFactory>
     .CreateInstanceFallback(provider, settings)
     .CreateLoginModule(provider.Options.ToDictionary());
 }
Пример #27
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);
        }
Пример #28
0
 /// <summary>
 /// Gets a <see cref="IProviderNode"/> object whose name is
 /// <paramref name="name"/> and is not associated with any group.
 /// </summary>
 /// <param name="name">
 /// The name of the <see cref="IProviderNode"/> to retrieve.
 /// </param>
 /// <remarks>
 /// This methos is a shortcut of
 /// the most common variation of the
 /// <see cref="IProvidersNodeGroup.GetProviderNode(string)"/> method.
 /// </remarks>
 public bool GetProviderNode(string name, out IProviderNode node) {
   IProvidersNodeGroup group;
   if (GetProvidersNodeGroup(string.Empty, out group)) {
     return group.GetProviderNode(name, out node);
   }
   node = default(IProviderNode);
   return false;
 }