Пример #1
0
        static AppMetrics()
        {
            string default_metrics_registry_class =
                ConfigurationManager.AppSettings[kDefaultMetricsRegistryKey];

            if (default_metrics_registry_class != null)
            {
                var runtime_type = new RuntimeType(default_metrics_registry_class);

                Type type = RuntimeType.GetSystemType(runtime_type);

                if (type != null)
                {
                    ForCurrentProcess =
                        RuntimeTypeFactory <IMetricsRegistry>
                        .CreateInstanceFallback(runtime_type);
                }
            }

            if (ForCurrentProcess == null)
            {
                ForCurrentProcess = new MetricsRegistry();
            }

            process_tags_ = new Tags.Builder();

            Configure();

            // Mark the class as not configured to allow users to
            // overrite the default behavior.
            configured_ = false;
        }
 /// <inheritdoc/>
 /// <remarks>
 /// The <typeparamref name="T"/> object should have a constructor that
 /// receives a single parameter of the type
 /// <see cref="SqlConnectionProvider"/>.
 /// </remarks>
 public T CreateProvider(IDictionary <string, string> options)
 {
     return(CreateProvider(options,
                           sql_connection_provider =>
                           RuntimeTypeFactory <T>
                           .CreateInstance(typeof(T), sql_connection_provider)));
 }
Пример #3
0
        private Dictionary <string, CILantroType> RegisterCustomTypes(CILProgram program)
        {
            var result = new Dictionary <string, CILantroType>();

            var runtimeTypeFactory = new RuntimeTypeFactory(Program.Assemblies.SingleOrDefault(), program.Modules.SingleOrDefault());

            foreach (var cilClass in program.Classes)
            {
                Type runtimeType = null;
                var  customType  = new CILantroType(cilClass, this);

                if (RuntimeTypeHelper.GetRuntimeType(cilClass.Extends) == typeof(Enum))
                {
                    runtimeType = runtimeTypeFactory.RegisterEnumType(cilClass);
                }
                else if (RuntimeTypeHelper.GetRuntimeType(cilClass.Extends) != null)
                {
                    runtimeType = runtimeTypeFactory.RegisterType(cilClass, this, customType);
                }

                cilClass.RuntimeType    = runtimeType;
                customType._runtimeType = runtimeType;

                result.Add(cilClass.ClassName.UniqueName, customType);
            }

            return(result);
        }
Пример #4
0
 ILoginModule CreateLoginModule(IConfiguration settings,
                                IProviderNode provider)
 {
     return(RuntimeTypeFactory <ILoginModuleFactory>
            .CreateInstanceFallback(provider, settings)
            .CreateLoginModule(provider.Options.ToDictionary()));
 }
Пример #5
0
        IMetricsDao CreateMetricsRepository()
        {
            IProviderNode provider = settings_.Providers
                                     .GetProviderNode(R.kMetricsDataProviderName);

            return(RuntimeTypeFactory <IMetricsRepositoryFactory>
                   .CreateInstanceFallback(provider, settings_)
                   .CreateMetricsDataProvider(provider.Options.ToDictionary()));
        }
Пример #6
0
        ICacheProvider GetCacheProvider()
        {
            IProviderNode provider =
                settings_.Providers.GetProviderNode(Strings.kCacheProviderName);

            return(RuntimeTypeFactory <ICacheProviderFactory>
                   .CreateInstanceFallback(provider, settings_)
                   .CreateCacheProvider(provider.Options.ToDictionary()));
        }
Пример #7
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));
        }
Пример #8
0
        /// <summary>
        /// Creates an instance of the <see cref="TResult"/> class using the
        /// information contained in the provider node named
        /// <see cref="node_name"/>.
        /// </summary>
        /// <typeparam name="TFactory">
        /// The type of the factory class that is used to create instances of the
        /// <see cref="TResult"/> class.
        /// </typeparam>
        /// <typeparam name="TResult">
        /// The type of the class that should be created.
        /// </typeparam>
        /// <param name="settings">
        /// A <see cref="IConfiguration"/> class contained the list of configured
        /// providers.
        /// </param>
        /// <param name="node_name">
        /// The name of the provider node that contains the information about the
        /// provider to be created.
        /// </param>
        /// <param name="instantiator">
        /// A <see cref="Func{T1, T2, TResult}"/> delegate that can be
        /// used to create an instance of the <see cref="TResult"/> class.
        /// </param>
        /// <returns>
        /// The <see cref="TFactory"/> class is created by either, using a
        /// constructor that accepts a <see cref="IConfiguration"/> object or a
        /// constructor that receives no parameters.
        /// </returns>
        public static TResult CreateProvider <TFactory, TResult>(
            this IConfiguration settings, string node_name,
            Func <TFactory, IDictionary <string, string>, TResult> instantiator)
            where TFactory : class
        {
            var node = settings
                       .Providers
                       .GetProviderNode(node_name);
            TFactory factory = RuntimeTypeFactory <TFactory>
                               .CreateInstanceFallback(node, settings);

            return(instantiator(factory, node.Options.ToDictionary()));
        }
