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.apache.log4j.PropertyConfigurator;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import java.io.BufferedReader;
34 import java.io.FileReader;
35 import java.io.IOException;
36 import java.io.StringReader;
37 import java.nio.file.FileSystemNotFoundException;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
40 import java.util.Properties;
45 LoggerFactory.getLogger(CalciteServerCaller.class);
46 private CommandLine
cmd = null;
57 Options options =
new Options();
60 Option.builder(
"p").hasArg().desc(
"port number").longOpt(
"port").build();
62 Option ssl_trust_store = Option.builder(
"T")
64 .desc(
"SSL_trust_store")
65 .longOpt(
"trust_store")
68 Option ssl_trust_passwd = Option.builder(
"P")
70 .desc(
"SSL_trust_password")
71 .longOpt(
"trust_store_pw")
75 Option.builder(
"Y").hasArg().desc(
"SSL keystore").longOpt(
"keystore").build();
77 Option ssl_keystore_password = Option.builder(
"Z")
79 .desc(
"SSL keystore password")
80 .longOpt(
"keystore_password")
83 Option mapdPort = Option.builder(
"m")
85 .desc(
"mapd port number")
89 Option data = Option.builder(
"d")
91 .desc(
"data directory")
96 Option extensions = Option.builder(
"e")
98 .desc(
"extension signatures directory")
99 .longOpt(
"extensions")
102 Option udf_file = Option.builder(
"u")
104 .desc(
"User Defined Functions file path")
108 Option config_file = Option.builder(
"c")
110 .desc(
"Configuration file")
113 options.addOption(port);
114 options.addOption(data);
115 options.addOption(extensions);
116 options.addOption(mapdPort);
117 options.addOption(ssl_trust_store);
118 options.addOption(ssl_trust_passwd);
119 options.addOption(ssl_keystore);
120 options.addOption(ssl_keystore_password);
121 options.addOption(udf_file);
122 options.addOption(config_file);
124 CommandLineParser
parser =
new DefaultParser();
127 cmd = parser.parse(options,
args);
128 }
catch (ParseException ex) {
129 MAPDLOGGER.error(ex.getLocalizedMessage());
134 int portNum = Integer.valueOf(cmd.getOptionValue(
"port",
"6279"));
135 int mapdPortNum = Integer.valueOf(cmd.getOptionValue(
"mapd_port",
"6274"));
136 String dataDir = cmd.getOptionValue(
"data",
"data");
137 String extensionsDir = cmd.getOptionValue(
"extensions",
"build/QueryEngine");
138 String trust_store = cmd.getOptionValue(
"trust_store",
"");
139 String trust_store_pw = cmd.getOptionValue(
"trust_store_pw",
"");
140 String key_store = cmd.getOptionValue(
"keystore",
"");
141 String key_store_pw = cmd.getOptionValue(
"keystore_password",
"");
142 String udfName = cmd.getOptionValue(
"udf",
"");
143 String configuration_file = cmd.getOptionValue(
"config",
"");
145 final Path extensionFunctionsAstFile =
146 Paths.get(extensionsDir,
"ExtensionFunctions.ast");
149 Properties p =
new Properties();
151 p.load(getClass().getResourceAsStream(
"/log4j.properties"));
152 }
catch (IOException ex) {
154 "Could not load log4j property file from resources " + ex.getMessage());
156 p.put(
"log.dir", dataDir);
157 PropertyConfigurator.configure(p);
160 Properties properties = null;
161 if (!configuration_file.isEmpty()) {
162 properties = CalciteServerCaller.readPropertyFile(configuration_file);
164 if (trust_store == null || trust_store.isEmpty()) {
165 client_skT = SockTransportProperties.getUnencryptedClient();
168 if (properties != null && trust_store_pw.isEmpty()) {
171 trust_store_pw = properties.getProperty(
"ssl-trust-password");
172 if (trust_store_pw == null) {
173 MAPDLOGGER.warn(
"Failed to load trust store password from config file ["
174 + configuration_file +
"] for trust store [" + trust_store +
"]");
178 client_skT = SockTransportProperties.getEncryptedClientSpecifiedTrustStore(
179 trust_store, trust_store_pw);
180 }
catch (Exception eX) {
182 "Loading encrypted client SockTransportProperties failed. Error - "
184 throw new RuntimeException(error);
187 if (key_store != null && !key_store.isEmpty()) {
191 if (properties != null && key_store_pw.isEmpty()) {
192 key_store_pw = properties.getProperty(
"ssl-keystore-password");
193 if (key_store_pw == null) {
194 String err =
"Failed to load key store password from config file ["
195 + configuration_file +
"] for key store [" + key_store +
"]";
196 throw new RuntimeException(err);
201 SockTransportProperties.getEncryptedServer(key_store, key_store_pw);
202 }
catch (Exception eX) {
204 "Loading encrypted Server SockTransportProperties failed. Error - "
209 server_skT = SockTransportProperties.getUnecryptedServer();
211 }
catch (Exception ex) {
212 MAPDLOGGER.error(
"Error opening SocketTransport. " + ex.getMessage());
219 if (!udfName.isEmpty()) {
220 udfPath = Paths.get(udfName);
222 }
catch (FileSystemNotFoundException ex1) {
223 MAPDLOGGER.error(
"Could not load udf file " + ex1.getMessage());
229 extensionFunctionsAstFile.toString(),
236 calciteServerWrapper.run();
237 if (calciteServerWrapper.
shutdown()) {
243 }
catch (InterruptedException ex) {
246 }
catch (Exception x) {
252 private void help(Options options) {
254 HelpFormatter formatter =
new HelpFormatter();
255 formatter.printHelp(
"CalciteServerCaller", options);
259 Properties properties =
new Properties();
261 BufferedReader bufferedReader =
new BufferedReader(
new FileReader(fileName));
262 StringBuffer sb =
new StringBuffer();
263 for (String
line = bufferedReader.readLine();
line != null;
264 line = bufferedReader.readLine()) {
265 if (
line.toLowerCase().equals(
"[web]")) {
268 line = line.replaceAll(
"[\",']",
"");
269 sb.append(
line +
"\n");
271 properties.load(
new StringReader(sb.toString()));
272 }
catch (java.io.IOException iE) {
273 MAPDLOGGER.warn(
"Could not load configuration file [" + fileName
274 +
"] to get keystore/truststore password. Error - " + iE.toString());
void help(Options options)
SockTransportProperties server_skT
static final Logger MAPDLOGGER
static void main(String[] args)
SockTransportProperties client_skT
void doWork(String[] args)
static Properties readPropertyFile(String fileName)