BaseHttpServlet.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 javax.servlet;
  31. using javax.servlet.http;
  32. using vmw.common;
  33. namespace System.Web.J2EE
  34. {
  35. public class BaseHttpServlet : HttpServlet
  36. {
  37. //private AppDomain _servletDomain;
  38. static LocalDataStoreSlot _servletRequestSlot = Thread.GetNamedDataSlot(J2EEConsts.SERVLET_REQUEST);
  39. static LocalDataStoreSlot _servletResponseSlot = Thread.GetNamedDataSlot(J2EEConsts.SERVLET_RESPONSE);
  40. static LocalDataStoreSlot _servletSlot = Thread.GetNamedDataSlot(J2EEConsts.CURRENT_SERVLET);
  41. public BaseHttpServlet()
  42. {
  43. }
  44. override public void init(ServletConfig config)
  45. {
  46. base.init(config);
  47. InitServlet(config);
  48. }
  49. protected virtual void InitServlet(ServletConfig config)
  50. {
  51. try
  52. {
  53. AppDomain servletDomain = createServletDomain(config);
  54. [email protected](servletDomain);
  55. //GH Infromation Initizalization
  56. int nowInt = DateTime.Now.ToString().GetHashCode();
  57. servletDomain.SetData(".domainId", nowInt.ToString("x"));
  58. nowInt += "/".GetHashCode ();
  59. servletDomain.SetData(".appId", nowInt.ToString("x"));
  60. servletDomain.SetData(".appName", nowInt.ToString("x"));
  61. servletDomain.SetData(J2EEConsts.CLASS_LOADER, vmw.common.TypeUtils.ToClass(this).getClassLoader());
  62. servletDomain.SetData(J2EEConsts.SERVLET_CONFIG, config);
  63. servletDomain.SetData(J2EEConsts.RESOURCE_LOADER, new [email protected](config.getServletContext()));
  64. config.getServletContext().setAttribute(J2EEConsts.APP_DOMAIN, servletDomain);
  65. }
  66. finally
  67. {
  68. [email protected]();
  69. [email protected]();
  70. }
  71. }
  72. override protected void service (HttpServletRequest req, HttpServletResponse resp)
  73. {
  74. #if !USE_APPSERVER_THREAD
  75. // temporary workaround
  76. PersonalServiceThread pt = new PersonalServiceThread (new PersonalServiceThread.ServiceDelegate (service2), req, resp);
  77. pt.RunWait ();
  78. }
  79. protected void service2(HttpServletRequest req, HttpServletResponse resp)
  80. {
  81. #endif
  82. try
  83. {
  84. // Very important - to update Virtual Path!!!
  85. AppDomain servletDomain = (AppDomain)this.getServletContext().getAttribute(J2EEConsts.APP_DOMAIN);
  86. servletDomain.SetData(IAppDomainConfig.APP_VIRT_DIR, req.getContextPath());
  87. servletDomain.SetData(".hostingVirtualPath", req.getContextPath());
  88. // Put to the TLS current AppDomain of the servlet, so anyone can use it.
  89. [email protected](servletDomain);
  90. //put request to the TLS
  91. Thread.SetData(_servletRequestSlot, req);
  92. //put response to the TLS
  93. Thread.SetData(_servletResponseSlot, resp);
  94. //put the servlet object to the TLS
  95. Thread.SetData(_servletSlot, this);
  96. resp.setHeader("X-Powered-By", "ASP.NET");
  97. resp.setHeader("X-AspNet-Version", "1.1.4322");
  98. //PageMapper.LoadFileList();
  99. resp.setContentType("text/html");
  100. HttpWorkerRequest gwr = new ServletWorkerRequest(this, req, resp);
  101. HttpRuntime.ProcessRequest(gwr);
  102. }
  103. finally
  104. {
  105. HttpContext.Current = null;
  106. Thread.SetData(_servletRequestSlot, null);
  107. Thread.SetData(_servletResponseSlot, null);
  108. Thread.SetData(_servletSlot, null);
  109. [email protected]();
  110. //cleaning
  111. //vmw.Utils.cleanTLS(); //clean up all TLS entries for current Thread.
  112. //java.lang.Thread.currentThread().setContextClassLoader(null);
  113. }
  114. }
  115. override public void destroy()
  116. {
  117. try
  118. {
  119. AppDomain servletDomain = (AppDomain)this.getServletContext().getAttribute(J2EEConsts.APP_DOMAIN);
  120. [email protected](servletDomain);
  121. #if DEBUG
  122. Console.WriteLine("Destroy of GhHttpServlet");
  123. #endif
  124. base.destroy();
  125. HttpRuntime.Close();
  126. [email protected](this);
  127. this.getServletContext().removeAttribute(J2EEConsts.APP_DOMAIN);
  128. java.lang.Thread.currentThread().setContextClassLoader(null);
  129. }
  130. catch(Exception e)
  131. {
  132. #if DEBUG
  133. Console.WriteLine("ERROR in Servlet Destroy {0},{1}",e.GetType(), e.Message);
  134. Console.WriteLine(e.StackTrace);
  135. #endif
  136. }
  137. finally
  138. {
  139. [email protected]();
  140. }
  141. }
  142. private AppDomain createServletDomain(ServletConfig config)
  143. {
  144. string rootPath = J2EEUtils.GetApplicationRealPath(config);
  145. AppDomainSetup domainSetup = new AppDomainSetup();
  146. string name = config.getServletName();//.getServletContextName();
  147. if (name == null)
  148. name = "GH Application";
  149. domainSetup.ApplicationName = name;
  150. domainSetup.ConfigurationFile = rootPath + "/Web.config";
  151. AppDomain servletDomain = AppDomain.CreateDomain(name, null, domainSetup);
  152. //servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config));
  153. //servletDomain.SetData(IAppDomainConfig.WEB_APP_DIR, rootPath);
  154. servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config));
  155. servletDomain.SetData(IAppDomainConfig.WEB_APP_DIR, rootPath);
  156. // The BaseDir is the full path to the physical dir of the app
  157. // and allows the application to modify files in the case of
  158. // open deployment.
  159. string webApp_baseDir = config.getServletContext().getRealPath("");
  160. if (webApp_baseDir == null || webApp_baseDir == "")
  161. webApp_baseDir = rootPath;
  162. servletDomain.SetData(IAppDomainConfig.APP_BASE_DIR , webApp_baseDir);
  163. #if DEBUG
  164. Console.WriteLine("Initialization of webapp " + webApp_baseDir);
  165. #endif
  166. // Mordechai : setting the web app deserializer object.
  167. servletDomain.SetData(J2EEConsts.DESERIALIZER_CONST , this.GetDeserializer());
  168. servletDomain.SetData([email protected]_DRIVER_UTILS_CONST, this.getDriverUtils());
  169. //servletDomain.SetData(".hostingVirtualPath", "/");
  170. //servletDomain.SetData(".hostingInstallDir", "/");
  171. return servletDomain;
  172. }
  173. virtual protected [email protected] GetDeserializer()
  174. {
  175. if (m_deseializer == null)
  176. m_deseializer = new GHWebDeseserializer();
  177. return m_deseializer;
  178. }
  179. protected [email protected] m_deseializer = null;
  180. /// Mordechai: This class comes to solve a problem in class deserialize
  181. /// within web application. The problem is that the classloader that created
  182. /// some user web class (for example aspx page) is not the class loader
  183. /// that de-serialize it - thus we end with ClassDefNotFoundException.
  184. /// To prevent this situation we delegate the serialization back the the
  185. /// web app (which has the correct class loader...)
  186. ///
  187. virtual protected [email protected] getDriverUtils()
  188. {
  189. //by default no driver utils, the specific servlet will override this method
  190. return null;
  191. }
  192. }
  193. public class GHWebDeseserializer : [email protected]
  194. {
  195. Object [email protected](java.io.ObjectInputStream stream)
  196. {
  197. object obj = stream.readObject();
  198. return obj;
  199. }
  200. }
  201. #if !USE_APPSERVER_THREAD
  202. public class PersonalServiceThread
  203. {
  204. public delegate void ServiceDelegate (HttpServletRequest req, HttpServletResponse resp);
  205. HttpServletRequest _req = null;
  206. HttpServletResponse _resp = null;
  207. Thread _worker = null;
  208. ServiceDelegate _service = null;
  209. public PersonalServiceThread (ServiceDelegate service, HttpServletRequest req, HttpServletResponse resp)
  210. {
  211. _service = service;
  212. _req = req;
  213. _resp = resp;
  214. _worker = new Thread (new ThreadStart (Run));
  215. }
  216. public void RunWait ()
  217. {
  218. _worker.Start ();
  219. _worker.Join ();
  220. }
  221. private void Run ()
  222. {
  223. _service(_req, _resp);
  224. }
  225. }
  226. #endif
  227. }
  228. namespace System.Web.GH
  229. {
  230. public class BaseHttpServlet : System.Web.J2EE.BaseHttpServlet
  231. {
  232. }
  233. }