/// <summary> /// Starts the application context using in-memory default configuration for the purposes of /// configuring the software /// </summary> /// <returns><c>true</c>, if temporary was started, <c>false</c> otherwise.</returns> public static bool StartTemporary(ConsoleParameters consoleParms) { try { var retVal = new MiniApplicationContext(); retVal.SetProgress("Run setup", 0); retVal.m_configurationManager = new MiniConfigurationManager(MiniConfigurationManager.GetDefaultConfiguration()); ApplicationContext.Current = retVal; ApplicationServiceContext.Current = ApplicationContext.Current; ApplicationServiceContext.HostType = OpenIZHostType.OtherClient; retVal.m_tracer = Tracer.GetTracer(typeof(MiniApplicationContext)); retVal.ThreadDefaultPrincipal = AuthenticationContext.SystemPrincipal; retVal.SetProgress("Loading configuration", 0.2f); var appService = retVal.GetService <IAppletManagerService>(); if (consoleParms.References != null) { // Load references foreach (var appletInfo in consoleParms.References)// Directory.GetFiles(this.m_configuration.GetSection<AppletConfigurationSection>().AppletDirectory)) { { try { retVal.m_tracer.TraceInfo("Loading applet {0}", appletInfo); String appletPath = appletInfo; if (!Path.IsPathRooted(appletInfo)) { appletPath = Path.Combine(Environment.CurrentDirectory, appletPath); } using (var fs = File.OpenRead(appletPath)) { var package = AppletPackage.Load(fs); retVal.m_tracer.TraceInfo("Loading {0} v{1}", package.Meta.Id, package.Meta.Version); // Is this applet in the allowed applets appService.LoadApplet(package.Unpack()); } } catch (Exception e) { retVal.m_tracer.TraceError("Loading applet {0} failed: {1}", appletInfo, e.ToString()); throw; } } } // Does openiz.js exist as an asset? var oizJs = appService.Applets.ResolveAsset("/org.openiz.core/js/openiz.js"); // Load all user-downloaded applets in the data directory foreach (var appletDir in consoleParms.AppletDirectories)// Directory.GetFiles(this.m_configuration.GetSection<AppletConfigurationSection>().AppletDirectory)) { { try { if (!Directory.Exists(appletDir) || !File.Exists(Path.Combine(appletDir, "manifest.xml"))) { continue; } retVal.m_tracer.TraceInfo("Loading applet {0}", appletDir); String appletPath = Path.Combine(appletDir, "manifest.xml"); using (var fs = File.OpenRead(appletPath)) { AppletManifest manifest = AppletManifest.Load(fs); (appService as MiniAppletManagerService).m_appletBaseDir.Add(manifest, appletDir); // Is this applet in the allowed applets // public key token match? appService.LoadApplet(manifest); } } catch (Exception e) { retVal.m_tracer.TraceError("Loading applet {0} failed: {1}", appletDir, e.ToString()); throw; } } if (oizJs?.Content != null) { oizJs.Content = oizJs.Content.ToString() + (appService as MiniAppletManagerService).GetShimMethods(); } retVal.Start(); return(true); } catch (Exception e) { Console.WriteLine("OpenIZ FATAL: {0}", e.ToString()); return(false); } }
/// <summary> /// Start the application context /// </summary> public static bool Start(ConsoleParameters consoleParms) { var retVal = new MiniApplicationContext(); retVal.m_configurationManager = new MiniConfigurationManager(); // Not configured if (!retVal.ConfigurationManager.IsConfigured) { return(false); } else { // load configuration try { // Set master application context ApplicationContext.Current = retVal; retVal.ConfigurationManager.Load(); retVal.AddServiceProvider(typeof(XamarinBackupService)); retVal.m_tracer = Tracer.GetTracer(typeof(MiniApplicationContext), retVal.ConfigurationManager.Configuration); var appService = retVal.GetService <IAppletManagerService>(); retVal.SetProgress("Loading configuration", 0.2f); if (consoleParms.References != null) { // Load references foreach (var appletInfo in consoleParms.References)// Directory.GetFiles(this.m_configuration.GetSection<AppletConfigurationSection>().AppletDirectory)) { { try { retVal.m_tracer.TraceInfo("Loading applet {0}", appletInfo); String appletPath = appletInfo; if (!Path.IsPathRooted(appletInfo)) { appletPath = Path.Combine(Environment.CurrentDirectory, appletPath); } using (var fs = File.OpenRead(appletPath)) { var package = AppletPackage.Load(fs); retVal.m_tracer.TraceInfo("Loading {0} v{1}", package.Meta.Id, package.Meta.Version); // Is this applet in the allowed applets appService.LoadApplet(package.Unpack()); } } catch (Exception e) { retVal.m_tracer.TraceError("Loading applet {0} failed: {1}", appletInfo, e.ToString()); throw; } } } // Does openiz.js exist as an asset? var oizJs = appService.Applets.ResolveAsset("/org.openiz.core/js/openiz.js"); // Load all user-downloaded applets in the data directory foreach (var appletDir in consoleParms.AppletDirectories)// Directory.GetFiles(this.m_configuration.GetSection<AppletConfigurationSection>().AppletDirectory)) { { try { if (!Directory.Exists(appletDir) || !File.Exists(Path.Combine(appletDir, "manifest.xml"))) { continue; } retVal.m_tracer.TraceInfo("Loading applet {0}", appletDir); String appletPath = Path.Combine(appletDir, "manifest.xml"); using (var fs = File.OpenRead(appletPath)) { AppletManifest manifest = AppletManifest.Load(fs); (appService as MiniAppletManagerService).m_appletBaseDir.Add(manifest, appletDir); // Is this applet in the allowed applets // public key token match? appService.LoadApplet(manifest); } } catch (Exception e) { retVal.m_tracer.TraceError("Loading applet {0} failed: {1}", appletDir, e.ToString()); throw; } } if (oizJs?.Content != null) { oizJs.Content = oizJs.Content.ToString() + (appService as MiniAppletManagerService).GetShimMethods(); } // Ensure data migration exists try { // If the DB File doesn't exist we have to clear the migrations if (!File.Exists(retVal.Configuration.GetConnectionString(retVal.Configuration.GetSection <DataConfigurationSection>().MainDataSourceConnectionStringName).Value)) { retVal.m_tracer.TraceWarning("Can't find the OpenIZ database, will re-install all migrations"); retVal.Configuration.GetSection <DataConfigurationSection>().MigrationLog.Entry.Clear(); } retVal.SetProgress("Migrating databases", 0.6f); DataMigrator migrator = new DataMigrator(); migrator.Ensure(); // Set the entity source EntitySource.Current = new EntitySource(retVal.GetService <IEntitySourceProvider>()); // Prepare clinical protocols //retVal.GetService<ICarePlanService>().Repository = retVal.GetService<IClinicalProtocolRepositoryService>(); ApplicationServiceContext.Current = ApplicationContext.Current; ApplicationServiceContext.HostType = OpenIZHostType.OtherClient; } catch (Exception e) { retVal.m_tracer.TraceError(e.ToString()); throw; } finally { retVal.ConfigurationManager.Save(); } if (!retVal.Configuration.GetSection <DiagnosticsConfigurationSection>().TraceWriter.Any(o => o.TraceWriterClassXml.Contains("Console"))) { retVal.Configuration.GetSection <DiagnosticsConfigurationSection>().TraceWriter.Add(new TraceWriterConfiguration() { TraceWriter = new ConsoleTraceWriter(EventLevel.Warning, "") }); } // Set the tracer writers for the PCL goodness! foreach (var itm in retVal.Configuration.GetSection <DiagnosticsConfigurationSection>().TraceWriter) { OpenIZ.Core.Diagnostics.Tracer.AddWriter(itm.TraceWriter, itm.Filter); } // Start daemons retVal.GetService <IThreadPoolService>().QueueUserWorkItem(o => { retVal.Start(); }); //retVal.Start(); } catch (Exception e) { retVal.m_tracer?.TraceError(e.ToString()); //ApplicationContext.Current = null; retVal.m_configurationManager = new MiniConfigurationManager(MiniConfigurationManager.GetDefaultConfiguration()); throw; } return(true); } }