PageRenderTime 303ms CodeModel.GetById 101ms app.highlight 6ms RepoModel.GetById 139ms app.codeStats 1ms

/Rhino.Etl.Cmd/RhinoEtlSetup.cs

http://github.com/ayende/rhino-etl
C# | 115 lines | 100 code | 11 blank | 4 comment | 4 complexity | 7113891d364c4ac3c049a078348b6eb7 MD5 | raw file
  1namespace Rhino.Etl.Cmd
  2{
  3    using System;
  4    using System.IO;
  5    using System.Reflection;
  6    using Boo.Lang.Useful.CommandLine;
  7    using Core;
  8    using log4net;
  9    using log4net.Config;
 10    using Dsl;
 11
 12    public class RhinoEtlSetup
 13    {
 14        private readonly ILog log = LogManager.GetLogger(typeof(RhinoEtlSetup));
 15
 16
 17        private static void Main(string[] args)
 18        {
 19            try
 20            {
 21                RhinoEtlCommandLineOptions options = new RhinoEtlCommandLineOptions();
 22                try
 23                {
 24                    options.Parse(args);
 25                }
 26                catch (CommandLineException e)
 27                {
 28                    Console.WriteLine(e.Message);
 29                    options.PrintOptions();
 30                    return;
 31                }
 32                new RhinoEtlSetup().Execute(options);
 33            }
 34            catch (Exception e)
 35            {
 36                Console.WriteLine(e);
 37            }
 38        }
 39
 40        private void Execute(RhinoEtlCommandLineOptions options)
 41        {
 42            RhinoEtlRunner.SetupLogging(options.Verbose);
 43
 44            log.DebugFormat("Starting with {0}", options.File);
 45            try
 46            {
 47                string ext = Path.GetExtension(options.File).ToLower();
 48                Type processType;
 49                if(ext==".exe" || ext==".dll")
 50                {
 51                    processType = GetFromAssembly(options);
 52                }
 53                else
 54                {
 55                    processType = GetFromDslFile(options.File);
 56                }
 57
 58                ExecuteProcessInSeparateAppDomain(processType, options);
 59            }
 60            catch (Exception e)
 61            {
 62                log.Debug(e);
 63                log.Error(e.Message);
 64            }
 65        }
 66
 67        private static Type GetFromAssembly(RhinoEtlCommandLineOptions options)
 68        {
 69            FileInfo _assemblyInfo = new FileInfo(options.File);
 70            Assembly asm = Assembly.LoadFile(_assemblyInfo.FullName);
 71            //Assembly asm = Assembly.Load(options.File);
 72            foreach (Type type in asm.GetTypes())
 73            {
 74                if(typeof(EtlProcess).IsAssignableFrom(type) && type.Name.Equals(options.Process, StringComparison.InvariantCultureIgnoreCase))
 75                    return type;
 76            }
 77            throw new InvalidOperationException("Could not find type nameed '" + options.Process + "' on: " +
 78                                                options.File);
 79        }
 80
 81        private static Type GetFromDslFile(string filename)
 82        {
 83            Type processType;
 84            EtlProcess process = EtlDslEngine.Factory.Create<EtlProcess>(filename);
 85            processType = process.GetType();
 86            return processType;
 87        }
 88
 89        private void ExecuteProcessInSeparateAppDomain(Type processType, RhinoEtlCommandLineOptions options)
 90        {
 91            try
 92            {
 93                FileInfo _assemblyInfo = new FileInfo(options.File);
 94                //we have to run the code in another appdomain, because we want to
 95                //setup our own app.config for it
 96                AppDomainSetup appDomainSetup = new AppDomainSetup();
 97                //setting this to the current executing directory because that's where the dsl's dll gets created.
 98                appDomainSetup.ApplicationBase = Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path));
 99                appDomainSetup.ConfigurationFile = string.IsNullOrEmpty(options.Configuration) ? options.File + ".config" : options.Configuration;
100                AppDomain appDomain = AppDomain.CreateDomain("etl.domain", null, appDomainSetup);
101                appDomain.Load(processType.Assembly.GetName());
102                RhinoEtlRunner runner = (RhinoEtlRunner)appDomain.CreateInstanceAndUnwrap(typeof (RhinoEtlRunner).Assembly.GetName().FullName,
103                                                                                          typeof (RhinoEtlRunner).FullName);
104                runner.Start(processType, options.Verbose);
105            }
106            catch (Exception e)
107            {
108                log.Debug(e);
109                log.Error(e.Message);
110            }
111        }
112
113    }
114}
115