using System;
using System.Collections.Generic;
using System.Reflection;
using DTS.Common.Events;
using DTS.Common.Core.PluginLib;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
// ReSharper disable EmptyConstructor
// ReSharper disable InconsistentNaming
namespace DTS.Viewer.PSDReport
{
///
/// I think we need it...
///
public class PSDReportSession
{
///
/// Custom Bootstrapper
///
///
/// Do not try to re-create the bootstrapper. Once modules are loaded, they will be be loaded into the DirectoryModuleCatalog and each
/// Module initialization will not be called (thus no registered types).
/// It appears that the current bootstrapper loads around 40 MB into memory.
/// To completely unload the bootstrapper would take same research and effort.
///
private Bootstrapper _bootstrapper;
public IUnityContainer Container { get; private set; }
public IServiceLocator _serviceLocator { get; private set; }
public IEventAggregator _eventAggregator { get; private set; }
public IRegionManager _regionManager { get; private set; }
private string _customConfigPath = string.Empty;
public string CustomConfigPath { get => _customConfigPath; set => _customConfigPath = value; }
///
/// empty constructor
///
public PSDReportSession()
{
}
public void CreateSession(bool standalone, string customConfigPath = "")
{
CustomConfigPath = customConfigPath;
CreateBootstrapper(standalone, customConfigPath);
if (_bootstrapper == null) return;
Container = _bootstrapper.Container;
_eventAggregator = Container.Resolve();
_serviceLocator = Container.Resolve();
_regionManager = Container.Resolve();
//TODO: review publishPlugins vs base.InitializeModules();
publishPlugins(_eventAggregator, LoadPlugins());
}
private List LoadPlugins()
{
var pluginManager = PluginManager.GetPluginManager(CustomConfigPath);
return pluginManager.GetPluginList();
}
private void publishPlugins(IEventAggregator eventAggregator, List pluginList)
{
eventAggregator.GetEvent().Publish(new AssemblyListInfo(pluginList));
}
///
/// Create bootstrapper for prism-based modules and views.
///
///
/// Currently, there is no course of action for destroying the bootstrapper.
///
private void CreateBootstrapper(bool standalone, string customConfigPath = "")
{
if (_bootstrapper == null)
{
try
{
_bootstrapper = new Bootstrapper(standalone, customConfigPath);
_bootstrapper.Run();
}
catch (ApplicationException ex)
{
throw new Exception("Failed to create bootstrapper ", ex);
}
}
if (_bootstrapper == null)
{
throw new Exception("Failed to create Bootstrapper!");
}
}
///
/// The Terminate method is called only when JMPS is in the process of shutting down.
/// If a component is just being closed, only the class destructor will be called.
/// Part of IComponent implementation.
///
public void Terminate()
{
}
}
}