1 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 45 public class LinkFactoryImpl implements LinkFactory 46 { 47 private DataSqueezer _dataSqueezer; 48 49 private ErrorLog _errorLog; 50 51 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 public void initializeService() 72 { 73 Orderer orderer = new Orderer(_errorLog, "encoder"); 74 75 Iterator i = _contributions.iterator(); 76 77 while (i.hasNext()) 78 { 79 ServiceEncoderContribution c = (ServiceEncoderContribution) i.next(); 80 81 orderer.add(c, c.getId(), c.getAfter(), c.getBefore()); 82 } 83 84 List ordered = orderer.getOrderedObjects(); 85 int count = ordered.size(); 86 87 _encoders = new ServiceEncoder[count]; 88 89 for (int j = 0; j < count; j++) 90 { 91 ServiceEncoderContribution c = (ServiceEncoderContribution) ordered.get(j); 92 93 _encoders[j] = c.getEncoder(); 94 } 95 96 } 97 98 public ILink constructLink(IRequestCycle cycle, Map parameters, boolean stateful) 99 { 100 Defense.notNull(cycle, "cycle"); 101 Defense.notNull(parameters, "parameters"); 102 103 squeezeServiceParameters(parameters); 104 105 IEngine engine = cycle.getEngine(); 106 107 ServiceEncoding serviceEncoding = createServiceEncoding(parameters); 108 109 112 if (stateful) 113 _persistenceStrategySource.addParametersForPersistentProperties(serviceEncoding, cycle); 114 115 String fullServletPath = _contextPath + serviceEncoding.getServletPath(); 116 117 return new EngineServiceLink(cycle, fullServletPath, engine.getOutputEncoding(), _codec, 118 _request, parameters, stateful); 119 } 120 121 public ServiceEncoder[] getServiceEncoders() 122 { 123 return _encoders; 124 } 125 126 129 130 private ServiceEncoding createServiceEncoding(Map parameters) 131 { 132 ServiceEncodingImpl result = new ServiceEncodingImpl(_servletPath, parameters); 133 134 for (int i = 0; i < _encoders.length; i++) 135 { 136 _encoders[i].encode(result); 137 138 if (result.isModified()) 139 break; 140 } 141 142 return result; 143 } 144 145 protected void squeezeServiceParameters(Map parameters) 146 { 147 Object [] serviceParameters = (Object []) parameters.get(ServiceConstants.PARAMETER); 148 149 if (serviceParameters == null) 150 return; 151 152 String [] squeezed = squeeze(serviceParameters); 153 154 parameters.put(ServiceConstants.PARAMETER, squeezed); 155 } 156 157 public Object [] extractListenerParameters(IRequestCycle cycle) 158 { 159 String [] squeezed = cycle.getParameters(ServiceConstants.PARAMETER); 160 161 if (Tapestry.size(squeezed) == 0) 162 return EMPTY; 163 164 try 165 { 166 return _dataSqueezer.unsqueeze(squeezed); 167 } 168 catch (IOException ex) 169 { 170 throw new ApplicationRuntimeException(ex); 171 } 172 } 173 174 private String [] squeeze(Object [] input) 175 { 176 try 177 { 178 return _dataSqueezer.squeeze(input); 179 } 180 catch (IOException ex) 181 { 182 throw new ApplicationRuntimeException(ex); 183 } 184 } 185 186 public void setDataSqueezer(DataSqueezer dataSqueezer) 187 { 188 _dataSqueezer = dataSqueezer; 189 } 190 191 public void setContributions(List contributions) 192 { 193 _contributions = contributions; 194 } 195 196 public void setErrorLog(ErrorLog errorLog) 197 { 198 _errorLog = errorLog; 199 } 200 201 public void setServletPath(String servletPath) 202 { 203 _servletPath = servletPath; 204 } 205 206 public void setContextPath(String contextPath) 207 { 208 _contextPath = contextPath; 209 } 210 211 public void setRequest(WebRequest request) 212 { 213 _request = request; 214 } 215 216 222 223 public void setPersistenceStrategySource( 224 PropertyPersistenceStrategySource persistenceStrategySource) 225 { 226 _persistenceStrategySource = persistenceStrategySource; 227 } 228 } | Popular Tags |