Clover coverage report - Code Coverage for tapestry release 4.0-rc-2
Coverage timestamp: Sat Dec 17 2005 09:39:46 PST
file stats: LOC: 177   Methods: 8
NCLOC: 97   Classes: 1
 
 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    protected ResponseRenderer _responseRenderer;
 47   
 48    /** @since 4.0 */
 49    protected LinkFactory _linkFactory;
 50   
 51    /** @since 4.0 */
 52    protected WebRequest _request;
 53   
 54    /** @since 4.0 */
 55    private IRequestCycle _requestCycle;
 56   
 57  65 public ILink getLink(boolean post, Object parameter)
 58    {
 59  65 Defense.isAssignable(parameter, DirectServiceParameter.class, "parameter");
 60   
 61  65 DirectServiceParameter dsp = (DirectServiceParameter) parameter;
 62   
 63  65 IComponent component = dsp.getDirect();
 64   
 65    // New since 1.0.1, we use the component to determine
 66    // the page, not the cycle. Through the use of tricky
 67    // things such as Block/InsertBlock, it is possible
 68    // that a component from a page different than
 69    // the response page will render.
 70    // In 1.0.6, we start to record *both* the render page
 71    // and the component page (if different).
 72   
 73  65 IPage activePage = _requestCycle.getPage();
 74  65 IPage componentPage = component.getPage();
 75   
 76  65 Map parameters = new HashMap();
 77   
 78  65 boolean stateful = _request.getSession(false) != null;
 79   
 80  65 parameters.put(ServiceConstants.PAGE, activePage.getPageName());
 81  65 parameters.put(ServiceConstants.COMPONENT, component.getIdPath());
 82  65 parameters.put(ServiceConstants.CONTAINER, componentPage == activePage ? null
 83    : componentPage.getPageName());
 84  65 parameters.put(ServiceConstants.SESSION, stateful ? "T" : null);
 85  65 parameters.put(ServiceConstants.PARAMETER, dsp.getServiceParameters());
 86   
 87  65 return _linkFactory.constructLink(this, post, parameters, true);
 88    }
 89   
 90  50 public void service(IRequestCycle cycle) throws IOException
 91    {
 92  50 String componentId = cycle.getParameter(ServiceConstants.COMPONENT);
 93  50 String componentPageName = cycle.getParameter(ServiceConstants.CONTAINER);
 94  50 String activePageName = cycle.getParameter(ServiceConstants.PAGE);
 95  50 boolean activeSession = cycle.getParameter(ServiceConstants.SESSION) != null;
 96   
 97  50 IPage page = cycle.getPage(activePageName);
 98   
 99  50 cycle.activate(page);
 100   
 101  50 IPage componentPage = componentPageName == null ? page : cycle.getPage(componentPageName);
 102   
 103  50 IComponent component = componentPage.getNestedComponent(componentId);
 104   
 105  50 IDirect direct = null;
 106   
 107  50 try
 108    {
 109  50 direct = (IDirect) component;
 110    }
 111    catch (ClassCastException ex)
 112    {
 113  1 throw new ApplicationRuntimeException(EngineMessages.wrongComponentType(
 114    component,
 115    IDirect.class), component, null, ex);
 116    }
 117   
 118    // Check for a StaleSession only when the session was stateful when
 119    // the link was created.
 120   
 121  49 if (activeSession && direct.isStateful())
 122    {
 123  12 WebSession session = _request.getSession(false);
 124   
 125  12 if (session == null || session.isNew())
 126  3 throw new StaleSessionException(EngineMessages.requestStateSession(direct),
 127    componentPage);
 128    }
 129   
 130  46 Object[] parameters = _linkFactory.extractListenerParameters(cycle);
 131   
 132  46 triggerComponent(cycle, direct, parameters);
 133   
 134    // Render the response. This will be the active page
 135    // unless the direct component (or its delegate) changes it.
 136   
 137  42 _responseRenderer.renderResponse(cycle);
 138    }
 139   
 140    /** @since 4.0 */
 141   
 142  46 protected void triggerComponent(IRequestCycle cycle, IDirect direct, Object[] parameters)
 143    {
 144  46 cycle.setListenerParameters(parameters);
 145   
 146  46 direct.trigger(cycle);
 147    }
 148   
 149  78 public String getName()
 150    {
 151  78 return Tapestry.DIRECT_SERVICE;
 152    }
 153   
 154    /** @since 4.0 */
 155  20 public void setResponseRenderer(ResponseRenderer responseRenderer)
 156    {
 157  20 _responseRenderer = responseRenderer;
 158    }
 159   
 160    /** @since 4.0 */
 161  24 public void setLinkFactory(LinkFactory linkFactory)
 162    {
 163  24 _linkFactory = linkFactory;
 164    }
 165   
 166    /** @since 4.0 */
 167  23 public void setRequest(WebRequest request)
 168    {
 169  23 _request = request;
 170    }
 171   
 172    /** @since 4.0 */
 173  21 public void setRequestCycle(IRequestCycle requestCycle)
 174    {
 175  21 _requestCycle = requestCycle;
 176    }
 177    }