BaseHttpServlet.cs 10.0 KB

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