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: 228   Methods: 14
NCLOC: 143   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
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.io.IOException;
 18   
 import java.util.Iterator;
 19   
 import java.util.List;
 20   
 import java.util.Map;
 21   
 
 22   
 import org.apache.commons.codec.net.URLCodec;
 23   
 import org.apache.hivemind.ApplicationRuntimeException;
 24   
 import org.apache.hivemind.ErrorLog;
 25   
 import org.apache.hivemind.order.Orderer;
 26   
 import org.apache.hivemind.util.Defense;
 27   
 import org.apache.tapestry.IEngine;
 28   
 import org.apache.tapestry.IRequestCycle;
 29   
 import org.apache.tapestry.Tapestry;
 30   
 import org.apache.tapestry.engine.EngineServiceLink;
 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 PropertyPersistenceStrategySource _persistenceStrategySource;
 70   
 
 71  50
     public void initializeService()
 72   
     {
 73  50
         Orderer orderer = new Orderer(_errorLog, "encoder");
 74   
 
 75  50
         Iterator i = _contributions.iterator();
 76   
 
 77  50
         while (i.hasNext())
 78   
         {
 79  4
             ServiceEncoderContribution c = (ServiceEncoderContribution) i.next();
 80   
 
 81  4
             orderer.add(c, c.getId(), c.getAfter(), c.getBefore());
 82   
         }
 83   
 
 84  50
         List ordered = orderer.getOrderedObjects();
 85  50
         int count = ordered.size();
 86   
 
 87  50
         _encoders = new ServiceEncoder[count];
 88   
 
 89  50
         for (int j = 0; j < count; j++)
 90   
         {
 91  4
             ServiceEncoderContribution c = (ServiceEncoderContribution) ordered.get(j);
 92   
 
 93  4
             _encoders[j] = c.getEncoder();
 94   
         }
 95   
 
 96   
     }
 97   
 
 98  223
     public ILink constructLink(IRequestCycle cycle, Map parameters, boolean stateful)
 99   
     {
 100  223
         Defense.notNull(cycle, "cycle");
 101  223
         Defense.notNull(parameters, "parameters");
 102   
 
 103  223
         squeezeServiceParameters(parameters);
 104   
 
 105  223
         IEngine engine = cycle.getEngine();
 106   
 
 107  223
         ServiceEncoding serviceEncoding = createServiceEncoding(parameters);
 108   
 
 109   
         // Give persistent property strategies a chance to store extra data
 110   
         // into the link.
 111   
 
 112  223
         if (stateful)
 113  161
             _persistenceStrategySource.addParametersForPersistentProperties(serviceEncoding, cycle);
 114   
 
 115  223
         String fullServletPath = _contextPath + serviceEncoding.getServletPath();
 116   
 
 117  223
         return new EngineServiceLink(cycle, fullServletPath, engine.getOutputEncoding(), _codec,
 118   
                 _request, parameters, stateful);
 119   
     }
 120   
 
 121  45
     public ServiceEncoder[] getServiceEncoders()
 122   
     {
 123  45
         return _encoders;
 124   
     }
 125   
 
 126   
     /**
 127   
      * Creates a new service encoding, and allows the encoders to modify it before returning.
 128   
      */
 129   
 
 130  223
     private ServiceEncoding createServiceEncoding(Map parameters)
 131   
     {
 132  223
         ServiceEncodingImpl result = new ServiceEncodingImpl(_servletPath, parameters);
 133   
 
 134  223
         for (int i = 0; i < _encoders.length; i++)
 135   
         {
 136  4
             _encoders[i].encode(result);
 137   
 
 138  4
             if (result.isModified())
 139  2
                 break;
 140   
         }
 141   
 
 142  223
         return result;
 143   
     }
 144   
 
 145  223
     protected void squeezeServiceParameters(Map parameters)
 146   
     {
 147  223
         Object[] serviceParameters = (Object[]) parameters.get(ServiceConstants.PARAMETER);
 148   
 
 149  223
         if (serviceParameters == null)
 150  211
             return;
 151   
 
 152  12
         String[] squeezed = squeeze(serviceParameters);
 153   
 
 154  12
         parameters.put(ServiceConstants.PARAMETER, squeezed);
 155   
     }
 156   
 
 157  56
     public Object[] extractListenerParameters(IRequestCycle cycle)
 158   
     {
 159  56
         String[] squeezed = cycle.getParameters(ServiceConstants.PARAMETER);
 160   
 
 161  56
         if (Tapestry.size(squeezed) == 0)
 162  37
             return EMPTY;
 163   
 
 164  19
         try
 165   
         {
 166  19
             return _dataSqueezer.unsqueeze(squeezed);
 167   
         }
 168   
         catch (IOException ex)
 169   
         {
 170  0
             throw new ApplicationRuntimeException(ex);
 171   
         }
 172   
     }
 173   
 
 174  12
     private String[] squeeze(Object[] input)
 175   
     {
 176  12
         try
 177   
         {
 178  12
             return _dataSqueezer.squeeze(input);
 179   
         }
 180   
         catch (IOException ex)
 181   
         {
 182  0
             throw new ApplicationRuntimeException(ex);
 183   
         }
 184   
     }
 185   
 
 186  46
     public void setDataSqueezer(DataSqueezer dataSqueezer)
 187   
     {
 188  46
         _dataSqueezer = dataSqueezer;
 189   
     }
 190   
 
 191  50
     public void setContributions(List contributions)
 192   
     {
 193  50
         _contributions = contributions;
 194   
     }
 195   
 
 196  50
     public void setErrorLog(ErrorLog errorLog)
 197   
     {
 198  50
         _errorLog = errorLog;
 199   
     }
 200   
 
 201  50
     public void setServletPath(String servletPath)
 202   
     {
 203  50
         _servletPath = servletPath;
 204   
     }
 205   
 
 206  50
     public void setContextPath(String contextPath)
 207   
     {
 208  50
         _contextPath = contextPath;
 209   
     }
 210   
 
 211  50
     public void setRequest(WebRequest request)
 212   
     {
 213  50
         _request = request;
 214   
     }
 215   
 
 216   
     /**
 217   
      * This is kind of limiting; it's possible that other things beyond persistence strategies will
 218   
      * want to have a hand at encoding data into URLs. If that comes to pass, we'll need to
 219   
      * implement an event coordinator/listener combo to let implementations know about links being
 220   
      * generated.
 221   
      */
 222   
 
 223  46
     public void setPersistenceStrategySource(
 224   
             PropertyPersistenceStrategySource persistenceStrategySource)
 225   
     {
 226  46
         _persistenceStrategySource = persistenceStrategySource;
 227   
     }
 228   
 }