Пример #9
0
 /// <summary>
 /// Creates new instances to all types that can be assigned
 /// to <typeparamref name="T" />.
 /// the types.
 /// </summary>
 /// <param name="types">
 /// A list of <see cref="Type"/> objects containing the type to be
 /// filtered and instantiated.
 /// </param>
 /// <param name="fallback">
 /// A value that indicates if we should fallback to the default constructor
 /// when a constructor that accepts the given <paramref name="args"/> as
 /// parameters is not found. Default to <c>true</c>.
 /// </param>
 /// <param name="throw">
 /// A value that indicates if exceptions should be propagated to the
 /// caller or silently ignored. Default to <c>true</c>.
 /// </param>
 /// <param name="args">
 /// An array of arguments that match the parameters of the constructor to
 /// invoke. If args is an empty array or null, or if a matching constructor
 /// is not found the constructor that takes no parameters(the default
 /// constructor) is invoked.
 /// </param>
 /// <returns>
 /// An collection of <typeparamref name="T"/> containing an instance for
 /// each type of the <paramref name="types"/> list that can be assigned to
 /// <typeparamref name="T" />.
 /// </returns>
 public static IEnumerable <T> CreateInstances <T>(
     this IEnumerable <Type> types, bool fallback, bool @throw,
     params object[] args) where T : class
 {
     return
         (types
          .Where(
              t =>
              typeof(T).IsAssignableFrom(t) && !(t.IsAbstract || t.IsInterface))
          .Select(t =>
                  RuntimeTypeFactory <T>
                  .CreateInstance(t, fallback, @throw, args)));
 }
Пример #10
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.
            }
        }
Пример #11
0
        IQueryExecutor[] GetQueryExecutors()
        {
            IProvidersNodeGroup executors_providers;

            if (settings_.Providers.GetProvidersNodeGroup(
                    Strings.kQueryExecutorsGroup, out executors_providers))
            {
                return(executors_providers
                       .Select(
                           provider => RuntimeTypeFactory <IQueryExecutorFactory>
                           .CreateInstanceFallback(provider, settings_)
                           .CreateQueryExecutor(provider.Options.ToDictionary()))
                       .ToArray());
            }
            return(new IQueryExecutor[0]);
        }
Пример #12
0
        IJsonCollectionFactory GetJsonCollectionFactory()
        {
            IProviderNode provider;

            if (settings_.Providers.GetProviderNode(Strings.kJsonCollectionProvider,
                                                    out provider))
            {
                try {
                    return(RuntimeTypeFactory <IJsonCollectionFactory>
                           .CreateInstanceFallback(provider, settings_));
                } catch (Exception exception) {
                    // log it and ignore
                    RestQLLogger.ForCurrentProcess.Error(
                        string.Format(StringResources.Log_MethodThrowsException,
                                      "GetJsonCollectionFactory", kClassName), exception);
                }
            }
            return(new JsonCollectionFactory());
        }
Пример #13
0
        /// <summary>
        /// Creates an instance of all configured providers that has a factory
        /// that implements the <see cref="IProviderFactory"/> intrface and has a
        /// constructor that accepts no parameters or a single parameter of the type
        /// <see cref="IConfiguration"/>.
        /// </summary>
        /// <param name="predicate">
        /// A <see cref="Func{TResult}"/> that is used to evaluate if we need to
        /// create a instance of the given provider node.
        /// </param>
        /// <param name="settings">
        /// A <see cref="IConfiguration"/> containing the configured providers.
        /// </param>
        /// <returns>
        /// A array containing all providers that was created using the configured
        /// factories that implements the <see cref="IProviderFactory"/> interface.
        /// </returns>
        public static TResult[] CreateProviders <TFactory, TResult>(
            this IConfiguration settings,
            Func <IProvidersNodeGroup, IProviderNode, bool> predicate,
            Func <TFactory, IDictionary <string, string>, TResult> factory)
            where TFactory : class
        {
            var providers = settings.Providers;
            var list      = new List <TResult>();

            foreach (IProvidersNodeGroup group in providers)
            {
                foreach (IProviderNode node in group)
                {
                    if (!predicate(group, node))
                    {
                        continue;
                    }

                    Type     type      = GetType(node);
                    TFactory factory_t = RuntimeTypeFactory <TFactory>
                                         .CreateInstance(type, true, false, settings);

                    if (factory_t != null)
                    {
                        try {
                            list.Add(factory(factory_t, node.Options.ToDictionary()));
                        } catch (Exception e) {
                            MustLogger.ForCurrentProcess.Error(
                                StringResources.Log_MethodThrowsException.Fmt(
                                    "CreateProviders", kClassName), e);
                            throw new ConfigurationException(e);
                        }
                    }
                }
            }
            return(list.ToArray());
        }
 /// <summary>
 /// Creates a new thunk type (and related dispatcher and inner delegate types) for the specified delegate type.
 /// </summary>
 /// <param name="delegateType">The delegate for which create a thunk type.</param>
 /// <returns>A newly created thunk type.</returns>
 protected override Type CreateThunkType(Type delegateType)
 {
     return(RuntimeTypeFactory.CreateThunkType(delegateType, ThunkBehavior.TieredCompilation));
 }
Пример #15
0
 /// <summary>
 /// Creates a new thunk type (and related dispatcher and inner delegate types) for the specified delegate type.
 /// </summary>
 /// <param name="delegateType">The delegate for which create a thunk type.</param>
 /// <returns>A newly created thunk type.</returns>
 protected override Type CreateThunkType(Type delegateType)
 {
     return(RuntimeTypeFactory.CreateThunkType(delegateType, ThunkBehavior.Interpreting));
 }