* ISessionHandler.cs: Pass the SessionStateModule to handlers when
[mono.git] / mcs / class / System.Web / System.Web.SessionState / SessionStateModule.cs
1 //
2 // System.Web.SessionState.SesionStateModule
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Stefan Görling (stefan@gorling.se)
7 //
8 // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
9 // (C) 2003 Stefan Görling (http://www.gorling.se)
10
11 using System.Web;
12 using System.Security.Cryptography;
13
14 namespace System.Web.SessionState
15 {
16         [MonoTODO]
17         public sealed class SessionStateModule : IHttpModule, IRequiresSessionState
18         {
19                 static SessionConfig config;
20                 static Type handlerType;
21                 ISessionHandler handler;
22
23                 private RandomNumberGenerator rng;
24                 
25                 public SessionStateModule ()
26                 {
27                         rng = new RNGCryptoServiceProvider ();
28                 }
29
30                 internal RandomNumberGenerator Rng {
31                         get { return rng; }
32                 }
33                 
34                 public void Dispose ()
35                 {
36                     if (handler!=null)
37                         handler.Dispose();
38                 }
39
40                 [MonoTODO]
41                 public void Init (HttpApplication app)
42                 {
43                         if (config == null) {
44                                 config = (SessionConfig) HttpContext.GetAppConfig ("system.web/sessionState");
45                                 if (config ==  null)
46                                         config = new SessionConfig (null);
47
48                                 if (config.Mode == SessionStateMode.StateServer)
49                                         throw new NotSupportedException ("StateServer mode is not supported.");
50
51                                 if (config.Mode == SessionStateMode.SQLServer)
52                                         handlerType = typeof (SessionSQLServerHandler);
53                                 
54                                 if (config.Mode == SessionStateMode.InProc)
55                                         handlerType = typeof (SessionInProcHandler);
56                         }
57                                 
58                         app.AddOnAcquireRequestStateAsync (
59                                 new BeginEventHandler (OnBeginAcquireState),
60                                 new EndEventHandler (OnEndAcquireState));
61
62                         app.ReleaseRequestState += new EventHandler (OnReleaseRequestState);
63                         app.EndRequest += new EventHandler (OnEndRequest);
64                         
65                         if (handlerType != null && handler == null) {
66                                 handler = (ISessionHandler) Activator.CreateInstance (handlerType);
67                                 handler.Init(app, config); //initialize
68                         }
69                 }
70
71                 void OnReleaseRequestState (object o, EventArgs args)
72                 {
73                         if (handler == null)
74                                 return;
75
76                         HttpApplication application = (HttpApplication) o;
77                         HttpContext context = application.Context;
78                         handler.UpdateHandler (context, this);
79                 }
80
81                 void OnEndRequest (object o, EventArgs args)
82                 {
83                 }
84
85                 IAsyncResult OnBeginAcquireState (object o, EventArgs args, AsyncCallback cb, object data)
86                 {
87
88                         HttpApplication application = (HttpApplication) o;
89                         HttpContext context = application.Context;
90
91                         bool isNew = false;
92                         if (handler != null)
93                             isNew = handler.UpdateContext (context, this);
94                         
95                         // In the future, we might want to move the Async stuff down to
96                         // the interface level, if we're going to support other than
97                         // InProc, we might actually want to do things async, now we
98                         // simply fake it.
99                         HttpAsyncResult result=new HttpAsyncResult (cb,this);
100                         result.Complete (true, o, null);
101                         if (isNew && Start != null)
102                                 Start (this, args);
103
104                         return result;
105                 }
106
107                 void OnEndAcquireState (IAsyncResult result)
108                 {
109                 }
110
111                 internal void OnEnd ()
112                 {
113                         if (End != null)
114                                 End (this, EventArgs.Empty);
115                 }
116                 
117                 public event EventHandler Start;
118                 public event EventHandler End;
119         }
120 }
121
122
123
124