Clover coverage report - Code Coverage for tapestry release 4.0.2
Coverage timestamp: Thu Apr 13 2006 10:52:06 EDT
file stats: LOC: 168   Methods: 7
NCLOC: 93   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ActionService.java 100% 100% 100% 100%
coverage
 1    // Copyright 2004, 2005 The Apache Software Foundation
 2    //
 3    // Licensed under the Apache License, Version 2.0 (the "License");
 4    // you may not use this file except in compliance with the License.
 5    // You may obtain a copy of the License at
 6    //
 7    // http://www.apache.org/licenses/LICENSE-2.0
 8    //
 9    // Unless required by applicable law or agreed to in writing, software
 10    // distributed under the License is distributed on an "AS IS" BASIS,
 11    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12    // See the License for the specific language governing permissions and
 13    // limitations under the License.
 14   
 15    package org.apache.tapestry.engine;
 16   
 17    import java.io.IOException;
 18    import java.util.HashMap;
 19    import java.util.Map;
 20   
 21    import org.apache.hivemind.ApplicationRuntimeException;
 22    import org.apache.hivemind.util.Defense;
 23    import org.apache.tapestry.IAction;
 24    import org.apache.tapestry.IComponent;
 25    import org.apache.tapestry.IPage;
 26    import org.apache.tapestry.IRequestCycle;
 27    import org.apache.tapestry.StaleSessionException;
 28    import org.apache.tapestry.Tapestry;
 29    import org.apache.tapestry.services.LinkFactory;
 30    import org.apache.tapestry.services.ResponseRenderer;
 31    import org.apache.tapestry.services.ServiceConstants;
 32    import org.apache.tapestry.web.WebRequest;
 33    import org.apache.tapestry.web.WebSession;
 34   
 35    /**
 36    * A context-sensitive service related to {@link org.apache.tapestry.form.Form}and
 37    * {@link org.apache.tapestry.link.ActionLink}. Encodes the page, component and an action id in the
 38    * service context.
 39    *
 40    * @author Howard Lewis Ship
 41    * @since 1.0.9
 42    * @deprecated To be removed in 4.1.
 43    */
 44   
 45    public class ActionService implements IEngineService
 46    {
 47    /** @since 4.0 */
 48    private ResponseRenderer _responseRenderer;
 49   
 50    /** @since 4.0 */
 51    private LinkFactory _linkFactory;
 52   
 53    /** @since 4.0 */
 54    private static final String ACTION = "action";
 55   
 56    /** @since 4.0 */
 57    private WebRequest _request;
 58   
 59    /** @since 4.0 */
 60    private IRequestCycle _requestCycle;
 61   
 62  15 public ILink getLink(boolean post, Object parameter)
 63    {
 64  15 Defense.isAssignable(parameter, ActionServiceParameter.class, "parameter");
 65   
 66  15 ActionServiceParameter asp = (ActionServiceParameter) parameter;
 67   
 68  15 IComponent component = asp.getComponent();
 69  15 IPage activePage = _requestCycle.getPage();
 70  15 IPage componentPage = component.getPage();
 71   
 72  15 Map parameters = new HashMap();
 73   
 74  15 boolean stateful = _request.getSession(false) != null;
 75   
 76  15 parameters.put(ServiceConstants.COMPONENT, component.getIdPath());
 77  15 parameters.put(ServiceConstants.PAGE, activePage.getPageName());
 78  15 parameters.put(ServiceConstants.CONTAINER, activePage == componentPage ? null
 79    : componentPage.getPageName());
 80  15 parameters.put(ACTION, asp.getActionId());
 81  15 parameters.put(ServiceConstants.SESSION, stateful ? "T" : null);
 82   
 83  15 return _linkFactory.constructLink(this, post, parameters, true);
 84    }
 85   
 86  11 public void service(IRequestCycle cycle) throws IOException
 87    {
 88  11 String componentId = cycle.getParameter(ServiceConstants.COMPONENT);
 89  11 String componentPageName = cycle.getParameter(ServiceConstants.CONTAINER);
 90  11 String activePageName = cycle.getParameter(ServiceConstants.PAGE);
 91  11 String actionId = cycle.getParameter(ACTION);
 92  11 boolean activeSession = cycle.getParameter(ServiceConstants.SESSION) != null;
 93   
 94  11 IPage page = cycle.getPage(activePageName);
 95   
 96    // Setup the page for the rewind, then do the rewind.
 97   
 98  11 cycle.activate(page);
 99   
 100  11 IPage componentPage = componentPageName == null ? page : cycle.getPage(componentPageName);
 101   
 102  11 IComponent component = componentPage.getNestedComponent(componentId);
 103   
 104  11 IAction action = null;
 105   
 106  11 try
 107    {
 108  11 action = (IAction) component;
 109    }
 110    catch (ClassCastException ex)
 111    {
 112  1 throw new ApplicationRuntimeException(EngineMessages.wrongComponentType(
 113    component,
 114    IAction.class), component, null, ex);
 115    }
 116   
 117    // Only perform the stateful check if the application was stateful
 118    // when the URL was rendered.
 119   
 120  10 if (activeSession && action.getRequiresSession())
 121    {
 122  4 WebSession session = _request.getSession(false);
 123   
 124  4 if (session == null || session.isNew())
 125  2 throw new StaleSessionException(EngineMessages.requestStateSession(component),
 126    componentPage);
 127   
 128    }
 129   
 130  8 cycle.rewindPage(actionId, action);
 131   
 132    // During the rewind, a component may change the page. This will take
 133    // effect during the second render, which renders the HTML response.
 134   
 135    // Render that response.
 136   
 137  8 _responseRenderer.renderResponse(cycle);
 138    }
 139   
 140  18 public String getName()
 141    {
 142  18 return Tapestry.ACTION_SERVICE;
 143    }
 144   
 145    /** @since 4.0 */
 146  9 public void setResponseRenderer(ResponseRenderer responseRenderer)
 147    {
 148  9 _responseRenderer = responseRenderer;
 149    }
 150   
 151    /** @since 4.0 */
 152  9 public void setLinkFactory(LinkFactory linkFactory)
 153    {
 154  9 _linkFactory = linkFactory;
 155    }
 156   
 157    /** @since 4.0 */
 158  11 public void setRequest(WebRequest request)
 159    {
 160  11 _request = request;
 161    }
 162   
 163    /** @since 4.0 */
 164  9 public void setRequestCycle(IRequestCycle requestCycle)
 165    {
 166  9 _requestCycle = requestCycle;
 167    }
 168    }