Added J2EE Portal support.
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web.SessionState / ServletSessionStateItemCollection.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;\r
29 using System.Collections.Generic;\r
30 using System.Text;\r
31 using System.Web.SessionState;\r
32 using System.Web;\r
33 using System.Threading;\r
34 \r
35 using javax.servlet;\r
36 using javax.servlet.http;\r
37 \r
38 namespace Mainsoft.Web.SessionState\r
39 {\r
40         public sealed partial class ServletSessionStateStoreProvider\r
41         {\r
42                 sealed class ServletSessionStateItemCollection : ISessionStateItemCollection, java.io.Externalizable\r
43                 {\r
44                         SessionStateItemCollection _items;\r
45                         HttpStaticObjectsCollection _staticObjects;\r
46                         bool _needSessionPersistence;\r
47 \r
48                         public ServletSessionStateItemCollection () {} // For Java deserialization\r
49 \r
50                         public ServletSessionStateItemCollection (HttpContext context)\r
51                                 : this () {\r
52 \r
53                                 _items = new SessionStateItemCollection ();\r
54                                 _staticObjects = new HttpStaticObjectsCollection ();\r
55 \r
56                                 if (context != null) {\r
57                                         ServletConfig config = ServletSessionStateStoreProvider.GetWorkerRequest (context).Servlet.getServletConfig ();\r
58                                         string sessionPersistance = J2EEUtils.GetInitParameterByHierarchy(config, J2EEConsts.Enable_Session_Persistency);\r
59                                         if (sessionPersistance == null)\r
60                                                 sessionPersistance = config.getServletContext().getInitParameter (J2EEConsts.Enable_Session_Persistency);\r
61                                         if (sessionPersistance != null) {\r
62                                                 try {\r
63                                                         _needSessionPersistence = Boolean.Parse (sessionPersistance);\r
64                                                 }\r
65                                                 catch (Exception) {\r
66                                                         _needSessionPersistence = false;\r
67 #if DEBUG\r
68                                                         Console.WriteLine ("EnableSessionPersistency init param's value is invalid. the value is " + sessionPersistance);\r
69 #endif\r
70                                                 }\r
71                                         }\r
72                                 }\r
73                         }\r
74 \r
75                         public HttpStaticObjectsCollection StaticObjects {\r
76                                 get { return _staticObjects; }\r
77                         }\r
78                         #region ISessionStateItemCollection Members\r
79 \r
80                         public void Clear () {\r
81                                 _items.Clear ();\r
82                         }\r
83 \r
84                         public bool Dirty {\r
85                                 get {\r
86                                         return _items.Dirty;\r
87                                 }\r
88                                 set {\r
89                                         _items.Dirty = value;\r
90                                 }\r
91                         }\r
92 \r
93                         public System.Collections.Specialized.NameObjectCollectionBase.KeysCollection Keys {\r
94                                 get { return _items.Keys; }\r
95                         }\r
96 \r
97                         public void Remove (string name) {\r
98                                 _items.Remove (name);\r
99                         }\r
100 \r
101                         public void RemoveAt (int index) {\r
102                                 _items.RemoveAt (index);\r
103                         }\r
104 \r
105                         public object this [int index] {\r
106                                 get {\r
107                                         return _items [index];\r
108                                 }\r
109                                 set {\r
110                                         _items [index] = value;\r
111                                 }\r
112                         }\r
113 \r
114                         public object this [string name] {\r
115                                 get {\r
116                                         return _items [name];\r
117                                 }\r
118                                 set {\r
119                                         _items [name] = value;\r
120                                 }\r
121                         }\r
122 \r
123                         #endregion\r
124 \r
125                         #region ICollection Members\r
126 \r
127                         public void CopyTo (Array array, int index) {\r
128                                 ((ICollection) _items).CopyTo (array, index);\r
129                         }\r
130 \r
131                         public int Count {\r
132                                 get { return ((ICollection) _items).Count; }\r
133                         }\r
134 \r
135                         public bool IsSynchronized {\r
136                                 get { return ((ICollection) _items).IsSynchronized; }\r
137                         }\r
138 \r
139                         public object SyncRoot {\r
140                                 get { return ((ICollection) _items).SyncRoot; }\r
141                         }\r
142 \r
143                         #endregion\r
144 \r
145                         #region IEnumerable Members\r
146 \r
147                         public System.Collections.IEnumerator GetEnumerator () {\r
148                                 return ((IEnumerable) _items).GetEnumerator ();\r
149                         }\r
150 \r
151                         #endregion\r
152 \r
153                         #region Externalizable Members\r
154 \r
155                         public void readExternal (java.io.ObjectInput input) {\r
156                                 lock (this) {\r
157                                         _needSessionPersistence = input.readBoolean ();\r
158                                         if (!_needSessionPersistence) { //nothing has been written \r
159                                                 if (_items == null)\r
160                                                         _items = new SessionStateItemCollection ();\r
161                                                 if (_staticObjects == null)\r
162                                                         _staticObjects = new HttpStaticObjectsCollection ();\r
163                                                 return;\r
164                                         }\r
165 \r
166                                         ObjectInputStream ms = new ObjectInputStream (input);\r
167                                         System.IO.BinaryReader br = new System.IO.BinaryReader (ms);\r
168                                         _items = SessionStateItemCollection.Deserialize (br);\r
169                                         _staticObjects = HttpStaticObjectsCollection.Deserialize (br);\r
170                                 }\r
171                         }\r
172 \r
173                         public void writeExternal (java.io.ObjectOutput output) {\r
174                                 lock (this) {\r
175                                         output.writeBoolean (_needSessionPersistence);\r
176                                         if (!_needSessionPersistence)\r
177                                                 //indicates that there is nothing to serialize for this object\r
178                                                 return;\r
179 \r
180                                         ObjectOutputStream ms = new ObjectOutputStream (output);\r
181                                         System.IO.BinaryWriter bw = new System.IO.BinaryWriter (ms);\r
182                                         _items.Serialize (bw);\r
183                                         _staticObjects.Serialize (bw);\r
184                                 }\r
185                         }\r
186 \r
187                         #endregion\r
188                 }\r
189         }\r
190 }\r