| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.util; |
| 7 |
|
| 8 |
import java.io.ByteArrayInputStream; |
| 9 |
import java.io.IOException; |
| 10 |
import java.io.PrintStream; |
| 11 |
import java.sql.SQLException; |
| 12 |
import java.util.Properties; |
| 13 |
import org.h2.api.ErrorCode; |
| 14 |
import org.h2.message.DbException; |
| 15 |
import org.h2.store.FileLister; |
| 16 |
import org.h2.store.fs.FileUtils; |
| 17 |
|
| 18 |
/** |
| 19 |
* Command line tools implement the tool interface so that they can be used in |
| 20 |
* the H2 Console. |
| 21 |
*/ |
| 22 |
public abstract class Tool { |
| 23 |
|
| 24 |
/** |
| 25 |
* The output stream where this tool writes to. |
| 26 |
*/ |
| 27 |
protected PrintStream out = System.out; |
| 28 |
|
| 29 |
private Properties resources; |
| 30 |
|
| 31 |
/** |
| 32 |
* Sets the standard output stream. |
| 33 |
* |
| 34 |
* @param out the new standard output stream |
| 35 |
*/ |
| 36 |
public void setOut(PrintStream out) { |
| 37 |
this.out = out; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Run the tool with the given output stream and arguments. |
| 42 |
* |
| 43 |
* @param args the argument list |
| 44 |
* @throws SQLException on failure |
| 45 |
*/ |
| 46 |
public abstract void runTool(String... args) throws SQLException; |
| 47 |
|
| 48 |
/** |
| 49 |
* Throw a SQLException saying this command line option is not supported. |
| 50 |
* |
| 51 |
* @param option the unsupported option |
| 52 |
* @return this method never returns normally |
| 53 |
* @throws SQLException on failure |
| 54 |
*/ |
| 55 |
protected SQLException showUsageAndThrowUnsupportedOption(String option) |
| 56 |
throws SQLException { |
| 57 |
showUsage(); |
| 58 |
throw throwUnsupportedOption(option); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Throw a SQLException saying this command line option is not supported. |
| 63 |
* |
| 64 |
* @param option the unsupported option |
| 65 |
* @return this method never returns normally |
| 66 |
* @throws SQLException on failure |
| 67 |
*/ |
| 68 |
protected SQLException throwUnsupportedOption(String option) |
| 69 |
throws SQLException { |
| 70 |
throw DbException.getJdbcSQLException( |
| 71 |
ErrorCode.FEATURE_NOT_SUPPORTED_1, option); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Print to the output stream that no database files have been found. |
| 76 |
* |
| 77 |
* @param dir the directory or null |
| 78 |
* @param db the database name or null |
| 79 |
*/ |
| 80 |
protected void printNoDatabaseFilesFound(String dir, String db) { |
| 81 |
StringBuilder buff; |
| 82 |
dir = FileLister.getDir(dir); |
| 83 |
if (!FileUtils.isDirectory(dir)) { |
| 84 |
buff = new StringBuilder("Directory not found: "); |
| 85 |
buff.append(dir); |
| 86 |
} else { |
| 87 |
buff = new StringBuilder("No database files have been found"); |
| 88 |
buff.append(" in directory ").append(dir); |
| 89 |
if (db != null) { |
| 90 |
buff.append(" for the database ").append(db); |
| 91 |
} |
| 92 |
} |
| 93 |
out.println(buff.toString()); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Print the usage of the tool. This method reads the description from the |
| 98 |
* resource file. |
| 99 |
*/ |
| 100 |
protected void showUsage() { |
| 101 |
if (resources == null) { |
| 102 |
resources = new Properties(); |
| 103 |
String resourceName = "/org/h2/res/javadoc.properties"; |
| 104 |
try { |
| 105 |
byte[] buff = Utils.getResource(resourceName); |
| 106 |
if (buff != null) { |
| 107 |
resources.load(new ByteArrayInputStream(buff)); |
| 108 |
} |
| 109 |
} catch (IOException e) { |
| 110 |
out.println("Cannot load " + resourceName); |
| 111 |
} |
| 112 |
} |
| 113 |
String className = getClass().getName(); |
| 114 |
out.println(resources.get(className)); |
| 115 |
out.println("Usage: java "+getClass().getName() + " <options>"); |
| 116 |
out.println(resources.get(className + ".main")); |
| 117 |
out.println("See also https://h2database.com/javadoc/" + |
| 118 |
className.replace('.', '/') + ".html"); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Check if the argument matches the option. |
| 123 |
* If the argument starts with this option, but doesn't match, |
| 124 |
* then an exception is thrown. |
| 125 |
* |
| 126 |
* @param arg the argument |
| 127 |
* @param option the command line option |
| 128 |
* @return true if it matches |
| 129 |
*/ |
| 130 |
public static boolean isOption(String arg, String option) { |
| 131 |
if (arg.equals(option)) { |
| 132 |
return true; |
| 133 |
} else if (arg.startsWith(option)) { |
| 134 |
throw DbException.getUnsupportedException( |
| 135 |
"expected: " + option + " got: " + arg); |
| 136 |
} |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
} |