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