Beispiel #1
0
        /// <summary>
        /// It is used to stop all service started by SIMINOV.
        /// When application shutdown they should call this. It do following services:
        ///
        /// <list type="bullet">
        /// <item>
        ///     <description>Close all database's opened by SIMINOV.</description>
        /// </item>
        /// <item>
        ///     <description>Deallocate all resources held by SIMINOV.</description>
        /// </item>
        /// </list>
        /// </summary>
        public static void Shutdown()
        {
            IsActive();

            IEnumerator <DatabaseDescriptor> databaseDescriptors = coreResourceManager.GetDatabaseDescriptors();

            bool failed = false;

            while (databaseDescriptors.MoveNext())
            {
                DatabaseDescriptor databaseDescriptor = databaseDescriptors.Current;
                DatabaseBundle     databaseBundle     = coreResourceManager.GetDatabaseBundle(databaseDescriptor.GetDatabaseName());
                IDatabaseImpl      database           = databaseBundle.GetDatabase();

                try
                {
                    database.Close(databaseDescriptor);
                }
                catch (DatabaseException databaseException)
                {
                    failed = true;

                    Log.Log.Error(typeof(Siminov).FullName, "Shutdown", "DatabaseException caught while closing database, " + databaseException.GetMessage());
                    continue;
                }
            }

            if (failed)
            {
                throw new SiminovCriticalException(typeof(Siminov).FullName, "Shutdown", "DatabaseException caught while closing database.");
            }

            ISiminovEvents coreEventHandler = coreResourceManager.GetSiminovEventHandler();

            if (coreResourceManager.GetSiminovEventHandler() != null)
            {
                coreEventHandler.OnSiminovStopped();
            }
        }
