Ejemplo n.º 1
0
        /// <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(IDialogProvider dialogProvider, String instanceName, SecurityApplication applicationId, SanteDBHostType hostType)
        {
            try
            {
                var retVal = new DcApplicationContext(dialogProvider, instanceName, applicationId, hostType);
                retVal.SetProgress("Run setup", 0);
                //retVal.AddServiceProvider(typeof(ConfigurationManager));
                ApplicationServiceContext.Current = DcApplicationContext.Current = retVal;
                retVal.m_tracer = Tracer.GetTracer(typeof(DcApplicationContext));
                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.GetService <IServiceManager>().AddServiceProvider(typeof(DefaultBackupService));

                var appletService = retVal.GetService <IAppletManagerService>();

                retVal.SetProgress("Loading configuration", 0.2f);
                // Load all user-downloaded applets in the data directory
                foreach (var appPath in Directory.GetFiles(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "applets")))
                {
                    try
                    {
                        retVal.m_tracer.TraceInfo("Installing applet {0}", appPath);
                        using (var fs = File.OpenRead(appPath))
                        {
                            AppletPackage package = AppletPackage.Load(fs);
                            appletService.Install(package, true);
                        }
                    }
                    catch (Exception e)
                    {
                        retVal.m_tracer.TraceError("Loading applet {0} failed: {1}", appPath, e.ToString());
                        throw;
                    }
                }

                retVal.GetService <IThreadPoolService>().QueueUserWorkItem((o) => retVal.Start());

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine("SanteDB FATAL: {0}", e.ToString());
                return(false);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Start a restore context
        /// </summary>
        /// <returns></returns>
        public static bool StartRestore(IDialogProvider dialogProvider, String instanceName, SecurityApplication applicationId, SanteDBHostType hostType)
        {
            try
            {
                var retVal = new DcApplicationContext(dialogProvider, instanceName, applicationId, hostType);
                retVal.SetProgress("Start restore", 0);
                //retVal.AddServiceProvider(typeof(ConfigurationManager));

                ApplicationServiceContext.Current = DcApplicationContext.Current = retVal;
                retVal.m_tracer = Tracer.GetTracer(typeof(DcApplicationContext));
                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.GetService <IServiceManager>().AddServiceProvider(typeof(DefaultBackupService));
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine("SanteDB FATAL: {0}", e.ToString());
                return(false);
            }
        }
Ejemplo n.º 3
0
        /// <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);
            }
        }