BaseHttpServlet.cs 8.0 KB

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