2 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Configuration;
30 using System.Web.Configuration;
31 using System.Threading;
32 using System.Web.Hosting;
36 using javax.servlet.http;
40 using java.lang.reflect;
42 using System.Globalization;
44 namespace Mainsoft.Web.Hosting
47 /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
49 public class BaseHttpServlet : HttpServlet
51 //private AppDomain _servletDomain;
52 //static readonly LocalDataStoreSlot _servletRequestSlot = Thread.GetNamedDataSlot(J2EEConsts.SERVLET_REQUEST);
53 //static readonly LocalDataStoreSlot _servletResponseSlot = Thread.GetNamedDataSlot(J2EEConsts.SERVLET_RESPONSE);
54 //static readonly LocalDataStoreSlot _servletSlot = Thread.GetNamedDataSlot(J2EEConsts.CURRENT_SERVLET);
56 bool _appVirDirInited = false;
58 public BaseHttpServlet()
62 override public void init(ServletConfig config)
65 InitRuntime (config, this);
68 public static void InitRuntime (ServletConfig config, object evidence) {
69 AppDomain servletDomain = createServletDomain (config);
70 vmw.@internal.EnvironmentUtils.setAppDomain (servletDomain);
73 ServletContext context = config.getServletContext ();
74 //GH Infromation Initizalization
75 long currentTime = java.lang.System.currentTimeMillis ();
76 servletDomain.SetData (".domainId", currentTime.ToString ("x"));
77 currentTime = ~currentTime;
78 servletDomain.SetData (".appId", currentTime.ToString ("x"));
79 servletDomain.SetData (".appName", servletDomain.SetupInformation.ApplicationName);
81 servletDomain.SetData (J2EEConsts.CLASS_LOADER, java.lang.Thread.currentThread ().getContextClassLoader ());
82 //servletDomain.SetData (J2EEConsts.CLASS_LOADER, vmw.common.TypeUtils.ToClass (evidence).getClassLoader ());
83 //servletDomain.SetData(J2EEConsts.SERVLET_CONFIG, config);
84 servletDomain.SetData (J2EEConsts.RESOURCE_LOADER, new vmw.@internal.j2ee.ServletResourceLoader (context));
87 if (context.getAttribute (J2EEConsts.APP_DOMAIN) == null)
88 context.setAttribute (J2EEConsts.APP_DOMAIN, servletDomain);
90 //config.getServletContext ().setAttribute (J2EEConsts.CURRENT_SERVLET, this);
93 vmw.@internal.EnvironmentUtils.cleanTLS ();
94 vmw.@internal.EnvironmentUtils.clearAppDomain ();
98 protected override void service (HttpServletRequest req, HttpServletResponse resp)
100 resp.setContentType ("text/html");
104 // Very important - to update Virtual Path!!!
105 AppDomain servletDomain = (AppDomain)this.getServletContext().getAttribute(J2EEConsts.APP_DOMAIN);
106 if (!_appVirDirInited) {
107 servletDomain.SetData (IAppDomainConfig.APP_VIRT_DIR, req.getContextPath ());
108 servletDomain.SetData (".hostingVirtualPath", req.getContextPath ());
109 _appVirDirInited = true;
112 // Put to the TLS current AppDomain of the servlet, so anyone can use it.
113 vmw.@internal.EnvironmentUtils.setAppDomain(servletDomain);
115 // put request to the TLS
116 //Thread.SetData(_servletRequestSlot, req);
117 //// put response to the TLS
118 //Thread.SetData(_servletResponseSlot, resp);
119 //// put the servlet object to the TLS
120 //Thread.SetData(_servletSlot, this);
122 resp.setHeader("X-Powered-By", "ASP.NET");
123 resp.setHeader("X-AspNet-Version", "1.1.4322");
125 HttpWorkerRequest gwr = new ServletWorkerRequest (this, req, resp);
126 CultureInfo culture = (CultureInfo) vmw.@internal.EnvironmentUtils.getCultureInfoFromLocale (req.getLocale ());
127 Thread currentTread = Thread.CurrentThread;
128 currentTread.CurrentCulture = culture;
129 currentTread.CurrentUICulture = culture;
130 HttpRuntime.ProcessRequest(gwr);
134 HttpContext.Current = null;
135 //Thread.SetData(_servletRequestSlot, null);
136 //Thread.SetData(_servletResponseSlot, null);
137 //Thread.SetData(_servletSlot, null);
138 vmw.@internal.EnvironmentUtils.clearAppDomain();
142 override public void destroy()
145 DestroyRuntime (getServletContext (), this);
148 public static void DestroyRuntime (ServletContext context, object evidence) {
149 AppDomain servletDomain = (AppDomain) context.getAttribute (J2EEConsts.APP_DOMAIN);
150 if (servletDomain == null)
154 vmw.@internal.EnvironmentUtils.setAppDomain (servletDomain);
156 Console.WriteLine ("Destroy of GhHttpServlet");
158 HttpRuntime.Close ();
159 vmw.@internal.EnvironmentUtils.cleanAllBeforeServletDestroy (evidence);
160 context.removeAttribute (J2EEConsts.APP_DOMAIN);
161 java.lang.Thread.currentThread ().setContextClassLoader (null);
163 catch (Exception e) {
165 Console.WriteLine ("ERROR in Servlet Destroy {0},{1}", e.GetType (), e.Message);
166 Console.WriteLine (e.StackTrace);
170 vmw.@internal.EnvironmentUtils.clearAppDomain ();
174 private static AppDomain createServletDomain(ServletConfig config)
176 string rootPath = J2EEUtils.GetApplicationRealPath(config.getServletContext ());
177 AppDomainSetup domainSetup = new AppDomainSetup();
178 string name = config.getServletName();//.getServletContextName();
180 name = "GH Application";
181 domainSetup.ApplicationName = name;
182 domainSetup.ConfigurationFile = Path.Combine (rootPath, "Web.config");
183 domainSetup.PrivateBinPath = Path.Combine (rootPath, "WEB-INF/lib");
185 AppDomain servletDomain = AppDomain.CreateDomain(name, null, domainSetup);
191 //servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config));
192 //servletDomain.SetData(IAppDomainConfig.WEB_APP_DIR, rootPath);
194 servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config.getServletContext ()));
195 servletDomain.SetData(IAppDomainConfig.WEB_APP_DIR, rootPath);
197 //Set DataDirectory substitution string (http://blogs.msdn.com/dataaccess/archive/2005/10/28/486273.aspx)
198 string dataDirectory = config.getServletContext ().getInitParameter ("DataDirectory");
199 if (dataDirectory == null)
200 dataDirectory = "App_Data";
202 if (!Path.IsPathRooted (dataDirectory)) {
203 java.io.InputStream inputStream = config.getServletContext ().getResourceAsStream ("/WEB-INF/classes/appData.properties");
205 if (inputStream != null) {
207 Properties props = new Properties ();
208 props.load (inputStream);
209 root = props.getProperty ("root.folder");
212 inputStream.close ();
216 root = config.getServletContext ().getRealPath ("/");
221 dataDirectory = Path.Combine (root, dataDirectory);
224 if (dataDirectory [dataDirectory.Length - 1] != Path.DirectorySeparatorChar)
225 dataDirectory += Path.DirectorySeparatorChar;
227 servletDomain.SetData ("DataDirectory", dataDirectory);
229 if (config.getServletContext ().getRealPath ("/") == null)
230 servletDomain.SetData(".appStartTime", DateTime.UtcNow);
232 // The BaseDir is the full path to the physical dir of the app
233 // and allows the application to modify files in the case of
235 string webApp_baseDir = config.getServletContext().getRealPath("");
236 if (webApp_baseDir == null || webApp_baseDir == "")
237 webApp_baseDir = rootPath;
238 servletDomain.SetData(IAppDomainConfig.APP_BASE_DIR , webApp_baseDir);
240 Console.WriteLine("Initialization of webapp " + webApp_baseDir);
242 //servletDomain.SetData(".hostingVirtualPath", "/");
243 //servletDomain.SetData(".hostingInstallDir", "/");
244 return servletDomain;
249 namespace System.Web.GH
252 /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
254 public class BaseHttpServlet : Mainsoft.Web.Hosting.BaseHttpServlet
260 namespace System.Web.J2EE
263 /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
265 public class BaseHttpServlet : Mainsoft.Web.Hosting.BaseHttpServlet
271 public class GhDynamicHttpServlet : System.Web.GH.BaseHttpServlet
275 public class GhStaticHttpServlet : System.Web.GH.BaseStaticHttpServlet
279 public class GhHttpServlet : System.Web.GH.BaseHttpServlet
281 GhStaticHttpServlet staticServlet;
283 public GhHttpServlet () {
284 staticServlet = new GhStaticHttpServlet ();
287 override public void init (ServletConfig config) {
289 staticServlet.init (config);
292 override protected void service (HttpServletRequest req, HttpServletResponse resp) {
293 string pathInfo = req.getRequestURI ();
294 string contextPath = req.getContextPath ();
295 if (pathInfo.Equals (contextPath) ||
296 ((pathInfo.Length - contextPath.Length) == 1) &&
297 pathInfo [pathInfo.Length - 1] == '/' && pathInfo.StartsWith (contextPath))
298 pathInfo = contextPath + req.getServletPath ();
299 if (pathInfo.EndsWith (".aspx") ||
300 pathInfo.EndsWith (".asmx") ||
301 pathInfo.EndsWith (".invoke")) {
302 base.service (req, resp);
305 staticServlet.service (req, resp);
309 override public void destroy () {
310 staticServlet.destroy ();