19
19
20
20
package org .apache .samza .sql .client .cli ;
21
21
22
+ import java .io .PrintWriter ;
23
+ import java .lang .reflect .Constructor ;
24
+ import java .lang .reflect .InvocationTargetException ;
25
+ import java .util .ArrayList ;
26
+ import java .util .Arrays ;
27
+ import java .util .HashMap ;
28
+ import java .util .List ;
29
+ import java .util .Map ;
30
+ import org .apache .samza .sql .client .exceptions .CommandHandlerException ;
31
+ import org .apache .samza .sql .client .interfaces .CommandHandler ;
22
32
import org .apache .samza .sql .client .interfaces .EnvironmentVariableHandler ;
23
33
import org .apache .samza .sql .client .interfaces .EnvironmentVariableSpecs ;
24
- import org .apache .samza .sql .client .interfaces .ExecutorException ;
34
+ import org .apache .samza .sql .client .exceptions .ExecutorException ;
25
35
import org .apache .samza .sql .client .interfaces .SqlExecutor ;
26
- import org .apache .samza .sql .client .util .CliException ;
36
+ import org .apache .samza .sql .client .exceptions .CliException ;
27
37
import org .apache .samza .sql .client .util .CliUtil ;
28
38
import org .apache .samza .sql .client .util .Pair ;
29
39
import org .slf4j .Logger ;
30
40
import org .slf4j .LoggerFactory ;
31
41
32
- import java .io .PrintWriter ;
33
- import java .lang .reflect .Constructor ;
34
- import java .lang .reflect .InvocationTargetException ;
35
- import java .util .HashMap ;
36
- import java .util .List ;
37
- import java .util .Map ;
38
-
39
42
/**
40
43
* CliEnvironment handles "environment variables" that configures the shell behavior.
41
44
*/
42
- class CliEnvironment {
45
+ public class CliEnvironment {
43
46
private EnvironmentVariableHandler shellEnvHandler ;
44
47
private EnvironmentVariableHandler executorEnvHandler ;
45
48
private SqlExecutor executor ;
49
+ private List <CommandHandler > commandHandlers ;
46
50
private Map <String , String > delayedExecutorVars ;
47
51
48
52
// shell.executor is special and is specifically handled by CliEnvironment
@@ -51,6 +55,7 @@ class CliEnvironment {
51
55
52
56
CliEnvironment () {
53
57
shellEnvHandler = new CliShellEnvironmentVariableHandler ();
58
+ commandHandlers = new ArrayList <>();
54
59
}
55
60
56
61
/** Sets the value of an environment variable.
@@ -61,14 +66,23 @@ class CliEnvironment {
61
66
* -1: invalid name
62
67
* -2: invalid value
63
68
*/
64
- int setEnvironmentVariable (String name , String value ) throws ExecutorException {
69
+ public int setEnvironmentVariable (String name , String value ) throws ExecutorException , CommandHandlerException {
70
+ name = name .toLowerCase ();
65
71
if (name .equals (CliConstants .CONFIG_EXECUTOR )) {
66
72
createShellExecutor (value );
67
73
activeExecutorClassName = value ;
68
74
executorEnvHandler = executor .getEnvironmentVariableHandler ();
69
75
return 0 ;
70
76
}
71
77
78
+ if (name .equals (CliConstants .CONFIG_COMMAND_HANDLER )) {
79
+ List <String > commandHandlersNames = Arrays .asList (value .split ("," ));
80
+ for (String commandHandlerName : commandHandlersNames ) {
81
+ createCommandHandler (commandHandlerName .trim ());
82
+ }
83
+ return 0 ;
84
+ }
85
+
72
86
EnvironmentVariableHandler handler = getAppropriateHandler (name );
73
87
if (handler == null ) {
74
88
// Shell doesn't recognize this variable. There's no executor handler yet. Save for future executor
@@ -132,7 +146,7 @@ public void printAll(PrintWriter writer) {
132
146
* Gives CliEnvironment a chance to apply settings, especially during initialization, things like
133
147
* making default values take effect
134
148
*/
135
- public void finishInitialization () {
149
+ public void finishInitialization () throws CliException {
136
150
if (executor == null ) {
137
151
try {
138
152
createShellExecutor (CliConstants .DEFAULT_EXECUTOR_CLASS );
@@ -144,7 +158,7 @@ public void finishInitialization() {
144
158
}
145
159
delayedExecutorVars = null ;
146
160
}
147
- } catch (ExecutorException e ) {
161
+ } catch (ExecutorException | CommandHandlerException e ) {
148
162
// Convert checked exception ExecutorException to an unchecked exception as
149
163
// we have failed to create even the default executor thus not recoverable
150
164
throw new CliException (e );
@@ -173,11 +187,39 @@ public SqlExecutor getExecutor() {
173
187
return executor ;
174
188
}
175
189
190
+ public List <CommandHandler > getCommandHandlers () { return commandHandlers ; }
191
+
176
192
private void createShellExecutor (String executorClassName ) throws ExecutorException {
177
193
try {
178
- Class <?> clazz = Class .forName (executorClassName );
194
+ executor = (SqlExecutor ) createInstance (executorClassName );
195
+ } catch (ClassCastException e ) {
196
+ String errMsg = String .format ("Error trying to cast Object of class %s to SqlExecutor" , executorClassName );
197
+ LOG .error (errMsg );
198
+ throw new ExecutorException (errMsg , e );
199
+ } catch (Exception e ) {
200
+ LOG .error (e .getMessage ());
201
+ throw new ExecutorException (e );
202
+ }
203
+ }
204
+
205
+ private void createCommandHandler (String handlerClassName ) throws CommandHandlerException {
206
+ try {
207
+ commandHandlers .add ((CommandHandler ) createInstance (handlerClassName ));
208
+ } catch (ClassCastException e ) {
209
+ String errMsg = String .format ("Error trying to cast Object of class %s to CommandHandler" , handlerClassName );
210
+ LOG .error (errMsg );
211
+ throw new CommandHandlerException (errMsg , e );
212
+ } catch (Exception e ) {
213
+ LOG .error (e .getMessage ());
214
+ throw new CommandHandlerException (e );
215
+ }
216
+ }
217
+
218
+ private Object createInstance (String className ) throws Exception {
219
+ try {
220
+ Class <?> clazz = Class .forName (className );
179
221
Constructor <?> ctor = clazz .getConstructor ();
180
- executor = ( SqlExecutor ) ctor .newInstance ();
222
+ return ctor .newInstance ();
181
223
} catch (ClassNotFoundException | NoSuchMethodException
182
224
| IllegalAccessException | InstantiationException | InvocationTargetException e ) {
183
225
throw new ExecutorException (e );
0 commit comments