Clover coverage report - Code Coverage for tapestry release 3.1-alpha-1
Coverage timestamp: Mon Feb 21 2005 09:16:14 EST
file stats: LOC: 198   Methods: 12
NCLOC: 128   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.5% 100% 97.1%
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.services.DataSqueezer;
 36   
 import org.apache.tapestry.services.LinkFactory;
 37   
 import org.apache.tapestry.services.ServiceConstants;
 38   
 
 39   
 /**
 40   
  * @author Howard M. Lewis Ship
 41   
  * @since 3.1
 42   
  */
 43   
 public class LinkFactoryImpl implements LinkFactory
 44   
 {
 45   
     private DataSqueezer _dataSqueezer;
 46   
 
 47   
     private ErrorLog _errorLog;
 48   
 
 49   
     /**
 50   
      * List of {@link org.apache.tapestry.services.impl.ServiceEncoderContribution}.
 51   
      */
 52   
 
 53   
     private List _contributions;
 54   
 
 55   
     private ServiceEncoder[] _encoders;
 56   
 
 57   
     private String _contextPath;
 58   
 
 59   
     private String _servletPath;
 60   
 
 61   
     private final Object[] EMPTY = new Object[0];
 62   
 
 63   
     private URLCodec _codec = new URLCodec();
 64   
 
 65  56
     public void initializeService()
 66   
     {
 67  56
         Orderer orderer = new Orderer(_errorLog, "encoder");
 68   
 
 69  56
         Iterator i = _contributions.iterator();
 70   
 
 71  56
         while (i.hasNext())
 72   
         {
 73  4
             ServiceEncoderContribution c = (ServiceEncoderContribution) i.next();
 74   
 
 75  4
             orderer.add(c, c.getId(), c.getAfter(), c.getBefore());
 76   
         }
 77   
 
 78  56
         List ordered = orderer.getOrderedObjects();
 79  56
         int count = ordered.size();
 80   
 
 81  56
         _encoders = new ServiceEncoder[count];
 82   
 
 83  56
         for (int j = 0; j < count; j++)
 84   
         {
 85  4
             ServiceEncoderContribution c = (ServiceEncoderContribution) ordered.get(j);
 86   
 
 87  4
             _encoders[j] = c.getEncoder();
 88   
         }
 89   
 
 90   
     }
 91   
 
 92  279
     public ILink constructLink(IRequestCycle cycle, Map parameters, boolean stateful)
 93   
     {
 94  279
         Defense.notNull(cycle, "cycle");
 95  279
         Defense.notNull(parameters, "parameters");
 96   
 
 97  279
         squeezeServiceParameters(parameters);
 98   
 
 99  279
         IEngine engine = cycle.getEngine();
 100   
 
 101  279
         ServiceEncoding serviceEncoding = createServiceEncoding(parameters);
 102   
 
 103  279
         String fullServletPath = _contextPath + serviceEncoding.getServletPath();
 104   
 
 105  279
         return new EngineServiceLink(cycle, fullServletPath, engine.getOutputEncoding(), _codec,
 106   
                 parameters, stateful);
 107   
     }
 108   
 
 109  52
     public ServiceEncoder[] getServiceEncoders()
 110   
     {
 111  52
         return _encoders;
 112   
     }
 113   
 
 114   
     /**
 115   
      * Creates a new service encoding, and allows the encoders to modify it before returning.
 116   
      */
 117   
 
 118  279
     private ServiceEncoding createServiceEncoding(Map parameters)
 119   
     {
 120  279
         ServiceEncodingImpl result = new ServiceEncodingImpl(_servletPath, parameters);
 121   
 
 122  279
         for (int i = 0; i < _encoders.length; i++)
 123   
         {
 124  4
             _encoders[i].encode(result);
 125   
 
 126  4
             if (result.isModified())
 127  2
                 break;
 128   
         }
 129   
 
 130  279
         return result;
 131   
     }
 132   
 
 133  279
     private void squeezeServiceParameters(Map parameters)
 134   
     {
 135  279
         Object[] serviceParameters = (Object[]) parameters.get(ServiceConstants.PARAMETER);
 136   
 
 137  279
         if (serviceParameters == null)
 138  229
             return;
 139   
 
 140  50
         String[] squeezed = squeeze(serviceParameters);
 141   
 
 142  50
         parameters.put(ServiceConstants.PARAMETER, squeezed);
 143   
     }
 144   
 
 145  67
     public Object[] extractServiceParameters(IRequestCycle cycle)
 146   
     {
 147  67
         String[] squeezed = cycle.getParameters(ServiceConstants.PARAMETER);
 148   
 
 149  67
         if (Tapestry.size(squeezed) == 0)
 150  21
             return EMPTY;
 151   
 
 152  46
         try
 153   
         {
 154  46
             return _dataSqueezer.unsqueeze(squeezed);
 155   
         }
 156   
         catch (IOException ex)
 157   
         {
 158  0
             throw new ApplicationRuntimeException(ex);
 159   
         }
 160   
     }
 161   
 
 162  50
     private String[] squeeze(Object[] input)
 163   
     {
 164  50
         try
 165   
         {
 166  50
             return _dataSqueezer.squeeze(input);
 167   
         }
 168   
         catch (IOException ex)
 169   
         {
 170  0
             throw new ApplicationRuntimeException(ex);
 171   
         }
 172   
     }
 173   
 
 174  53
     public void setDataSqueezer(DataSqueezer dataSqueezer)
 175   
     {
 176  53
         _dataSqueezer = dataSqueezer;
 177   
     }
 178   
 
 179  56
     public void setContributions(List contributions)
 180   
     {
 181  56
         _contributions = contributions;
 182   
     }
 183   
 
 184  56
     public void setErrorLog(ErrorLog errorLog)
 185   
     {
 186  56
         _errorLog = errorLog;
 187   
     }
 188   
 
 189  56
     public void setServletPath(String servletPath)
 190   
     {
 191  56
         _servletPath = servletPath;
 192   
     }
 193   
 
 194  56
     public void setContextPath(String contextPath)
 195   
     {
 196  56
         _contextPath = contextPath;
 197   
     }
 198   
 }