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