[Microsoft.Build] Fix expected output newline from ProcessWrapper.OutputStreamChanged...
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web.SessionState / ServletSessionStateStoreProvider.cs
1 //\r
2 // (C) 2006 Mainsoft Corporation (http://www.mainsoft.com)\r
3 // Author: Konstantin Triger <kostat@mainsoft.com>\r
4 //\r
5 \r
6 //\r
7 // Permission is hereby granted, free of charge, to any person obtaining\r
8 // a copy of this software and associated documentation files (the\r
9 // "Software"), to deal in the Software without restriction, including\r
10 // without limitation the rights to use, copy, modify, merge, publish,\r
11 // distribute, sublicense, and/or sell copies of the Software, and to\r
12 // permit persons to whom the Software is furnished to do so, subject to\r
13 // the following conditions:\r
14 //\r
15 // The above copyright notice and this permission notice shall be\r
16 // included in all copies or substantial portions of the Software.\r
17 //\r
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
19 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
20 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
21 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
22 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
23 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
24 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
25 //\r
26 \r
27 using System;\r
28 using System.Collections.Generic;\r
29 using System.Text;\r
30 using System.Web.SessionState;\r
31 using System.Web;\r
32 using System.Web.Hosting;\r
33 using javax.servlet;\r
34 using javax.servlet.http;\r
35 using Mainsoft.Web.Hosting;\r
36 \r
37 namespace Mainsoft.Web.SessionState\r
38 {\r
39         /// <summary>\r
40         /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>\r
41         /// <para>Manages session state information using Java EE session API.</para>\r
42         /// </summary>\r
43         public sealed partial class ServletSessionStateStoreProvider : SessionStateStoreProviderBase\r
44         {\r
45                 const int MAX_MINUTES_TIMEOUT = int.MaxValue / 60;\r
46                 #region Public Interface\r
47 \r
48                 public override SessionStateStoreData CreateNewStoreData (HttpContext context, int timeout) {\r
49 \r
50                         // we ignore this timeout and use web.xml settings.\r
51                         //must set now as this can be a last chance for ro item\r
52                         //GetSession (context, false).setMaxInactiveInterval (timeout * 60);\r
53                         int javaTimeoutInSeconds = GetSession (context, false).getMaxInactiveInterval ();                       \r
54                         timeout = GetIntervalInMinutes (javaTimeoutInSeconds);\r
55                         ServletSessionStateItemCollection sessionState = new ServletSessionStateItemCollection (context);\r
56                         return new SessionStateStoreData (\r
57                                 sessionState,\r
58                                 sessionState.StaticObjects,\r
59                                 timeout);\r
60                 }\r
61 \r
62                 public override void CreateUninitializedItem (HttpContext context, string id, int timeout) {\r
63                 }\r
64 \r
65                 public override void Dispose () {\r
66                         \r
67                 }\r
68 \r
69                 public override void EndRequest (HttpContext context) {\r
70                 }\r
71 \r
72                 public override SessionStateStoreData GetItem (HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions) {\r
73                         locked = false;\r
74                         lockAge = TimeSpan.Zero;\r
75                         lockId = null;\r
76                         actions = SessionStateActions.None;\r
77                         if (id == null)\r
78                                 return null;\r
79                         HttpSession session = GetSession (context, false, false);\r
80                         if (session == null)\r
81                                 return null;\r
82                         ServletSessionStateItemCollection sessionState = session.getAttribute (J2EEConsts.SESSION_STATE) as ServletSessionStateItemCollection;\r
83                         if (sessionState == null) //was not set\r
84                                 sessionState = new ServletSessionStateItemCollection (context);\r
85                         return new SessionStateStoreData (\r
86                                 sessionState,\r
87                                 sessionState.StaticObjects,\r
88                                 GetIntervalInMinutes(session.getMaxInactiveInterval ()));\r
89                 }\r
90 \r
91                 public override SessionStateStoreData GetItemExclusive (HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions) {\r
92                         return GetItem (context, id, out locked, out lockAge, out lockId, out actions);\r
93                 }\r
94 \r
95                 public override void InitializeRequest (HttpContext context) {\r
96                 }\r
97 \r
98                 public override void ReleaseItemExclusive (HttpContext context, string id, object lockId) {\r
99                 }\r
100 \r
101                 public override void RemoveItem (HttpContext context, string id, object lockId, SessionStateStoreData item) {\r
102                         GetSession (context, false).invalidate ();\r
103                 }\r
104 \r
105                 public override void ResetItemTimeout (HttpContext context, string id) {\r
106                         if (context == null)\r
107                                 throw new ArgumentNullException ("context");\r
108                         HttpSession session = GetSession (context, false);\r
109                         int current = session.getMaxInactiveInterval ();\r
110                         int requested = context.Session.Timeout * 60;\r
111                         if (current != requested)\r
112                                 session.setMaxInactiveInterval (requested);\r
113                 }\r
114 \r
115                 public override void SetAndReleaseItemExclusive (HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) {\r
116                         if (id == null)\r
117                                 return;\r
118 \r
119                         if (item.Items.Dirty)\r
120                                 GetSession(context, false).setAttribute (J2EEConsts.SESSION_STATE, item.Items);\r
121 \r
122                         ReleaseItemExclusive (context, id, lockId);\r
123                 }\r
124 \r
125                 public override bool SetItemExpireCallback (SessionStateItemExpireCallback expireCallback) {\r
126                         return true; //we call session.invalidate so our session listener will call Session_OnEnd\r
127                 }\r
128 \r
129                 #endregion\r
130 \r
131                 #region helpers\r
132 \r
133                 internal static HttpSessionStateContainer CreateContainer (HttpSession session) {\r
134                         ServletSessionStateItemCollection sessionState = session.getAttribute (J2EEConsts.SESSION_STATE) as ServletSessionStateItemCollection;\r
135                         if (sessionState == null) //was not set\r
136                                 sessionState = new ServletSessionStateItemCollection (null);\r
137 \r
138                         return new HttpSessionStateContainer (session.getId (),\r
139                                 sessionState, sessionState.StaticObjects,\r
140                                 GetIntervalInMinutes (session.getMaxInactiveInterval ()),\r
141                                 session.isNew (),\r
142                                 HttpCookieMode.AutoDetect, SessionStateMode.Custom,\r
143                                 true);\r
144                 }\r
145 \r
146                 internal static HttpSession GetSession (HttpContext context, bool create) {\r
147                         return GetSession (context, create, true);\r
148                 }\r
149 \r
150                 internal static HttpSession GetSession (HttpContext context, bool create, bool throwOnError) {\r
151                         HttpSession session = J2EEUtils.GetWorkerRequest (context).GetSession (create);\r
152                         if (session == null && throwOnError)\r
153                                 throw new HttpException ("Session is not established");\r
154 \r
155                         return session;\r
156                 }\r
157 \r
158                 static int GetIntervalInMinutes (int seconds)\r
159                 {\r
160                         if (seconds == -1)\r
161                                 return MAX_MINUTES_TIMEOUT;\r
162                         return (int) Math.Ceiling ((double) seconds / 60);              \r
163                 }\r
164 \r
165                 #endregion\r
166         }\r
167 }\r