Esempio n. 1
0
        FlowFileControllerService GetControllerService(IFlowFileController flowController)
        {
            // resolve repository to store flow file process details
            var logRepository = _flowFileLogFactory.Get();

            // set base path for the flow
            logRepository.DataDir = _config.BasePath;

            FlowFileControllerService processor = _flowControllerServiceFactory.Get(logRepository, flowController);

            return(processor);
        }
Esempio n. 2
0
        /// <summary>
        ///     Starts a controller.
        /// </summary>
        /// <param name="definition">Definition to configure the controller.</param>
        protected void StartController(FlowControllerDefinition definition)
        {
            Guard.ArgumentNotNull(definition, nameof(definition));

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug($"Starting controller {definition.ControllerName}.");
            }

            //flow controller type
            Type flowControllerType;

            // resolve from precompiled list of well known processors
            // or assembly resolve
            if (_dictOfControllerTypes.ContainsKey(definition.ControllerName))
            {
                flowControllerType = _dictOfControllerTypes[definition.ControllerName];
            }
            else
            {
                flowControllerType = Type.GetType(definition.TypeName);
            }

            // ReSharper disable once UsePatternMatching
            IFlowFileController flowController = _flowControllerFactory
                                                 .Create(flowControllerType);

            // flow controller
            if (flowController == null)
            {
                throw new InvalidOperationException(
                          $"Controller type does not implement {typeof(IFlowFileController).Name}.");
            }

            // build rules that will be used to validate outgoing entitities
            var specProviderBuilder = new SpecProviderBuilder(_specProviderFactory);

            // spec providers expected to be single instance in
            // the application's scope
            if (definition.FieldValidationRules != null)
            {
                Type type = null;

                if (_logger.IsTraceEnabled)
                {
                    _logger.Trace($"Activating controller type {flowController?.TargetEntityType?.AssemblyQualifiedTypeName}.");
                }

                try
                {
                    type = Type.GetType(flowController.TargetEntityType.AssemblyQualifiedTypeName);
                }
                catch (Exception ex)
                {
                    throw new ApplicationException($"Can't load type {flowController.TargetEntityType.AssemblyQualifiedTypeName}.", ex);
                }

                specProviderBuilder.Build(
                    type,
                    definition.FieldValidationRules.ToList());
            }

            // configure data source log
            flowController.Config.InDirectory  = definition.Input;
            flowController.Config.OutDirectory = definition.Output;

            flowController.Flow           = _config.Flow;
            flowController.ControllerName = definition.ControllerName;

            // apply controller configuration details
            if (definition.ConfigurationDetails != null)
            {
                var type = flowController.GetType();

                if (_logger.IsDebugEnabled)
                {
                    _logger.Debug($"Configuring properties of controller type {type.Name} has been initialized.");
                }

                foreach (var configDetail in definition.ConfigurationDetails)
                {
                    var property = type.GetProperty(configDetail.Key);
                    if (property != null)
                    {
                        if (property.GetSetMethod() != null)
                        {
                            property.SetValue(flowController, configDetail.Value);
                        }
                    }
                }
            }

            // complete initialization
            flowController.Initialize();

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug($"Flow controller {definition.ControllerName} has been initialized.");
            }


            FlowFileControllerService processor = GetControllerService(flowController);

            // configure polling internval
            processor.Interval = TimeSpan.FromSeconds(definition.PollInterval);

            // log how many files were processed
            processor.FlowFileProcessed += (o, e) => { Processed++; };

            // start reading from incoming data source
            processor.Start();

            if (_logger.IsDebugEnabled)
            {
                _logger.Debug($"Flow controller {definition.ControllerName} has started.");
            }

            // active processor
            _processor = processor;

            // add to collection of initialized controllers
            _flowControllers.Add(flowController);
        }