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: 162   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
DirectService.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.IComponent;
 24   
 import org.apache.tapestry.IDirect;
 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   
  * Implementation of the direct service, which encodes the page and component id in the service
 37   
  * context, and passes application-defined parameters as well.
 38   
  * 
 39   
  * @author Howard Lewis Ship
 40   
  * @since 1.0.9
 41   
  */
 42   
 
 43   
 public class DirectService implements IEngineService
 44   
 {
 45   
     /** @since 4.0 */
 46   
     private ResponseRenderer _responseRenderer;
 47   
 
 48   
     /** @since 4.0 */
 49   
     private LinkFactory _linkFactory;
 50   
 
 51   
     /** @since 4.0 */
 52   
     private WebRequest _request;
 53   
 
 54  101
     public ILink getLink(IRequestCycle cycle, Object parameter)
 55   
     {
 56  101
         Defense.isAssignable(parameter, DirectServiceParameter.class, "parameter");
 57   
 
 58  101
         DirectServiceParameter dsp = (DirectServiceParameter) parameter;
 59   
 
 60  101
         IComponent component = dsp.getDirect();
 61   
 
 62   
         // New since 1.0.1, we use the component to determine
 63   
         // the page, not the cycle. Through the use of tricky
 64   
         // things such as Block/InsertBlock, it is possible
 65   
         // that a component from a page different than
 66   
         // the response page will render.
 67   
         // In 1.0.6, we start to record *both* the render page
 68   
         // and the component page (if different).
 69   
 
 70  101
         IPage activePage = cycle.getPage();
 71  101
         IPage componentPage = component.getPage();
 72   
 
 73  101
         Map parameters = new HashMap();
 74   
 
 75  101
         boolean stateful = _request.getSession(false) != null;
 76   
 
 77  101
         parameters.put(ServiceConstants.SERVICE, Tapestry.DIRECT_SERVICE);
 78  101
         parameters.put(ServiceConstants.PAGE, activePage.getPageName());
 79  101
         parameters.put(ServiceConstants.COMPONENT, component.getIdPath());
 80  101
         parameters.put(ServiceConstants.CONTAINER, componentPage == activePage ? null
 81   
                 : componentPage.getPageName());
 82  101
         parameters.put(ServiceConstants.SESSION, stateful ? "T" : null);
 83  101
         parameters.put(ServiceConstants.PARAMETER, dsp.getServiceParameters());
 84   
 
 85  101
         return _linkFactory.constructLink(cycle, parameters, true);
 86   
     }
 87   
 
 88  63
     public void service(IRequestCycle cycle) throws IOException
 89   
     {
 90  63
         String componentId = cycle.getParameter(ServiceConstants.COMPONENT);
 91  63
         String componentPageName = cycle.getParameter(ServiceConstants.CONTAINER);
 92  63
         String activePageName = cycle.getParameter(ServiceConstants.PAGE);
 93  63
         boolean activeSession = cycle.getParameter(ServiceConstants.SESSION) != null;
 94   
 
 95  63
         IPage page = cycle.getPage(activePageName);
 96   
 
 97  63
         cycle.activate(page);
 98   
 
 99  63
         IPage componentPage = componentPageName == null ? page : cycle.getPage(componentPageName);
 100   
 
 101  63
         IComponent component = componentPage.getNestedComponent(componentId);
 102   
 
 103  63
         IDirect direct = null;
 104   
 
 105  63
         try
 106   
         {
 107  63
             direct = (IDirect) component;
 108   
         }
 109   
         catch (ClassCastException ex)
 110   
         {
 111  1
             throw new ApplicationRuntimeException(EngineMessages.wrongComponentType(
 112   
                     component,
 113   
                     IDirect.class), component, null, ex);
 114   
         }
 115   
 
 116   
         // Check for a StaleSession only when the session was stateful when
 117   
         // the link was created.
 118   
 
 119  62
         if (activeSession && direct.isStateful())
 120   
         {
 121  17
             WebSession session = _request.getSession(false);
 122   
 
 123  17
             if (session == null || session.isNew())
 124  3
                 throw new StaleSessionException(EngineMessages.requestStateSession(direct),
 125   
                         componentPage);
 126   
         }
 127   
 
 128  59
         Object[] parameters = _linkFactory.extractListenerParameters(cycle);
 129   
 
 130  59
         cycle.setListenerParameters(parameters);
 131   
 
 132  59
         direct.trigger(cycle);
 133   
 
 134   
         // Render the response. This will be the active page
 135   
         // unless the direct component (or its delegate) changes it.
 136   
 
 137  53
         _responseRenderer.renderResponse(cycle);
 138   
     }
 139   
 
 140  23
     public String getName()
 141   
     {
 142  23
         return Tapestry.DIRECT_SERVICE;
 143   
     }
 144   
 
 145   
     /** @since 4.0 */
 146  26
     public void setResponseRenderer(ResponseRenderer responseRenderer)
 147   
     {
 148  26
         _responseRenderer = responseRenderer;
 149   
     }
 150   
 
 151   
     /** @since 4.0 */
 152  29
     public void setLinkFactory(LinkFactory linkFactory)
 153   
     {
 154  29
         _linkFactory = linkFactory;
 155   
     }
 156   
 
 157   
     /** @since 4.0 */
 158  28
     public void setRequest(WebRequest request)
 159   
     {
 160  28
         _request = request;
 161   
     }
 162   
 }