Merge branch 'master' of git://github.com/mono/mono
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web / BaseHttpContext.cs
1 //\r
2 // (C) 2007 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 \r
30 using System.Text;\r
31 using System.Configuration;\r
32 using System.Web;\r
33 using Mainsoft.Web.Hosting;\r
34 using javax.faces.lifecycle;\r
35 \r
36 namespace Mainsoft.Web\r
37 {\r
38         /// <summary>\r
39         /// BaseHttpContext contains all of the per-request state information related to the processing of a single request, \r
40         /// and the rendering of the corresponding response.\r
41         /// </summary>\r
42         public abstract class BaseHttpContext\r
43         {\r
44                 protected readonly HttpContext _context;\r
45                 static readonly object _contextKey = new object ();\r
46 \r
47                 protected BaseHttpContext (HttpContext context) {\r
48                         _context = context;\r
49                         context.Items [_contextKey] = this;\r
50                 }\r
51 \r
52                 /// <summary>\r
53                 /// Gets the Mainsoft.Web.BaseHttpContext object for the current request.\r
54                 /// </summary>\r
55                 /// <param name="context"></param>\r
56                 /// <returns></returns>\r
57                 public static BaseHttpContext GetCurrent(HttpContext context) {\r
58                         if (context == null)\r
59                                 throw new ArgumentNullException ("context");\r
60 \r
61                         BaseHttpContext baseContext = (BaseHttpContext) context.Items [_contextKey];\r
62                         return baseContext ?? GetWorker (context).CreateContext (context);\r
63                 }\r
64 \r
65                 /// <summary>\r
66                 /// Returns the javax.faces.lifecycle.Lifecycle instance.\r
67                 /// </summary>\r
68                 public Lifecycle Lifecycle {\r
69                         get { return BaseHttpServlet.Lifecycle; }\r
70                 }\r
71 \r
72                 /// <summary>\r
73                 /// Returns the Mainsoft.Web.Hosting.BaseWorkerRequest object for the current request.\r
74                 /// </summary>\r
75                 protected BaseWorkerRequest Worker {\r
76                         get { return GetWorker (_context); }\r
77                 }\r
78 \r
79                 static BaseWorkerRequest GetWorker (HttpContext context) {\r
80                         return (BaseWorkerRequest) ((IServiceProvider) context).GetService (typeof (HttpWorkerRequest));\r
81                 }\r
82         }\r
83 }\r