/*********************************************************************/ /* */ /* AgentService Framework */ /* www.agentservice.it */ /* */ /* Copyright (C) 2007 l.i.d.o. - DIST, University of Genoa, Italy */ /* GNU Lesser General Public License */ /* */ /* This library is free software; you can redistribute it and/or */ /* modify it under the terms of the GNU Lesser General Public */ /* License as published by the Free Software Foundation, */ /* version 2.1 of the License. */ /* */ /* This library is distributed in the hope that it will be useful, */ /* but WITHOUT ANY WARRANTY; without even the implied warranty of */ /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU */ /* Lesser General Public License for more details. */ /* */ /* You should have received a copy of the GNU Lesser General Public */ /* License along with this library; if not, write to the */ /* Free Software Foundation, Inc., 59 Temple Place - Suite 330, */ /* Boston, MA 02111-1307, USA. */ /* */ /*********************************************************************/ #region XML /// ///
/// /// 07/13/2004 /// 1.0 /// /// /// /// /// Wraps and handles the command line. /// /// /// /// /// /// /// /// Defines the different commands that can be used. /// /// /// /// /// /// /// /// Runs the AgentService Platform, use it to install, execute, uninstall the platform. /// /// /// /// /// /// /// ///
/// /// #endregion using System; using System.IO; using System.Collections; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; using System.Collections.Specialized; using AgentService.Platform; using AgentService.Platform.Configuration; namespace AgentService.Platform { /// /// Enum CommandType. Defines the different commands that can be used. /// [Flags] public enum CommandType { /// E:\DEVELOPMENT\AgentServiceSVN\AgentServiceRunner\Driver.cs /// Unrecognized command /// Unknown = 0, /// /// Installs the platform /// Install = 1, /// /// Uninstalls the platform /// Uninstall = 2, /// /// Runs the platform /// Run = 4, /// /// Executes a batch job on the platform /// ExecuteBatch = 8, /// /// Debug mode /// Debug = 16, /// /// Clear Persistence /// Clean = 32 } /// /// Class CommandLineParameters. Wraps and handles the command line. /// class CommandLineParameters { /// /// true if there are errors in the command line /// private bool bHasErrors; /// /// Gets the flag indicating whether there are errors /// in the command line /// public bool HasErrors { get { return this.bHasErrors; } } /// /// selected command /// private CommandType command; /// /// Gets the selected command /// public CommandType Command { get { return this.command; } } /// /// file associated with the command /// private string file; /// /// Gets the file associated with the command. /// public string File { get { return this.file; } } /// /// Constructs a CommandLineParameters object. /// /// command line public CommandLineParameters(string[] commandLine) { this.bHasErrors = true; if (commandLine.Length == 0) { this.bHasErrors = false; // assuming defaults this.file = System.IO.Path.Combine(Platform.ConfDir, Platform.DefaultConfigurationFile); this.command = CommandType.Run; } if (commandLine.Length >= 1) { bool bExit = false; int i = 0; do { if (commandLine[i].StartsWith("-r:") == true) { this.command |= CommandType.Run; this.file = commandLine[i].Substring(3); this.bHasErrors = (System.IO.File.Exists(this.File) == false); bExit = true; } else if (commandLine[i].StartsWith("-i:") == true) { this.command |= CommandType.Install; this.file = commandLine[i].Substring(3); this.bHasErrors = (System.IO.File.Exists(this.File) == false); bExit = true; } else if (commandLine[i].StartsWith("-u:") == true) { this.file = commandLine[i].Substring(3); this.bHasErrors = (System.IO.File.Exists(this.File) == false); this.command |= CommandType.Uninstall; bExit = true; } else if (commandLine[i].StartsWith("-x:") == true) { this.file = commandLine[i].Substring(3); this.bHasErrors = (System.IO.File.Exists(this.File) == false); this.command |= CommandType.ExecuteBatch; bExit = true; } else if (commandLine[i].StartsWith("-d") == true) { this.command = this.command | CommandType.Debug; if ((commandLine.Length == 1) || (((commandLine.Length == 2) && ((this.command & CommandType.Clean) > 0)))) { // assuming defaults this.file = System.IO.Path.Combine(Platform.ConfDir, Platform.DefaultConfigurationFile); this.command = this.command | CommandType.Run; this.bHasErrors = false; } bExit = false; } else if (commandLine[i].StartsWith("-c") == true) { this.command = this.command | CommandType.Clean; if ((commandLine.Length == 1) || (((commandLine.Length == 2 ) && ((this.command & CommandType.Debug) > 0)))) { // assuming defaults this.file = System.IO.Path.Combine(Platform.ConfDir, Platform.DefaultConfigurationFile); this.command = this.command | CommandType.Run; this.bHasErrors = false; } bExit = false; } else { this.command = CommandType.Unknown; } i++; bExit = bExit || (i >= commandLine.Length); } while (bExit == false); } } } /// /// Class Driver. Installs/Uninstalls/Runs the AgentService Platform /// class Driver { /// /// Main Method. Application Launcher /// [STAThread] // MOD: 27/04/2006 : Marco Garr Added DomainNeutralOptionControl [LoaderOptimization(LoaderOptimization.MultiDomainHost)] // ENDMOD static void Main(string[] args) { try { CommandLineParameters cmdLine = new CommandLineParameters(args); if (cmdLine.HasErrors) { Driver.ShowHelp(); Console.ReadLine(); } else { CommandType command = cmdLine.Command; if ((cmdLine.Command & CommandType.Debug) > 0) { Console.WriteLine("Attach debugger and press [Enter]"); Console.ReadLine(); command = (command & ~CommandType.Debug); } ManagerFactory managerFactory = Driver.LoadManagerFactory(cmdLine); if (managerFactory != null) { // discriminate beetween the command type // and call the appropriate platform method switch (command) { case CommandType.Install: Driver.InstallPlatform(cmdLine.File, managerFactory); break; case CommandType.Uninstall: Driver.UninstallPlatform(cmdLine.File, managerFactory); break; case CommandType.Run: Driver.RunPlatform(cmdLine.File, managerFactory); break; case CommandType.ExecuteBatch: Driver.ExecuteBatch(cmdLine.File, managerFactory); break; case CommandType.Unknown: System.Console.WriteLine("AgentService platform driver: invalid parameters"); System.Console.WriteLine(); Driver.ShowHelp(); break; } } } } catch (Exception ex) { System.Console.WriteLine("Unexpected exception occured during the execution of the driver."); System.Console.WriteLine("Exception Type: {0}", ex.GetType().ToString()); System.Console.WriteLine("Exception Details: {0}", ex.Message); System.Console.WriteLine("Exception StackTrace: {0}", ex.StackTrace); } } /// /// Shows the help for the command line parameters /// private static void ShowHelp() { System.Console.WriteLine(" AgentService 1.0 platform driver."); System.Console.WriteLine(); System.Console.WriteLine(" usage: asdrv [-d] [ -i:install-file | -u:install-file | -r:config-file ]"); System.Console.WriteLine(); System.Console.WriteLine(" commands:"); System.Console.WriteLine(" -d: executes and waits for debug"); System.Console.WriteLine(" -i:install-file installs a new instance of the AgentService platform, as specified in"); System.Console.WriteLine(" in the install-file."); System.Console.WriteLine(" -u:install-file uninstalls the instance of the AgentService platform identified with "); System.Console.WriteLine(" the install-file."); System.Console.WriteLine(" -r:config-file runs the instance of the AgentService platform defined by the configuration"); System.Console.WriteLine(" file config-file."); System.Console.WriteLine(" -x:batch-script executes the batch script defined in the file."); System.Console.WriteLine(); System.Console.WriteLine(" default:"); System.Console.WriteLine(" asdrv -r:conf/platform.conf"); System.Console.WriteLine(); } /// /// Loads the manager object used to read and write installation /// and configuration files. /// /// cmdLine parameters /// private static ManagerFactory LoadManagerFactory(CommandLineParameters cmdLine) { // just only late binds the XML reading library // future implementations will be able to lookup // configuration managers. try { string xmldll = "xmlconf.dll"; System.Reflection.Assembly assembly = Assembly.LoadFrom(xmldll); ManagerFactory manager = (ManagerFactory)assembly.CreateInstance("AgentService.Platform.Configuration.XmlManagerFactory", false, System.Reflection.BindingFlags.CreateInstance, null, null, null, null); return manager; } catch (System.Reflection.TargetInvocationException tiex) { System.Console.WriteLine("[ERROR] could not load configuration/installation manager: " + tiex.InnerException.Message); return null; } catch (System.IO.IOException ioex) { System.Console.WriteLine("[ERROR] could not load manager library: " + ioex.Message); return null; } } /// /// Runs an instance the AgentService platform /// /// platform configuration file /// factory object for configuration and installation file managers private static void RunPlatform(string configFile, ManagerFactory managerFactory) { // Platform hap = new Platform(configFile, bTextFormat, Console.Out, Console.Error); FileSystem pm = new FileSystem(System.IO.Directory.GetCurrentDirectory()); PlatformConsole console = new PlatformConsole(Console.Out, Console.Error, Console.Out, Console.Out, Console.In); PlatformController controller = new PlatformController(console, pm); controller.BindToPlatform(configFile, managerFactory); controller.StartPlatform(); controller.RunPlatform(); System.Console.In.ReadLine(); controller.StopPlatform(); controller.ShutdownPlatform(); } /// /// Exceutes a script with the platform /// /// file batch name /// factory of configuration and installation files manager private static void ExecuteBatch(string batchFile, ManagerFactory managerFactory) { PlatformController controller = null; System.IO.StreamReader sr = new StreamReader(batchFile); StringCollection commands = new StringCollection(); bool bMore = true; do { string line = sr.ReadLine(); if (line != null) { commands.Add(line); } else { bMore = false; } } while(bMore == true); sr.Close(); string conf = null; string assembly = null; string type = null; string file = null; foreach(string cmd in commands) { if (cmd.StartsWith("conf=") == true) conf = cmd.Substring(5); else if (cmd.StartsWith("assembly=") == true) assembly = cmd.Substring(9); else if (cmd.StartsWith("type=") == true) type = cmd.Substring(5); else if (cmd.StartsWith("file=") == true) file = cmd.Substring(5); } if ((assembly == null) || (type == null)) { System.Console.WriteLine("Error: found errors in script"); return; } if (System.IO.File.Exists(assembly) == true) { string asmPath = System.IO.Path.GetFullPath(assembly); System.Reflection.Assembly asm = System.Reflection.Assembly.LoadFile(asmPath); try { object golem = asm.CreateInstance(type); IPlatformBatch batch = golem as IPlatformBatch; if (batch != null) { controller = new PlatformController(new FileSystem(System.IO.Directory.GetCurrentDirectory())); if (conf == null) { conf = System.IO.Path.Combine(Platform.ConfDir, Platform.DefaultConfigurationFile); } controller.BindToPlatform(conf,managerFactory); if (file == null) { batch.Run(controller); } else { batch.Run(controller, file); } } else { System.Console.WriteLine("Type {0} does not implement interface AgentService.Platform.IPlatformBatch", type); } } catch(System.Exception ex) { System.Console.WriteLine("Error: " + ex.Message); System.Console.WriteLine("StackTrace: " + ex.StackTrace); if (controller != null) { if (controller.PlatformState == PlatformState.Running) { controller.ShutdownPlatform(); } } return; } } else { System.Console.WriteLine("Assembly " + assembly + " not found."); return; } } /// /// Installs an instance of the AgentService platform /// /// platform installation file /// factory object for configuration and installation file managers private static void InstallPlatform(string installFile, ManagerFactory managerFactory) { // TODO: REVIEW implementation of Driver.InstallPlatform FileSystem pm = new FileSystem(System.IO.Directory.GetCurrentDirectory()); // 11/05/2006 MOD: Christian Vecchiola - do not filter path to installation file // string absoluteInstallFile = pm.GetPathFor(installFile); PlatformController controller = new PlatformController(Console.Out, Console.Error, Console.In, pm); controller.InstallPlatform(installFile, managerFactory); } /// /// Uninstalls an instance of the AgentService platform /// /// platform installation file /// factory object for configuration and installation file managers private static void UninstallPlatform(string installFile, ManagerFactory managerFactory) { // TODO: REVIEW implementation of Driver.UninstallPlatform FileSystem pm = new FileSystem(System.IO.Directory.GetCurrentDirectory()); PlatformConsole console = new PlatformConsole(Console.Out, Console.Error, Console.In); PlatformController controller = new PlatformController(console,pm); controller.Uninstall(installFile, managerFactory); } } }