Beispiel #2
0
        /// <summary>
        /// It process all DatabaseDescriptor.xml and initialize Database and stores in Resource Manager.
        /// </summary>
        protected static void ProcessDatabase()
        {
            ApplicationDescriptor            applicationDescriptor = coreResourceManager.GetApplicationDescriptor();
            IEnumerator <DatabaseDescriptor> databaseDescriptors   = applicationDescriptor.GetDatabaseDescriptors();

            while (databaseDescriptors.MoveNext())
            {
                DatabaseDescriptor databaseDescriptor = databaseDescriptors.Current;

                String         databasePath   = new DatabaseUtils().GetDatabasePath(databaseDescriptor);
                DatabaseBundle databaseBundle = null;

                try
                {
                    databaseBundle = DatabaseHelper.CreateDatabase(databaseDescriptor);
                }
                catch (DatabaseException databaseException)
                {
                    Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while getting database instance from database factory, DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName() + ", " + databaseException.GetMessage());
                    throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage());
                }

                IDatabaseImpl database     = databaseBundle.GetDatabase();
                IQueryBuilder queryBuilder = databaseBundle.GetQueryBuilder();



                /*
                 * If Database exists then open and return.
                 * If Database does not exists create the database.
                 */

                String databaseName = databaseDescriptor.GetDatabaseName();
                if (!databaseName.EndsWith(".db"))
                {
                    databaseName = databaseName + ".db";
                }


                bool fileExists = FileUtils.DoesFileExists(databasePath, databaseName, FileUtils.LOCAL_FOLDER);
                if (fileExists)
                {
                    /*
                     * Open Database
                     */
                    try
                    {
                        database.OpenOrCreate(databaseDescriptor);
                    }
                    catch (DatabaseException databaseException)
                    {
                        Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while opening database, " + databaseException.GetMessage());
                        throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage());
                    }


                    /*
                     * Enable Foreign Key Constraints
                     */
                    try
                    {
                        database.ExecuteQuery(databaseDescriptor, null, Constants.SQLITE_DATABASE_QUERY_TO_ENABLE_FOREIGN_KEYS_MAPPING);
                    }
                    catch (DatabaseException databaseException)
                    {
                        FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER);

                        Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while executing query to enable foreign keys, " + databaseException.GetMessage());
                        throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage());
                    }


                    /*
                     * Safe MultiThread Transaction
                     */
                    /*try
                     * {
                     *  database.ExecuteMethod(Constants.SQLITE_DATABASE_ENABLE_LOCKING, databaseDescriptor.IsTransactionSafe());
                     * }
                     * catch (DatabaseException databaseException)
                     * {
                     *  FileUtils.DoesFileExists(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER);
                     *
                     *  Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while enabling locking on database, " + databaseException.GetMessage());
                     *  throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage());
                     * }*/



                    /*
                     * Upgrade Database
                     */
                    try
                    {
                        DatabaseHelper.UpgradeDatabase(databaseDescriptor);
                    }
                    catch (DatabaseException databaseException)
                    {
                        Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while upgrading database, " + databaseException.GetMessage());
                        throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage());
                    }
                }
                else
                {
                    /*
                     * Create Database Directory
                     */
                    try
                    {
                        FileUtils.GetFolder(databasePath, FileUtils.LOCAL_FOLDER);
                    }
                    catch (System.Exception exception)
                    {
                        Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "Exception caught while creating database directories, DATABASE-PATH: " + databasePath + ", DATABASE-DESCRIPTOR: " + databaseDescriptor.GetDatabaseName() + ", " + exception.Message);
                        throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", exception.Message);
                    }


                    /*
                     * Create Database File.
                     */
                    try
                    {
                        database.OpenOrCreate(databaseDescriptor);
                    }
                    catch (DatabaseException databaseException)
                    {
                        FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER);

                        Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while creating database, " + databaseException.GetMessage());
                        throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage());
                    }


                    /*
                     * Set Database Version
                     */
                    IDictionary <String, Object> parameters = new Dictionary <String, Object>();
                    parameters.Add(IQueryBuilder.FORM_UPDATE_DATABASE_VERSION_QUERY_DATABASE_VERSION_PARAMETER, databaseDescriptor.GetVersion());


                    try
                    {
                        String updateDatabaseVersionQuery = queryBuilder.FormUpdateDatabaseVersionQuery(parameters);
                        database.ExecuteQuery(databaseDescriptor, null, updateDatabaseVersionQuery);
                    }
                    catch (DatabaseException databaseException)
                    {
                        Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "Database Exception caught while updating database version, " + databaseException.GetMessage());
                        throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage());
                    }



                    IDatabaseEvents databaseEventHandler = coreResourceManager.GetDatabaseEventHandler();
                    if (databaseEventHandler != null)
                    {
                        databaseEventHandler.OnDatabaseCreated(databaseDescriptor);
                    }


                    /*
                     * Enable Foreign Key Constraints
                     */
                    try
                    {
                        database.ExecuteQuery(databaseDescriptor, null, Constants.SQLITE_DATABASE_QUERY_TO_ENABLE_FOREIGN_KEYS_MAPPING);
                    }
                    catch (DatabaseException databaseException)
                    {
                        FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER);

                        Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while executing query to enable foreign keys, " + databaseException.GetMessage());
                        throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage());
                    }


                    /*
                     * Safe MultiThread Transaction
                     */
                    /*try
                     * {
                     *  database.ExecuteMethod(Constants.SQLITE_DATABASE_ENABLE_LOCKING, databaseDescriptor.IsTransactionSafe());
                     * }
                     * catch (DatabaseException databaseException)
                     * {
                     *  FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER);
                     *
                     *  Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while enabling locking on database, " + databaseException.GetMessage());
                     *  throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage());
                     * }*/



                    /*
                     * Create Tables
                     */
                    try
                    {
                        DatabaseHelper.CreateTables(databaseDescriptor.OrderedEntityDescriptors());
                    }
                    catch (DatabaseException databaseException)
                    {
                        FileUtils.DeleteFile(databasePath, databaseDescriptor.GetDatabaseName(), FileUtils.LOCAL_FOLDER);

                        Log.Log.Error(typeof(Siminov).FullName, "ProcessDatabase", "DatabaseException caught while creating tables, " + databaseException.GetMessage());
                        throw new DeploymentException(typeof(Siminov).FullName, "ProcessDatabase", databaseException.GetMessage());
                    }
                }
            }
        }