Exemplo n.º 1
0
        //
        // Initialize the generator.
        //
        protected override void Initialize( )
        {
            this.GUID      = Guid.NewGuid( ).ToString( );
            this.Directory = Path.Combine(Settings.TemporaryGeneratorFilesDirectory, this.GUID);

            //
            // Unpack
            //
            try {
                System.IO.Directory.CreateDirectory(this.Directory);
                Zip.Unpack(this.Flow.FilenameFullPath, this.Directory);

                //
                // Verify Bin and Config directories.
                //
                if (!System.IO.Directory.Exists(Path.Combine(this.Directory, "Bin")))
                {
                    string msg = string.Format("The flow file {0} does not contain a Bin directory.", this.Flow.FilenameFullPath);
                    throw new GeneratorException(msg);
                }
                if (!System.IO.Directory.Exists(Path.Combine(this.Directory, "Config")))
                {
                    string msg = string.Format("The flow file {0} does not contain a Config directory.", this.Flow.FilenameFullPath);
                    throw new GeneratorException(msg);
                }

                /* Do not check for data directory, it does not have to bee there. data directory argument in interface then becomes null.
                 * if ( !System.IO.Directory.Exists( Path.Combine( this.Directory, "Data" ) ) ) {
                 *  string msg = string.Format( "The flow file {0} does not contain a Data directory.", this.Flow.FilenameFullPath );
                 *  throw new GeneratorException( msg );
                 * }
                 * */
                /* Do not check for config.xml file, it does not have to be there.
                 * if ( !File.Exists( Path.Combine( Path.Combine( this.Directory, "Config" ), "Config.xml" ) ) ) {
                 *  string msg = string.Format("The flow file {0} does not contain the required Config\\Config.xml file.", this.Flow.FilenameFullPath);
                 *  throw new RetrivalException( msg );
                 * }
                 */
                if (!File.Exists(Path.Combine(Path.Combine(this.Directory, "Config"), "Binding.xml")))
                {
                    string msg = string.Format("The flow file {0} does not contain the required Config\\Binding.xml file.", this.Flow.FilenameFullPath);
                    throw new GeneratorException(msg);
                }
            }
            catch (StackOverflowException) {
                throw;
            }
            catch (OutOfMemoryException) {
                throw;
            }
            catch (ThreadAbortException) {
                throw;
            }
            catch (Exception x) {
                GeneratorException ge = new GeneratorException("Catastophics failure. Error while unpacking flow.", x);
                throw ge;
            }

            //
            // Copy needed assemblies.
            //
            try {
                string[] files = new string[] { "ItSoftware.CompuFlow.Generator.HostRuntime.dll",
                                                "ItSoftware.ExceptionHandler.dll",
                                                "ItSoftware.CompuFlow.Manifest.dll",
                                                "ItSoftware.CompuFlow.Generator.Interfaces.dll",
                                                "ItSoftware.CompuFlow.Util.dll",
                                                "ItSoftware.CompuFlow.Common.dll" };

                foreach (string file in files)
                {
                    string fromFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file);
                    string toFile   = Path.Combine(this.Directory, string.Format("Bin\\{0}", file));
                    File.Copy(fromFile, toFile, true);
                }
            }
            catch (StackOverflowException) {
                throw;
            }
            catch (OutOfMemoryException) {
                throw;
            }
            catch (ThreadAbortException) {
                throw;
            }
            catch (Exception x) {
                GeneratorException ae = new GeneratorException("Catastrophic failure. Could not copy required host assemblies because one or more files was not found.", x);
                throw ae;
            }

            //
            // Fix Flow.config
            //
            try {
                string sourceFlowConfigFilename = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Flow.config");
                string outputFlowConfigFilename = Path.Combine(this.Directory, "Flow.config");
                if (File.Exists(outputFlowConfigFilename))
                {
                    //
                    // Modify existing Flow.config file.
                    //
                    XmlDocument xdSource = new XmlDocument( );
                    xdSource.Load(sourceFlowConfigFilename);

                    XmlDocument xdOutput = new XmlDocument( );
                    xdOutput.Load(outputFlowConfigFilename);

                    XmlDocument xdResult = FlowConfiguration.Merge(xdSource, xdOutput);
                    xdResult.Save(outputFlowConfigFilename);
                }
                else
                {
                    //
                    // Create new Flow.config file.
                    //
                    File.Copy(sourceFlowConfigFilename, outputFlowConfigFilename);
                }
            }
            catch (StackOverflowException) {
                throw;
            }
            catch (OutOfMemoryException) {
                throw;
            }
            catch (ThreadAbortException) {
                throw;
            }
            catch (Exception x) {
                GeneratorException ae = new GeneratorException("Catastrophic failure. Could not configure Flow.config.", x);
                throw ae;
            }

            //
            // Create app domain
            //
            try {
                AppDomainSetup ads = new AppDomainSetup( );
                ads.ApplicationBase     = this.Directory;
                ads.ConfigurationFile   = "Flow.config";
                ads.PrivateBinPath      = "Bin";
                ads.PrivateBinPathProbe = "*";
                this.AppDomain          = AppDomain.CreateDomain(this.Flow.FilenameFullPath, null, ads);
            }
            catch (StackOverflowException) {
                throw;
            }
            catch (OutOfMemoryException) {
                throw;
            }
            catch (ThreadAbortException) {
                throw;
            }
            catch (Exception x) {
                GeneratorException ae = new GeneratorException("Catastrophic failure. Could not create application domain.", x);
                throw ae;
            }

            //
            // Create type
            //
            try {
                this.RealFlow = (RealGenerator)this.AppDomain.CreateInstanceAndUnwrap("ItSoftware.CompuFlow.Generator.HostRuntime", "ItSoftware.CompuFlow.Generator.HostRuntime.RealGenerator");
            }
            catch (StackOverflowException) {
                throw;
            }
            catch (OutOfMemoryException) {
                throw;
            }
            catch (ThreadAbortException) {
                throw;
            }
            catch (Exception x) {
                AppDomain.Unload(this.AppDomain);
                this.AppDomain = null;
                GeneratorException ae = new GeneratorException("Could not create RealRetrival type in appdomain.", x);
                throw ae;
            }

            //
            // Mark initialization complete.
            //
            this.Initialized = true;
        }
