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.invocation;
43 import org.apache.avalon.framework.logger.Logger;
44 import tweed.context.Context;
45 import tweed.system.FunctionalException;
46 import tweed.system.Instantiator;
47
48 /***
49 * This class helps the server executing a {@link Command} object with
50 * only its {@link ExternalizedCommand} as input/output parameter.
51 * It is intended only to be used when implementing a connectivity layer
52 * between client and server.
53 * Command implementors should not manipulate this class directly.
54 *
55 * @author Laurent Caillette
56 * @version $Id$
57 */
58 public class CommandExecutor {
59
60 private final Context context ;
61 private final Logger logger ;
62
63 public CommandExecutor( Context context ) {
64 this.context = context ;
65 commandConstructorParameters = new Object[] { context } ;
66 logger = context.getLogger().getChildLogger( getChildLoggerName() ) ;
67
68 getLogger().debug( "Context assigned to a " + getClass().getName() ) ;
69 }
70
71 protected final Context getContext() {
72 return context ;
73 }
74
75 protected final Logger getLogger() {
76 return logger;
77 }
78
79
80
81 private Object[] commandConstructorParameters ;
82 private final static Class[] COMMAND_CONSTRUCTOR_PARAMETERTYPES =
83 new Class[] {
84 Context.class
85 } ;
86 private final Instantiator instantiator = new Instantiator(
87 Command.class,
88 COMMAND_CONSTRUCTOR_PARAMETERTYPES
89 ) ;
90
91 /***
92 * Returns the name of the child logger of Context's logger.
93 * @return the <code>"command-executor"</code> String.
94 */
95 protected String getChildLoggerName() {
96 return "command-executor" ;
97 }
98
99 public void execute( ExternalizedCommand externalizedCommand ) {
100
101 getLogger().debug( "Preparing to execute " + externalizedCommand ) ;
102
103 boolean externalizedCommandHadLogger = (
104 externalizedCommand.getLogger() == null ) ;
105
106 if( ! externalizedCommandHadLogger ) {
107 externalizedCommand.enableLogging( getLogger() ) ;
108 }
109
110 try {
111
112 if( ! getContext().isServerSide() ) {
113 String msg = "Context not server-side" ;
114 getLogger().error( msg ) ;
115 throw new IllegalArgumentException( msg ) ;
116 }
117 Class commandClass ;
118 try {
119 commandClass = Class.forName(
120 externalizedCommand.getServerCommandClassName() ) ;
121 } catch( ClassNotFoundException ex ) {
122 getLogger().error( "Class not found", ex ) ;
123 throw new RuntimeException( ex );
124 }
125 Command command = createCommand( commandClass ) ;
126
127 externalizedCommand.prepareExecutionOnServer( command ) ;
128 getLogger().debug( "Command ready for execution: " + command ) ;
129 try {
130 command.execute() ;
131 } catch( FunctionalException ex ) {
132 externalizedCommand.setException( ex ) ;
133 getLogger().debug(
134 "Functional exception thrown during execution of Command " +
135 command
136 ) ;
137 }
138 externalizedCommand.prepareReturnToClient( command ) ;
139 getLogger().debug( "Success for Command " + command ) ;
140
141 } finally {
142 if( ! externalizedCommandHadLogger ) {
143 externalizedCommand.enableLogging( null ) ;
144 }
145 }
146 }
147
148
149 /***
150 * Creates a new <code>Command</code> instance correctly configured.
151 * The exact behavior of a Command depends if the context is set to
152 * Client-side or Server-side.
153 *
154 * @param commandClass the concrete class of the <code>Command</code>
155 * to instantiate, cannot be null.
156 * @return a non-null <code>Command</code> instance.
157 * @throws RuntimeException (wrapping an instantiation-related exception)
158 * if instantiation failed for some reason.
159 */
160 public Command createCommand( Class commandClass ) {
161
162 RuntimeException exception = null ;
163 if( commandClass == null ) {
164 exception = new NullPointerException( "commandClass") ;
165 } else if( ! Command.class.isAssignableFrom( commandClass ) ) {
166 exception = new IllegalArgumentException(
167 "Cannot create a Command whose concrete class " +
168 "is not a subclass of " + Command.class.getName() +
169 ": " + commandClass.getName()
170 ) ;
171 }
172 if( exception != null ) {
173 getLogger().error( exception.getMessage() ) ;
174 throw exception ;
175 }
176
177 // TODO Cache instantiators for better performances.
178
179 try {
180 instantiator.setConcreteClass( commandClass );
181 } catch( Exception ex ) {
182 getLogger().error(
183 "Could not instantiate command of class " +
184 commandClass.toString()
185 ) ;
186 throw new RuntimeException( ex ) ;
187 }
188
189 Command command = ( Command )
190 instantiator.instantiateQuiet( commandConstructorParameters ) ;
191 command.enableLogging( getLogger().getChildLogger( "command" ) ) ;
192
193 getLogger().debug(
194 "Command of class " + commandClass.getName() +
195 " successfully instantiated: " + command
196 ) ;
197
198 return command ;
199 }
200
201 }
This page was automatically generated by Maven