001// Copyright 2013 Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.tapestry5.internal.services;
016
017import org.apache.tapestry5.internal.util.Holder;
018import org.apache.tapestry5.ioc.IOOperation;
019import org.apache.tapestry5.ioc.OperationTracker;
020import org.apache.tapestry5.services.*;
021
022import java.io.IOException;
023
024/**
025 * Uses {@link OperationTracker} to add an operation generally describing each request.
026 *
027 * @since 5.4
028 */
029public class RequestOperationTracker implements ComponentRequestFilter
030{
031    private final OperationTracker tracker;
032
033    private final Request request;
034
035    public RequestOperationTracker(OperationTracker tracker, Request request)
036    {
037        this.tracker = tracker;
038        this.request = request;
039    }
040
041    public void handleComponentEvent(final ComponentEventRequestParameters parameters, final ComponentRequestHandler handler) throws IOException
042    {
043        String componentId = parameters.getNestedComponentId().equals("")
044                ? parameters.getContainingPageName()
045                : parameters.getContainingPageName() + ":" + parameters.getNestedComponentId();
046
047        tracker.perform(String.format("Handling %s '%s' component event request for %s.",
048                request.isXHR() ? "Ajax" : "traditional",
049                parameters.getEventType(),
050                componentId),
051                new IOOperation<Void>()
052                {
053                    public Void perform() throws IOException
054                    {
055                        handler.handleComponentEvent(parameters);
056
057                        return null;
058                    }
059                });
060    }
061
062    public void handlePageRender(final PageRenderRequestParameters parameters, final ComponentRequestHandler handler) throws IOException
063    {
064        final Holder<IOException> holder = Holder.create();
065
066        tracker.run("Handling page render request for page " + parameters.getLogicalPageName(),
067                new Runnable()
068                {
069                    public void run()
070                    {
071                        try
072                        {
073                            handler.handlePageRender(parameters);
074                        } catch (IOException e)
075                        {
076                            holder.put(e);
077                        }
078                    }
079                }
080        );
081
082        if (holder.hasValue())
083        {
084            throw holder.get();
085        }
086    }
087}