internal static void LoadModules(IApplicationModuleManager container, ApplicationConfiguration config) { if (config == null) { return; } if (container == null) { return; } foreach (ModuleDefinition moduleDef in config.ModulesConfiguration.Modules) { Type moduleType = Type.GetType(moduleDef.Type, false, true); if (moduleType == null) { throw new InvalidOperationException( string.Format("Cannot load type from string \"{0}\".", moduleDef.Type)); } if (!typeof(IApplicationModule).IsAssignableFrom(moduleType)) { throw new InvalidCastException(string.Format("Type {0} does not implements type {1}.", moduleType, typeof(IApplicationModule))); } IApplicationModule module = (IApplicationModule)Activator.CreateInstance(moduleType); container.Load(module); } }
public void Shutdown() { m_heartbeatTimer.Stop(); m_heartbeatTimer.Dispose(); m_commandHandlers.Clear(); // Shut down the scene factory first, to make sure all of the // scenes shut down before the application modules IApplicationModule sceneFactory = GetAppModule <ISceneFactory>() as IApplicationModule; if (sceneFactory != null) { m_log.Info("Shutting down scene factory " + sceneFactory); sceneFactory.Stop(); } foreach (IApplicationModule module in m_applicationModules) { if (!(module is ISceneFactory)) { m_log.Info("Shutting down application module " + module); try { module.Stop(); } catch (Exception ex) { m_log.Error("Error stopping application module " + module + ": " + ex.Message); } } } }
private void CheckRequiredModules(IApplicationModule module) { var requiredModules = module.GetRequiredModules(); foreach (var requiredModule in requiredModules.Where(requiredModule => Modules.All(m => !requiredModule.IsInstanceOfType(m)))) { throw new Exception($"Module {module} require module {requiredModule} to be included"); } }
/// <summary> /// Loads a module. Loading a module through this interface is necessary to make the module part of the application's /// lifetime management. If <see cref="ModuleManager"/> is provided and the module implements <see cref="Ninject.Core.IModule"/>, /// then the module will also be loaded in Ninject. /// </summary> /// <param name="module">The application module to load.</param> public void Load(IApplicationModule module) { if (module == null) throw new ArgumentNullException("module"); if (_modules.Exists(m => m.Name == module.Name)) throw new InvalidOperationException("Module with name {0} already loaded.".FormatEx(module.Name)); _modules.Add(module); if (ModuleManager != null && typeof(Ninject.Core.IModule).IsAssignableFrom(module.GetType())) ModuleManager.Load((Ninject.Core.IModule)module); else module.Load(); // don't use Ninject. }
/// <summary> /// Activates a module. Generally this means, for a module, to interact with an <see cref="IPresentationController"/>. /// It is invalid to activate a module that is not loaded yet. /// </summary> /// <param name="module">A module that must be loaded.</param> /// <exception cref="InvalidOperationException">Thrown when activating a module that's not loaded.</exception> public void Activate(IApplicationModule module) { if (module == null) { throw new ArgumentNullException("module"); } if (!IsLoaded(module.Name)) { throw new InvalidOperationException("Module with name {0} is not loaded".FormatEx(module.Name)); } module.Activate(PresentationController); }
/// <summary> /// Activates a module. Generally this means, for a module, to interact with an <see cref="IPresentationController"/>. /// It is invalid to activate a module that is not loaded yet. /// </summary> /// <param name="name">Name of the module to activate.</param> /// <exception cref="InvalidOperationException">Thrown when activating a module that's not loaded.</exception> public void Activate(string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } if (!IsLoaded(name)) { throw new InvalidOperationException("Module with name {0} is not loaded".FormatEx(name)); } IApplicationModule module = _modules.Find(m => m.Name == name); module.Activate(PresentationController); }
/// <summary> /// Unloads a module. If <see cref="ModuleManager"/> is provided and the module implements <see cref="Ninject.Core.IModule"/>, /// then the module will also be unloaded from Ninject. It is invalid to unload a module that's not been loaded. /// </summary> /// <param name="name">Name of a module to unload.</param> /// <exception cref="InvalidOperationException">Thrown if the module has not been loaded.</exception> public void Unload(string name) { if (string.IsNullOrEmpty(name)) { throw new ArgumentNullException("name"); } if (!IsLoaded(name)) { throw new InvalidOperationException("Module with name {0} is not loaded".FormatEx(name)); } IApplicationModule module = _modules.Find(m => m.Name == name); Unload(module); }
/// <summary> /// Deactivates a module. Generally this means, for a module, to interact with an <see cref="IPresentationController"/>. /// It is invalid to deactivated a module that not loaded and not activated. /// </summary> /// <param name="module">A module to deactivate</param> /// <exception cref="InvalidOperationException">Thrown when deactivating a module that's not loaded.</exception> public void Deactivate(IApplicationModule module) { if (module == null) { throw new ArgumentNullException("module"); } if (!IsLoaded(module.Name)) { throw new InvalidOperationException("Module with name {0} is not loaded".FormatEx(module.Name)); } if (!module.IsActivated) { return; } module.Deactivate(); }
protected virtual void CreateModules(IKernelConfig config) { for (int i = 0; i < config.Modules.Count; i++) { IModuleBuildInfo info = config.Modules[i]; if (info.Active) { IModuleBuilder builder = info.Builder; Type registerType = builder.RegisterType; IApplicationModule module = builder.Build(this, info.Arguments); AddModule(registerType, module); Log.Debug($"Build Module: registerType:'{registerType}', module:'{module}'."); } } }
protected override void OnUninitializeModules() { for (int i = Config.Modules.Count - 1; i >= 0; i--) { IModuleBuilder builder = Config.Modules[i].Builder; IApplicationModule module = Modules[builder.RegisterType]; module.Uninitialize(); } if (Modules.Count != Config.Modules.Count) { foreach (KeyValuePair <Type, IApplicationModule> pair in Modules) { if (pair.Value.IsInitialized) { pair.Value.Uninitialize(); } } } }
/// <summary> /// Loads a module. Loading a module through this interface is necessary to make the module part of the application's /// lifetime management. If <see cref="ModuleManager"/> is provided and the module implements <see cref="Ninject.Core.IModule"/>, /// then the module will also be loaded in Ninject. /// </summary> /// <param name="module">The application module to load.</param> public void Load(IApplicationModule module) { if (module == null) { throw new ArgumentNullException("module"); } if (_modules.Exists(m => m.Name == module.Name)) { throw new InvalidOperationException("Module with name {0} already loaded.".FormatEx(module.Name)); } _modules.Add(module); if (ModuleManager != null && typeof(Ninject.Core.IModule).IsAssignableFrom(module.GetType())) { ModuleManager.Load((Ninject.Core.IModule)module); } else { module.Load(); // don't use Ninject. } }
/// <summary> /// Unloads a module. If <see cref="ModuleManager"/> is provided and the module implements <see cref="Ninject.Core.IModule"/>, /// then the module will also be unloaded from Ninject. It is invalid to unload a module that's not been loaded. /// </summary> /// <param name="module">A module to unload.</param> /// <exception cref="InvalidOperationException">Thrown if the module has not been loaded.</exception> public void Unload(IApplicationModule module) { if (module == null) { throw new ArgumentNullException("module"); } if (!IsLoaded(module.Name)) { throw new InvalidOperationException("Module with name {0} is not loaded".FormatEx(module.Name)); } if (ModuleManager != null && typeof(Ninject.Core.IModule).IsAssignableFrom(module.GetType())) { ModuleManager.Unload((Ninject.Core.IModule)module); } else { //don't use Ninject. module.Unload(); } _modules.Remove(module); }
/// <summary> /// Activates a module. Generally this means, for a module, to interact with an <see cref="IPresentationController"/>. /// It is invalid to activate a module that is not loaded yet. /// </summary> /// <param name="module">A module that must be loaded.</param> /// <exception cref="InvalidOperationException">Thrown when activating a module that's not loaded.</exception> public void Activate(IApplicationModule module) { if (module == null) throw new ArgumentNullException("module"); if (!IsLoaded(module.Name)) throw new InvalidOperationException("Module with name {0} is not loaded".FormatEx(module.Name)); module.Activate(PresentationController); }
public ModuleInfo(IApplicationModule module) : base(new ModuleBuilder(module), ModuleBuildArguments <object> .Empty) { }
/// <summary> /// Unloads a module. If <see cref="ModuleManager"/> is provided and the module implements <see cref="Ninject.Core.IModule"/>, /// then the module will also be unloaded from Ninject. It is invalid to unload a module that's not been loaded. /// </summary> /// <param name="module">A module to unload.</param> /// <exception cref="InvalidOperationException">Thrown if the module has not been loaded.</exception> public void Unload(IApplicationModule module) { if (module == null) throw new ArgumentNullException("module"); if (!IsLoaded(module.Name)) throw new InvalidOperationException("Module with name {0} is not loaded".FormatEx(module.Name)); if (ModuleManager != null && typeof(Ninject.Core.IModule).IsAssignableFrom(module.GetType())) ModuleManager.Unload((Ninject.Core.IModule)module); else //don't use Ninject. module.Unload(); _modules.Remove(module); }
/// <summary> /// Deactivates a module. Generally this means, for a module, to interact with an <see cref="IPresentationController"/>. /// It is invalid to deactivated a module that not loaded and not activated. /// </summary> /// <param name="module">A module to deactivate</param> /// <exception cref="InvalidOperationException">Thrown when deactivating a module that's not loaded.</exception> public void Deactivate(IApplicationModule module) { if (module == null) throw new ArgumentNullException("module"); if (!IsLoaded(module.Name)) throw new InvalidOperationException("Module with name {0} is not loaded".FormatEx(module.Name)); if (!module.IsActivated) return; module.Deactivate(); }
public ModuleBuilder(IApplicationModule module) { Module = module; RegisterType = module.GetType(); }
public bool LoadModules() { #region Application Module Whitelist Loading // Load the application module whitelist List <KeyValuePair <int, string> > whitelist = new List <KeyValuePair <int, string> >(); IConfig config = Config.Configs["ApplicationModules"]; if (config != null) { foreach (string key in config.GetKeys()) { int runLevel = config.GetInt(key, -1); if (runLevel >= 0) { whitelist.Add(new KeyValuePair <int, string>(runLevel, key)); } } } // Sort the list based on runlevel whitelist.Sort(delegate(KeyValuePair <int, string> lhs, KeyValuePair <int, string> rhs) { return(lhs.Key.CompareTo(rhs.Key)); }); #endregion Application Module Whitelist Loading #region Module Container Loading AggregateCatalog catalog = new AggregateCatalog(); AssemblyCatalog assemblyCatalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly()); DirectoryCatalog directoryCatalog = new DirectoryCatalog(".", "Simian.*.dll"); catalog.Catalogs.Add(assemblyCatalog); catalog.Catalogs.Add(directoryCatalog); m_moduleContainer = new CompositionContainer(catalog, true); try { m_log.InfoFormat("Found {0} modules in the current assembly and {1} modules in external assemblies", assemblyCatalog.Parts.Count(), directoryCatalog.Parts.Count()); } catch (System.Reflection.ReflectionTypeLoadException ex) { StringBuilder error = new StringBuilder("Error(s) encountered loading extension modules. You may have an incompatible or out of date extension .dll in the current folder."); foreach (Exception loaderEx in ex.LoaderExceptions) { error.Append("\n " + loaderEx.Message); } m_log.Error(error.ToString()); return(false); } #endregion Module Container Loading #region Module Loading IEnumerable <Lazy <object, object> > exportEnumerable = m_moduleContainer.GetExports(typeof(IApplicationModule), null, null); Dictionary <string, Lazy <object, object> > exports = new Dictionary <string, Lazy <object, object> >(); List <IApplicationModule> imports = new List <IApplicationModule>(); List <string> notLoaded = new List <string>(); // Reshuffle exportEnumerable into a dictionary mapping module names to their lazy instantiations foreach (Lazy <object, object> lazyExport in exportEnumerable) { IDictionary <string, object> metadata = (IDictionary <string, object>)lazyExport.Metadata; object nameObj; if (metadata.TryGetValue("Name", out nameObj)) { string name = (string)nameObj; if (!exports.ContainsKey(name)) { exports.Add(name, lazyExport); } else { m_log.Warn("Found an IApplicationModule with a duplicate name: " + name); } } } // Load modules in the order they appear in the whitelist foreach (KeyValuePair <int, string> kvp in whitelist) { string whitelisted = kvp.Value; Lazy <object, object> lazyExport; if (exports.TryGetValue(whitelisted, out lazyExport)) { imports.Add((IApplicationModule)lazyExport.Value); exports.Remove(whitelisted); } else { notLoaded.Add(whitelisted); } } // Populate m_applicationModules m_applicationModules = imports.ToArray(); // Start the application modules for (int i = 0; i < m_applicationModules.Length; i++) { IApplicationModule module = m_applicationModules[i]; if (!(module is ISceneFactory)) { module.Start(this); } } // ISceneFactory modules are always started last for (int i = 0; i < m_applicationModules.Length; i++) { IApplicationModule module = m_applicationModules[i]; if (module is ISceneFactory) { module.Start(this); } } #endregion Module Loading #region Logging m_log.InfoFormat("Loaded {0} application modules", m_applicationModules.Length); if (exports.Count > 0) { StringBuilder skippedStr = new StringBuilder("Skipped application modules: "); foreach (string exportName in exports.Keys) { skippedStr.Append(exportName + " "); } m_log.Info(skippedStr.ToString()); } if (notLoaded.Count > 0) { StringBuilder notLoadedStr = new StringBuilder("Did not load whitelisted application modules: "); foreach (string entry in notLoaded) { notLoadedStr.Append(entry + " "); } m_log.Warn(notLoadedStr.ToString()); } #endregion Logging // Get a reference to the HTTP server if we have one m_httpServer = GetAppModule <IHttpServer>(); if (m_httpServer != null) { m_capabilityRouter = new CapabilityRouter(m_httpServer.HttpAddress.Combine(CAPABILITY_PATH)); m_httpServer.AddHandler(null, null, CAPABILITY_PATH, false, false, m_capabilityRouter.RouteCapability); } // Get a reference to the ISceneFactory if we have one m_sceneFactory = GetAppModule <ISceneFactory>(); if (m_sceneFactory != null) { AddCommandHandler("scene", SceneHandler); } return(true); }