1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
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 |
| |
43 |
| |
44 |
| |
45 |
| public class LinkFactoryImpl implements LinkFactory |
46 |
| { |
47 |
| private DataSqueezer _dataSqueezer; |
48 |
| |
49 |
| private ErrorLog _errorLog; |
50 |
| |
51 |
| |
52 |
| |
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 |
46
| public void initializeService()
|
72 |
| { |
73 |
46
| Orderer orderer = new Orderer(_errorLog, "encoder");
|
74 |
| |
75 |
46
| Iterator i = _contributions.iterator();
|
76 |
| |
77 |
46
| 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 |
46
| List ordered = orderer.getOrderedObjects();
|
85 |
46
| int count = ordered.size();
|
86 |
| |
87 |
46
| _encoders = new ServiceEncoder[count];
|
88 |
| |
89 |
46
| 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 |
222
| public ILink constructLink(IRequestCycle cycle, Map parameters, boolean stateful)
|
99 |
| { |
100 |
222
| Defense.notNull(cycle, "cycle");
|
101 |
222
| Defense.notNull(parameters, "parameters");
|
102 |
| |
103 |
222
| squeezeServiceParameters(parameters);
|
104 |
| |
105 |
222
| IEngine engine = cycle.getEngine();
|
106 |
| |
107 |
222
| ServiceEncoding serviceEncoding = createServiceEncoding(parameters);
|
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
222
| if (stateful)
|
113 |
141
| _persistenceStrategySource.addParametersForPersistentProperties(serviceEncoding, cycle);
|
114 |
| |
115 |
222
| String fullServletPath = _contextPath + serviceEncoding.getServletPath();
|
116 |
| |
117 |
222
| return new EngineServiceLink(cycle, fullServletPath, engine.getOutputEncoding(), _codec,
|
118 |
| _request, parameters, stateful); |
119 |
| } |
120 |
| |
121 |
41
| public ServiceEncoder[] getServiceEncoders()
|
122 |
| { |
123 |
41
| return _encoders;
|
124 |
| } |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| |
130 |
222
| private ServiceEncoding createServiceEncoding(Map parameters)
|
131 |
| { |
132 |
222
| ServiceEncodingImpl result = new ServiceEncodingImpl(_servletPath, parameters);
|
133 |
| |
134 |
222
| 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 |
222
| return result;
|
143 |
| } |
144 |
| |
145 |
222
| protected void squeezeServiceParameters(Map parameters)
|
146 |
| { |
147 |
222
| Object[] serviceParameters = (Object[]) parameters.get(ServiceConstants.PARAMETER);
|
148 |
| |
149 |
222
| if (serviceParameters == null)
|
150 |
210
| return;
|
151 |
| |
152 |
12
| String[] squeezed = squeeze(serviceParameters);
|
153 |
| |
154 |
12
| parameters.put(ServiceConstants.PARAMETER, squeezed);
|
155 |
| } |
156 |
| |
157 |
46
| public Object[] extractListenerParameters(IRequestCycle cycle)
|
158 |
| { |
159 |
46
| String[] squeezed = cycle.getParameters(ServiceConstants.PARAMETER);
|
160 |
| |
161 |
46
| if (Tapestry.size(squeezed) == 0)
|
162 |
27
| 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 |
42
| public void setDataSqueezer(DataSqueezer dataSqueezer)
|
187 |
| { |
188 |
42
| _dataSqueezer = dataSqueezer;
|
189 |
| } |
190 |
| |
191 |
46
| public void setContributions(List contributions)
|
192 |
| { |
193 |
46
| _contributions = contributions;
|
194 |
| } |
195 |
| |
196 |
46
| public void setErrorLog(ErrorLog errorLog)
|
197 |
| { |
198 |
46
| _errorLog = errorLog;
|
199 |
| } |
200 |
| |
201 |
46
| public void setServletPath(String servletPath)
|
202 |
| { |
203 |
46
| _servletPath = servletPath;
|
204 |
| } |
205 |
| |
206 |
46
| public void setContextPath(String contextPath)
|
207 |
| { |
208 |
46
| _contextPath = contextPath;
|
209 |
| } |
210 |
| |
211 |
46
| public void setRequest(WebRequest request)
|
212 |
| { |
213 |
46
| _request = request;
|
214 |
| } |
215 |
| |
216 |
| |
217 |
| |
218 |
| |
219 |
| |
220 |
| |
221 |
| |
222 |
| |
223 |
42
| public void setPersistenceStrategySource(
|
224 |
| PropertyPersistenceStrategySource persistenceStrategySource) |
225 |
| { |
226 |
42
| _persistenceStrategySource = persistenceStrategySource;
|
227 |
| } |
228 |
| } |