BaseHttpServlet.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. static String DataDirectory = String.Concat (System.IO.Path.DirectorySeparatorChar, "App_Data", System.IO.Path.DirectorySeparatorChar);
  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. //Set DataDirectory substitution string (http://blogs.msdn.com/dataaccess/archive/2005/10/28/486273.aspx)
  90. servletDomain.SetData ("DataDirectory", String.Concat (req.getContextPath (), DataDirectory));
  91. // Put to the TLS current AppDomain of the servlet, so anyone can use it.
  92. [email protected](servletDomain);
  93. //put request to the TLS
  94. Thread.SetData(_servletRequestSlot, req);
  95. //put response to the TLS
  96. Thread.SetData(_servletResponseSlot, resp);
  97. //put the servlet object to the TLS
  98. Thread.SetData(_servletSlot, this);
  99. resp.setHeader("X-Powered-By", "ASP.NET");
  100. resp.setHeader("X-AspNet-Version", "1.1.4322");
  101. //PageMapper.LoadFileList();
  102. resp.setContentType("text/html");
  103. HttpWorkerRequest gwr = new ServletWorkerRequest(this, req, resp);
  104. HttpRuntime.ProcessRequest(gwr);
  105. }
  106. finally
  107. {
  108. HttpContext.Current = null;
  109. Thread.SetData(_servletRequestSlot, null);
  110. Thread.SetData(_servletResponseSlot, null);
  111. Thread.SetData(_servletSlot, null);
  112. [email protected]();
  113. //cleaning
  114. //vmw.Utils.cleanTLS(); //clean up all TLS entries for current Thread.
  115. //java.lang.Thread.currentThread().setContextClassLoader(null);
  116. }
  117. }
  118. override public void destroy()
  119. {
  120. try
  121. {
  122. AppDomain servletDomain = (AppDomain)this.getServletContext().getAttribute(J2EEConsts.APP_DOMAIN);
  123. [email protected](servletDomain);
  124. #if DEBUG
  125. Console.WriteLine("Destroy of GhHttpServlet");
  126. #endif
  127. base.destroy();
  128. HttpRuntime.Close();
  129. [email protected](this);
  130. this.getServletContext().removeAttribute(J2EEConsts.APP_DOMAIN);
  131. java.lang.Thread.currentThread().setContextClassLoader(null);
  132. }
  133. catch(Exception e)
  134. {
  135. #if DEBUG
  136. Console.WriteLine("ERROR in Servlet Destroy {0},{1}",e.GetType(), e.Message);
  137. Console.WriteLine(e.StackTrace);
  138. #endif
  139. }
  140. finally
  141. {
  142. [email protected]();
  143. }
  144. }
  145. private AppDomain createServletDomain(ServletConfig config)
  146. {
  147. string rootPath = J2EEUtils.GetApplicationRealPath(config);
  148. AppDomainSetup domainSetup = new AppDomainSetup();
  149. string name = config.getServletName();//.getServletContextName();
  150. if (name == null)
  151. name = "GH Application";
  152. domainSetup.ApplicationName = name;
  153. domainSetup.ConfigurationFile = rootPath + "/Web.config";
  154. AppDomain servletDomain = AppDomain.CreateDomain(name, null, domainSetup);
  155. //servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config));
  156. //servletDomain.SetData(IAppDomainConfig.WEB_APP_DIR, rootPath);
  157. servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config));
  158. servletDomain.SetData(IAppDomainConfig.WEB_APP_DIR, rootPath);
  159. // The BaseDir is the full path to the physical dir of the app
  160. // and allows the application to modify files in the case of
  161. // open deployment.
  162. string webApp_baseDir = config.getServletContext().getRealPath("");
  163. if (webApp_baseDir == null || webApp_baseDir == "")
  164. webApp_baseDir = rootPath;
  165. servletDomain.SetData(IAppDomainConfig.APP_BASE_DIR , webApp_baseDir);
  166. #if DEBUG
  167. Console.WriteLine("Initialization of webapp " + webApp_baseDir);
  168. #endif
  169. // Mordechai : setting the web app deserializer object.
  170. servletDomain.SetData(J2EEConsts.DESERIALIZER_CONST , this.GetDeserializer());
  171. servletDomain.SetData([email protected]_DRIVER_UTILS_CONST, this.getDriverUtils());
  172. //servletDomain.SetData(".hostingVirtualPath", "/");
  173. //servletDomain.SetData(".hostingInstallDir", "/");
  174. return servletDomain;
  175. }
  176. virtual protected [email protected] GetDeserializer()
  177. {
  178. if (m_deseializer == null)
  179. m_deseializer = new GHWebDeseserializer();
  180. return m_deseializer;
  181. }
  182. protected [email protected] m_deseializer = null;
  183. /// Mordechai: This class comes to solve a problem in class deserialize
  184. /// within web application. The problem is that the classloader that created
  185. /// some user web class (for example aspx page) is not the class loader
  186. /// that de-serialize it - thus we end with ClassDefNotFoundException.
  187. /// To prevent this situation we delegate the serialization back the the
  188. /// web app (which has the correct class loader...)
  189. ///
  190. virtual protected [email protected] getDriverUtils()
  191. {
  192. //by default no driver utils, the specific servlet will override this method
  193. return null;
  194. }
  195. }
  196. public class GHWebDeseserializer : [email protected]
  197. {
  198. Object [email protected](java.io.ObjectInputStream stream)
  199. {
  200. object obj = stream.readObject();
  201. return obj;
  202. }
  203. }
  204. #if !USE_APPSERVER_THREAD
  205. public class PersonalServiceThread
  206. {
  207. public delegate void ServiceDelegate (HttpServletRequest req, HttpServletResponse resp);
  208. HttpServletRequest _req = null;
  209. HttpServletResponse _resp = null;
  210. Thread _worker = null;
  211. ServiceDelegate _service = null;
  212. public PersonalServiceThread (ServiceDelegate service, HttpServletRequest req, HttpServletResponse resp)
  213. {
  214. _service = service;
  215. _req = req;
  216. _resp = resp;
  217. _worker = new Thread (new ThreadStart (Run));
  218. }
  219. public void RunWait ()
  220. {
  221. _worker.Start ();
  222. _worker.Join ();
  223. }
  224. private void Run ()
  225. {
  226. _service(_req, _resp);
  227. }
  228. }
  229. #endif
  230. }
  231. namespace System.Web.GH
  232. {
  233. public class BaseHttpServlet : System.Web.J2EE.BaseHttpServlet
  234. {
  235. }
  236. }