Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 163   Methods: 6
NCLOC: 92   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 javax.servlet.ServletException;
 22   
 import javax.servlet.http.HttpServletRequest;
 23   
 import javax.servlet.http.HttpSession;
 24   
 
 25   
 import org.apache.hivemind.ApplicationRuntimeException;
 26   
 import org.apache.hivemind.util.Defense;
 27   
 import org.apache.tapestry.IAction;
 28   
 import org.apache.tapestry.IComponent;
 29   
 import org.apache.tapestry.IPage;
 30   
 import org.apache.tapestry.IRequestCycle;
 31   
 import org.apache.tapestry.StaleSessionException;
 32   
 import org.apache.tapestry.Tapestry;
 33   
 import org.apache.tapestry.request.ResponseOutputStream;
 34   
 import org.apache.tapestry.services.LinkFactory;
 35   
 import org.apache.tapestry.services.ResponseRenderer;
 36   
 import org.apache.tapestry.services.ServiceConstants;
 37   
 
 38   
 /**
 39   
  * A context-sensitive service related to {@link org.apache.tapestry.form.Form}and
 40   
  * {@link org.apache.tapestry.link.ActionLink}. Encodes the page, component and an action id in the
 41   
  * service context.
 42   
  * 
 43   
  * @author Howard Lewis Ship
 44   
  * @since 1.0.9
 45   
  */
 46   
 
 47   
 public class ActionService implements IEngineService
 48   
 {
 49   
     /** @since 3.1 */
 50   
     private ResponseRenderer _responseRenderer;
 51   
 
 52   
     /** @since 3.1 */
 53   
     private LinkFactory _linkFactory;
 54   
 
 55   
     /** @since 3.1 */
 56   
     private static final String ACTION = "action";
 57   
 
 58   
     /** @since 3.1 */
 59   
     private HttpServletRequest _request;
 60   
 
 61  20
     public ILink getLink(IRequestCycle cycle, Object parameter)
 62   
     {
 63  20
         Defense.isAssignable(parameter, ActionServiceParameter.class, "parameter");
 64   
 
 65  20
         ActionServiceParameter asp = (ActionServiceParameter) parameter;
 66   
 
 67  20
         IComponent component = asp.getComponent();
 68  20
         IPage activePage = cycle.getPage();
 69  20
         IPage componentPage = component.getPage();
 70   
 
 71  20
         Map parameters = new HashMap();
 72   
 
 73  20
         boolean stateful = _request.getSession(false) != null;
 74   
 
 75  20
         parameters.put(ServiceConstants.SERVICE, Tapestry.ACTION_SERVICE);
 76  20
         parameters.put(ServiceConstants.COMPONENT, component.getIdPath());
 77  20
         parameters.put(ServiceConstants.PAGE, activePage.getPageName());
 78  20
         parameters.put(ServiceConstants.CONTAINER, activePage == componentPage ? null
 79   
                 : componentPage.getPageName());
 80  20
         parameters.put(ACTION, asp.getActionId());
 81  20
         parameters.put(ServiceConstants.SESSION, stateful ? "T" : null);
 82   
 
 83  20
         return _linkFactory.constructLink(cycle, parameters, true);
 84   
     }
 85   
 
 86  14
     public void service(IRequestCycle cycle, ResponseOutputStream output) throws ServletException,
 87   
             IOException
 88   
     {
 89  14
         String componentId = cycle.getParameter(ServiceConstants.COMPONENT);
 90  14
         String componentPageName = cycle.getParameter(ServiceConstants.CONTAINER);
 91  14
         String activePageName = cycle.getParameter(ServiceConstants.PAGE);
 92  14
         String actionId = cycle.getParameter(ACTION);
 93  14
         boolean activeSession = cycle.getParameter(ServiceConstants.SESSION) != null;
 94   
 
 95  14
         IPage page = cycle.getPage(activePageName);
 96   
 
 97   
         // Setup the page for the rewind, then do the rewind.
 98   
 
 99  14
         cycle.activate(page);
 100   
 
 101  14
         IPage componentPage = componentPageName == null ? page : cycle.getPage(componentPageName);
 102   
 
 103  14
         IComponent component = componentPage.getNestedComponent(componentId);
 104   
 
 105  14
         IAction action = null;
 106   
 
 107  14
         try
 108   
         {
 109  14
             action = (IAction) component;
 110   
         }
 111   
         catch (ClassCastException ex)
 112   
         {
 113  1
             throw new ApplicationRuntimeException(EngineMessages.wrongComponentType(
 114   
                     component,
 115   
                     IAction.class), component, null, ex);
 116   
         }
 117   
 
 118   
         // Only perform the stateful check if the application was stateful
 119   
         // when the URL was rendered.
 120   
 
 121  13
         if (activeSession && action.getRequiresSession())
 122   
         {
 123  6
             HttpSession session = _request.getSession();
 124   
 
 125  6
             if (session == null || session.isNew())
 126  3
                 throw new StaleSessionException(EngineMessages.requestStateSession(component),
 127   
                         componentPage);
 128   
 
 129   
         }
 130   
 
 131  10
         cycle.rewindPage(actionId, action);
 132   
 
 133   
         // During the rewind, a component may change the page. This will take
 134   
         // effect during the second render, which renders the HTML response.
 135   
 
 136   
         // Render that response.
 137   
 
 138  10
         _responseRenderer.renderResponse(cycle, output);
 139   
     }
 140   
 
 141  70
     public String getName()
 142   
     {
 143  70
         return Tapestry.ACTION_SERVICE;
 144   
     }
 145   
 
 146   
     /** @since 3.1 */
 147  55
     public void setResponseRenderer(ResponseRenderer responseRenderer)
 148   
     {
 149  55
         _responseRenderer = responseRenderer;
 150   
     }
 151   
 
 152   
     /** @since 3.1 */
 153  54
     public void setLinkFactory(LinkFactory linkFactory)
 154   
     {
 155  54
         _linkFactory = linkFactory;
 156   
     }
 157   
 
 158   
     /** @since 3.1 */
 159  56
     public void setRequest(HttpServletRequest request)
 160   
     {
 161  56
         _request = request;
 162   
     }
 163   
 }