view src/main/java/org/monetdb/client/JMonetDB.java @ 937:d416e9b6b3d0

Update Copyright year.
author Martin van Dinther <martin.van.dinther@monetdbsolutions.com>
date Thu, 02 Jan 2025 13:27:58 +0100 (3 months ago)
parents e890195256ac
children
line wrap: on
line source
/*
 * SPDX-License-Identifier: MPL-2.0
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0.  If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Copyright 2024, 2025 MonetDB Foundation;
 * Copyright August 2008 - 2023 MonetDB B.V.;
 * Copyright 1997 - July 2008 CWI.
 */

package org.monetdb.client;

import org.monetdb.merovingian.Control;
import org.monetdb.merovingian.SabaothDB;

import org.monetdb.util.CmdLineOpts;
import org.monetdb.util.OptionsException;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * This program mimics the monetdb tool.  It is meant as demonstration
 * and test of the MeroControl library.
 *
 * @author Fabian Groffen
 * @version 1.0
 */

public final class JMonetDB {
	private static PrintWriter out;

	public final static void main(String[] args) throws Exception {
		final CmdLineOpts copts = new CmdLineOpts();

		// arguments which take exactly one argument
		copts.addOption("h", "host", CmdLineOpts.CAR_ONE, "localhost",
				"The hostname of the host that runs the MonetDB server.  " +
				"A port number can be supplied by use of a colon, i.e. " +
				"-h somehost:12345.");
		copts.addOption("p", "port", CmdLineOpts.CAR_ONE, "50000",
				"The port number to connect to.");
		copts.addOption("P", "passphrase", CmdLineOpts.CAR_ONE, null,
				"The passphrase to tell the MonetDB server");
		copts.addOption("c", "command", CmdLineOpts.CAR_ONE_MANY, null,
				"The command to execute on the MonetDB server");

		// arguments which have no argument(s)
		copts.addOption(null, "help", CmdLineOpts.CAR_ZERO, null,
				"This help screen.");

		// extended options
		copts.addOption(null, "Xhash", CmdLineOpts.CAR_ONE, null,
				"Use the given hash algorithm during challenge response.  " +
				"Supported algorithm names: SHA512, SHA384, SHA256 and SHA1.");
		// arguments which can have zero or one argument(s)
		copts.addOption(null, "Xdebug", CmdLineOpts.CAR_ONE, null,
				"Writes a transmission log to disk for debugging purposes.  " +
				"A file name must be given.");

		try {
			copts.processArgs(args);
		} catch (OptionsException e) {
			System.err.println("Error: " + e.getMessage());
			System.exit(1);
		}

		if (copts.getOption("help").isPresent()) {
			System.out.print(
				"Usage java -jar jmonetdb.jar\n" +
				"                  -h host[:port] -p port -P passphrase [-X<opt>] -c cmd ...\n" +
				"or using long option equivalents --host --port --passphrase.\n" +
				"Arguments may be written directly after the option like -p50000.\n" +
				"\n" +
				"If no host and port are given, localhost and 50000 are assumed.\n" +
				"\n" +
				"OPTIONS\n" + copts.produceHelpMessage()
				);
			System.exit(0);
		}

		out = new PrintWriter(new java.io.BufferedWriter(new java.io.OutputStreamWriter(System.out)));

		String pass = copts.getOption("passphrase").getArgument();

		// we need the password from the user, fetch it with a pseudo
		// password protector
		if (pass == null) {
			final char[] tmp = System.console().readPassword("passphrase: ");
			if (tmp == null) {
				System.err.println("Invalid passphrase!");
				System.exit(1);
			}
			pass = String.valueOf(tmp);
		}

		// build the hostname
		String host = copts.getOption("host").getArgument();
		String sport = copts.getOption("port").getArgument();
		int pos = host.indexOf(':');
		if (pos != -1) {
			sport = host.substring(pos + 1);
			host = host.substring(0, pos);
		}
		int port = Integer.parseInt(sport);

		String hash = null;
		if (copts.getOption("Xhash").isPresent())
			hash = copts.getOption("Xhash").getArgument();

		if (!copts.getOption("command").isPresent()) {
			System.err.println("need a command to execute (-c)");
			System.exit(1);
		}

		Control ctl = null;
		try {
			ctl = new Control(host, port, pass);
		} catch (IllegalArgumentException e) {
			System.err.println(e.getMessage());
			System.exit(1);
		}
		// FIXME: Control needs to respect Xhash

		if (copts.getOption("Xdebug").isPresent()) {
			final String fname = copts.getOption("Xdebug").getArgument();
			ctl.setDebug(fname);
		}

		final String[] commands = copts.getOption("command").getArguments();
		if (commands[0].equals("status")) {
			List<SabaothDB> sdbs;
			if (commands.length == 1) {
				sdbs = ctl.getAllStatuses();
			} else {
				sdbs = new ArrayList<SabaothDB>();
				for (int i = 1; i < commands.length; i++)
					sdbs.add(ctl.getStatus(commands[i]));
			}
			final java.util.Iterator<SabaothDB> it = sdbs.iterator();
			while (it.hasNext()) {
				SabaothDB sdb = it.next();
				System.out.println(sdb.getName() + " " + sdb.getURI());
			}
		}
	}
}