Clover coverage report - Code Coverage for tapestry release 4.0.2
Coverage timestamp: Thu Apr 13 2006 10:52:06 EDT
file stats: LOC: 248   Methods: 16
NCLOC: 157   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
LinkFactoryImpl.java 100% 96.3% 100% 97.7%
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  191 public ILink constructLink(IEngineService service, boolean post, Map parameters,
 101    boolean stateful)
 102    {
 103  191 finalizeParameters(service, parameters);
 104   
 105  190 IEngine engine = _requestCycle.getEngine();
 106   
 107  190 ServiceEncoding serviceEncoding = createServiceEncoding(parameters);
 108   
 109    // Give persistent property strategies a chance to store extra data
 110    // into the link.
 111   
 112  190 if (stateful)
 113  113 _persistenceStrategySource.addParametersForPersistentProperties(serviceEncoding, post);
 114   
 115  190 String fullServletPath = _contextPath + serviceEncoding.getServletPath();
 116   
 117  190 return new EngineServiceLink(_requestCycle, fullServletPath, engine.getOutputEncoding(),
 118    _codec, _request, parameters, stateful);
 119    }
 120   
 121  191 protected void finalizeParameters(IEngineService service, Map parameters)
 122    {
 123  191 Defense.notNull(service, "service");
 124  191 Defense.notNull(parameters, "parameters");
 125   
 126  191 String serviceName = service.getName();
 127   
 128  191 if (serviceName == null)
 129  1 throw new ApplicationRuntimeException(ImplMessages.serviceNameIsNull());
 130   
 131  190 parameters.put(ServiceConstants.SERVICE, serviceName);
 132   
 133  190 squeezeServiceParameters(parameters);
 134    }
 135   
 136  39 public ServiceEncoder[] getServiceEncoders()
 137    {
 138  39 return _encoders;
 139    }
 140   
 141    /**
 142    * Creates a new service encoding, and allows the encoders to modify it before returning.
 143    */
 144   
 145  190 private ServiceEncoding createServiceEncoding(Map parameters)
 146    {
 147  190 ServiceEncodingImpl result = new ServiceEncodingImpl(_servletPath, parameters);
 148   
 149  190 for (int i = 0; i < _encoders.length; i++)
 150    {
 151  4 _encoders[i].encode(result);
 152   
 153  4 if (result.isModified())
 154  2 break;
 155    }
 156   
 157  190 return result;
 158    }
 159   
 160  190 protected void squeezeServiceParameters(Map parameters)
 161    {
 162  190 Object[] serviceParameters = (Object[]) parameters.get(ServiceConstants.PARAMETER);
 163   
 164  190 if (serviceParameters == null)
 165  178 return;
 166   
 167  12 String[] squeezed = squeeze(serviceParameters);
 168   
 169  12 parameters.put(ServiceConstants.PARAMETER, squeezed);
 170    }
 171   
 172  43 public Object[] extractListenerParameters(IRequestCycle cycle)
 173    {
 174  43 String[] squeezed = cycle.getParameters(ServiceConstants.PARAMETER);
 175   
 176  43 if (Tapestry.size(squeezed) == 0)
 177  29 return EMPTY;
 178   
 179  14 try
 180    {
 181  14 return _dataSqueezer.unsqueeze(squeezed);
 182    }
 183    catch (Exception ex)
 184    {
 185  0 throw new ApplicationRuntimeException(ex);
 186    }
 187    }
 188   
 189  12 private String[] squeeze(Object[] input)
 190    {
 191  12 try
 192    {
 193  12 return _dataSqueezer.squeeze(input);
 194    }
 195    catch (Exception ex)
 196    {
 197  0 throw new ApplicationRuntimeException(ex);
 198    }
 199    }
 200   
 201  40 public void setDataSqueezer(DataSqueezer dataSqueezer)
 202    {
 203  40 _dataSqueezer = dataSqueezer;
 204    }
 205   
 206  44 public void setContributions(List contributions)
 207    {
 208  44 _contributions = contributions;
 209    }
 210   
 211  44 public void setErrorLog(ErrorLog errorLog)
 212    {
 213  44 _errorLog = errorLog;
 214    }
 215   
 216  44 public void setServletPath(String servletPath)
 217    {
 218  44 _servletPath = servletPath;
 219    }
 220   
 221  44 public void setContextPath(String contextPath)
 222    {
 223  44 _contextPath = contextPath;
 224    }
 225   
 226  44 public void setRequest(WebRequest request)
 227    {
 228  44 _request = request;
 229    }
 230   
 231    /**
 232    * This is kind of limiting; it's possible that other things beyond persistence strategies will
 233    * want to have a hand at encoding data into URLs. If that comes to pass, we'll need to
 234    * implement an event coordinator/listener combo to let implementations know about links being
 235    * generated.
 236    */
 237   
 238  40 public void setPersistenceStrategySource(
 239    PropertyPersistenceStrategySource persistenceStrategySource)
 240    {
 241  40 _persistenceStrategySource = persistenceStrategySource;
 242    }
 243   
 244  44 public void setRequestCycle(IRequestCycle requestCycle)
 245    {
 246  44 _requestCycle = requestCycle;
 247    }
 248    }