16 package com.mapd.parser.server;
18 import static java.lang.System.exit;
22 import org.apache.commons.cli.CommandLine;
23 import org.apache.commons.cli.CommandLineParser;
24 import org.apache.commons.cli.DefaultParser;
25 import org.apache.commons.cli.HelpFormatter;
26 import org.apache.commons.cli.Option;
27 import org.apache.commons.cli.Options;
28 import org.apache.commons.cli.ParseException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import java.io.BufferedReader;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.io.StringReader;
36 import java.nio.file.FileSystemNotFoundException;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.Properties;
44 LoggerFactory.getLogger(CalciteServerCaller.class);
45 private CommandLine
cmd = null;
56 Options options =
new Options();
59 Option.builder(
"p").hasArg().desc(
"port number").longOpt(
"port").build();
61 Option ssl_trust_store = Option.builder(
"T")
63 .desc(
"SSL_trust_store")
64 .longOpt(
"trust_store")
67 Option ssl_trust_passwd = Option.builder(
"P")
69 .desc(
"SSL_trust_password")
70 .longOpt(
"trust_store_pw")
74 Option.builder(
"Y").hasArg().desc(
"SSL keystore").longOpt(
"keystore").build();
76 Option ssl_keystore_password = Option.builder(
"Z")
78 .desc(
"SSL keystore password")
79 .longOpt(
"keystore_password")
82 Option dbPort = Option.builder(
"m")
84 .desc(
"HEAVY.AI port number")
88 Option data = Option.builder(
"d")
90 .desc(
"data directory")
95 Option extensions = Option.builder(
"e")
97 .desc(
"extension signatures directory")
98 .longOpt(
"extensions")
101 Option udf_file = Option.builder(
"u")
103 .desc(
"User Defined Functions file path")
107 Option config_file = Option.builder(
"c")
109 .desc(
"Configuration file")
112 options.addOption(port);
113 options.addOption(data);
114 options.addOption(extensions);
115 options.addOption(dbPort);
116 options.addOption(ssl_trust_store);
117 options.addOption(ssl_trust_passwd);
118 options.addOption(ssl_keystore);
119 options.addOption(ssl_keystore_password);
120 options.addOption(udf_file);
121 options.addOption(config_file);
123 CommandLineParser
parser =
new DefaultParser();
126 cmd = parser.parse(options,
args);
127 }
catch (ParseException ex) {
128 HEAVYDBLOGGER.error(ex.getLocalizedMessage());
133 int portNum = Integer.valueOf(cmd.getOptionValue(
"port",
"6279"));
134 int dbPortNum = Integer.valueOf(cmd.getOptionValue(
"db_port",
"6274"));
135 String dataDir = cmd.getOptionValue(
"data",
"data");
136 String extensionsDir = cmd.getOptionValue(
"extensions",
"build/QueryEngine");
137 String trust_store = cmd.getOptionValue(
"trust_store",
"");
138 String trust_store_pw = cmd.getOptionValue(
"trust_store_pw",
"");
139 String key_store = cmd.getOptionValue(
"keystore",
"");
140 String key_store_pw = cmd.getOptionValue(
"keystore_password",
"");
141 String udfName = cmd.getOptionValue(
"udf",
"");
142 String configuration_file = cmd.getOptionValue(
"config",
"");
144 final Path extensionFunctionsAstFile =
145 Paths.get(extensionsDir,
"ExtensionFunctions.ast");
148 Properties p =
new Properties();
150 p.load(getClass().getResourceAsStream(
"/log4j2.properties"));
151 }
catch (IOException ex) {
153 "Could not load log4j property file from resources " + ex.getMessage());
155 p.put(
"log.dir", dataDir);
158 Properties properties = null;
159 if (!configuration_file.isEmpty()) {
160 properties = CalciteServerCaller.readPropertyFile(configuration_file);
162 if (trust_store == null || trust_store.isEmpty()) {
163 client_skT = SockTransportProperties.getUnencryptedClient();
166 if (properties != null && trust_store_pw.isEmpty()) {
169 trust_store_pw = properties.getProperty(
"ssl-trust-password");
170 if (trust_store_pw == null) {
171 HEAVYDBLOGGER.warn(
"Failed to load trust store password from config file ["
172 + configuration_file +
"] for trust store [" + trust_store +
"]");
176 client_skT = SockTransportProperties.getEncryptedClientSpecifiedTrustStore(
177 trust_store, trust_store_pw);
178 }
catch (Exception eX) {
180 "Loading encrypted client SockTransportProperties failed. Error - "
182 throw new RuntimeException(error);
185 if (key_store != null && !key_store.isEmpty()) {
189 if (properties != null && key_store_pw.isEmpty()) {
190 key_store_pw = properties.getProperty(
"ssl-keystore-password");
191 if (key_store_pw == null) {
192 String err =
"Failed to load key store password from config file ["
193 + configuration_file +
"] for key store [" + key_store +
"]";
194 throw new RuntimeException(err);
199 SockTransportProperties.getEncryptedServer(key_store, key_store_pw);
200 }
catch (Exception eX) {
202 "Loading encrypted Server SockTransportProperties failed. Error - "
207 server_skT = SockTransportProperties.getUnecryptedServer();
209 }
catch (Exception ex) {
210 HEAVYDBLOGGER.error(
"Error opening SocketTransport. " + ex.getMessage());
217 if (!udfName.isEmpty()) {
218 udfPath = Paths.get(udfName);
220 }
catch (FileSystemNotFoundException ex1) {
221 HEAVYDBLOGGER.error(
"Could not load udf file " + ex1.getMessage());
227 extensionFunctionsAstFile.toString(),
234 calciteServerWrapper.run();
235 if (calciteServerWrapper.
shutdown()) {
241 }
catch (InterruptedException ex) {
244 }
catch (Exception x) {
250 private void help(Options options) {
252 HelpFormatter formatter =
new HelpFormatter();
253 formatter.printHelp(
"CalciteServerCaller", options);
257 Properties properties =
new Properties();
259 BufferedReader bufferedReader =
new BufferedReader(
new FileReader(fileName));
260 StringBuffer sb =
new StringBuffer();
261 for (String
line = bufferedReader.readLine();
line != null;
262 line = bufferedReader.readLine()) {
263 if (
line.toLowerCase().equals(
"[web]")) {
266 line = line.replaceAll(
"[\",']",
"");
267 sb.append(
line +
"\n");
269 properties.load(
new StringReader(sb.toString()));
270 }
catch (java.io.IOException iE) {
271 HEAVYDBLOGGER.warn(
"Could not load configuration file [" + fileName
272 +
"] to get keystore/truststore password. Error - " + iE.toString());
void help(Options options)
SockTransportProperties server_skT
static final Logger HEAVYDBLOGGER
static void main(String[] args)
SockTransportProperties client_skT
void doWork(String[] args)
static Properties readPropertyFile(String fileName)