BaseHttpServlet.cs 7.4 KB

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