used Trace/Debug.WriteLine instead Console.
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web.Hosting / BaseHttpServlet.cs
index 440df05fc2d776a31d8ed3947b59561158658e53..f52bc847f776d0478108a720d69169c6ff7ab01d 100644 (file)
@@ -36,17 +36,25 @@ using javax.servlet;
 using javax.servlet.http;
 using vmw.common;
 using java.util;
+using vmw.@internal;
+using java.lang.reflect;
+using java.net;
+using System.Globalization;
+using System.Diagnostics;
 
 namespace Mainsoft.Web.Hosting
 {
+       /// <summary>
+       /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
+       /// </summary>
        public class BaseHttpServlet : HttpServlet
        {
                //private AppDomain _servletDomain;
-               static readonly LocalDataStoreSlot _servletRequestSlot = Thread.GetNamedDataSlot(J2EEConsts.SERVLET_REQUEST);
-               static readonly LocalDataStoreSlot _servletResponseSlot = Thread.GetNamedDataSlot(J2EEConsts.SERVLET_RESPONSE);
-               static readonly LocalDataStoreSlot _servletSlot = Thread.GetNamedDataSlot(J2EEConsts.CURRENT_SERVLET);
+               //static readonly LocalDataStoreSlot _servletRequestSlot = Thread.GetNamedDataSlot(J2EEConsts.SERVLET_REQUEST);
+               //static readonly LocalDataStoreSlot _servletResponseSlot = Thread.GetNamedDataSlot(J2EEConsts.SERVLET_RESPONSE);
+               //static readonly LocalDataStoreSlot _servletSlot = Thread.GetNamedDataSlot(J2EEConsts.CURRENT_SERVLET);
 
-               bool _performedInit = false;
+               bool _appVirDirInited = false;
 
                public BaseHttpServlet()
                {
@@ -55,78 +63,79 @@ namespace Mainsoft.Web.Hosting
                override public void init(ServletConfig config)
                {
                        base.init(config);
-                       InitServlet(config);
-                       
+                       InitRuntime (config, this);
                }
 
-               protected virtual void InitServlet(ServletConfig config)
-               {
-                       if (config.getServletContext().getAttribute(J2EEConsts.APP_DOMAIN) != null)
-                               return;
-
-                       try 
-                       {
-                               AppDomain servletDomain = createServletDomain(config);
-                               vmw.@internal.EnvironmentUtils.setAppDomain(servletDomain);
+               public static void InitRuntime (ServletConfig config, object evidence) {
+                       AppDomain servletDomain = createServletDomain (config);
+                       vmw.@internal.EnvironmentUtils.setAppDomain (servletDomain);
 
+                       try {
+                               ServletContext context = config.getServletContext ();
                                //GH Infromation Initizalization
-                               int nowInt = DateTime.Now.ToString().GetHashCode();
-                               servletDomain.SetData(".domainId", nowInt.ToString("x"));
-                               nowInt += "/".GetHashCode ();
-                               servletDomain.SetData(".appId", nowInt.ToString("x"));
-                               servletDomain.SetData(".appName", nowInt.ToString("x"));
-
-                               servletDomain.SetData(J2EEConsts.CLASS_LOADER, vmw.common.TypeUtils.ToClass(this).getClassLoader());
-                               servletDomain.SetData(J2EEConsts.SERVLET_CONFIG, config);
-                               servletDomain.SetData(J2EEConsts.RESOURCE_LOADER, new vmw.@internal.j2ee.ServletResourceLoader(config.getServletContext()));
-
-                               config.getServletContext().setAttribute(J2EEConsts.APP_DOMAIN, servletDomain);
-                               _performedInit = true;
+                               long currentTime = java.lang.System.currentTimeMillis ();
+                               servletDomain.SetData (".domainId", currentTime.ToString ("x"));
+                               currentTime = ~currentTime;
+                               servletDomain.SetData (".appId", currentTime.ToString ("x"));
+                               servletDomain.SetData (".appName", servletDomain.SetupInformation.ApplicationName);
+
+                               servletDomain.SetData (J2EEConsts.CLASS_LOADER, java.lang.Thread.currentThread ().getContextClassLoader ());
+                               //servletDomain.SetData (J2EEConsts.CLASS_LOADER, vmw.common.TypeUtils.ToClass (evidence).getClassLoader ());
+                               //servletDomain.SetData(J2EEConsts.SERVLET_CONFIG, config);
+                               servletDomain.SetData (J2EEConsts.RESOURCE_LOADER, new vmw.@internal.j2ee.ServletResourceLoader (context));
+
+                               lock (evidence) {
+                                       if (context.getAttribute (J2EEConsts.APP_DOMAIN) == null)
+                                               context.setAttribute (J2EEConsts.APP_DOMAIN, servletDomain);
+                               }
+                               //config.getServletContext ().setAttribute (J2EEConsts.CURRENT_SERVLET, this);
                        }
-                       finally 
-                       {
-                               vmw.@internal.EnvironmentUtils.cleanTLS();
-                               vmw.@internal.EnvironmentUtils.clearAppDomain();
+                       finally {
+                               vmw.@internal.EnvironmentUtils.cleanTLS ();
+                               vmw.@internal.EnvironmentUtils.clearAppDomain ();
                        }
                }
 
                protected override void service (HttpServletRequest req, HttpServletResponse resp)
                {
-                       resp.setContentType("text/html");
-                       service(req, resp, resp.getOutputStream());
-               }
+                       resp.setContentType ("text/html");
 
-               public virtual void service(HttpServletRequest req, HttpServletResponse resp, java.io.OutputStream output)
-               {
                        try 
                        {
                                // Very important - to update Virtual Path!!!
                                AppDomain servletDomain = (AppDomain)this.getServletContext().getAttribute(J2EEConsts.APP_DOMAIN);
-                               servletDomain.SetData(IAppDomainConfig.APP_VIRT_DIR, req.getContextPath());
-                               servletDomain.SetData(".hostingVirtualPath", req.getContextPath());
+                               if (!_appVirDirInited) {
+                                       servletDomain.SetData (IAppDomainConfig.APP_VIRT_DIR, req.getContextPath ());
+                                       servletDomain.SetData (".hostingVirtualPath", req.getContextPath ());
+                                       _appVirDirInited = true;
+                               }
 
                                // Put to the TLS current AppDomain of the servlet, so anyone can use it.
                                vmw.@internal.EnvironmentUtils.setAppDomain(servletDomain);
 
                                // put request to the TLS
-                               Thread.SetData(_servletRequestSlot, req);
-                               // put response to the TLS
-                               Thread.SetData(_servletResponseSlot, resp);
-                               // put the servlet object to the TLS
-                               Thread.SetData(_servletSlot, this);
+                               //Thread.SetData(_servletRequestSlot, req);
+                               //// put response to the TLS
+                               //Thread.SetData(_servletResponseSlot, resp);
+                               //// put the servlet object to the TLS
+                               //Thread.SetData(_servletSlot, this);
 
                                resp.setHeader("X-Powered-By", "ASP.NET");
                                resp.setHeader("X-AspNet-Version", "1.1.4322");
 
-                               HttpWorkerRequest gwr = new ServletWorkerRequest(this, req, resp, output);
+                               HttpWorkerRequest gwr = new ServletWorkerRequest (this, req, resp);
+                               CultureInfo culture = (CultureInfo) vmw.@internal.EnvironmentUtils.getCultureInfoFromLocale (req.getLocale ());
+                               Thread currentTread = Thread.CurrentThread;
+                               currentTread.CurrentCulture = culture;
+                               currentTread.CurrentUICulture = culture;
                                HttpRuntime.ProcessRequest(gwr);
                        }
                        finally 
                        {
                                HttpContext.Current = null;
-                               Thread.SetData(_servletRequestSlot, null);
-                               Thread.SetData(_servletResponseSlot, null);
-                               Thread.SetData(_servletSlot, null);
+                               //Thread.SetData(_servletRequestSlot, null);
+                               //Thread.SetData(_servletResponseSlot, null);
+                               //Thread.SetData(_servletSlot, null);
                                vmw.@internal.EnvironmentUtils.clearAppDomain();
                        }
                }
@@ -134,37 +143,34 @@ namespace Mainsoft.Web.Hosting
                override public void destroy()
                {
                        base.destroy();
-                       if (!_performedInit)
+                       DestroyRuntime (getServletContext (), this);
+               }
+
+               public static void DestroyRuntime (ServletContext context, object evidence) {
+                       AppDomain servletDomain = (AppDomain) context.getAttribute (J2EEConsts.APP_DOMAIN);
+                       if (servletDomain == null)
                                return;
 
-                       try 
-                       {
-                               AppDomain servletDomain = (AppDomain)this.getServletContext().getAttribute(J2EEConsts.APP_DOMAIN);
-                               vmw.@internal.EnvironmentUtils.setAppDomain(servletDomain);
-#if DEBUG
-                               Console.WriteLine("Destroy of GhHttpServlet");
-#endif
-                               HttpRuntime.Close();
-                               vmw.@internal.EnvironmentUtils.cleanAllBeforeServletDestroy(this);
-                               this.getServletContext().removeAttribute(J2EEConsts.APP_DOMAIN);
-                               java.lang.Thread.currentThread().setContextClassLoader(null);
+                       try {
+                               vmw.@internal.EnvironmentUtils.setAppDomain (servletDomain);
+                               Debug.WriteLine ("Destroy of GhHttpServlet");
+                               HttpRuntime.Close ();
+                               vmw.@internal.EnvironmentUtils.cleanAllBeforeServletDestroy (evidence);
+                               context.removeAttribute (J2EEConsts.APP_DOMAIN);
+                               java.lang.Thread.currentThread ().setContextClassLoader (null);
                        }
-                       catch(Exception e) 
-                       {
-#if DEBUG
-                               Console.WriteLine("ERROR in Servlet Destroy {0},{1}",e.GetType(), e.Message);
-                               Console.WriteLine(e.StackTrace);
-#endif
+                       catch (Exception e) {
+                               Debug.WriteLine (String.Format ("ERROR in Servlet Destroy {0},{1}", e.GetType (), e.Message));
+                               Debug.WriteLine (e.StackTrace);
                        }
-                       finally
-                       {
-                               vmw.@internal.EnvironmentUtils.clearAppDomain();
+                       finally {
+                               vmw.@internal.EnvironmentUtils.clearAppDomain ();
                        }
                }
 
-               private AppDomain createServletDomain(ServletConfig config)
+               private static AppDomain createServletDomain(ServletConfig config)
                {
-                               string rootPath = J2EEUtils.GetApplicationRealPath(config);
+                               string rootPath = J2EEUtils.GetApplicationRealPath(config.getServletContext ());
                                AppDomainSetup domainSetup = new AppDomainSetup();
                                string name = config.getServletName();//.getServletContextName();
                                if (name == null)
@@ -182,13 +188,13 @@ namespace Mainsoft.Web.Hosting
                                //servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config));
                                //servletDomain.SetData(IAppDomainConfig.WEB_APP_DIR, rootPath);
 
-                               servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config));
+                               servletDomain.SetData(IAppDomainConfig.APP_PHYS_DIR, J2EEUtils.GetApplicationPhysicalPath(config.getServletContext ()));
                                servletDomain.SetData(IAppDomainConfig.WEB_APP_DIR, rootPath);
 
                                //Set DataDirectory substitution string (http://blogs.msdn.com/dataaccess/archive/2005/10/28/486273.aspx)
-                               string dataDirectory = J2EEUtils.GetInitParameterByHierarchy(config, "DataDirectory");
+                               string dataDirectory = config.getServletContext ().getInitParameter ("DataDirectory");
                                if (dataDirectory == null)
-                                       dataDirectory = "APP_DATA";
+                                       dataDirectory = "App_Data";
 
                                if (!Path.IsPathRooted (dataDirectory)) {
                                        java.io.InputStream inputStream = config.getServletContext ().getResourceAsStream ("/WEB-INF/classes/appData.properties");
@@ -227,53 +233,19 @@ namespace Mainsoft.Web.Hosting
                                if (webApp_baseDir == null || webApp_baseDir == "")
                                        webApp_baseDir = rootPath;
                                servletDomain.SetData(IAppDomainConfig.APP_BASE_DIR , webApp_baseDir);
-#if DEBUG
-                               Console.WriteLine("Initialization of webapp " + webApp_baseDir);
-#endif
-                               // Mordechai : setting the web app deserializer object.
-                               servletDomain.SetData(J2EEConsts.DESERIALIZER_CONST , this.GetDeserializer());
-                               servletDomain.SetData(vmw.@internal.EnvironmentUtils.GH_DRIVER_UTILS_CONST, this.getDriverUtils());
+                               Debug.WriteLine("Initialization of webapp " + webApp_baseDir);
                                //servletDomain.SetData(".hostingVirtualPath", "/");
                                //servletDomain.SetData(".hostingInstallDir", "/");
                                return servletDomain;
                }
-       
-               virtual protected vmw.@internal.io.IObjectsDeserializer GetDeserializer()
-               {
-                       if (m_deseializer == null)
-                               m_deseializer = new GHWebDeseserializer();
-                       return m_deseializer;
-               }
-
-               protected vmw.@internal.io.IObjectsDeserializer m_deseializer = null;
-               /// Mordechai: This class comes to solve a problem in class deserialize
-               /// within web application. The problem is that the classloader that created 
-               /// some user web class (for example aspx page) is not the class loader
-               /// that de-serialize it - thus we end with ClassDefNotFoundException.
-               /// To prevent this situation we delegate the serialization back the the 
-               /// web app (which has the correct class loader...)
-               /// 
-
-               virtual protected vmw.@internal.IDriverUtils getDriverUtils()
-               {
-                       //by default no driver utils, the specific servlet will override this method
-                       return null;
-               }
-       }
-
-       public class GHWebDeseserializer : vmw.@internal.io.IObjectsDeserializer 
-       {
-
-                       Object vmw.@internal.io.IObjectsDeserializer.Deserialize(java.io.ObjectInputStream stream)
-                       {
-                               object obj = stream.readObject();
-                               return obj;
-                       }
        }
 }
 
 namespace System.Web.GH
 {
+       /// <summary>
+       /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
+       /// </summary>
        public class BaseHttpServlet : Mainsoft.Web.Hosting.BaseHttpServlet
        {
        }
@@ -282,8 +254,55 @@ namespace System.Web.GH
 
 namespace System.Web.J2EE
 {
+       /// <summary>
+       /// <para>This class supports the Framework infrastructure and is not intended to be used directly from your code.</para>
+       /// </summary>
        public class BaseHttpServlet : Mainsoft.Web.Hosting.BaseHttpServlet
        {
        }
 
 }
+
+public class GhDynamicHttpServlet : System.Web.GH.BaseHttpServlet
+{
+}
+
+public class GhStaticHttpServlet : System.Web.GH.BaseStaticHttpServlet
+{ 
+}
+
+public class GhHttpServlet : System.Web.GH.BaseHttpServlet
+{
+       GhStaticHttpServlet staticServlet;
+
+       public GhHttpServlet () {
+               staticServlet = new GhStaticHttpServlet ();
+       }
+
+       override public void init (ServletConfig config) {
+               base.init (config);
+               staticServlet.init (config);
+       }
+
+       override protected void service (HttpServletRequest req, HttpServletResponse resp) {
+               string pathInfo = req.getRequestURI ();
+               string contextPath = req.getContextPath ();
+               if (pathInfo.Equals (contextPath) ||
+                       ((pathInfo.Length - contextPath.Length) == 1) &&
+                       pathInfo [pathInfo.Length - 1] == '/' && pathInfo.StartsWith (contextPath))
+                       pathInfo = contextPath + req.getServletPath ();
+               if (pathInfo.EndsWith (".aspx") ||
+                       pathInfo.EndsWith (".asmx") ||
+                       pathInfo.EndsWith (".invoke")) {
+                       base.service (req, resp);
+               }
+               else {
+                       staticServlet.service (req, resp);
+               }
+       }
+
+       override public void destroy () {
+               staticServlet.destroy ();
+               base.destroy ();
+       }
+}