BaseHttpServlet.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. //
  2. // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
  3. //
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. //
  24. #define USE_APPSERVER_THREAD
  25. using System;
  26. using System.Configuration;
  27. using System.Web.Configuration;
  28. using System.Threading;
  29. using System.Web.Hosting;
  30. using System.IO;
  31. using javax.servlet;
  32. using javax.servlet.http;
  33. using vmw.common;
  34. namespace System.Web.J2EE
  35. {
  36. public class BaseHttpServlet : HttpServlet
  37. {
  38. //private AppDomain _servletDomain;
  39. static LocalDataStoreSlot _servletRequestSlot = Thread.GetNamedDataSlot(J2EEConsts.SERVLET_REQUEST);
  40. static LocalDataStoreSlot _servletResponseSlot = Thread.GetNamedDataSlot(J2EEConsts.SERVLET_RESPONSE);
  41. static LocalDataStoreSlot _servletSlot = Thread.GetNamedDataSlot(J2EEConsts.CURRENT_SERVLET);
  42. public BaseHttpServlet()
  43. {
  44. }
  45. override public void init(ServletConfig config)
  46. {
  47. base.init(config);
  48. InitServlet(config);
  49. }
  50. protected virtual void InitServlet(ServletConfig config)
  51. {
  52. try
  53. {
  54. AppDomain servletDomain = createServletDomain(config);
  55. [email protected](servletDomain);
  56. //GH Infromation Initizalization
  57. int nowInt = DateTime.Now.ToString().GetHashCode();
  58. servletDomain.SetData(".domainId", nowInt.ToString("x"));
  59. nowInt += "/".GetHashCode ();
  60. servletDomain.SetData(".appId", nowInt.ToString("x"));
  61. servletDomain.SetData(".appName", nowInt.ToString("x"));
  62. servletDomain.SetData(J2EEConsts.CLASS_LOADER, vmw.common.TypeUtils.ToClass(this).getClassLoader());
  63. servletDomain.SetData(J2EEConsts.SERVLET_CONFIG, config);
  64. servletDomain.SetData(J2EEConsts.RESOURCE_LOADER, new [email protected](config.getServletContext()));
  65. config.getServletContext().setAttribute(J2EEConsts.APP_DOMAIN, servletDomain);
  66. }
  67. finally
  68. {
  69. [email protected]();
  70. [email protected]();
  71. }
  72. }
  73. override protected void service (HttpServletRequest req, HttpServletResponse resp)
  74. {
  75. #if !USE_APPSERVER_THREAD
  76. // temporary workaround
  77. PersonalServiceThread pt = new PersonalServiceThread (new PersonalServiceThread.ServiceDelegate (service2), req, resp);
  78. pt.RunWait ();
  79. }
  80. protected void service2(HttpServletRequest req, HttpServletResponse resp)
  81. {
  82. #endif
  83. try
  84. {
  85. // Very important - to update Virtual Path!!!
  86. AppDomain servletDomain = (AppDomain)this.getServletContext().getAttribute(J2EEConsts.APP_DOMAIN);
  87. servletDomain.SetData(IAppDomainConfig.APP_VIRT_DIR, req.getContextPath());
  88. servletDomain.SetData(".hostingVirtualPath", req.getContextPath());
  89. // Put to the TLS current AppDomain of the servlet, so anyone can use it.
  90. [email protected](servletDomain);
  91. //put request to the TLS
  92. Thread.SetData(_servletRequestSlot, req);
  93. //put response to the TLS
  94. Thread.SetData(_servletResponseSlot, resp);
  95. //put the servlet object to the TLS
  96. Thread.SetData(_servletSlot, this);
  97. resp.setHeader("X-Powered-By", "ASP.NET");
  98. resp.setHeader("X-AspNet-Version", "1.1.4322");
  99. //PageMapper.LoadFileList();
  100. resp.setContentType("text/html");
  101. HttpWorkerRequest gwr = new ServletWorkerRequest(this, req, resp);
  102. HttpRuntime.ProcessRequest(gwr);
  103. }
  104. finally
  105. {
  106. HttpContext.Current = null;
  107. Thread.SetData(_servletRequestSlot, null);
  108. Thread.SetData(_servletResponseSlot, null);
  109. Thread.SetData(_servletSlot, null);
  110. [email protected]();
  111. //cleaning
  112. //vmw.Utils.cleanTLS(); //clean up all TLS entries for current Thread.
  113. //java.lang.Thread.currentThread().setContextClassLoader(null);
  114. }
  115. }
  116. override public void destroy()
  117. {
  118. try
  119. {
  120. AppDomain servletDomain = (AppDomain)this.getServletContext().getAttribute(J2EEConsts.APP_DOMAIN);
  121. [email protected](servletDomain);
  122. #if DEBUG
  123. Console.WriteLine("Destroy of GhHttpServlet");
  124. #endif
  125. base.destroy();
  126. HttpRuntime.Close();
  127. [email protected](this);
  128. this.getServletContext().removeAttribute(J2EEConsts.APP_DOMAIN);
  129. java.lang.Thread.currentThread().setContextClassLoader(null);
  130. }
  131. catch(Exception e)
  132. {
  133. #if DEBUG
  134. Console.WriteLine("ERROR in Servlet Destroy {0},{1}",e.GetType(), e.Message);
  135. Console.WriteLine(e.StackTrace);
  136. #endif
  137. }
  138. finally
  139. {
  140. [email protected]();
  141. }
  142. }
  143. private AppDomain createServletDomain(ServletConfig config)
  144. {
  145. string rootPath = J2EEUtils.GetApplicationRealPath(config);
  146. AppDomainSetup domainSetup = new AppDomainSetup();
  147. string name = config.getServletName();//.getServletContextName();
  148. if (name == null)
  149. name = "GH Application";
  150. domainSetup.ApplicationName = name;
  151. domainSetup.ConfigurationFile = rootPath + "/Web.config";
  152. AppDomain servletDomain = AppDomain.CreateDomain(name, null, domainSetup);
  153. //servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config));
  154. //servletDomain.SetData(IAppDomainConfig.WEB_APP_DIR, rootPath);
  155. servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config));
  156. servletDomain.SetData(IAppDomainConfig.WEB_APP_DIR, rootPath);
  157. //Set DataDirectory substitution string (http://blogs.msdn.com/dataaccess/archive/2005/10/28/486273.aspx)
  158. string realPath = config.getServletContext ().getRealPath ("/");
  159. if (realPath == null)
  160. realPath = String.Empty;
  161. string dataDirectory = Path.Combine (realPath, "App_Data");
  162. dataDirectory.Replace ('\\', '/');
  163. if (dataDirectory [dataDirectory.Length - 1] != '/')
  164. dataDirectory += "/";
  165. servletDomain.SetData ("DataDirectory", dataDirectory);
  166. // The BaseDir is the full path to the physical dir of the app
  167. // and allows the application to modify files in the case of
  168. // open deployment.
  169. string webApp_baseDir = config.getServletContext().getRealPath("");
  170. if (webApp_baseDir == null || webApp_baseDir == "")
  171. webApp_baseDir = rootPath;
  172. servletDomain.SetData(IAppDomainConfig.APP_BASE_DIR , webApp_baseDir);
  173. #if DEBUG
  174. Console.WriteLine("Initialization of webapp " + webApp_baseDir);
  175. #endif
  176. // Mordechai : setting the web app deserializer object.
  177. servletDomain.SetData(J2EEConsts.DESERIALIZER_CONST , this.GetDeserializer());
  178. servletDomain.SetData([email protected]_DRIVER_UTILS_CONST, this.getDriverUtils());
  179. //servletDomain.SetData(".hostingVirtualPath", "/");
  180. //servletDomain.SetData(".hostingInstallDir", "/");
  181. return servletDomain;
  182. }
  183. virtual protected [email protected] GetDeserializer()
  184. {
  185. if (m_deseializer == null)
  186. m_deseializer = new GHWebDeseserializer();
  187. return m_deseializer;
  188. }
  189. protected [email protected] m_deseializer = null;
  190. /// Mordechai: This class comes to solve a problem in class deserialize
  191. /// within web application. The problem is that the classloader that created
  192. /// some user web class (for example aspx page) is not the class loader
  193. /// that de-serialize it - thus we end with ClassDefNotFoundException.
  194. /// To prevent this situation we delegate the serialization back the the
  195. /// web app (which has the correct class loader...)
  196. ///
  197. virtual protected [email protected] getDriverUtils()
  198. {
  199. //by default no driver utils, the specific servlet will override this method
  200. return null;
  201. }
  202. }
  203. public class GHWebDeseserializer : [email protected]
  204. {
  205. Object [email protected](java.io.ObjectInputStream stream)
  206. {
  207. object obj = stream.readObject();
  208. return obj;
  209. }
  210. }
  211. #if !USE_APPSERVER_THREAD
  212. public class PersonalServiceThread
  213. {
  214. public delegate void ServiceDelegate (HttpServletRequest req, HttpServletResponse resp);
  215. HttpServletRequest _req = null;
  216. HttpServletResponse _resp = null;
  217. Thread _worker = null;
  218. ServiceDelegate _service = null;
  219. public PersonalServiceThread (ServiceDelegate service, HttpServletRequest req, HttpServletResponse resp)
  220. {
  221. _service = service;
  222. _req = req;
  223. _resp = resp;
  224. _worker = new Thread (new ThreadStart (Run));
  225. }
  226. public void RunWait ()
  227. {
  228. _worker.Start ();
  229. _worker.Join ();
  230. }
  231. private void Run ()
  232. {
  233. _service(_req, _resp);
  234. }
  235. }
  236. #endif
  237. }
  238. namespace System.Web.GH
  239. {
  240. public class BaseHttpServlet : System.Web.J2EE.BaseHttpServlet
  241. {
  242. }
  243. }