001: /*
002: * $Id: CompoundRequestCycleProcessor.java 458408 2005-12-30 21:47:06Z jonl $
003: * $Revision: 458408 $ $Date: 2005-12-30 22:47:06 +0100 (Fri, 30 Dec 2005) $
004: *
005: * ==============================================================================
006: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
007: * use this file except in compliance with the License. You may obtain a copy of
008: * the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations under
016: * the License.
017: */
018: package wicket.request.compound;
019:
020: import wicket.request.IRequestCodingStrategy;
021:
022: /**
023: * Default implementation of {@link
024: * wicket.request.compound.AbstractCompoundRequestCycleProcessor} that expects
025: * the delegate strategies to be set once at construction time.
026: * <p>
027: * This class call the appropriate factory methods to lazily create the
028: * strategies if they were not set at construction time. After that the
029: * strategies are cached and used for multiple thread access. Hence, if you use
030: * this class, your strategies need to be thread safe. If this is not what you
031: * want, consider overriding one of the getXXX methods, in which case the newXXX
032: * methods will obviously not be used unless you call them directly.
033: * </p>
034: *
035: * @author Eelco Hillenius
036: */
037: public class CompoundRequestCycleProcessor extends
038: AbstractCompoundRequestCycleProcessor {
039: /** the cached strategy for the event processor method. */
040: private IEventProcessorStrategy eventProcessorStrategy;
041:
042: /** the cached strategy for the exception response method. */
043: private IExceptionResponseStrategy exceptionResponseStrategy;
044:
045: /** the cached strategy for constructing request parameters. */
046: private IRequestCodingStrategy requestCodingStrategy;
047:
048: /** the cached strategy for the target resolver method. */
049: private IRequestTargetResolverStrategy requestTargetResolverStrategy;
050:
051: /** the cached strategy for the response method. */
052: private IResponseStrategy responseStrategy;
053:
054: /**
055: * Default constructor. If you use this constructor, please note that it
056: * will fall back on calling the factory methods (newXXX), which may or may
057: * not provide a default. Some implementations need to be provided still; if
058: * they are not, an exception will be thrown.
059: */
060: public CompoundRequestCycleProcessor() {
061: }
062:
063: /**
064: * Constructor with the only strategy we don't have a default for.
065: *
066: * @param requestCodingStrategy
067: * the strategy for constructing request parameters
068: */
069: public CompoundRequestCycleProcessor(
070: IRequestCodingStrategy requestCodingStrategy) {
071: this .requestCodingStrategy = requestCodingStrategy;
072: }
073:
074: /**
075: * Bulk constructor. Constructs using the given strategies; all of which may
076: * be null in which case the factory methods will be used.
077: *
078: * @param requestCodingStrategy
079: * the strategy for constructing request parameters
080: * @param requestTargetResolverStrategy
081: * the strategy for the target resolver method
082: * @param eventProcessorStrategy
083: * the strategy for the event processor method
084: * @param responseStrategy
085: * the strategy for the response method
086: * @param exceptionResponseStrategy
087: * the strategy for the exception response method
088: */
089: public CompoundRequestCycleProcessor(
090: IRequestCodingStrategy requestCodingStrategy,
091: IRequestTargetResolverStrategy requestTargetResolverStrategy,
092: IEventProcessorStrategy eventProcessorStrategy,
093: IResponseStrategy responseStrategy,
094: IExceptionResponseStrategy exceptionResponseStrategy) {
095: this .requestCodingStrategy = requestCodingStrategy;
096: this .requestTargetResolverStrategy = requestTargetResolverStrategy;
097: this .eventProcessorStrategy = eventProcessorStrategy;
098: this .responseStrategy = responseStrategy;
099: this .exceptionResponseStrategy = exceptionResponseStrategy;
100: }
101:
102: /**
103: * Gets the cached request encoder instance or create one by calling
104: * {@link #newRequestCodingStrategy()}.
105: *
106: * @see wicket.request.IRequestCycleProcessor#getRequestCodingStrategy()
107: */
108: public IRequestCodingStrategy getRequestCodingStrategy() {
109: // lazily create
110: if (requestCodingStrategy == null) {
111: requestCodingStrategy = newRequestCodingStrategy();
112: }
113:
114: // still null?
115: if (requestCodingStrategy == null) {
116: throw new IllegalStateException(
117: "No requestCodingStrategy set");
118: }
119:
120: // return the strategy
121: return requestCodingStrategy;
122: }
123:
124: /**
125: * Gets the cached event processor instance or create one by calling
126: * {@link #newEventProcessorStrategy()}.
127: *
128: * @see wicket.request.compound.AbstractCompoundRequestCycleProcessor#getEventProcessorStrategy()
129: */
130: protected IEventProcessorStrategy getEventProcessorStrategy() {
131: // lazily create
132: if (eventProcessorStrategy == null) {
133: eventProcessorStrategy = newEventProcessorStrategy();
134: }
135:
136: // still null?
137: if (eventProcessorStrategy == null) {
138: throw new IllegalStateException(
139: "no eventProcessorStrategy set");
140: }
141:
142: // return the strategy
143: return eventProcessorStrategy;
144: }
145:
146: /**
147: * Gets the cached exception response strategy instance or create one by
148: * calling {@link #newExceptionResponseStrategy()}.
149: *
150: * @see wicket.request.compound.AbstractCompoundRequestCycleProcessor#getExceptionResponseStrategy()
151: */
152: protected IExceptionResponseStrategy getExceptionResponseStrategy() {
153: // lazily create
154: if (exceptionResponseStrategy == null) {
155: exceptionResponseStrategy = newExceptionResponseStrategy();
156: }
157:
158: // still null?
159: if (exceptionResponseStrategy == null) {
160: throw new IllegalStateException(
161: "no exceptionResponseStrategy set");
162: }
163:
164: // return the strategy
165: return exceptionResponseStrategy;
166: }
167:
168: /**
169: * Gets the cached target resolver instance or create one by calling
170: * {@link #newRequestTargetResolverStrategy()}.
171: *
172: * @see wicket.request.compound.AbstractCompoundRequestCycleProcessor#getRequestTargetResolverStrategy()
173: */
174: protected IRequestTargetResolverStrategy getRequestTargetResolverStrategy() {
175: // lazily create
176: if (requestTargetResolverStrategy == null) {
177: requestTargetResolverStrategy = newRequestTargetResolverStrategy();
178: }
179:
180: // still null?
181: if (requestTargetResolverStrategy == null) {
182: throw new IllegalStateException(
183: "no requestTargetResolverStrategy set");
184: }
185:
186: // return the strategy
187: return requestTargetResolverStrategy;
188: }
189:
190: /**
191: * Gets the cached response strategy instance or create one by calling
192: * {@link #newResponseStrategy()}.
193: *
194: * @see wicket.request.compound.AbstractCompoundRequestCycleProcessor#getResponseStrategy()
195: */
196: protected IResponseStrategy getResponseStrategy() {
197: // lazily create
198: if (responseStrategy == null) {
199: responseStrategy = newResponseStrategy();
200: }
201:
202: // still null?
203: if (responseStrategy == null) {
204: throw new IllegalStateException("no responseStrategy set");
205: }
206:
207: // return the strategy
208: return responseStrategy;
209: }
210:
211: /**
212: * Overridable factory method for creating the event processor. Called by
213: * {@link #getEventProcessorStrategy()}.
214: *
215: * @return a new event processor instance
216: */
217: protected IEventProcessorStrategy newEventProcessorStrategy() {
218: return new DefaultEventProcessorStrategy();
219: }
220:
221: /**
222: * Overridable factory method for creating the exception response strategy.
223: * Called by {@link #getExceptionResponseStrategy()}.
224: *
225: * @return a new response strategy instance
226: */
227: protected IExceptionResponseStrategy newExceptionResponseStrategy() {
228: return new DefaultExceptionResponseStrategy();
229: }
230:
231: /**
232: * Overridable factory method for creating the request encoder. Called by
233: * {@link #getRequestCodingStrategy()}. <strong>as there is no generic default for
234: * the request encoder, this method throws an exception by default. You
235: * either have to provide an instance as a constructor argument, or override
236: * this method or {@link #getRequestCodingStrategy()}</strong>.
237: *
238: * @return a new target resolver instance
239: */
240: protected IRequestCodingStrategy newRequestCodingStrategy() {
241: throw new IllegalStateException(
242: "there is no default for the request encoder. Please provide your strategy by either "
243: + "providing it as a constructor argument, or by overriding newRequestCodingStrategy "
244: + "or getRequestCodingStrategy");
245: }
246:
247: /**
248: * Overridable factory method for creating the target resolver strategy.
249: * Called by {@link #getRequestTargetResolverStrategy()}.
250: *
251: * @return a new target resolver instance
252: */
253: protected IRequestTargetResolverStrategy newRequestTargetResolverStrategy() {
254: return new DefaultRequestTargetResolverStrategy();
255: }
256:
257: /**
258: * Overridable factory method for creating the response strategy. Called by
259: * {@link #getResponseStrategy()}.
260: *
261: * @return a new response strategy instance
262: */
263: protected IResponseStrategy newResponseStrategy() {
264: return new DefaultResponseStrategy();
265: }
266: }
|