Exemplo n.º 2
0
        private void WorkerMethod()
        {
            //
            // Thread sleep for debugging purposes only. Se conditional attribute.
            //
            ThreadSleep(15000);

            try
            {
                //
                // Get/Load Settings
                //
                Settings settings = new Settings();

                //
                // Delete temporary flow files, temp files and folders
                //
                DeleteTemporaryFiles(settings);

                //
                // Create controller object.
                //
                Controller <Settings, TransparentGenerator, RealGenerator, HostRuntimeSettings> controller = new Controller <Settings, TransparentGenerator, RealGenerator, HostRuntimeSettings>(settings, false);

                //
                // Status Information Host
                //
                StatusInformationHost <Settings, TransparentGenerator, RealGenerator, HostRuntimeSettings> statusInformationHost = new StatusInformationHost <Settings, TransparentGenerator, RealGenerator, HostRuntimeSettings>(controller, "net.pipe://localhost/ItSoftware.CompuFlow.Generator");

                //
                // Worker loop.
                //
                MessageQueue mq = new MessageQueue(settings.SourceMsmqPath);
                mq.Formatter = new BinaryMessageFormatter();
                System.Messaging.Message sourceMsg = null;
                do
                {
                    try
                    {
                        if (this.Paused)
                        {
                            Thread.Sleep(new TimeSpan(0, 0, 5));
                        }
                        else
                        {
                            try
                            {
                                sourceMsg = null;
                                sourceMsg = mq.Receive(new TimeSpan(0, 0, 5));
                            }
                            catch (MessageQueueException mqe)
                            {
                                if (mqe.MessageQueueErrorCode != MessageQueueErrorCode.IOTimeout)
                                {
                                    GeneratorException ge = new GeneratorException("MessageQueue.Receive threw an unexpected exception.", mqe);
                                    ExceptionManager.PublishException(ge, "Log Warning Policy");

                                    Thread.Sleep(new TimeSpan(0, 0, 5));
                                }
                            }
                            if (sourceMsg != null)
                            {
                                FlowManifest flowManifest = new FlowManifest(sourceMsg.Body as string);
                                controller.ExecuteFlow(flowManifest);
                            }
                        }
                    }
                    catch (OutOfMemoryException)
                    {
                        throw;
                    }
                    catch (StackOverflowException)
                    {
                        throw;
                    }
                    catch (ThreadAbortException)
                    {
                        controller.Stop();
                        throw;
                    }
                    catch (Exception x)
                    {
                        ExceptionManager.PublishException(x, "Error");
                    }
                } while (!this.Stopped);
                controller.Stop();
            }
            catch (ThreadAbortException tae)
            {
                if (!this.Stopped)
                {
                    ExceptionManager.PublishException(tae, "Error");
                    System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(base.ServiceName);
                    sc.Stop();
                }
            }
            catch (SettingsException se)
            {
                GeneratorException ge = new GeneratorException("Generator could not continue execution.", se);
                ExceptionManager.PublishException(ge, "Error");

                System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(base.ServiceName);
                sc.Stop();
            }
            catch (GeneratorException ge)
            {
                ExceptionManager.PublishException(ge, "Error");

                System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(base.ServiceName);
                sc.Stop();
            }
            catch (RuntimeWrappedException rwe)
            {
                GeneratorException ge = new GeneratorException("An unhandled RuntimeWrappedException exception was thrown.\r\nGenerator could not continue execution.", rwe);
                ExceptionManager.PublishException(ge, "Error");

                System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(base.ServiceName);
                sc.Stop();
            }
            catch (Exception x)
            {
                GeneratorException ge = new GeneratorException("An unhandled CLS complient exception was thrown.\r\nGenerator could not continue execution.", x);
                ExceptionManager.PublishException(ge, "Error");

                System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(base.ServiceName);
                sc.Stop();
            }
        }