Beispiel #1
0
        /// <summary>
        /// Set the session id for this current thread.
        /// Each request has its own thread.
        /// Threads can be reused for different users.
        /// </summary>
        public static void InitThread(string AThreadDescription, string AConfigFileName, string ASessionID = null)
        {
            TLogWriter.ResetStaticVariables();
            TLogging.ResetStaticVariables();

            new TAppSettingsManager(AConfigFileName);
            new TLogging(TSrvSetting.ServerLogFile);
            TLogging.DebugLevel = TAppSettingsManager.GetInt16("Server.DebugLevel", 0);

            string httprequest = "";

            if ((HttpContext.Current != null) && (HttpContext.Current.Request != null))
            {
                httprequest = " for path " + HttpContext.Current.Request.PathInfo;
            }

            TLogging.LogAtLevel(4, AThreadDescription + ": Running InitThread for thread id " + Thread.CurrentThread.ManagedThreadId.ToString() + httprequest);

            FSessionID     = ASessionID;
            FSessionValues = null;

            string sessionID;

            if (ASessionID == null)
            {
                sessionID = FindSessionID();
            }
            else
            {
                sessionID = ASessionID;
            }

            TDataBase db = null;

            // avoid dead lock on parallel logins
            FDeleteSessionMutex.WaitOne();

            try
            {
                db = ConnectDB("SessionInitThread");

                TDBTransaction t            = new TDBTransaction();
                bool           SubmissionOK = false;
                bool           newSession   = false;

                db.WriteTransaction(ref t,
                                    ref SubmissionOK,
                                    delegate
                {
                    // get the session ID, or start a new session
                    // load the session values from the database
                    // update the session last access in the database
                    // clean old sessions
                    newSession = InitSession(sessionID, t);

                    SubmissionOK = true;
                });

                if (newSession)
                {
                    // use a separate transaction to clean old sessions
                    db.WriteTransaction(ref t,
                                        ref SubmissionOK,
                                        delegate
                    {
                        CleanOldSessions(t);
                        SubmissionOK = true;
                    });
                }
            }
            finally
            {
                db.CloseDBConnection();

                FDeleteSessionMutex.ReleaseMutex();
            }
        }
        /// <summary>
        /// initialise the server for each Web Request
        /// </summary>
        private static bool Init()
        {
            string ConfigFileName = string.Empty;

            // make sure the correct config file is used
            string Instance = HttpContext.Current.Request.Url.ToString().Replace("http://", "").Replace("https://", "");

            Instance = Instance.Substring(0, Instance.IndexOf(".")).Replace("op_", "op").Replace("op", "op_");

            // for demo etc
            if (!Instance.StartsWith("op_"))
            {
                Instance = "op_" + Instance;
            }

            ConfigFileName = "/home/" + Instance + "/etc/PetraServerConsole.config";

            if (File.Exists(ConfigFileName))
            {
                // we are in a multi tenant hosting scenario
            }
            else if (Environment.CommandLine.Contains("/appconfigfile="))
            {
                // this happens when we use fastcgi-mono-server4
                ConfigFileName = Environment.CommandLine.Substring(
                    Environment.CommandLine.IndexOf("/appconfigfile=") + "/appconfigfile=".Length);

                if (ConfigFileName.IndexOf(" ") != -1)
                {
                    ConfigFileName = ConfigFileName.Substring(0, ConfigFileName.IndexOf(" "));
                }
            }
            else
            {
                // this is the normal behaviour when running with local http server
                ConfigFileName = AppDomain.CurrentDomain.BaseDirectory + Path.DirectorySeparatorChar + "web.config";
            }

            TLogWriter.ResetStaticVariables();
            TLogging.ResetStaticVariables();
            TTypedDataTable.ResetStaticVariables();
            TPdfPrinter.ResetStaticVariables();
            THTTPUtils.ResetStaticVariables();
            TSharedDataCache.TMPartner.ResetStaticVariables();
            TServerManagerBase.ResetStaticVariables();
            TClientManager.ResetStaticVariables();

            new TAppSettingsManager(ConfigFileName);
            new TLogging(TSrvSetting.ServerLogFile);
            TLogging.DebugLevel = TAppSettingsManager.GetInt16("Server.DebugLevel", 0);
            TSession.InitThread();

            if (TLogging.DebugLevel >= 4)
            {
                TLogging.Log("TOpenPetraOrgSessionManager.Init");
                TLogging.Log(HttpContext.Current.Request.PathInfo);
            }

            if (HttpContext.Current != null)
            {
                HttpContext.Current.Server.ScriptTimeout = Convert.ToInt32(
                    TimeSpan.FromMinutes(TAppSettingsManager.GetInt32("WebRequestTimeOutInMinutes", 15)).
                    TotalSeconds);
            }

            // if the Login Method is called: reset cookie, ignore any old session
            if ((HttpContext.Current != null) && (HttpContext.Current.Request.PathInfo == "/Login"))
            {
                TSession.CloseSession();
                TSession.InitThread();
            }

            Catalog.Init();

            ErrorCodeInventory.Init();
            ErrorCodeInventory.BuildErrorCodeInventory(typeof(Ict.Petra.Shared.PetraErrorCodes));
            ErrorCodeInventory.BuildErrorCodeInventory(typeof(Ict.Common.Verification.TStringChecks));

            TServerManager.TheServerManager = new TServerManager();

            // initialise the cached tables and the delegates
            TSetupDelegates.Init();

            TLogging.LogAtLevel(4, "Server has been initialised");

            return(true);
        }