11using System ;
2- using System . Collections . Generic ;
3- using System . Linq ;
4- using System . Threading . Tasks ;
2+ using GeneXus . Configuration ;
53using GeneXus . Encryption ;
4+ using GxClasses . Helpers ;
5+ using log4net ;
66
77namespace GeneXus . Services
88{
99 public class GXSessionServiceFactory
1010 {
11+ private static readonly ILog log = log4net . LogManager . GetLogger ( typeof ( GXSessionServiceFactory ) ) ;
1112 static string REDIS = "REDIS" ;
1213 static string DATABASE = "DATABASE" ;
13- static string SESSION_ADDRESS = "SESSION_PROVIDER_ADDRESS" ;
14- static string SESSION_INSTANCE = "SESSION_PROVIDER_INSTANCE_NAME" ;
15- static string SESSION_PASSWORD = "SESSION_PROVIDER_PASSWORD" ;
16- static string SESSION_SCHEMA = "SESSION_PROVIDER_SCHEMA" ;
17- static string SESSION_TABLE_NAME = "SESSION_PROVIDER_TABLE_NAME" ;
18- static string SESSION_TIMEOUT = "SESSION_PROVIDER_SESSION_TIMEOUT" ;
14+
1915 public static ISessionService GetProvider ( )
2016 {
21- GXService instance = GXServices . Instance ? . Get ( GXServices . SESSION_SERVICE ) ;
22- if ( instance != null )
17+ ISessionService sessionService = null ;
18+ GXService providerService = GXServices . Instance ? . Get ( GXServices . SESSION_SERVICE ) ;
19+ if ( providerService != null )
2320 {
24- string password = instance . Properties . Get ( SESSION_PASSWORD ) ;
25- if ( ! string . IsNullOrEmpty ( password ) )
26- {
27- password = CryptoImpl . Decrypt ( password ) ;
28- }
29- if ( instance . Name . Equals ( REDIS , StringComparison . OrdinalIgnoreCase ) )
21+ try
3022 {
31- string sessionTimeout = instance . Properties . Get ( SESSION_TIMEOUT ) ;
32- int timeout = 0 ;
33- if ( ! string . IsNullOrEmpty ( sessionTimeout ) )
34- int . TryParse ( sessionTimeout , out timeout ) ;
35-
36- return new GxRedisSession ( instance . Properties . Get ( SESSION_ADDRESS ) ,
37- password ,
38- instance . Properties . Get ( SESSION_INSTANCE ) ,
39- timeout ) ;
23+ string className = providerService . ClassName ;
24+ //Compatibility
25+ if ( string . IsNullOrEmpty ( className ) )
26+ {
27+ if ( providerService . Name . Equals ( REDIS , StringComparison . OrdinalIgnoreCase ) )
28+ className = typeof ( GxRedisSession ) . FullName ;
29+ else if ( providerService . Name . Equals ( DATABASE , StringComparison . OrdinalIgnoreCase ) )
30+ className = typeof ( GxDatabaseSession ) . FullName ;
31+ }
32+
33+ GXLogging . Debug ( log , "Loading Session provider:" , providerService . ClassName ) ;
34+ if ( ! string . IsNullOrEmpty ( providerService . ClassName ) )
35+ {
36+ #if ! NETCORE
37+ Type type = Type . GetType ( providerService . ClassName , true , true ) ;
38+ #else
39+ Type type = AssemblyLoader . GetType ( providerService . ClassName ) ;
40+ #endif
41+ sessionService = ( ISessionService ) Activator . CreateInstance ( type , new object [ ] { providerService } ) ;
42+ }
4043 }
41- else if ( instance . Name . Equals ( DATABASE , StringComparison . OrdinalIgnoreCase ) )
44+ catch ( Exception e )
4245 {
43- return new GxDatabaseSession ( instance . Properties . Get ( SESSION_ADDRESS ) ,
44- password ,
45- instance . Properties . Get ( SESSION_SCHEMA ) ,
46- instance . Properties . Get ( SESSION_TABLE_NAME ) ) ;
46+ GXLogging . Error ( log , "Couldn´t create Session provider." , e . Message , e ) ;
47+ throw e ;
4748 }
4849 }
4950 return null ;
@@ -52,6 +53,34 @@ public static ISessionService GetProvider()
5253 }
5354 public class GxRedisSession : ISessionService
5455 {
56+ internal static string SESSION_ADDRESS = "SESSION_PROVIDER_ADDRESS" ;
57+ internal static string SESSION_INSTANCE = "SESSION_PROVIDER_INSTANCE_NAME" ;
58+ internal static string SESSION_PASSWORD = "SESSION_PROVIDER_PASSWORD" ;
59+ static string SESSION_TIMEOUT = "SESSION_PROVIDER_SESSION_TIMEOUT" ;
60+
61+ public GxRedisSession ( GXService serviceProvider )
62+ {
63+ string password = serviceProvider . Properties . Get ( SESSION_PASSWORD ) ;
64+ if ( ! string . IsNullOrEmpty ( password ) )
65+ {
66+ password = CryptoImpl . Decrypt ( password ) ;
67+ }
68+ string host = serviceProvider . Properties . Get ( SESSION_ADDRESS ) ;
69+ string instanceName = serviceProvider . Properties . Get ( SESSION_INSTANCE ) ;
70+ ConnectionString = $ "{ host } ";
71+ if ( ! string . IsNullOrEmpty ( password ) )
72+ {
73+ ConnectionString += $ ",password={ password } ";
74+ }
75+ InstanceName = instanceName ;
76+
77+ int sessionTimeoutMinutes = Preferences . SessionTimeout ;
78+ string sessionTimeoutStrCompatibility = serviceProvider . Properties . Get ( SESSION_TIMEOUT ) ;
79+ if ( ! string . IsNullOrEmpty ( sessionTimeoutStrCompatibility ) )
80+ int . TryParse ( sessionTimeoutStrCompatibility , out sessionTimeoutMinutes ) ;
81+
82+ SessionTimeout = sessionTimeoutMinutes ;
83+ }
5584 public GxRedisSession ( string host , string password , string instanceName , int sessionTimeout )
5685 {
5786 ConnectionString = $ "{ host } ";
@@ -72,6 +101,53 @@ public GxRedisSession(string host, string password, string instanceName, int ses
72101 }
73102 public class GxDatabaseSession : ISessionService
74103 {
104+ internal static string SESSION_ADDRESS = "SESSION_PROVIDER_ADDRESS" ;
105+ internal static string SESSION_PASSWORD = "SESSION_PROVIDER_PASSWORD" ;
106+ internal static string SESSION_SCHEMA = "SESSION_PROVIDER_SCHEMA" ;
107+ internal static string SESSION_TABLE_NAME = "SESSION_PROVIDER_TABLE_NAME" ;
108+ internal static string SESSION_PROVIDER_SERVER = "SESSION_PROVIDER_SERVER" ;
109+ internal static string SESSION_PROVIDER_DATABASE = "SESSION_PROVIDER_DATABASE" ;
110+ internal static string SESSION_PROVIDER_USER = "SESSION_PROVIDER_USER" ;
111+
112+ internal GxDatabaseSession ( GXService serviceProvider )
113+ {
114+ string password = serviceProvider . Properties . Get ( SESSION_PASSWORD ) ;
115+ if ( ! string . IsNullOrEmpty ( password ) )
116+ {
117+ password = CryptoImpl . Decrypt ( password ) ;
118+ }
119+ string serverName = serviceProvider . Properties . Get ( SESSION_PROVIDER_SERVER ) ;
120+ string userName = serviceProvider . Properties . Get ( SESSION_PROVIDER_USER ) ;
121+ string database = serviceProvider . Properties . Get ( SESSION_PROVIDER_DATABASE ) ;
122+ string schema = serviceProvider . Properties . Get ( SESSION_SCHEMA ) ;
123+ string tableName = serviceProvider . Properties . Get ( SESSION_TABLE_NAME ) ;
124+
125+ string sessionAddresCompatibility = serviceProvider . Properties . Get ( GxDatabaseSession . SESSION_ADDRESS ) ;
126+ if ( ! string . IsNullOrEmpty ( sessionAddresCompatibility ) )
127+ {
128+ ConnectionString = sessionAddresCompatibility ;
129+ }
130+
131+ if ( ! string . IsNullOrEmpty ( serverName ) )
132+ {
133+ ConnectionString = $ "Data Source={ serverName } ;";
134+ }
135+ if ( ! string . IsNullOrEmpty ( database ) )
136+ {
137+ ConnectionString = $ "Initial Catalog={ database } ";
138+ }
139+ if ( ! string . IsNullOrEmpty ( password ) )
140+ {
141+ ConnectionString += $ ";password={ password } ";
142+ }
143+ if ( ! string . IsNullOrEmpty ( userName ) )
144+ {
145+ ConnectionString += $ ";user={ userName } ";
146+ }
147+ Schema = schema ;
148+ TableName = tableName ;
149+ SessionTimeout = Preferences . SessionTimeout ;
150+ }
75151 public GxDatabaseSession ( string host , string password , string schema , string tableName )
76152 {
77153 ConnectionString = $ "{ host } ";
@@ -90,7 +166,7 @@ public GxDatabaseSession(string host, string password, string schema, string tab
90166
91167 public string InstanceName => throw new NotImplementedException ( ) ;
92168
93- public int SessionTimeout => throw new NotImplementedException ( ) ;
169+ public int SessionTimeout { get ; }
94170 }
95171
96172 public interface ISessionService
0 commit comments