1 /*
2 Copyright (c) 2003, Laurent Caillette.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modifica-
6 tion, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10
11 2. Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
14
15 3. The end-user documentation included with the redistribution, if any, must
16 include the following acknowledgment: "This product includes software
17 written by Laurent Caillette."
18 Alternately, this acknowledgment may appear in the software itself, if
19 and wherever such third-party acknowledgments normally appear.
20
21 4. The name "Laurent Caillette" must not be used to endorse or
22 promote products derived from this software without
23 prior written permission. For written permission, please contact
24 laurent.caillette@laposte.net
25
26 5. Products derived from this software may not be called
27 "Laurent Caillette", nor may "Laurent Caillette" appear
28 in their name, without prior written permission of the
29 author.
30
31 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
32 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
33 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
34 AUTHOR (LAURENT CAILLETTE) BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
36 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
37 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
42 package tweed.context;
43 import org.apache.avalon.framework.logger.AbstractLogEnabled;
44 import org.apache.avalon.framework.logger.Logger;
45 import tweed.action.ActionPerformer;
46 import tweed.binding.InputValidationHub;
47 import tweed.invocation.CommandInvoker;
48 import tweed.system.Instantiator;
49 import tweed.system.bus.EventBus;
50
51 /***
52 * Creates {@link Context} objects.
53 *
54 * @see ContextPatcher for modifying already-instantiated <code>Context</code>
55 * objects.
56 * @author Laurent Caillette
57 * @version $Id$
58 */
59 public class DefaultContextFactory extends AbstractLogEnabled {
60
61 // ==================
62 // Some configurators
63 // ==================
64
65 private boolean serverSide = false ;
66
67 public void setServerSide( boolean serverSide ) {
68 this.serverSide = serverSide ;
69 }
70
71 private final static String CONTEXTLOGGERCHILD_DEFAULTNAME = "context" ;
72
73 public void enableLogging( Logger logger ) {
74 if( logger == null ) {
75 throw new IllegalArgumentException( "logger == null" ) ;
76 }
77 super.enableLogging( logger ) ;
78 if( contextLogger == null ) {
79 contextLogger = logger.getChildLogger( CONTEXTLOGGERCHILD_DEFAULTNAME ) ;
80 }
81 }
82
83 private Logger contextLogger = null ;
84
85 public void setContextLogger( Logger contextLogger ) {
86 if( contextLogger == null ) {
87 throw new IllegalArgumentException( "contextLogger == null" ) ;
88 }
89 this.contextLogger = contextLogger;
90 }
91
92 private Instantiator commandInvokerInstantiator = new Instantiator(
93 CommandInvoker.class,
94 new Class[] { Context.class }
95 ) ;
96
97 public void setCommandInvokerClass( Class commandInvokerClass ) {
98 if( commandInvokerClass == null ) {
99 throw new IllegalArgumentException( "commandInvokerClass == null" ) ;
100 }
101 commandInvokerInstantiator.setConcreteClassQuiet( commandInvokerClass ) ;
102 }
103
104 private DefaultContext context = null ;
105
106 // ==================
107 // The factory method
108 // ==================
109
110 public Context createContext() {
111
112 if( contextLogger == null ) {
113 String msg = "Logger was not set" ;
114 getLogger().error( msg ); ;
115 throw new IllegalStateException( msg ) ;
116 }
117
118 context = instantiateContext() ;
119
120 context.setLogger( contextLogger ) ;
121 context.setServerSide( serverSide ) ;
122
123 if( serverSide ) {
124 // TODO: fail if client-side only members were set
125 } else {
126 configureClientSideContext( context ) ;
127 }
128
129 getLogger().info(
130 "Created and configured an instance of " +
131 context.getClass().getName()
132 ) ;
133
134 return context ;
135 }
136
137 protected DefaultContext instantiateContext() {
138 return new DefaultContext() ;
139 }
140
141 private void configureClientSideContext( DefaultContext context ) {
142 CommandInvoker invoker = instantiateCommandInvoker( context ) ;
143 context.setInvoker( invoker ) ;
144 context.initializeClientSide() ;
145 context.setActionPerformer( new ActionPerformer( context ) ) ;
146 context.setValidationHub( new InputValidationHub( context ) ) ;
147 }
148
149 protected CommandInvoker instantiateCommandInvoker( Context context ) {
150 CommandInvoker invoker = ( CommandInvoker )
151 commandInvokerInstantiator.instantiateQuiet( context ) ;
152 return invoker ;
153 }
154
155
156 // ====================
157 // The concrete Context
158 // ====================
159
160 /***
161 * A mutable <code>Context</code> which should not be instantiated
162 * from outside the enclosing class or its derivates.
163 */
164 protected class DefaultContext implements Context {
165
166 private CommandInvoker invoker ;
167 private EventBus eventBus ;
168
169 public Context getContext() {
170 return this ;
171 }
172
173 public CommandInvoker getInvoker() {
174 return invoker ;
175 }
176
177 public void setInvoker( CommandInvoker invoker ) {
178 this.invoker = invoker ;
179 }
180
181 private boolean serverSide = false ;
182
183 public boolean isClientSide() {
184 return ! serverSide ;
185 }
186
187 public boolean isServerSide() {
188 return serverSide ;
189 }
190
191 public void setServerSide( boolean serverSide ) {
192 this.serverSide = serverSide ;
193 }
194
195 private Logger logger = null ;
196
197 public Logger getLogger() {
198 return logger ;
199 }
200
201 public void setLogger( Logger logger ) {
202 this.logger = logger ;
203 }
204
205 private InputValidationHub validationHub = null ;
206
207 public InputValidationHub getInputValidationHub() {
208 return validationHub;
209 }
210
211 public void setValidationHub( InputValidationHub validationHub ) {
212 this.validationHub = validationHub;
213 }
214
215 public EventBus getBus() {
216 return eventBus ;
217 }
218
219 private ActionPerformer actionPerformer ;
220
221 public ActionPerformer getActionPerformer() {
222 return actionPerformer;
223 }
224
225 public void setActionPerformer( ActionPerformer actionPerformer ) {
226 this.actionPerformer = actionPerformer;
227 }
228
229 protected void initializeClientSide() {
230 eventBus = new EventBus( this ) ;
231
232 }
233
234
235 }
236
237 }
This page was automatically generated by Maven