revert changes from r 96616
[mono.git] / mcs / class / Mainsoft.Web / Mainsoft.Web.Hosting / BaseHttpServlet.cs
index e770977adab537fa1446170bab19ba248ea8ff36..e00b3142c609315a518a7c28181c5bc9742c692e 100644 (file)
@@ -42,25 +42,44 @@ using java.net;
 using System.Globalization;
 using System.Diagnostics;
 using javax.faces;
+using javax.faces.context;
+using javax.faces.lifecycle;
+using javax.faces.webapp;
+using javax.faces.render;
 
 namespace Mainsoft.Web.Hosting
 {
+       public interface IJDBCDriverDeregisterer
+       {
+               void DeregisterDriver (java.sql.Driver driver);
+       }
        /// <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
+       public class BaseHttpServlet : HttpServlet, IJDBCDriverDeregisterer
        {
-               //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);
-
                bool _appVirDirInited = false;
 
+               static FacesContextFactory _facesContextFactory;
+               static Lifecycle _lifecycle;
+               static RenderKitFactory _renderKitFactory;
+
                public BaseHttpServlet()
                {
                }
 
+               public static RenderKitFactory RenderKitFactory {
+                       get { return _renderKitFactory; }
+               }
+
+               public static FacesContextFactory FacesContextFactory {
+                       get { return _facesContextFactory; }
+               }
+
+               public static Lifecycle Lifecycle {
+                       get { return _lifecycle; }
+               }
+
                override public void init(ServletConfig config)
                {
                        base.init(config);
@@ -68,11 +87,26 @@ namespace Mainsoft.Web.Hosting
                }
 
                public static void InitRuntime (ServletConfig config, object evidence) {
+
+                       ServletContext context = config.getServletContext ();
+
+                       if (context.getAttribute (J2EEConsts.APP_DOMAIN) != null)
+                               return;
+
+                       _facesContextFactory = (FacesContextFactory) FactoryFinder.getFactory (FactoryFinder.FACES_CONTEXT_FACTORY);
+                       //TODO: null-check for Weblogic, that tries to initialize Servlet before ContextListener
+
+                       //Javadoc says: Lifecycle instance is shared across multiple simultaneous requests, it must be implemented in a thread-safe manner.
+                       //So we can acquire it here once:
+                       LifecycleFactory lifecycleFactory = (LifecycleFactory) FactoryFinder.getFactory (FactoryFinder.LIFECYCLE_FACTORY);
+                       _lifecycle = lifecycleFactory.getLifecycle (context.getInitParameter (FacesServlet.LIFECYCLE_ID_ATTR) ?? LifecycleFactory.DEFAULT_LIFECYCLE);
+
+                       _renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory (FactoryFinder.RENDER_KIT_FACTORY);
+
                        AppDomain servletDomain = createServletDomain (config);
                        vmw.@internal.EnvironmentUtils.setAppDomain (servletDomain);
 
                        try {
-                               ServletContext context = config.getServletContext ();
                                //GH Infromation Initizalization
                                long currentTime = java.lang.System.currentTimeMillis ();
                                servletDomain.SetData (".domainId", currentTime.ToString ("x"));
@@ -99,6 +133,30 @@ namespace Mainsoft.Web.Hosting
 
                protected override void service (HttpServletRequest req, HttpServletResponse resp)
                {
+                       const string assemblies = "/assemblies";
+                       const string getping = "getping";
+                       const string setping = "setping";
+                       string servletPath = req.getServletPath ();
+
+                       if (String.CompareOrdinal (assemblies, 0, servletPath, 0, assemblies.Length) == 0) {
+                               if (servletPath.Length == assemblies.Length ||
+                                               servletPath [assemblies.Length] == '/') {
+                                       string requestURI = req.getRequestURI ();
+                                       bool getp = requestURI.EndsWith (getping, StringComparison.Ordinal);
+                                       if (!getp && requestURI.EndsWith (setping, StringComparison.Ordinal)) {
+                                               getServletContext ().setAttribute (getping, "1");
+                                               getp = true;
+                                       }
+
+                                       if (getp) {
+                                               string ping = (string) getServletContext ().getAttribute (getping);
+                                               if (ping == null)
+                                                       ping = "0";
+                                               resp.getOutputStream ().print (ping);
+                                               return;
+                                       }
+                               }
+                       }
                        resp.setContentType ("text/html");
 
                        try 
@@ -106,7 +164,10 @@ namespace Mainsoft.Web.Hosting
                                // Very important - to update Virtual Path!!!
                                AppDomain servletDomain = (AppDomain)this.getServletContext().getAttribute(J2EEConsts.APP_DOMAIN);
                                if (!_appVirDirInited) {
-                                       servletDomain.SetData (IAppDomainConfig.APP_VIRT_DIR, req.getContextPath ());
+                                       string appVPath = req.getContextPath ();
+                                       if (appVPath == null || appVPath.Length == 0)
+                                               appVPath = "/";
+                                       servletDomain.SetData (IAppDomainConfig.APP_VIRT_DIR, appVPath);
                                        servletDomain.SetData (".hostingVirtualPath", req.getContextPath ());
                                        _appVirDirInited = true;
                                }
@@ -147,7 +208,7 @@ namespace Mainsoft.Web.Hosting
                        DestroyRuntime (getServletContext (), this);
                }
 
-               public static void DestroyRuntime (ServletContext context, object evidence) {
+               public static void DestroyRuntime (ServletContext context, IJDBCDriverDeregisterer evidence) {
                        AppDomain servletDomain = (AppDomain) context.getAttribute (J2EEConsts.APP_DOMAIN);
                        if (servletDomain == null)
                                return;
@@ -162,6 +223,18 @@ namespace Mainsoft.Web.Hosting
                                        FactoryFinder.releaseFactories ();
                                }
                                catch (FacesException) { }
+
+                               java.lang.ClassLoader appClassLoader = vmw.common.TypeUtils.ToClass (evidence).getClassLoader ();
+
+                               IJDBCDriverDeregisterer dereg = evidence;
+
+                               java.util.Enumeration en = java.sql.DriverManager.getDrivers ();
+                               while (en.hasMoreElements ()) {
+                                       Object o = en.nextElement ();
+                                       if (vmw.common.TypeUtils.ToClass (o).getClassLoader () == appClassLoader)
+                                               dereg.DeregisterDriver ((java.sql.Driver) o);
+                               }
+
                                java.lang.Thread.currentThread ().setContextClassLoader (null);
                        }
                        catch (Exception e) {
@@ -243,6 +316,14 @@ namespace Mainsoft.Web.Hosting
                                //servletDomain.SetData(".hostingInstallDir", "/");
                                return servletDomain;
                }
+
+               #region IJDBCDriverDeregisterer Members
+
+               public void DeregisterDriver (java.sql.Driver driver) {
+                       java.sql.DriverManager.deregisterDriver (driver);
+               }
+
+               #endregion
        }
 }