protected void setupForRequest()

in tapestry-framework/src/org/apache/tapestry/engine/AbstractEngine.java [1069:1351]


    protected void setupForRequest(RequestContext context)
    {
        HttpServlet servlet = context.getServlet();
        ServletContext servletContext = servlet.getServletContext();
        HttpServletRequest request = context.getRequest();
        HttpSession session = context.getSession();


        if (session != null)
            _sessionId = context.getSession().getId();
        else
            _sessionId = null;

        // Previously, this used getRemoteHost(), but that requires an
        // expensive reverse DNS lookup. Possibly, the host name lookup
        // should occur ... but only if there's an actual error message
        // to display.

        if (_clientAddress == null)
            _clientAddress = request.getRemoteAddr();

        // servletPath is null, so this means either we're doing the
        // first request in this session, or we're handling a subsequent
        // request in another JVM (i.e. another server in the cluster).
        // In any case, we have to do some late (re-)initialization.

        if (_servletPath == null)
        {
            // Get the path *within* the servlet context

            // In rare cases related to the tagsupport service, getServletPath() is wrong
            // (its a JSP, which invokes Tapestry as an include, thus muddling what
            // the real servlet and servlet path is).  In those cases, the JSP tag
            // will inform us.

            String path =
                    (String) request.getAttribute(Tapestry.TAG_SUPPORT_SERVLET_PATH_ATTRIBUTE);

            if (path == null)
                path = request.getServletPath();

            // Get the context path, which may be the empty string
            // (but won't be null).

            _contextPath = request.getContextPath();

            _servletPath = _contextPath + path;
        }

        String servletName = context.getServlet().getServletName();

        // Synchronize on the servlet to make sure that two different IEngine instances
        // do not try ot create some of the common dependencies simulataneously. We enclose
        // all the code that uses the ServletContext as a cache.  This is the motivation for
        // adding an IoC container to Tapestry 4.

        // Note: by borrowing ConcurrentBarrier from Tapestry 5, we could perhaps optimize this
        // a bit ... but in most cases, we'll "flit" into the synchronized block, see non-nulls
        // for all the fields, and "flit" back out with virtually no chance of contention.

        synchronized (servlet)
        {
            if (_propertySource == null)
            {
                String name = PROPERTY_SOURCE_NAME + ":" + servletName;

                _propertySource = (IPropertySource) servletContext.getAttribute(name);

                if (_propertySource == null)
                {
                    _propertySource = createPropertySource(context);

                    servletContext.setAttribute(name, _propertySource);
                }
            }

            if (_classFactory == null)
            {
                String name = CLASS_FACTORY_NAME + ":" + servletName;

                _classFactory = (IEnhancedClassFactory) servletContext.getAttribute(name);

                if (_classFactory == null)
                {
                    _classFactory = createClassFactory();

                    servletContext.setAttribute(name, _classFactory);
                }
            }

            if (_enhancer == null)
            {
                String name = ENHANCER_NAME + ":" + servletName;

                _enhancer = (IComponentClassEnhancer) servletContext.getAttribute(name);

                if (_enhancer == null)
                {
                    _enhancer = createComponentClassEnhancer(context);

                    servletContext.setAttribute(name, _enhancer);
                }
            }

            if (_pool == null)
            {
                String name = POOL_NAME + ":" + servletName;

                _pool = (Pool) servletContext.getAttribute(name);

                _pool = createPool(context);

            }

            if (_templateSource == null)
            {
                String name = TEMPLATE_SOURCE_NAME + ":" + servletName;

                _templateSource = (ITemplateSource) servletContext.getAttribute(name);

                if (_templateSource == null)
                {
                    _templateSource = createTemplateSource(context);

                    servletContext.setAttribute(name, _templateSource);
                }
            }

            if (_specificationSource == null)
            {
                String name = SPECIFICATION_SOURCE_NAME + ":" + servletName;

                _specificationSource = (ISpecificationSource) servletContext.getAttribute(name);

                if (_specificationSource == null)
                {
                    _specificationSource = createSpecificationSource(context);

                    servletContext.setAttribute(name, _specificationSource);
                }
            }

            if (_pageSource == null)
            {
                String name = PAGE_SOURCE_NAME + ":" + servletName;

                _pageSource = (IPageSource) servletContext.getAttribute(name);

                if (_pageSource == null)
                {
                    _pageSource = createPageSource(context);

                    servletContext.setAttribute(name, _pageSource);
                }
            }

            if (_scriptSource == null)
            {
                String name = SCRIPT_SOURCE_NAME + ":" + servletName;

                _scriptSource = (IScriptSource) servletContext.getAttribute(name);

                if (_scriptSource == null)
                {
                    _scriptSource = createScriptSource(context);

                    servletContext.setAttribute(name, _scriptSource);
                }
            }

            if (_serviceMap == null)
            {
                String name = SERVICE_MAP_NAME + ":" + servletName;

                _serviceMap = (Map) servletContext.getAttribute(name);

                if (_serviceMap == null)
                {
                    _serviceMap = createServiceMap();

                    servletContext.setAttribute(name, _serviceMap);
                }
            }

            if (_stringsSource == null)
            {
                String name = STRINGS_SOURCE_NAME + ":" + servletName;

                _stringsSource = (IComponentMessagesSource) servletContext.getAttribute(name);

                if (_stringsSource == null)
                {
                    _stringsSource = createComponentStringsSource(context);

                    servletContext.setAttribute(name, _stringsSource);
                }
            }

            if (_dataSqueezer == null)
            {
                String name = DATA_SQUEEZER_NAME + ":" + servletName;

                _dataSqueezer = (DataSqueezer) servletContext.getAttribute(name);

                if (_dataSqueezer == null)
                {
                    _dataSqueezer = createDataSqueezer();

                    servletContext.setAttribute(name, _dataSqueezer);
                }
            }

            if (_global == null)
            {
                String name = GLOBAL_NAME + ":" + servletName;

                _global = servletContext.getAttribute(name);

                if (_global == null)
                {
                    _global = createGlobal(context);

                    servletContext.setAttribute(name, _global);
                }
            }

            if (_resourceChecksumSource == null)
            {
                String name = RESOURCE_CHECKSUM_SOURCE_NAME + ":" + servletName;

                _resourceChecksumSource = (ResourceChecksumSource) servletContext.getAttribute(name);

                if (_resourceChecksumSource == null)
                {
                    _resourceChecksumSource = createResourceChecksumSource();

                    servletContext.setAttribute(name, _resourceChecksumSource);
                }
            }

            if (_expressionEvaluator == null)
            {
                String name = EXPRESSION_EVALUATOR_NAME + ":" + servletName;

                _expressionEvaluator = (ExpressionEvaluator) servletContext.getAttribute(name);

                if (_expressionEvaluator == null)
                {
                    _expressionEvaluator = createExpressionEvaluator();

                    servletContext.setAttribute(name, _expressionEvaluator);
                }
            }

            // That's the end of reading to and from the servlet context, so we can leave the block
            // synchronized against the servlet.
        }

        String encoding = request.getCharacterEncoding();
        if (encoding == null)

        {
            encoding = getOutputEncoding();
            try
            {
                request.setCharacterEncoding(encoding);
            }
            catch (UnsupportedEncodingException e)
            {
                throw new IllegalArgumentException(Tapestry.format("illegal-encoding", encoding));
            }
            catch (NoSuchMethodError e)
            {
                // Servlet API 2.2 compatibility
                // Behave okay if the setCharacterEncoding() method is unavailable
            }
            catch (AbstractMethodError e)
            {
                // Servlet API 2.2 compatibility
                // Behave okay if the setCharacterEncoding() method is unavailable
            }
        }
    }