remove dead code
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web.Hosting / BaseHttpServlet.cs
1 //
2 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
3 //
4
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25
26 using System;
27
28 using System.Configuration;
29 using System.Web;
30 using System.Web.Configuration;
31 using System.Threading;
32 using System.Web.Hosting;
33 using System.IO;
34
35 using javax.servlet;
36 using javax.servlet.http;
37 using vmw.common;
38 using java.util;
39 using vmw.@internal;
40 using java.lang.reflect;
41 using java.net;
42 using System.Globalization;
43
44 namespace Mainsoft.Web.Hosting
45 {
46         /// <summary>
47         /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
48         /// </summary>
49         public class BaseHttpServlet : HttpServlet
50         {
51                 //private AppDomain _servletDomain;
52                 //static readonly LocalDataStoreSlot _servletRequestSlot = Thread.GetNamedDataSlot(J2EEConsts.SERVLET_REQUEST);
53                 //static readonly LocalDataStoreSlot _servletResponseSlot = Thread.GetNamedDataSlot(J2EEConsts.SERVLET_RESPONSE);
54                 //static readonly LocalDataStoreSlot _servletSlot = Thread.GetNamedDataSlot(J2EEConsts.CURRENT_SERVLET);
55
56                 bool _appVirDirInited = false;
57
58                 public BaseHttpServlet()
59                 {
60                 }
61
62                 override public void init(ServletConfig config)
63                 {
64                         base.init(config);
65                         InitRuntime (config, this);
66                 }
67
68                 public static void InitRuntime (ServletConfig config, object evidence) {
69                         AppDomain servletDomain = createServletDomain (config);
70                         vmw.@internal.EnvironmentUtils.setAppDomain (servletDomain);
71
72                         try {
73                                 ServletContext context = config.getServletContext ();
74                                 //GH Infromation Initizalization
75                                 long currentTime = java.lang.System.currentTimeMillis ();
76                                 servletDomain.SetData (".domainId", currentTime.ToString ("x"));
77                                 currentTime = ~currentTime;
78                                 servletDomain.SetData (".appId", currentTime.ToString ("x"));
79                                 servletDomain.SetData (".appName", servletDomain.SetupInformation.ApplicationName);
80
81                                 servletDomain.SetData (J2EEConsts.CLASS_LOADER, java.lang.Thread.currentThread ().getContextClassLoader ());
82                                 //servletDomain.SetData (J2EEConsts.CLASS_LOADER, vmw.common.TypeUtils.ToClass (evidence).getClassLoader ());
83                                 //servletDomain.SetData(J2EEConsts.SERVLET_CONFIG, config);
84                                 servletDomain.SetData (J2EEConsts.RESOURCE_LOADER, new vmw.@internal.j2ee.ServletResourceLoader (context));
85
86                                 lock (evidence) {
87                                         if (context.getAttribute (J2EEConsts.APP_DOMAIN) == null)
88                                                 context.setAttribute (J2EEConsts.APP_DOMAIN, servletDomain);
89                                 }
90                                 //config.getServletContext ().setAttribute (J2EEConsts.CURRENT_SERVLET, this);
91                         }
92                         finally {
93                                 vmw.@internal.EnvironmentUtils.cleanTLS ();
94                                 vmw.@internal.EnvironmentUtils.clearAppDomain ();
95                         }
96                 }
97
98                 protected override void service (HttpServletRequest req, HttpServletResponse resp)
99                 {
100                         resp.setContentType ("text/html");
101
102                         try 
103                         {
104                                 // Very important - to update Virtual Path!!!
105                                 AppDomain servletDomain = (AppDomain)this.getServletContext().getAttribute(J2EEConsts.APP_DOMAIN);
106                                 if (!_appVirDirInited) {
107                                         servletDomain.SetData (IAppDomainConfig.APP_VIRT_DIR, req.getContextPath ());
108                                         servletDomain.SetData (".hostingVirtualPath", req.getContextPath ());
109                                         _appVirDirInited = true;
110                                 }
111
112                                 // Put to the TLS current AppDomain of the servlet, so anyone can use it.
113                                 vmw.@internal.EnvironmentUtils.setAppDomain(servletDomain);
114
115                                 // put request to the TLS
116                                 //Thread.SetData(_servletRequestSlot, req);
117                                 //// put response to the TLS
118                                 //Thread.SetData(_servletResponseSlot, resp);
119                                 //// put the servlet object to the TLS
120                                 //Thread.SetData(_servletSlot, this);
121
122                                 resp.setHeader("X-Powered-By", "ASP.NET");
123                                 resp.setHeader("X-AspNet-Version", "1.1.4322");
124
125                                 HttpWorkerRequest gwr = new ServletWorkerRequest (this, req, resp);
126                                 CultureInfo culture = (CultureInfo) vmw.@internal.EnvironmentUtils.getCultureInfoFromLocale (req.getLocale ());
127                                 Thread currentTread = Thread.CurrentThread;
128                                 currentTread.CurrentCulture = culture;
129                                 currentTread.CurrentUICulture = culture;
130                                 HttpRuntime.ProcessRequest(gwr);
131                         }
132                         finally 
133                         {
134                                 HttpContext.Current = null;
135                                 //Thread.SetData(_servletRequestSlot, null);
136                                 //Thread.SetData(_servletResponseSlot, null);
137                                 //Thread.SetData(_servletSlot, null);
138                                 vmw.@internal.EnvironmentUtils.clearAppDomain();
139                         }
140                 }
141
142                 override public void destroy()
143                 {
144                         base.destroy();
145                         DestroyRuntime (getServletContext (), this);
146                 }
147
148                 public static void DestroyRuntime (ServletContext context, object evidence) {
149                         AppDomain servletDomain = (AppDomain) context.getAttribute (J2EEConsts.APP_DOMAIN);
150                         if (servletDomain == null)
151                                 return;
152
153                         try {
154                                 vmw.@internal.EnvironmentUtils.setAppDomain (servletDomain);
155 #if DEBUG
156                                 Console.WriteLine ("Destroy of GhHttpServlet");
157 #endif
158                                 HttpRuntime.Close ();
159                                 vmw.@internal.EnvironmentUtils.cleanAllBeforeServletDestroy (evidence);
160                                 context.removeAttribute (J2EEConsts.APP_DOMAIN);
161                                 java.lang.Thread.currentThread ().setContextClassLoader (null);
162                         }
163                         catch (Exception e) {
164 #if DEBUG
165                                 Console.WriteLine ("ERROR in Servlet Destroy {0},{1}", e.GetType (), e.Message);
166                                 Console.WriteLine (e.StackTrace);
167 #endif
168                         }
169                         finally {
170                                 vmw.@internal.EnvironmentUtils.clearAppDomain ();
171                         }
172                 }
173
174                 private static AppDomain createServletDomain(ServletConfig config)
175                 {
176                                 string rootPath = J2EEUtils.GetApplicationRealPath(config.getServletContext ());
177                                 AppDomainSetup domainSetup = new AppDomainSetup();
178                                 string name = config.getServletName();//.getServletContextName();
179                                 if (name == null)
180                                         name = "GH Application";
181                                 domainSetup.ApplicationName = name;
182                                 domainSetup.ConfigurationFile = Path.Combine (rootPath, "Web.config");
183                                 domainSetup.PrivateBinPath = Path.Combine (rootPath, "WEB-INF/lib");
184
185                                 AppDomain servletDomain = AppDomain.CreateDomain(name, null, domainSetup);
186
187
188
189
190
191                                 //servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config));
192                                 //servletDomain.SetData(IAppDomainConfig.WEB_APP_DIR, rootPath);
193
194                                 servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config.getServletContext ()));
195                                 servletDomain.SetData(IAppDomainConfig.WEB_APP_DIR, rootPath);
196
197                                 //Set DataDirectory substitution string (http://blogs.msdn.com/dataaccess/archive/2005/10/28/486273.aspx)
198                                 string dataDirectory = config.getServletContext ().getInitParameter ("DataDirectory");
199                                 if (dataDirectory == null)
200                                         dataDirectory = "App_Data";
201
202                                 if (!Path.IsPathRooted (dataDirectory)) {
203                                         java.io.InputStream inputStream = config.getServletContext ().getResourceAsStream ("/WEB-INF/classes/appData.properties");
204                                         string root;
205                                         if (inputStream != null) {
206                                                 try {
207                                                         Properties props = new Properties ();
208                                                         props.load (inputStream);
209                                                         root = props.getProperty ("root.folder");
210                                                 }
211                                                 finally {
212                                                         inputStream.close ();
213                                                 }
214                                         }
215                                         else
216                                                 root = config.getServletContext ().getRealPath ("/");
217
218                                         if (root == null)
219                                                 root = String.Empty;
220
221                                         dataDirectory = Path.Combine (root, dataDirectory);
222                                 }
223
224                                 if (dataDirectory [dataDirectory.Length - 1] != Path.DirectorySeparatorChar)
225                                         dataDirectory += Path.DirectorySeparatorChar;
226
227                                 servletDomain.SetData ("DataDirectory", dataDirectory);
228
229                                 if (config.getServletContext ().getRealPath ("/") == null)
230                                         servletDomain.SetData(".appStartTime", DateTime.UtcNow);
231
232                                 // The BaseDir is the full path to the physical dir of the app
233                                 // and allows the application to modify files in the case of
234                                 // open deployment.
235                                 string webApp_baseDir = config.getServletContext().getRealPath("");
236                                 if (webApp_baseDir == null || webApp_baseDir == "")
237                                         webApp_baseDir = rootPath;
238                                 servletDomain.SetData(IAppDomainConfig.APP_BASE_DIR , webApp_baseDir);
239 #if DEBUG
240                                 Console.WriteLine("Initialization of webapp " + webApp_baseDir);
241 #endif
242                                 //servletDomain.SetData(".hostingVirtualPath", "/");
243                                 //servletDomain.SetData(".hostingInstallDir", "/");
244                                 return servletDomain;
245                 }
246         }
247 }
248
249 namespace System.Web.GH
250 {
251         /// <summary>
252         /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
253         /// </summary>
254         public class BaseHttpServlet : Mainsoft.Web.Hosting.BaseHttpServlet
255         {
256         }
257
258 }
259
260 namespace System.Web.J2EE
261 {
262         /// <summary>
263         /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
264         /// </summary>
265         public class BaseHttpServlet : Mainsoft.Web.Hosting.BaseHttpServlet
266         {
267         }
268
269 }
270
271 public class GhDynamicHttpServlet : System.Web.GH.BaseHttpServlet
272 {
273 }
274
275 public class GhStaticHttpServlet : System.Web.GH.BaseStaticHttpServlet
276
277 }
278
279 public class GhHttpServlet : System.Web.GH.BaseHttpServlet
280 {
281         GhStaticHttpServlet staticServlet;
282
283         public GhHttpServlet () {
284                 staticServlet = new GhStaticHttpServlet ();
285         }
286
287         override public void init (ServletConfig config) {
288                 base.init (config);
289                 staticServlet.init (config);
290         }
291
292         override protected void service (HttpServletRequest req, HttpServletResponse resp) {
293                 string pathInfo = req.getRequestURI ();
294                 string contextPath = req.getContextPath ();
295                 if (pathInfo.Equals (contextPath) ||
296                         ((pathInfo.Length - contextPath.Length) == 1) &&
297                         pathInfo [pathInfo.Length - 1] == '/' && pathInfo.StartsWith (contextPath))
298                         pathInfo = contextPath + req.getServletPath ();
299                 if (pathInfo.EndsWith (".aspx") ||
300                         pathInfo.EndsWith (".asmx") ||
301                         pathInfo.EndsWith (".invoke")) {
302                         base.service (req, resp);
303                 }
304                 else {
305                         staticServlet.service (req, resp);
306                 }
307         }
308
309         override public void destroy () {
310                 staticServlet.destroy ();
311                 base.destroy ();
312         }
313 }