KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > services > impl > LinkFactoryImpl


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 JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
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 JavaDoc _contributions;
56
57     private ServiceEncoder[] _encoders;
58
59     private String JavaDoc _contextPath;
60
61     private String JavaDoc _servletPath;
62
63     private final Object JavaDoc[] EMPTY = new Object JavaDoc[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 JavaDoc 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 JavaDoc 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 JavaDoc 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         // Give persistent property strategies a chance to store extra data
110
// into the link.
111

112         if (stateful)
113             _persistenceStrategySource.addParametersForPersistentProperties(serviceEncoding, cycle);
114
115         String JavaDoc 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     /**
127      * Creates a new service encoding, and allows the encoders to modify it before returning.
128      */

129
130     private ServiceEncoding createServiceEncoding(Map JavaDoc 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 JavaDoc parameters)
146     {
147         Object JavaDoc[] serviceParameters = (Object JavaDoc[]) parameters.get(ServiceConstants.PARAMETER);
148
149         if (serviceParameters == null)
150             return;
151
152         String JavaDoc[] squeezed = squeeze(serviceParameters);
153
154         parameters.put(ServiceConstants.PARAMETER, squeezed);
155     }
156
157     public Object JavaDoc[] extractListenerParameters(IRequestCycle cycle)
158     {
159         String JavaDoc[] 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 JavaDoc ex)
169         {
170             throw new ApplicationRuntimeException(ex);
171         }
172     }
173
174     private String JavaDoc[] squeeze(Object JavaDoc[] input)
175     {
176         try
177         {
178             return _dataSqueezer.squeeze(input);
179         }
180         catch (IOException JavaDoc 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 JavaDoc contributions)
192     {
193         _contributions = contributions;
194     }
195
196     public void setErrorLog(ErrorLog errorLog)
197     {
198         _errorLog = errorLog;
199     }
200
201     public void setServletPath(String JavaDoc servletPath)
202     {
203         _servletPath = servletPath;
204     }
205
206     public void setContextPath(String JavaDoc contextPath)
207     {
208         _contextPath = contextPath;
209     }
210
211     public void setRequest(WebRequest request)
212     {
213         _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     public void setPersistenceStrategySource(
224             PropertyPersistenceStrategySource persistenceStrategySource)
225     {
226         _persistenceStrategySource = persistenceStrategySource;
227     }
228 }
Popular Tags