19
19
20
20
import java .io .IOException ;
21
21
import java .io .InputStream ;
22
+ import java .lang .reflect .Field ;
23
+ import java .lang .reflect .Modifier ;
22
24
import java .sql .Driver ;
23
- import java .util .Arrays ;
24
- import java .util .Collections ;
25
- import java .util .List ;
25
+ import java .util .*;
26
26
import org .apache .commons .cli .CommandLine ;
27
27
import org .apache .commons .cli .Options ;
28
28
import org .apache .commons .cli .ParseException ;
@@ -41,6 +41,12 @@ public class KyuubiBeeLine extends BeeLine {
41
41
private static final int ERRNO_ARGS = 1 ;
42
42
private static final int ERRNO_OTHER = 2 ;
43
43
44
+ private static final ResourceBundle beelineResourceBundle =
45
+ ResourceBundle .getBundle (BeeLine .class .getSimpleName ());
46
+ private static final ResourceBundle kyuubiResourceBundle = new KyuubiBeelineResourceBundle ();
47
+ private static final String PYTHON_MODE_PREFIX = "--python-mode" ;
48
+ private boolean pythonMode = false ;
49
+
44
50
public KyuubiBeeLine () {
45
51
this (true );
46
52
}
@@ -50,6 +56,13 @@ public KyuubiBeeLine(boolean isBeeLine) {
50
56
super (isBeeLine );
51
57
try {
52
58
DynFields .builder ().hiddenImpl (BeeLine .class , "commands" ).buildChecked (this ).set (commands );
59
+
60
+ Field resourceBundleField = BeeLine .class .getDeclaredField ("resourceBundle" );
61
+ resourceBundleField .setAccessible (true );
62
+ Field modifiers = Field .class .getDeclaredField ("modifiers" );
63
+ modifiers .setAccessible (true );
64
+ modifiers .setInt (resourceBundleField , resourceBundleField .getModifiers () & ~Modifier .FINAL );
65
+ resourceBundleField .set (null , kyuubiResourceBundle );
53
66
} catch (Throwable t ) {
54
67
throw new ExceptionInInitializerError ("Failed to inject kyuubi commands" );
55
68
}
@@ -64,6 +77,15 @@ public KyuubiBeeLine(boolean isBeeLine) {
64
77
}
65
78
}
66
79
80
+ public boolean isPythonMode () {
81
+ return pythonMode ;
82
+ }
83
+
84
+ // Visible for testing
85
+ public void setPythonMode (boolean pythonMode ) {
86
+ this .pythonMode = pythonMode ;
87
+ }
88
+
67
89
/** Starts the program. */
68
90
public static void main (String [] args ) throws IOException {
69
91
mainWithInputRedirection (args , null );
@@ -125,7 +147,20 @@ int initArgs(String[] args) {
125
147
.<Options >buildStaticChecked ()
126
148
.get ();
127
149
128
- beelineParser = new BeelineParser ();
150
+ beelineParser =
151
+ new BeelineParser () {
152
+ @ Override
153
+ protected void processOption (String arg , ListIterator iter ) throws ParseException {
154
+ if (PYTHON_MODE_PREFIX .equals (arg )) {
155
+ String stripped = arg .substring (2 , arg .length ());
156
+ String [] parts = split (stripped , "=" );
157
+ String value = parts .length >= 2 ? parts [1 ] : "true" ;
158
+ pythonMode = Boolean .parseBoolean (value );
159
+ } else {
160
+ super .processOption (arg , iter );
161
+ }
162
+ }
163
+ };
129
164
cl = beelineParser .parse (options , args );
130
165
131
166
connSuccessful =
@@ -248,4 +283,34 @@ int runInit() {
248
283
}
249
284
return executionResult ;
250
285
}
286
+
287
+ static class KyuubiBeelineResourceBundle extends ListResourceBundle {
288
+ static String CMD_USAGE = "cmd-usage" ;
289
+
290
+ private Object [][] contents = new Object [beelineResourceBundle .keySet ().size ()][];
291
+
292
+ public KyuubiBeelineResourceBundle () {
293
+ int i = 0 ;
294
+ for (String key : beelineResourceBundle .keySet ()) {
295
+ String value = beelineResourceBundle .getString (key );
296
+ if (key .equals (CMD_USAGE )) {
297
+ StringBuilder stringBuilder = new StringBuilder ();
298
+ stringBuilder .append (value ).append ("\n " );
299
+ stringBuilder
300
+ .append ("Usage: java " + KyuubiBeeLine .class .getCanonicalName ())
301
+ .append ("\n " );
302
+ stringBuilder .append (
303
+ " --python-mode=[true/false] Execute python code/script." );
304
+ value = stringBuilder .toString ();
305
+ }
306
+ contents [i ] = new Object [] {key , value };
307
+ i ++;
308
+ }
309
+ }
310
+
311
+ @ Override
312
+ protected Object [][] getContents () {
313
+ return contents ;
314
+ }
315
+ }
251
316
}
0 commit comments