comparison src/main/java/org/monetdb/client/JMonetDB.java @ 391:f523727db392

Moved Java classes from packages starting with nl.cwi.monetdb.* to package org.monetdb.* This naming complies to the Java Package Naming convention as MonetDB's main website is www.monetdb.org.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 12 Nov 2020 22:02:01 +0100 (2020-11-12)
parents src/main/java/nl/cwi/monetdb/client/JMonetDB.java@34ce6e1b1be8
children bf9f6b6ecf40
comparison
equal deleted inserted replaced
390:6199e0be3c6e 391:f523727db392
1 /*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2020 MonetDB B.V.
7 */
8
9 package org.monetdb.client;
10
11 import org.monetdb.merovingian.Control;
12 import org.monetdb.merovingian.SabaothDB;
13
14 import org.monetdb.util.CmdLineOpts;
15 import org.monetdb.util.OptionsException;
16
17 import java.io.PrintWriter;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 /**
22 * This program mimics the monetdb tool. It is meant as demonstration
23 * and test of the MeroControl library.
24 *
25 * @author Fabian Groffen
26 * @version 1.0
27 */
28
29 public final class JMonetDB {
30 private static PrintWriter out;
31
32 public final static void main(String[] args) throws Exception {
33 final CmdLineOpts copts = new CmdLineOpts();
34
35 // arguments which take exactly one argument
36 copts.addOption("h", "host", CmdLineOpts.CAR_ONE, "localhost",
37 "The hostname of the host that runs the MonetDB server. " +
38 "A port number can be supplied by use of a colon, i.e. " +
39 "-h somehost:12345.");
40 copts.addOption("p", "port", CmdLineOpts.CAR_ONE, "50000",
41 "The port number to connect to.");
42 copts.addOption("P", "passphrase", CmdLineOpts.CAR_ONE, null,
43 "The passphrase to tell the MonetDB server");
44 copts.addOption("c", "command", CmdLineOpts.CAR_ONE_MANY, null,
45 "The command to execute on the MonetDB server");
46
47 // arguments which have no argument(s)
48 copts.addOption(null, "help", CmdLineOpts.CAR_ZERO, null,
49 "This help screen.");
50
51 // extended options
52 copts.addOption(null, "Xhash", CmdLineOpts.CAR_ONE, null,
53 "Use the given hash algorithm during challenge response. " +
54 "Supported algorithm names: SHA512, SHA384, SHA256 and SHA1.");
55 // arguments which can have zero or one argument(s)
56 copts.addOption(null, "Xdebug", CmdLineOpts.CAR_ONE, null,
57 "Writes a transmission log to disk for debugging purposes. " +
58 "A file name must be given.");
59
60 try {
61 copts.processArgs(args);
62 } catch (OptionsException e) {
63 System.err.println("Error: " + e.getMessage());
64 System.exit(1);
65 }
66
67 if (copts.getOption("help").isPresent()) {
68 System.out.print(
69 "Usage java -jar jmonetdb.jar\n" +
70 " -h host[:port] -p port -P passphrase [-X<opt>] -c cmd ...\n" +
71 "or using long option equivalents --host --port --passphrase.\n" +
72 "Arguments may be written directly after the option like -p50000.\n" +
73 "\n" +
74 "If no host and port are given, localhost and 50000 are assumed.\n" +
75 "\n" +
76 "OPTIONS\n" + copts.produceHelpMessage()
77 );
78 System.exit(0);
79 }
80
81 out = new PrintWriter(new java.io.BufferedWriter(new java.io.OutputStreamWriter(System.out)));
82
83 String pass = copts.getOption("passphrase").getArgument();
84
85 // we need the password from the user, fetch it with a pseudo
86 // password protector
87 if (pass == null) {
88 final char[] tmp = System.console().readPassword("passphrase: ");
89 if (tmp == null) {
90 System.err.println("Invalid passphrase!");
91 System.exit(1);
92 }
93 pass = String.valueOf(tmp);
94 }
95
96 // build the hostname
97 String host = copts.getOption("host").getArgument();
98 String sport = copts.getOption("port").getArgument();
99 int pos;
100 if ((pos = host.indexOf(":")) != -1) {
101 sport = host.substring(pos + 1);
102 host = host.substring(0, pos);
103 }
104 int port = Integer.parseInt(sport);
105
106 String hash = null;
107 if (copts.getOption("Xhash").isPresent())
108 hash = copts.getOption("Xhash").getArgument();
109
110 if (!copts.getOption("command").isPresent()) {
111 System.err.println("need a command to execute (-c)");
112 System.exit(1);
113 }
114
115 Control ctl = null;
116 try {
117 ctl = new Control(host, port, pass);
118 } catch (IllegalArgumentException e) {
119 System.err.println(e.getMessage());
120 System.exit(1);
121 }
122 // FIXME: Control needs to respect Xhash
123
124 if (copts.getOption("Xdebug").isPresent()) {
125 final String fname = copts.getOption("Xdebug").getArgument();
126 ctl.setDebug(fname);
127 }
128
129 final String[] commands = copts.getOption("command").getArguments();
130 if (commands[0].equals("status")) {
131 List<SabaothDB> sdbs;
132 if (commands.length == 1) {
133 sdbs = ctl.getAllStatuses();
134 } else {
135 sdbs = new ArrayList<SabaothDB>();
136 for (int i = 1; i < commands.length; i++)
137 sdbs.add(ctl.getStatus(commands[i]));
138 }
139 final java.util.Iterator<SabaothDB> it = sdbs.iterator();
140 while (it.hasNext()) {
141 SabaothDB sdb = it.next();
142 System.out.println(sdb.getName() + " " + sdb.getURI());
143 }
144 }
145 }
146 }