File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\util\Tool.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.util;
8
9 import java.io.ByteArrayInputStream;
10 import java.io.IOException;
11 import java.io.PrintStream;
12 import java.sql.SQLException;
13 import java.util.Properties;
14 import org.h2.constant.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) throws SQLException {
55 showUsage();
56 throw throwUnsupportedOption(option);
57 }
58
59 /**
60 * Throw a SQLException saying this command line option is not supported.
61 *
62 * @param option the unsupported option
63 * @return this method never returns normally
64 */
65 protected SQLException throwUnsupportedOption(String option) throws SQLException {
66 throw DbException.get(ErrorCode.FEATURE_NOT_SUPPORTED_1, option).getSQLException();
67 }
68
69 /**
70 * Print to the output stream that no database files have been found.
71 *
72 * @param dir the directory or null
73 * @param db the database name or null
74 */
75 protected void printNoDatabaseFilesFound(String dir, String db) {
76 StringBuilder buff;
77 dir = FileLister.getDir(dir);
78 if (!FileUtils.isDirectory(dir)) {
79 buff = new StringBuilder("Directory not found: ");
80 buff.append(dir);
81 } else {
82 buff = new StringBuilder("No database files have been found");
83 buff.append(" in directory ").append(dir);
84 if (db != null) {
85 buff.append(" for the database ").append(db);
86 }
87 }
88 out.println(buff.toString());
89 }
90
91 /**
92 * Print the usage of the tool. This method reads the description from the
93 * resource file.
94 */
95 protected void showUsage() {
96 if (resources == null) {
97 resources = new Properties();
98 String resourceName = "/org/h2/res/javadoc.properties";
99 try {
100 byte[] buff = Utils.getResource(resourceName);
101 if (buff != null) {
102 resources.load(new ByteArrayInputStream(buff));
103 }
104 } catch (IOException e) {
105 out.println("Cannot load " + resourceName);
106 }
107 }
108 String className = getClass().getName();
109 out.println(resources.get(className));
110 out.println("Usage: java "+getClass().getName() + " <options>");
111 out.println(resources.get(className + ".main"));
112 out.println("See also http://h2database.com/javadoc/" + className.replace('.', '/') + ".html");
113 }
114
115 /**
116 * Check if the argument matches the option.
117 * If the argument starts with this option, but doesn't match,
118 * then an exception is thrown.
119 *
120 * @param arg the argument
121 * @param option the command line option
122 * @return true if it matches
123 */
124 public static boolean isOption(String arg, String option) {
125 if (arg.equals(option)) {
126 return true;
127 } else if (arg.startsWith(option)) {
128 throw DbException.getUnsupportedException("expected: " + option + " got: " + arg);
129 }
130 return false;
131 }
132
133 }
134