|
20 | 20 | import java.io.IOException;
|
21 | 21 | import java.io.InputStream;
|
22 | 22 | import java.lang.reflect.Field;
|
| 23 | +import java.lang.reflect.Method; |
23 | 24 | import java.sql.Driver;
|
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.Iterator; |
| 28 | +import java.util.List; |
| 29 | +import org.apache.commons.cli.CommandLine; |
| 30 | +import org.apache.commons.cli.Options; |
| 31 | +import org.apache.commons.cli.ParseException; |
24 | 32 |
|
25 | 33 | public class KyuubiBeeLine extends BeeLine {
|
26 | 34 | public static final String KYUUBI_BEELINE_DEFAULT_JDBC_DRIVER =
|
@@ -97,4 +105,97 @@ String getApplicationTitle() {
|
97 | 105 | "Apache Kyuubi (Incubating)",
|
98 | 106 | });
|
99 | 107 | }
|
| 108 | + |
| 109 | + @Override |
| 110 | + int initArgs(String[] args) { |
| 111 | + List<String> commands = Collections.emptyList(); |
| 112 | + |
| 113 | + CommandLine cl; |
| 114 | + BeelineParser beelineParser; |
| 115 | + boolean connSuccessful; |
| 116 | + boolean exit; |
| 117 | + Field exitField; |
| 118 | + |
| 119 | + try { |
| 120 | + Field optionsField = BeeLine.class.getDeclaredField("options"); |
| 121 | + optionsField.setAccessible(true); |
| 122 | + Options options = (Options) optionsField.get(this); |
| 123 | + |
| 124 | + beelineParser = new BeelineParser(); |
| 125 | + cl = beelineParser.parse(options, args); |
| 126 | + |
| 127 | + Method connectUsingArgsMethod = |
| 128 | + BeeLine.class.getDeclaredMethod( |
| 129 | + "connectUsingArgs", BeelineParser.class, CommandLine.class); |
| 130 | + connectUsingArgsMethod.setAccessible(true); |
| 131 | + connSuccessful = (boolean) connectUsingArgsMethod.invoke(this, beelineParser, cl); |
| 132 | + |
| 133 | + exitField = BeeLine.class.getDeclaredField("exit"); |
| 134 | + exitField.setAccessible(true); |
| 135 | + exit = (boolean) exitField.get(this); |
| 136 | + |
| 137 | + } catch (ParseException e1) { |
| 138 | + output(e1.getMessage()); |
| 139 | + usage(); |
| 140 | + return -1; |
| 141 | + } catch (Exception t) { |
| 142 | + error(t.getMessage()); |
| 143 | + return 1; |
| 144 | + } |
| 145 | + |
| 146 | + // checks if default hs2 connection configuration file is present |
| 147 | + // and uses it to connect if found |
| 148 | + // no-op if the file is not present |
| 149 | + if (!connSuccessful && !exit) { |
| 150 | + try { |
| 151 | + Method defaultBeelineConnectMethod = |
| 152 | + BeeLine.class.getDeclaredMethod("defaultBeelineConnect"); |
| 153 | + defaultBeelineConnectMethod.setAccessible(true); |
| 154 | + connSuccessful = (boolean) defaultBeelineConnectMethod.invoke(this); |
| 155 | + |
| 156 | + } catch (Exception t) { |
| 157 | + error(t.getMessage()); |
| 158 | + return 1; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + int code = 0; |
| 163 | + if (cl.getOptionValues('e') != null) { |
| 164 | + commands = Arrays.asList(cl.getOptionValues('e')); |
| 165 | + try { |
| 166 | + Field optsField = BeeLine.class.getDeclaredField("opts"); |
| 167 | + optsField.setAccessible(true); |
| 168 | + BeeLineOpts opts = (BeeLineOpts) optsField.get(this); |
| 169 | + opts.setAllowMultiLineCommand(false); |
| 170 | + } catch (Exception t) { |
| 171 | + error(t.getMessage()); |
| 172 | + return 1; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + if (!commands.isEmpty() && getOpts().getScriptFile() != null) { |
| 177 | + error("The '-e' and '-f' options cannot be specified simultaneously"); |
| 178 | + return 1; |
| 179 | + } else if (!commands.isEmpty() && !connSuccessful) { |
| 180 | + error("Cannot run commands specified using -e. No current connection"); |
| 181 | + return 1; |
| 182 | + } |
| 183 | + if (!commands.isEmpty()) { |
| 184 | + for (Iterator<String> i = commands.iterator(); i.hasNext(); ) { |
| 185 | + String command = i.next().toString(); |
| 186 | + debug(loc("executing-command", command)); |
| 187 | + if (!dispatch(command)) { |
| 188 | + code++; |
| 189 | + } |
| 190 | + } |
| 191 | + try { |
| 192 | + exit = true; |
| 193 | + exitField.set(this, exit); |
| 194 | + } catch (Exception e) { |
| 195 | + error(e.getMessage()); |
| 196 | + return 1; |
| 197 | + } |
| 198 | + } |
| 199 | + return code; |
| 200 | + } |
100 | 201 | }
|
0 commit comments