parallel connection testing: avoid deadlocks – preload drivers + better exception handling and logging
3 * Copyright © 2013 František Kučera (frantovo.cz)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package info.globalcode.sql.dk;
20 import static info.globalcode.sql.dk.Functions.isNotEmpty;
21 import static info.globalcode.sql.dk.Functions.equalz;
22 import info.globalcode.sql.dk.InfoLister.InfoType;
23 import info.globalcode.sql.dk.configuration.Properties;
24 import info.globalcode.sql.dk.configuration.Property;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.EnumSet;
30 import java.util.LinkedHashSet;
31 import java.util.List;
33 import java.util.regex.Pattern;
34 import java.util.regex.PatternSyntaxException;
37 * Holds options from command line, validates them, combines with configuration and provides derived
40 * @author Ing. František Kučera (frantovo.cz)
42 public class CLIOptions {
44 public static final String DEFAULT_NAME_PREFIX = ":";
45 public static final String DEFAULT_NAME_SUFFIX = "(?=([^\\w]|$))";
47 private String databaseName;
48 private Set<String> databaseNamesToTest = new LinkedHashSet<>();
49 private Set<String> databaseNamesToListProperties = new LinkedHashSet<>();
50 private String namePrefix = DEFAULT_NAME_PREFIX;
51 private String nameSuffix = DEFAULT_NAME_SUFFIX;
52 private String formatterName;
53 private boolean batch;
54 private Properties formatterProperties = new Properties();
55 private Properties databaseProperties = new Properties();
64 private final List<NamedParameter> namedParameters = new ArrayList<>();
65 private final List<Parameter> numberedParameters = new ArrayList<>();
66 private final EnumSet<InfoType> showInfo = EnumSet.noneOf(InfoType.class);
68 public void validate() throws InvalidOptionsException {
69 InvalidOptionsException e = new InvalidOptionsException();
71 MODE mode = getMode();
73 e.addProblem(new InvalidOptionsException.OptionProblem("Invalid combination of DB, SQL and BATCH – please specify just 2 of this 3 options"));
74 } else if (mode == MODE.JUST_SHOW_INFO) {
75 if (!namedParameters.isEmpty()) {
76 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use named parameters if just showing info."));
78 if (!numberedParameters.isEmpty()) {
79 e.addProblem(new InvalidOptionsException.OptionProblem("Do not use numbered parameters if just showing info."));
81 if (isNotEmpty(sql, false)) {
82 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify SQL if just showing info."));
84 if (isNotEmpty(databaseName, false)) {
85 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify database if just showing info."));
88 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify batch if just showing info."));
90 if (!equalz(namePrefix, DEFAULT_NAME_PREFIX)) {
91 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name prefix if just showing info."));
93 if (!equalz(nameSuffix, DEFAULT_NAME_SUFFIX)) {
94 e.addProblem(new InvalidOptionsException.OptionProblem("Do not specify name suffix if just showing info."));
96 if (showInfo.contains(InfoType.CONNECTION) && databaseNamesToTest.isEmpty()) {
97 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify which database should be tested."));
99 if (showInfo.contains(InfoType.JDBC_PROPERTIES) && databaseNamesToListProperties.isEmpty()) {
100 e.addProblem(new InvalidOptionsException.OptionProblem("Please specify for which database the properties should be listed."));
104 if (!namedParameters.isEmpty() && !numberedParameters.isEmpty()) {
105 e.addProblem(new InvalidOptionsException.OptionProblem("Named and numbered parameters can not be used together in one command."));
109 Pattern.compile(namePrefix + "test" + nameSuffix);
110 } catch (PatternSyntaxException regexException) {
111 e.addProblem(new InvalidOptionsException.OptionProblem("Ivalid regular expression in name prefix or suffix", regexException));
114 if (e.hasProblems()) {
119 private boolean hasSql() {
120 return isNotEmpty(getSql(), true);
123 private boolean hasDb() {
124 return isNotEmpty(getDatabaseName(), true);
128 * Depends on options: DB, BATCH, SQL
130 * @return mode | or null if options are not yet initialized or combination of options is
133 public MODE getMode() {
134 if (hasDb() && !batch && hasSql()) {
135 return MODE.QUERY_NOW;
136 } else if (!hasDb() && batch && hasSql()) {
137 return MODE.PREPARE_BATCH;
138 } else if (hasDb() && batch && !hasSql()) {
139 return MODE.EXECUTE_BATCH;
141 return showInfo.isEmpty() ? null : MODE.JUST_SHOW_INFO;
145 public String getSql() {
149 public void setSql(String sql) {
153 public String getDatabaseName() {
157 public void setDatabaseName(String databaseName) {
158 this.databaseName = databaseName;
161 public void setBatch(boolean batch) {
165 public Collection<NamedParameter> getNamedParameters() {
166 return namedParameters;
169 public List<Parameter> getNumberedParameters() {
170 return numberedParameters;
173 public void addNumberedParameter(Parameter p) {
174 numberedParameters.add(p);
177 public void addNamedParameter(NamedParameter p) {
178 namedParameters.add(p);
181 public Properties getDatabaseProperties() {
182 return databaseProperties;
185 public Properties getFormatterProperties() {
186 return formatterProperties;
189 public void addDatabaseProperty(Property p) {
190 databaseProperties.add(p);
193 public void addFormatterProperty(Property p) {
194 formatterProperties.add(p);
198 * @return regular expression describing the name prefix
200 public String getNamePrefix() {
205 * @see #getNamePrefix()
207 public void setNamePrefix(String namePrefix) {
208 this.namePrefix = namePrefix;
212 * @return regular expression describing the name prefix
214 public String getNameSuffix() {
219 * @see #getNameSuffix()
221 public void setNameSuffix(String nameSuffix) {
222 this.nameSuffix = nameSuffix;
225 public String getFormatterName() {
226 return formatterName;
229 public void setFormatterName(String formatterName) {
230 this.formatterName = formatterName;
233 public void addShowInfo(InfoType info) {
237 public EnumSet<InfoType> getShowInfo() {
241 public Set<String> getDatabaseNamesToTest() {
242 return databaseNamesToTest;
245 public void addDatabaseNameToTest(String name) {
246 databaseNamesToTest.add(name);
249 public Set<String> getDatabaseNamesToListProperties() {
250 return databaseNamesToListProperties;
253 public void addDatabaseNameToListProperties(String name) {
254 databaseNamesToListProperties.add(name);
257 public SQLCommand getSQLCommand() {
258 if (namedParameters.isEmpty()) {
259 return new SQLCommandNumbered(sql, numberedParameters);
261 return new SQLCommandNamed(sql, namedParameters, namePrefix, nameSuffix);
265 public OutputStream getOutputStream() {
269 public InputStream getInputStream() {