public void ConvertProcessorElementToEndpointWithEmptyProcessorConfigurator()
        {
            var element = new FileProcessorElement
            {
                Name = "a",
                DropPath = "b",
                Filter = "*",
                SuccessPath = "c",
                FailurePath = "d",
                InProgressPath = "e",
                ScavengeInterval = 100,
                RecoveryInterval = 100,
                PollingInterval = 130
            };

            var expected = new FileProcessorEndpoint
            {
                Name = "a",
                DropPath = "b",
                Filter = "*",
                SuccessPath = "c",
                FailurePath = "d",
                InProgressPath = "e",
                NumberOfConsumers = 1,
                ScavengeInterval = new TimeSpan(0, 0, 100),
                RecoveryInterval = new TimeSpan(0, 0, 100),
                ProcessorConfigurator = "EnergyTrading.FileProcessing.Registrars.EventBasedProcessorDefaultRegistrar, EnergyTrading.Unity",
                AdditionalFilter = typeof(DefaultFileFilter),
                PollingRestartInterval = new TimeSpan(0, 0, 60),
                PollingInterval = 130000
            };

            var candidate = element.ToEndpoint();
            Check(expected, candidate);
        }
        public void ConvertProcessorElementToEndpointWithNtfsBasedProcessorConfigurator()
        {
            var element = new FileProcessorElement
            {
                Name = "a",
                DropPath = "b",
                Filter = "*",
                SuccessPath = "c",
                FailurePath = "d",
                InProgressPath = "e",
                ScavengeInterval = 100,
                RecoveryInterval = 100,
                ProcessorConfiguratorType = "EventBased"
            };

            var expected = new FileProcessorEndpoint
            {
                Name = "a",
                DropPath = "b",
                Filter = "*",
                SuccessPath = "c",
                FailurePath = "d",
                InProgressPath = "e",
                NumberOfConsumers = 1,
                ScavengeInterval = new TimeSpan(0, 0, 100),
                RecoveryInterval = new TimeSpan(0, 0, 100),
                ProcessorConfigurator = FileProcessorEndpoint.EventBasedConfiguratorType,
                AdditionalFilter = typeof(DefaultFileFilter),
                PollingRestartInterval = new TimeSpan(0, 0, 60)
            };

            var candidate = element.ToEndpoint();
            this.Check(expected, candidate);
        }
        public FileProcessor(FileProcessorEndpoint endpoint, IFileHandler handler, IFilePostProcessor postProcessor)
        {
            if (endpoint == null)
            {
                throw new ArgumentException("endpoint");
            }

            if (handler == null)
            {
                throw new ArgumentException("handler");
            }

            if (postProcessor == null)
            {
                throw new ArgumentNullException("postProcessor");
            }

            this.instanceId = Guid.Empty;
            this.Endpoint = endpoint;
            this.Handler = handler;
            this.PostProcessor = postProcessor;

            this.timer = new Timer(this.Endpoint.ScavengeInterval.TotalMilliseconds) { AutoReset = true };
            this.timer.Elapsed += this.TimerElapsed;

            this.pendingFiles = new ConcurrentQueue<string>();
            this.eventHandle = new AutoResetEvent(false);
            this.quitHandle = new ManualResetEvent(false);
            this.quitting = false;

            Logger.DebugFormat("Scavenger interval set at {0:0.0} seconds", endpoint.ScavengeInterval.TotalSeconds);
        }
        public FileProcessor(FileProcessorEndpoint endpoint, IFileHandler handler, IFilePostProcessor postProcessor)
        {
            if (endpoint == null)
            {
                throw new ArgumentException("endpoint");
            }

            if (handler == null)
            {
                throw new ArgumentException("handler");
            }

            if (postProcessor == null)
            {
                throw new ArgumentNullException("postProcessor");
            }

            this.instanceId    = Guid.Empty;
            this.Endpoint      = endpoint;
            this.Handler       = handler;
            this.PostProcessor = postProcessor;

            this.timer = new Timer(this.Endpoint.ScavengeInterval.TotalMilliseconds)
            {
                AutoReset = true
            };
            this.timer.Elapsed += this.TimerElapsed;

            this.pendingFiles = new ConcurrentQueue <string>();
            this.eventHandle  = new AutoResetEvent(false);
            this.quitHandle   = new ManualResetEvent(false);
            this.quitting     = false;

            Logger.DebugFormat("Scavenger interval set at {0:0.0} seconds", endpoint.ScavengeInterval.TotalSeconds);
        }
        public static void RegisterProcessor(this IUnityContainer container, FileProcessorEndpoint endpoint)
        {
            endpoint.Validate();

            // Register the endpoint instance under the name, needed for the processor
            container.RegisterInstance(endpoint.Name, endpoint);

            // create file processor configurator instance and invoke
            var typeResolver = container.Resolve<ITypeResolver>();
            var fileProcessorRegistrar = typeResolver.Resolve<IFileProcessorRegistrar>(endpoint.ProcessorConfigurator);
            fileProcessorRegistrar.Register(container, endpoint);
        }
        public void CreateDirectories()
        {
            DeleteDirectories();

            dropLocation = Directory.CreateDirectory(DropDirectoryName).FullName;
            acknowledgedLocation = Directory.CreateDirectory(AcknowledgedDirectoryName).FullName;
            tempLocation = Directory.CreateDirectory(TempDirectoryName).FullName;

            endpoint = new FileProcessorEndpoint
            {
                DropPath = dropLocation,
                InProgressPath = acknowledgedLocation,
            };
        }
 protected override void RegisterProcessor(IUnityContainer container, FileProcessorEndpoint endpoint)
 {
     // Tie the parameters to the processor
     container.RegisterType(
         typeof(IFileProcessor),
         typeof(EventBasedFileProcessor),
         endpoint.Name,
         new PerResolveLifetimeManager(),
         new InjectionConstructor(
             new ResolvedParameter<FileProcessorEndpoint>(endpoint.Name),
             new ResolvedParameter<IFileHandler>(endpoint.Name),
             new ResolvedParameter<IFilePostProcessor>(endpoint.Name),
             new ResolvedParameter<IFileFilter>(endpoint.Name)));
 }
        public void RegisterWithoutRestartMechanism(IUnityContainer container, FileProcessorEndpoint fileProcessorEndpoint)
        {
            container.RegisterType(typeof(IHandleFiles),
                                   typeof(FileProducerConsumerQueue),
                                   fileProcessorEndpoint.Name,
                                   new PerResolveLifetimeManager(),
                                   new InjectionConstructor(
                                       fileProcessorEndpoint.NumberOfConsumers,
                                       new ResolvedParameter<IFileProcessResultHandler>(fileProcessorEndpoint.Name),
                                       new ResolvedParameter<IFileHandler>(fileProcessorEndpoint.Name)));

            container.RegisterType(typeof (IFileProcessor),
                                   typeof (PollingBasedFileProcessor),
                                   fileProcessorEndpoint.Name,
                                   new PerResolveLifetimeManager(),
                                   new InjectionConstructor(
                                       fileProcessorEndpoint,
                                       new ResolvedParameter<IHandleFiles>(fileProcessorEndpoint.Name)));
        }
 public void TestValidate(string name, string processConfigurator, string inProgressPath, bool expectException)
 {
     var failSuffix = name + ", " + processConfigurator + ", " + inProgressPath;
     try
     {
         var endpoint = new FileProcessorEndpoint { Name = name, ProcessorConfigurator = processConfigurator, InProgressPath = inProgressPath };
         var candidate = endpoint.Validate();
         if (expectException)
         {
             Assert.Fail("expected Exception was not thrown : " + failSuffix);
         }
         Assert.IsTrue(candidate);
     }
     catch (NotSupportedException)
     {
         if (!expectException)
         {
             Assert.Fail("Exception was thrown but not expected : " + failSuffix);
         }
     }
 }
        public void RegisterWithRestartMechanism(IUnityContainer container, FileProcessorEndpoint fileProcessorEndpoint)
        {
            var producerConsumerQueue = new FileProducerConsumerQueue(fileProcessorEndpoint.NumberOfConsumers,
                                                                      container.Resolve<IFileProcessResultHandler>(fileProcessorEndpoint.Name),
                                                                      container.Resolve<IFileHandler>(fileProcessorEndpoint.Name));

            var fileThroughputAlerter = new FileThroughPutAlerter(producerConsumerQueue, fileProcessorEndpoint.PollingRestartInterval);

            var fileProcessor = new PollingBasedFileProcessor(fileProcessorEndpoint,
                                                              fileThroughputAlerter, 
                                                              container.Resolve<IFileFilter>(fileProcessorEndpoint.Name));

            fileThroughputAlerter.Alert += (sender, args) => fileProcessor.Restart();

            container.RegisterInstance(typeof(IHandleFiles),
                                   fileProcessorEndpoint.Name,
                                   fileThroughputAlerter);

            container.RegisterInstance(typeof (IFileProcessor),
                                   fileProcessorEndpoint.Name,
                                   fileProcessor);
        }
        public void Register(IUnityContainer container, FileProcessorEndpoint fileProcessorEndpoint)
        {
            // Registration for the handler if present
            if (fileProcessorEndpoint.Handler != null)
            {
                container.RegisterType(typeof(IFileHandler), fileProcessorEndpoint.Handler, fileProcessorEndpoint.Name);
            }

            container.RegisterType<IFileProcessResultHandler, StrategyFileEventHandler>(
                fileProcessorEndpoint.Name,
                new InjectionConstructor(new DeleteSuccessfulFileHandlingStrategy(),
                                         new MoveFileHandlingStrategy(fileProcessorEndpoint.DropPath),
                                         new MoveFileHandlingStrategy(fileProcessorEndpoint.FailurePath)));

            if (fileProcessorEndpoint.PostProcessor == null)
            {
                if ((fileProcessorEndpoint.Handler != null) && fileProcessorEndpoint.Handler.Implements<IFilePostProcessor>())
                {
                    fileProcessorEndpoint.PostProcessor = fileProcessorEndpoint.Handler;
                }
                else
                {
                    fileProcessorEndpoint.PostProcessor = typeof(NullPostProcessor);
                }
            }

            // Registration for the post processor
            container.RegisterType(typeof(IFilePostProcessor), fileProcessorEndpoint.PostProcessor, fileProcessorEndpoint.Name);
            container.RegisterType(typeof(IFileFilter), fileProcessorEndpoint.AdditionalFilter, fileProcessorEndpoint.Name);

            if (fileProcessorEndpoint.PollingRestartInterval.TotalSeconds > 0)
            {
                RegisterWithRestartMechanism(container, fileProcessorEndpoint);
            }
            else
            {
                RegisterWithoutRestartMechanism(container, fileProcessorEndpoint);
            }
        }
        public void Register(IUnityContainer container, FileProcessorEndpoint endpoint)
        {
            // Registration for the handler if present
            if (endpoint.Handler != null)
            {
                container.RegisterType(typeof(IFileHandler), endpoint.Handler, endpoint.Name);
            }

            if (endpoint.PostProcessor == null)
            {
                if ((endpoint.Handler != null) && endpoint.Handler.Implements<IFilePostProcessor>())
                {
                    endpoint.PostProcessor = endpoint.Handler;
                }
                else
                {
                    endpoint.PostProcessor = typeof(NullPostProcessor);
                }
            }

            // Registration for the post processor
            container.RegisterType(typeof(IFilePostProcessor), endpoint.PostProcessor, endpoint.Name);
            container.RegisterType(typeof(IFileFilter), endpoint.AdditionalFilter, endpoint.Name);

            // Tie the parameters to the processor
            container.RegisterType(
                typeof(IFileProcessor),
                typeof(EventBasedFileProcessor),
                endpoint.Name,
                new PerResolveLifetimeManager(),
                new InjectionConstructor(
                    new ResolvedParameter<FileProcessorEndpoint>(endpoint.Name),
                    new ResolvedParameter<IFileHandler>(endpoint.Name),
                    new ResolvedParameter<IFilePostProcessor>(endpoint.Name),
                    new ResolvedParameter<IFileFilter>(endpoint.Name)));
        }
 public FileProcessorHelper(FileProcessorEndpoint endpoint, IFileHandler handler, IFilePostProcessor postProcessor)
     : base(endpoint, handler, postProcessor)
 {
 }
 public CustomFileProcessor(FileProcessorEndpoint endpoint, IFileHandler handler, IFilePostProcessor postProcessor)
     : base(endpoint, handler, postProcessor)
 {
 }
        public void ConvertProcessorElementWithCustomProcessor()
        {
            var element = new FileProcessorElement
            {
                Name = "a",
                DropPath = "b",
                Filter = "*",
                SuccessPath = "c",
                FailurePath = "d",
                InProgressPath = "e",
                ScavengeInterval = 100,
                RecoveryInterval = 100,
                ProcessorConfiguratorType = "EnergyTrading.UnitTest.FileProcessing.Configuration.ConfigurationExtensionsFixture+CustomFileProcessor, EnergyTrading.UnitTest",
                Handler = "EnergyTrading.UnitTest.FileProcessing.FileHandler, EnergyTrading.UnitTest"
            };

            var expected = new FileProcessorEndpoint
            {
                Name = "a",
                DropPath = "b",
                Filter = "*",
                SuccessPath = "c",
                FailurePath = "d",
                InProgressPath = "e",
                NumberOfConsumers = 1,
                ScavengeInterval = new TimeSpan(0, 0, 100),
                RecoveryInterval = new TimeSpan(0, 0, 100),
                ProcessorConfigurator = "EnergyTrading.UnitTest.FileProcessing.Configuration.ConfigurationExtensionsFixture+CustomFileProcessor, EnergyTrading.UnitTest",
                Handler = typeof(FileHandler),
                AdditionalFilter = typeof(DefaultFileFilter),
                PollingRestartInterval = new TimeSpan(0, 0, 60)
            };

            var candidate = element.ToEndpoint();
            this.Check(expected, candidate);
        }