2 // (C) 2006 Mainsoft Corporation (http://www.mainsoft.com)
\r
3 // Author: Konstantin Triger <kostat@mainsoft.com>
\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
15 // The above copyright notice and this permission notice shall be
\r
16 // included in all copies or substantial portions of the Software.
\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
28 using System.Collections.Generic;
\r
30 using System.Web.SessionState;
\r
32 using System.Web.Hosting;
\r
33 using javax.servlet;
\r
34 using javax.servlet.http;
\r
35 using Mainsoft.Web.Hosting;
\r
37 namespace Mainsoft.Web.SessionState
\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
43 public sealed partial class ServletSessionStateStoreProvider : SessionStateStoreProviderBase
\r
45 const int MAX_MINUTES_TIMEOUT = int.MaxValue / 60;
\r
46 #region Public Interface
\r
48 public override SessionStateStoreData CreateNewStoreData (HttpContext context, int timeout) {
\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
58 sessionState.StaticObjects,
\r
62 public override void CreateUninitializedItem (HttpContext context, string id, int timeout) {
\r
65 public override void Dispose () {
\r
69 public override void EndRequest (HttpContext context) {
\r
72 public override SessionStateStoreData GetItem (HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions) {
\r
74 lockAge = TimeSpan.Zero;
\r
76 actions = SessionStateActions.None;
\r
79 HttpSession session = GetSession (context, false, false);
\r
80 if (session == 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
87 sessionState.StaticObjects,
\r
88 GetIntervalInMinutes(session.getMaxInactiveInterval ()));
\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
95 public override void InitializeRequest (HttpContext context) {
\r
98 public override void ReleaseItemExclusive (HttpContext context, string id, object lockId) {
\r
101 public override void RemoveItem (HttpContext context, string id, object lockId, SessionStateStoreData item) {
\r
102 GetSession (context, false).invalidate ();
\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
115 public override void SetAndReleaseItemExclusive (HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) {
\r
119 if (item.Items.Dirty)
\r
120 GetSession(context, false).setAttribute (J2EEConsts.SESSION_STATE, item.Items);
\r
122 ReleaseItemExclusive (context, id, lockId);
\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
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
138 return new HttpSessionStateContainer (session.getId (),
\r
139 sessionState, sessionState.StaticObjects,
\r
140 GetIntervalInMinutes (session.getMaxInactiveInterval ()),
\r
142 HttpCookieMode.AutoDetect, SessionStateMode.Custom,
\r
146 internal static HttpSession GetSession (HttpContext context, bool create) {
\r
147 return GetSession (context, create, true);
\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
158 static int GetIntervalInMinutes (int seconds)
\r
161 return MAX_MINUTES_TIMEOUT;
\r
162 return (int) Math.Ceiling ((double) seconds / 60);
\r