Clover coverage report - Code Coverage for tapestry release 4.0-alpha-3
Coverage timestamp: Mon May 16 2005 09:05:49 EDT
file stats: LOC: 159   Methods: 6
NCLOC: 89   Classes: 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 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   
  */
 43   
 
 44   
 public class ActionService implements IEngineService
 45   
 {
 46   
     /** @since 4.0 */
 47   
     private ResponseRenderer _responseRenderer;
 48   
 
 49   
     /** @since 4.0 */
 50   
     private LinkFactory _linkFactory;
 51   
 
 52   
     /** @since 4.0 */
 53   
     private static final String ACTION = "action";
 54   
 
 55   
     /** @since 4.0 */
 56   
     private WebRequest _request;
 57   
 
 58  19
     public ILink getLink(IRequestCycle cycle, Object parameter)
 59   
     {
 60  19
         Defense.isAssignable(parameter, ActionServiceParameter.class, "parameter");
 61   
 
 62  19
         ActionServiceParameter asp = (ActionServiceParameter) parameter;
 63   
 
 64  19
         IComponent component = asp.getComponent();
 65  19
         IPage activePage = cycle.getPage();
 66  19
         IPage componentPage = component.getPage();
 67   
 
 68  19
         Map parameters = new HashMap();
 69   
 
 70  19
         boolean stateful = _request.getSession(false) != null;
 71   
 
 72  19
         parameters.put(ServiceConstants.SERVICE, Tapestry.ACTION_SERVICE);
 73  19
         parameters.put(ServiceConstants.COMPONENT, component.getIdPath());
 74  19
         parameters.put(ServiceConstants.PAGE, activePage.getPageName());
 75  19
         parameters.put(ServiceConstants.CONTAINER, activePage == componentPage ? null
 76   
                 : componentPage.getPageName());
 77  19
         parameters.put(ACTION, asp.getActionId());
 78  19
         parameters.put(ServiceConstants.SESSION, stateful ? "T" : null);
 79   
 
 80  19
         return _linkFactory.constructLink(cycle, parameters, true);
 81   
     }
 82   
 
 83  13
     public void service(IRequestCycle cycle) throws IOException
 84   
     {
 85  13
         String componentId = cycle.getParameter(ServiceConstants.COMPONENT);
 86  13
         String componentPageName = cycle.getParameter(ServiceConstants.CONTAINER);
 87  13
         String activePageName = cycle.getParameter(ServiceConstants.PAGE);
 88  13
         String actionId = cycle.getParameter(ACTION);
 89  13
         boolean activeSession = cycle.getParameter(ServiceConstants.SESSION) != null;
 90   
 
 91  13
         IPage page = cycle.getPage(activePageName);
 92   
 
 93   
         // Setup the page for the rewind, then do the rewind.
 94   
 
 95  13
         cycle.activate(page);
 96   
 
 97  13
         IPage componentPage = componentPageName == null ? page : cycle.getPage(componentPageName);
 98   
 
 99  13
         IComponent component = componentPage.getNestedComponent(componentId);
 100   
 
 101  13
         IAction action = null;
 102   
 
 103  13
         try
 104   
         {
 105  13
             action = (IAction) component;
 106   
         }
 107   
         catch (ClassCastException ex)
 108   
         {
 109  1
             throw new ApplicationRuntimeException(EngineMessages.wrongComponentType(
 110   
                     component,
 111   
                     IAction.class), component, null, ex);
 112   
         }
 113   
 
 114   
         // Only perform the stateful check if the application was stateful
 115   
         // when the URL was rendered.
 116   
 
 117  12
         if (activeSession && action.getRequiresSession())
 118   
         {
 119  5
             WebSession session = _request.getSession(false);
 120   
 
 121  5
             if (session == null || session.isNew())
 122  2
                 throw new StaleSessionException(EngineMessages.requestStateSession(component),
 123   
                         componentPage);
 124   
 
 125   
         }
 126   
 
 127  10
         cycle.rewindPage(actionId, action);
 128   
 
 129   
         // During the rewind, a component may change the page. This will take
 130   
         // effect during the second render, which renders the HTML response.
 131   
 
 132   
         // Render that response.
 133   
 
 134  10
         _responseRenderer.renderResponse(cycle);
 135   
     }
 136   
 
 137  8
     public String getName()
 138   
     {
 139  8
         return Tapestry.ACTION_SERVICE;
 140   
     }
 141   
 
 142   
     /** @since 4.0 */
 143  11
     public void setResponseRenderer(ResponseRenderer responseRenderer)
 144   
     {
 145  11
         _responseRenderer = responseRenderer;
 146   
     }
 147   
 
 148   
     /** @since 4.0 */
 149  10
     public void setLinkFactory(LinkFactory linkFactory)
 150   
     {
 151  10
         _linkFactory = linkFactory;
 152   
     }
 153   
 
 154   
     /** @since 4.0 */
 155  12
     public void setRequest(WebRequest request)
 156   
     {
 157  12
         _request = request;
 158   
     }
 159   
 }