Clover coverage report - Code Coverage for tapestry release 4.0-beta-13
Coverage timestamp: Sat Nov 12 2005 13:42:12 EST
file stats: LOC: 243   Methods: 15
NCLOC: 153   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LinkFactoryImpl.java 100% 96.2% 100% 97.6%
coverage 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.services.impl;
 16   
 17    import java.util.Iterator;
 18    import java.util.List;
 19    import java.util.Map;
 20   
 21    import org.apache.commons.codec.net.URLCodec;
 22    import org.apache.hivemind.ApplicationRuntimeException;
 23    import org.apache.hivemind.ErrorLog;
 24    import org.apache.hivemind.order.Orderer;
 25    import org.apache.hivemind.util.Defense;
 26    import org.apache.tapestry.IEngine;
 27    import org.apache.tapestry.IRequestCycle;
 28    import org.apache.tapestry.Tapestry;
 29    import org.apache.tapestry.engine.EngineServiceLink;
 30    import org.apache.tapestry.engine.IEngineService;
 31    import org.apache.tapestry.engine.ILink;
 32    import org.apache.tapestry.engine.ServiceEncoder;
 33    import org.apache.tapestry.engine.ServiceEncoding;
 34    import org.apache.tapestry.engine.ServiceEncodingImpl;
 35    import org.apache.tapestry.record.PropertyPersistenceStrategySource;
 36    import org.apache.tapestry.services.DataSqueezer;
 37    import org.apache.tapestry.services.LinkFactory;
 38    import org.apache.tapestry.services.ServiceConstants;
 39    import org.apache.tapestry.web.WebRequest;
 40   
 41    /**
 42    * @author Howard M. Lewis Ship
 43    * @since 4.0
 44    */
 45    public class LinkFactoryImpl implements LinkFactory
 46    {
 47    private DataSqueezer _dataSqueezer;
 48   
 49    private ErrorLog _errorLog;
 50   
 51    /**
 52    * List of {@link org.apache.tapestry.services.impl.ServiceEncoderContribution}.
 53    */
 54   
 55    private List _contributions;
 56   
 57    private ServiceEncoder[] _encoders;
 58   
 59    private String _contextPath;
 60   
 61    private String _servletPath;
 62   
 63    private final Object[] EMPTY = new Object[0];
 64   
 65    private URLCodec _codec = new URLCodec();
 66   
 67    private WebRequest _request;
 68   
 69    private IRequestCycle _requestCycle;
 70   
 71    private PropertyPersistenceStrategySource _persistenceStrategySource;
 72   
 73  44 public void initializeService()
 74    {
 75  44 Orderer orderer = new Orderer(_errorLog, "encoder");
 76   
 77  44 Iterator i = _contributions.iterator();
 78   
 79  44 while (i.hasNext())
 80    {
 81  4 ServiceEncoderContribution c = (ServiceEncoderContribution) i.next();
 82   
 83  4 orderer.add(c, c.getId(), c.getAfter(), c.getBefore());
 84    }
 85   
 86  44 List ordered = orderer.getOrderedObjects();
 87  44 int count = ordered.size();
 88   
 89  44 _encoders = new ServiceEncoder[count];
 90   
 91  44 for (int j = 0; j < count; j++)
 92    {
 93  4 ServiceEncoderContribution c = (ServiceEncoderContribution) ordered.get(j);
 94   
 95  4 _encoders[j] = c.getEncoder();
 96    }
 97   
 98    }
 99   
 100  192 public ILink constructLink(IEngineService service, boolean post, Map parameters,
 101    boolean stateful)
 102    {
 103  192 Defense.notNull(service, "service");
 104  192 Defense.notNull(parameters, "parameters");
 105   
 106  192 String serviceName = service.getName();
 107   
 108  192 if (serviceName == null)
 109  1 throw new ApplicationRuntimeException(ImplMessages.serviceNameIsNull());
 110   
 111  191 parameters.put(ServiceConstants.SERVICE, serviceName);
 112   
 113  191 squeezeServiceParameters(parameters);
 114   
 115  191 IEngine engine = _requestCycle.getEngine();
 116   
 117  191 ServiceEncoding serviceEncoding = createServiceEncoding(parameters);
 118   
 119    // Give persistent property strategies a chance to store extra data
 120    // into the link.
 121   
 122  191 if (stateful)
 123  114 _persistenceStrategySource.addParametersForPersistentProperties(serviceEncoding, post);
 124   
 125  191 String fullServletPath = _contextPath + serviceEncoding.getServletPath();
 126   
 127  191 return new EngineServiceLink(_requestCycle, fullServletPath, engine.getOutputEncoding(),
 128    _codec, _request, parameters, stateful);
 129    }
 130   
 131  39 public ServiceEncoder[] getServiceEncoders()
 132    {
 133  39 return _encoders;
 134    }
 135   
 136    /**
 137    * Creates a new service encoding, and allows the encoders to modify it before returning.
 138    */
 139   
 140  191 private ServiceEncoding createServiceEncoding(Map parameters)
 141    {
 142  191 ServiceEncodingImpl result = new ServiceEncodingImpl(_servletPath, parameters);
 143   
 144  191 for (int i = 0; i < _encoders.length; i++)
 145    {
 146  4 _encoders[i].encode(result);
 147   
 148  4 if (result.isModified())
 149  2 break;
 150    }
 151   
 152  191 return result;
 153    }
 154   
 155  191 protected void squeezeServiceParameters(Map parameters)
 156    {
 157  191 Object[] serviceParameters = (Object[]) parameters.get(ServiceConstants.PARAMETER);
 158   
 159  191 if (serviceParameters == null)
 160  179 return;
 161   
 162  12 String[] squeezed = squeeze(serviceParameters);
 163   
 164  12 parameters.put(ServiceConstants.PARAMETER, squeezed);
 165    }
 166   
 167  43 public Object[] extractListenerParameters(IRequestCycle cycle)
 168    {
 169  43 String[] squeezed = cycle.getParameters(ServiceConstants.PARAMETER);
 170   
 171  43 if (Tapestry.size(squeezed) == 0)
 172  29 return EMPTY;
 173   
 174  14 try
 175    {
 176  14 return _dataSqueezer.unsqueeze(squeezed);
 177    }
 178    catch (Exception ex)
 179    {
 180  0 throw new ApplicationRuntimeException(ex);
 181    }
 182    }
 183   
 184  12 private String[] squeeze(Object[] input)
 185    {
 186  12 try
 187    {
 188  12 return _dataSqueezer.squeeze(input);
 189    }
 190    catch (Exception ex)
 191    {
 192  0 throw new ApplicationRuntimeException(ex);
 193    }
 194    }
 195   
 196  40 public void setDataSqueezer(DataSqueezer dataSqueezer)
 197    {
 198  40 _dataSqueezer = dataSqueezer;
 199    }
 200   
 201  44 public void setContributions(List contributions)
 202    {
 203  44 _contributions = contributions;
 204    }
 205   
 206  44 public void setErrorLog(ErrorLog errorLog)
 207    {
 208  44 _errorLog = errorLog;
 209    }
 210   
 211  44 public void setServletPath(String servletPath)
 212    {
 213  44 _servletPath = servletPath;
 214    }
 215   
 216  44 public void setContextPath(String contextPath)
 217    {
 218  44 _contextPath = contextPath;
 219    }
 220   
 221  44 public void setRequest(WebRequest request)
 222    {
 223  44 _request = request;
 224    }
 225   
 226    /**
 227    * This is kind of limiting; it's possible that other things beyond persistence strategies will
 228    * want to have a hand at encoding data into URLs. If that comes to pass, we'll need to
 229    * implement an event coordinator/listener combo to let implementations know about links being
 230    * generated.
 231    */
 232   
 233  40 public void setPersistenceStrategySource(
 234    PropertyPersistenceStrategySource persistenceStrategySource)
 235    {
 236  40 _persistenceStrategySource = persistenceStrategySource;
 237    }
 238   
 239  44 public void setRequestCycle(IRequestCycle requestCycle)
 240    {
 241  44 _requestCycle = requestCycle;
 242    }
 243    }