/// <summary> /// Start the application context /// </summary> public static bool Start(DebuggerParameters consoleParms) { var retVal = new DebugApplicationContext(consoleParms); try { // Set master application context ApplicationServiceContext.Current = ApplicationContext.Current = retVal; retVal.m_tracer = Tracer.GetTracer(typeof(DebugApplicationContext)); 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; } } } // Ensure data migration exists try { // If the DB File doesn't exist we have to clear the migrations if (!File.Exists(retVal.ConfigurationManager.GetConnectionString(retVal.Configuration.GetSection <DcDataConfigurationSection>().MainDataSourceConnectionStringName).GetComponent("dbfile"))) { retVal.m_tracer.TraceWarning("Can't find the SanteDB database, will re-install all migrations"); retVal.Configuration.GetSection <DcDataConfigurationSection>().MigrationLog.Entry.Clear(); } retVal.SetProgress("Migrating databases", 0.6f); ConfigurationMigrator migrator = new ConfigurationMigrator(); migrator.Ensure(true); // 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; } catch (Exception e) { retVal.m_tracer.TraceError(e.ToString()); throw; } finally { } // Set the tracer writers for the PCL goodness! foreach (var itm in retVal.Configuration.GetSection <DiagnosticsConfigurationSection>().TraceWriter) { SanteDB.Core.Diagnostics.Tracer.AddWriter(Activator.CreateInstance(itm.TraceWriter, itm.Filter, itm.InitializationData, new Dictionary <String, EventLevel>()) as TraceWriter, itm.Filter); } // Start daemons retVal.GetService <IThreadPoolService>().QueueUserWorkItem(o => { retVal.Start(); }); //retVal.Start(); } catch (Exception e) { retVal.m_tracer?.TraceError(e.ToString()); throw; } return(true); }
/// <summary> /// Start the application context /// </summary> public static bool Start(ConsoleParameters consoleParms) { var retVal = new MiniApplicationContext(consoleParms.InstanceName); // Not configured if (!retVal.ConfigurationPersister.IsConfigured) { return(false); } else { // load configuration try { // Set master application context ApplicationServiceContext.Current = ApplicationContext.Current = retVal; retVal.ConfigurationPersister.Backup(retVal.Configuration); retVal.GetService <IServiceManager>().AddServiceProvider(typeof(DefaultBackupService)); retVal.GetService <IBackupService>().AutoRestore(); retVal.m_tracer = Tracer.GetTracer(typeof(MiniApplicationContext)); var configuration = retVal.Configuration.GetSection <DiagnosticsConfigurationSection>(); foreach (var tr in configuration.TraceWriter) { Tracer.AddWriter(Activator.CreateInstance(tr.TraceWriter, tr.Filter, tr.InitializationData, configuration.Sources.ToDictionary(o => o.SourceName, o => o.Filter)) as TraceWriter, tr.Filter); } var appService = retVal.GetService <IAppletManagerService>(); retVal.SetProgress("Loading configuration", 0.2f); if (consoleParms.References != null) { MiniApplicationContext.LoadReferences(retVal, consoleParms.References); } // Does openiz.js exist as an asset? var oizJs = appService.Applets.ResolveAsset("/org.santedb.core/js/santedb.js"); // Load all solution manifests and attempt to find their pathspec if (!String.IsNullOrEmpty(consoleParms.SolutionFile)) { LoadSolution(consoleParms.SolutionFile, appService); } // Load all user-downloaded applets in the data directory if (consoleParms.AppletDirectories != null) { LoadApplets(consoleParms.AppletDirectories.OfType <String>(), appService); } if (oizJs?.Content != null) { byte[] content = appService.Applets.RenderAssetContent(oizJs); var oizJsStr = Encoding.UTF8.GetString(content, 0, content.Length); oizJs.Content = oizJsStr + (appService as MiniAppletManagerService).GetShimMethods(); } // Set the entity source EntitySource.Current = new EntitySource(retVal.GetService <IEntitySourceProvider>()); // Ensure data migration exists var hasDatabase = retVal.ConfigurationManager.Configuration.GetSection <DcDataConfigurationSection>().ConnectionString.Count > 0; try { // If the DB File doesn't exist we have to clear the migrations if (hasDatabase && !File.Exists(retVal.ConfigurationManager.GetConnectionString(retVal.Configuration.GetSection <DcDataConfigurationSection>().MainDataSourceConnectionStringName).GetComponent("dbfile"))) { retVal.m_tracer.TraceWarning("Can't find the SanteDB database, will re-install all migrations"); retVal.Configuration.GetSection <DcDataConfigurationSection>().MigrationLog.Entry.Clear(); } retVal.SetProgress("Migrating databases", 0.6f); ConfigurationMigrator migrator = new ConfigurationMigrator(); migrator.Ensure(hasDatabase); // Prepare clinical protocols //retVal.GetService<ICarePlanService>().Repository = retVal.GetService<IClinicalProtocolRepositoryService>(); } catch (Exception e) { retVal.m_tracer.TraceError(e.ToString()); throw; } finally { retVal.ConfigurationPersister.Save(retVal.Configuration); } if (!retVal.Configuration.GetSection <DiagnosticsConfigurationSection>().TraceWriter.Any(o => o.TraceWriterClassXml.Contains("Console"))) { retVal.Configuration.GetSection <DiagnosticsConfigurationSection>().TraceWriter.Add(new TraceWriterConfiguration() { TraceWriter = typeof(ConsoleTraceWriter), #if DEBUG Filter = EventLevel.Informational #else Filter = EventLevel.Warning #endif }); } // Start daemons retVal.GetService <IThreadPoolService>().QueueUserWorkItem((o) => retVal.Start()); }
/// <summary> /// Start the application context /// </summary> public static bool StartContext(IDialogProvider dialogProvider, String instanceName, SecurityApplication applicationId, SanteDBHostType hostType) { // Not configured if (!new DcConfigurationManager(instanceName).IsConfigured) { return(false); } else { // Set master application context DcApplicationContext retVal = null; try { try { retVal = new DcApplicationContext(dialogProvider, instanceName, applicationId, hostType); ApplicationServiceContext.Current = DcApplicationContext.Current = retVal; //retVal.AddServiceProvider(typeof(ConfigurationManager)); if (retVal.ConfigurationPersister == null) { throw new InvalidOperationException("Missing configuration persistence service"); } retVal.ConfigurationPersister.Backup(retVal.Configuration); } catch (Exception e) { Trace.TraceWarning("Error loading configuration: {0}", e); if (retVal.ConfigurationPersister.HasBackup() && retVal.Confirm(Strings.err_configuration_invalid_restore_prompt)) { retVal.ConfigurationPersister.Restore(); retVal.ConfigurationManager.Reload(); } else { throw new Exception("Could not load or backup configuration", e); } } if (retVal.GetService <IBackupService>() == null) { retVal.GetService <IServiceManager>().AddServiceProvider(typeof(DefaultBackupService)); } // Is there a backup, and if so, does the user want to restore from that backup? var backupSvc = retVal.GetService <IBackupService>(); if (retVal.ConfigurationManager.GetAppSetting("ignore.restore") == null && backupSvc.HasBackup(BackupMedia.Public) && retVal.Confirm(Strings.locale_confirm_restore)) { backupSvc.Restore(BackupMedia.Public); } else { backupSvc.AutoRestore(); } // Ignore restoration if (retVal.ConfigurationManager.GetAppSetting("ignore.restore") == null) { retVal.Configuration.GetSection <ApplicationServiceContextConfigurationSection>().AppSettings.Add(new AppSettingKeyValuePair() { Key = "ignore.restore", Value = "true" }); } // Add tracers retVal.m_tracer = Tracer.GetTracer(typeof(DcApplicationContext)); retVal.m_tracer.TraceInfo("Starting logging infrastructure"); var configuration = retVal.Configuration.GetSection <DiagnosticsConfigurationSection>(); foreach (var tr in configuration.TraceWriter) { Tracer.AddWriter(Activator.CreateInstance(tr.TraceWriter, tr.Filter, tr.InitializationData, configuration.Sources.ToDictionary(o => o.SourceName, o => o.Filter)) as TraceWriter, tr.Filter); } retVal.SetProgress("Loading configuration", 0.2f); // Load all user-downloaded applets in the data directory var configuredApplets = retVal.Configuration.GetSection <AppletConfigurationSection>().Applets; var appletService = retVal.GetService <IAppletManagerService>(); var updateService = retVal.GetService <IUpdateManager>(); foreach (var appletInfo in configuredApplets.ToArray())// Directory.GetFiles(this.m_configuration.GetSection<AppletConfigurationSection>().AppletDirectory)) { { try { retVal.m_tracer.TraceInfo("Loading applet {0}", appletInfo); String appletPath = Path.Combine(retVal.Configuration.GetSection <AppletConfigurationSection>().AppletDirectory, appletInfo.Id); using (var fs = File.OpenRead(appletPath)) { AppletManifest manifest = AppletManifest.Load(fs); // Is this applet in the allowed applets // public key token match? if (appletInfo.PublicKeyToken != manifest.Info.PublicKeyToken) { retVal.m_tracer.TraceWarning("Applet {0} failed validation", appletInfo); ; // TODO: Raise an error } appletService.LoadApplet(manifest); } } catch (Exception e) { if (retVal.Confirm(String.Format(Strings.err_applet_corrupt_reinstall, appletInfo.Id))) { String appletPath = Path.Combine(retVal.Configuration.GetSection <AppletConfigurationSection>().AppletDirectory, appletInfo.Id); if (File.Exists(appletPath)) { File.Delete(appletPath); } try { configuredApplets.Remove(appletInfo); updateService.Install(appletInfo.Id); } catch { retVal.Alert(String.Format(Strings.err_updateFailed)); } } else { retVal.m_tracer.TraceError("Loading applet {0} failed: {1}", appletInfo, e.ToString()); throw new Exception($"Could not load applet {appletInfo}", e); } } } // Set the entity source EntitySource.Current = new EntitySource(retVal.GetService <IEntitySourceProvider>()); ApplicationServiceContext.Current = ApplicationContext.Current; // Ensure data migration exists bool hasDatabase = retVal.ConfigurationManager.Configuration.GetSection <DcDataConfigurationSection>().ConnectionString.Count > 0; try { // If the DB File doesn't exist we have to clear the migrations if (hasDatabase && !File.Exists(retVal.ConfigurationManager.GetConnectionString(retVal.Configuration.GetSection <DcDataConfigurationSection>().MainDataSourceConnectionStringName).GetComponent("dbfile"))) { retVal.m_tracer.TraceWarning("Can't find the SanteDB database, will re-install all migrations"); retVal.Configuration.GetSection <DcDataConfigurationSection>().MigrationLog.Entry.Clear(); } retVal.SetProgress("Migrating databases", 0.6f); ConfigurationMigrator migrator = new ConfigurationMigrator(); migrator.Ensure(hasDatabase); // Prepare clinical protocols //retVal.GetService<ICarePlanService>().Repository = retVal.GetService<IClinicalProtocolRepositoryService>(); } catch (Exception e) { retVal.m_tracer.TraceError(e.ToString()); throw new Exception("Error executing migrations", e); } finally { retVal.ConfigurationPersister.Save(retVal.Configuration); } // Update the applets if there are new versions foreach (var appPath in Directory.GetFiles(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "applets"))) { try { using (var fs = File.OpenRead(appPath)) { retVal.m_tracer.TraceInfo("Checking if {0} is upgradable", appPath); AppletPackage package = AppletPackage.Load(fs); var existing = appletService.GetApplet(package.Meta.Id); retVal.m_tracer.TraceInfo("{0} = {1} , existing = {2}", appPath, package.Meta, existing?.Info); if (existing == null || new Version(existing.Info.Version) < new Version(package.Meta.Version)) { if (existing != null) { retVal.m_tracer.TraceInfo("Upgrading applet {0} from {1} to {2}", package.Meta.Id, existing.Info.Version, package.Meta.Version); } appletService.Install(package, true); } } } catch (Exception e) { retVal.m_tracer.TraceError("Checking upgrade for applet {0} failed: {1}", appPath, e.ToString()); } } if (retVal.GetService <IThreadPoolService>() == null) { throw new InvalidOperationException(("Missing thread pool service(s)")); } // Start daemons updateService?.AutoUpdate(); retVal.GetService <IThreadPoolService>().QueueUserWorkItem((o) => retVal.Start()); //retVal.Start(); } catch (Exception e) { //ApplicationContext.Current = null; throw new Exception("Error starting context", e); } return(true); } }