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.beanmodel.services.*; 018import org.apache.tapestry5.http.services.Request; 019import org.apache.tapestry5.internal.util.Holder; 020import org.apache.tapestry5.ioc.IOOperation; 021import org.apache.tapestry5.ioc.OperationTracker; 022import org.apache.tapestry5.services.ComponentEventRequestParameters; 023import org.apache.tapestry5.services.ComponentRequestFilter; 024import org.apache.tapestry5.services.ComponentRequestHandler; 025import org.apache.tapestry5.services.PageRenderRequestParameters; 026 027import java.io.IOException; 028 029/** 030 * Uses {@link OperationTracker} to add an operation generally describing each request. 031 * 032 * @since 5.4 033 */ 034public class RequestOperationTracker implements ComponentRequestFilter 035{ 036 private final OperationTracker tracker; 037 038 private final Request request; 039 040 public RequestOperationTracker(OperationTracker tracker, Request request) 041 { 042 this.tracker = tracker; 043 this.request = request; 044 } 045 046 public void handleComponentEvent(final ComponentEventRequestParameters parameters, final ComponentRequestHandler handler) throws IOException 047 { 048 String componentId = parameters.getNestedComponentId().equals("") 049 ? parameters.getContainingPageName() 050 : parameters.getContainingPageName() + ":" + parameters.getNestedComponentId(); 051 052 tracker.perform(String.format("Handling %s '%s' component event request for %s.", 053 request.isXHR() ? "Ajax" : "traditional", 054 parameters.getEventType(), 055 componentId), 056 new IOOperation<Void>() 057 { 058 public Void perform() throws IOException 059 { 060 handler.handleComponentEvent(parameters); 061 062 return null; 063 } 064 }); 065 } 066 067 public void handlePageRender(final PageRenderRequestParameters parameters, final ComponentRequestHandler handler) throws IOException 068 { 069 final Holder<IOException> holder = Holder.create(); 070 071 tracker.run("Handling page render request for page " + parameters.getLogicalPageName(), 072 new Runnable() 073 { 074 public void run() 075 { 076 try 077 { 078 handler.handlePageRender(parameters); 079 } catch (IOException e) 080 { 081 holder.put(e); 082 } 083 } 084 } 085 ); 086 087 if (holder.hasValue()) 088 { 089 throw holder.get(); 090 } 091 } 092}