2016-06-22 17:49:16 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2020-01-06 16:49:18 +01:00
|
|
|
// Copyright (c) 2010-2020 Daniele Teti and the DMVCFramework Team
|
2016-06-22 17:49:16 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
// *************************************************************************** }
|
2015-12-22 12:38:17 +01:00
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
unit MVCFramework.ApplicationSession;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
interface
|
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
System.DateUtils,
|
|
|
|
System.Generics.Collections;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
type
|
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
TWebApplicationSession = class abstract
|
|
|
|
private
|
|
|
|
{ private declarations }
|
|
|
|
protected
|
|
|
|
function GetItems(const AKey: string): string; virtual; abstract;
|
|
|
|
procedure SetItems(const AKey, AValue: string); virtual; abstract;
|
2013-10-30 00:48:23 +01:00
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
|
|
|
destructor Destroy; override;
|
2017-03-20 19:08:01 +01:00
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
function ToString: string; override;
|
2017-03-20 19:08:01 +01:00
|
|
|
|
|
|
|
property Items[const AKey: string]: string read GetItems write SetItems; default;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
TWebApplicationSessionClass = class of TWebApplicationSession;
|
|
|
|
|
|
|
|
TWebApplicationSessionMemory = class(TWebApplicationSession)
|
2017-03-20 19:08:01 +01:00
|
|
|
private
|
2013-10-30 00:48:23 +01:00
|
|
|
FData: TDictionary<string, string>;
|
2017-03-20 19:08:01 +01:00
|
|
|
protected
|
|
|
|
function GetItems(const AKey: String): String; override;
|
|
|
|
procedure SetItems(const AKey, AValue: String); override;
|
2013-10-30 00:48:23 +01:00
|
|
|
public
|
|
|
|
constructor Create; override;
|
|
|
|
destructor Destroy; override;
|
2017-03-20 19:08:01 +01:00
|
|
|
|
|
|
|
function ToString: String; override;
|
2017-03-23 18:51:25 +01:00
|
|
|
|
|
|
|
property Data: TDictionary<string, string> read FData;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
TMVCApplicationSessionFactory = class sealed
|
2017-03-20 19:08:01 +01:00
|
|
|
private
|
2013-10-30 00:48:23 +01:00
|
|
|
FRegisteredApplicationSessionTypes: TDictionary<String, TWebApplicationSessionClass>;
|
2017-03-23 18:51:25 +01:00
|
|
|
private
|
|
|
|
class var Instance: TMVCApplicationSessionFactory;
|
2017-03-20 19:08:01 +01:00
|
|
|
public
|
2017-03-23 18:51:25 +01:00
|
|
|
constructor Create;
|
2015-04-01 17:01:23 +02:00
|
|
|
destructor Destroy; override;
|
2017-03-20 19:08:01 +01:00
|
|
|
|
|
|
|
procedure RegisterSessionType(const AName: String; AWebApplicationSessionClass: TWebApplicationSessionClass);
|
|
|
|
function CreateNewByType(const AName: String): TWebApplicationSession;
|
2017-03-23 18:51:25 +01:00
|
|
|
|
|
|
|
class function GetInstance: TMVCApplicationSessionFactory; static;
|
|
|
|
class procedure DestroyInstance; static;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
{ TWebApplicationSession }
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
constructor TWebApplicationSession.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TWebApplicationSession.Destroy;
|
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
inherited Destroy;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TWebApplicationSession.ToString: string;
|
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
Result := EmptyStr;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
{ TWebApplicationSessionMemory }
|
|
|
|
|
2013-10-30 00:48:23 +01:00
|
|
|
constructor TWebApplicationSessionMemory.Create;
|
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
inherited Create;
|
2013-10-30 00:48:23 +01:00
|
|
|
FData := TDictionary<string, string>.Create;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TWebApplicationSessionMemory.Destroy;
|
|
|
|
begin
|
|
|
|
FData.Free;
|
2017-03-23 18:51:25 +01:00
|
|
|
inherited Destroy;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
function TWebApplicationSessionMemory.GetItems(const AKey: String): String;
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
TMonitor.Enter(Self);
|
2013-10-30 00:48:23 +01:00
|
|
|
try
|
2017-03-20 19:08:01 +01:00
|
|
|
if not FData.TryGetValue(AKey, Result) then
|
2017-03-23 18:51:25 +01:00
|
|
|
Result := EmptyStr;
|
2013-10-30 00:48:23 +01:00
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
TMonitor.Exit(Self);
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
procedure TWebApplicationSessionMemory.SetItems(const AKey, AValue: String);
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
TMonitor.Enter(Self);
|
2013-10-30 00:48:23 +01:00
|
|
|
try
|
2017-03-20 19:08:01 +01:00
|
|
|
FData.AddOrSetValue(AKey, AValue);
|
2013-10-30 00:48:23 +01:00
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
TMonitor.Exit(Self);
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TWebApplicationSessionMemory.ToString: String;
|
|
|
|
var
|
2017-03-20 19:08:01 +01:00
|
|
|
LKey: String;
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
TMonitor.Enter(Self);
|
2013-10-30 00:48:23 +01:00
|
|
|
try
|
2017-03-23 18:51:25 +01:00
|
|
|
Result := EmptyStr;
|
2017-03-20 19:08:01 +01:00
|
|
|
for LKey in FData.Keys do
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
2017-03-20 19:08:01 +01:00
|
|
|
Result := LKey + ' = ' + QuotedStr(FData.Items[LKey]) + sLineBreak;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
finally
|
2017-03-23 18:51:25 +01:00
|
|
|
TMonitor.Exit(Self);
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
{ TMVCApplicationSessionFactory }
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
constructor TMVCApplicationSessionFactory.Create;
|
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
inherited Create;
|
2013-10-30 00:48:23 +01:00
|
|
|
FRegisteredApplicationSessionTypes := TDictionary<string, TWebApplicationSessionClass>.Create;
|
|
|
|
end;
|
|
|
|
|
2016-09-27 14:22:17 +02:00
|
|
|
function TMVCApplicationSessionFactory.CreateNewByType(const AName: String): TWebApplicationSession;
|
2013-10-30 00:48:23 +01:00
|
|
|
var
|
2017-03-23 18:51:25 +01:00
|
|
|
Clazz: TWebApplicationSessionClass;
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
if not FRegisteredApplicationSessionTypes.TryGetValue(AName, Clazz) then
|
2013-10-30 00:48:23 +01:00
|
|
|
raise Exception.Create('Unknown application session type');
|
2017-03-23 18:51:25 +01:00
|
|
|
Result := Clazz.Create;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TMVCApplicationSessionFactory.Destroy;
|
|
|
|
begin
|
|
|
|
FRegisteredApplicationSessionTypes.Free;
|
2017-03-23 18:51:25 +01:00
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
class procedure TMVCApplicationSessionFactory.DestroyInstance;
|
|
|
|
begin
|
|
|
|
if Assigned(Instance) then
|
|
|
|
Instance.Free;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
class function TMVCApplicationSessionFactory.GetInstance: TMVCApplicationSessionFactory;
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
2017-03-23 18:51:25 +01:00
|
|
|
if not Assigned(Instance) then
|
|
|
|
Instance := TMVCApplicationSessionFactory.Create;
|
|
|
|
Result := Instance;
|
2013-10-30 00:48:23 +01:00
|
|
|
end;
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
procedure TMVCApplicationSessionFactory.RegisterSessionType(const AName: String; AWebApplicationSessionClass: TWebApplicationSessionClass);
|
2013-10-30 00:48:23 +01:00
|
|
|
begin
|
|
|
|
FRegisteredApplicationSessionTypes.AddOrSetValue(AName, AWebApplicationSessionClass);
|
|
|
|
end;
|
|
|
|
|
|
|
|
initialization
|
|
|
|
|
2017-03-20 19:08:01 +01:00
|
|
|
TMVCApplicationSessionFactory.GetInstance.RegisterSessionType('memory', TWebApplicationSessionMemory);
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
finalization
|
|
|
|
|
2017-03-23 18:51:25 +01:00
|
|
|
TMVCApplicationSessionFactory.DestroyInstance;
|
2013-10-30 00:48:23 +01:00
|
|
|
|
|
|
|
end.
|