• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / generator / #1178

pending completion
#1178

push

github

web-flow
Merge pull request #974 from mybatis/renovate/org.apache.logging.log4j-log4j-api-2.x

Update dependency org.apache.logging.log4j:log4j-api to v2.20.0

11250 of 12741 relevant lines covered (88.3%)

0.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/core/mybatis-generator-core/src/main/java/org/mybatis/generator/api/ShellRunner.java
1
/*
2
 *    Copyright 2006-2023 the original author or authors.
3
 *
4
 *    Licensed under the Apache License, Version 2.0 (the "License");
5
 *    you may not use this file except in compliance with the License.
6
 *    You may obtain a copy of the License at
7
 *
8
 *       https://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 *    Unless required by applicable law or agreed to in writing, software
11
 *    distributed under the License is distributed on an "AS IS" BASIS,
12
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 *    See the License for the specific language governing permissions and
14
 *    limitations under the License.
15
 */
16
package org.mybatis.generator.api;
17

18
import static org.mybatis.generator.internal.util.messages.Messages.getString;
19

20
import java.io.File;
21
import java.io.IOException;
22
import java.sql.SQLException;
23
import java.util.ArrayList;
24
import java.util.HashMap;
25
import java.util.List;
26
import java.util.Map;
27
import java.util.Set;
28

29
import org.mybatis.generator.config.Configuration;
30
import org.mybatis.generator.config.xml.ConfigurationParser;
31
import org.mybatis.generator.exception.InvalidConfigurationException;
32
import org.mybatis.generator.exception.XMLParserException;
33
import org.mybatis.generator.internal.DefaultShellCallback;
34
import org.mybatis.generator.internal.util.StringUtility;
35
import org.mybatis.generator.logging.LogFactory;
36

37
/**
38
 * This class allows the code generator to be run from the command line.
39
 *
40
 * @author Jeff Butler
41
 */
42
public class ShellRunner {
×
43
    private static final String CONFIG_FILE = "-configfile"; //$NON-NLS-1$
44
    private static final String OVERWRITE = "-overwrite"; //$NON-NLS-1$
45
    private static final String CONTEXT_IDS = "-contextids"; //$NON-NLS-1$
46
    private static final String TABLES = "-tables"; //$NON-NLS-1$
47
    private static final String VERBOSE = "-verbose"; //$NON-NLS-1$
48
    private static final String FORCE_JAVA_LOGGING = "-forceJavaLogging"; //$NON-NLS-1$
49
    private static final String HELP_1 = "-?"; //$NON-NLS-1$
50
    private static final String HELP_2 = "-h"; //$NON-NLS-1$
51

52
    public static void main(String[] args) {
53
        if (args.length == 0) {
×
54
            usage();
×
55
            System.exit(0);
×
56
            return; // only to satisfy compiler, never returns
×
57
        }
58

59
        Map<String, String> arguments = parseCommandLine(args);
×
60

61
        if (arguments.containsKey(HELP_1)) {
×
62
            usage();
×
63
            System.exit(0);
×
64
            return; // only to satisfy compiler, never returns
×
65
        }
66

67
        if (!arguments.containsKey(CONFIG_FILE)) {
×
68
            writeLine(getString("RuntimeError.0")); //$NON-NLS-1$
×
69
            return;
×
70
        }
71

72
        List<String> warnings = new ArrayList<>();
×
73

74
        String configfile = arguments.get(CONFIG_FILE);
×
75
        File configurationFile = new File(configfile);
×
76
        if (!configurationFile.exists()) {
×
77
            writeLine(getString("RuntimeError.1", configfile)); //$NON-NLS-1$
×
78
            return;
×
79
        }
80

81
        Set<String> fullyQualifiedTables = StringUtility.tokenize(arguments.get(TABLES));
×
82

83
        Set<String> contexts = StringUtility.tokenize(arguments.get(CONTEXT_IDS));
×
84

85
        try {
86
            ConfigurationParser cp = new ConfigurationParser(warnings);
×
87
            Configuration config = cp.parseConfiguration(configurationFile);
×
88

89
            DefaultShellCallback shellCallback = new DefaultShellCallback(arguments.containsKey(OVERWRITE));
×
90

91
            MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
×
92

93
            ProgressCallback progressCallback = arguments.containsKey(VERBOSE) ? new VerboseProgressCallback()
×
94
                    : null;
×
95

96
            myBatisGenerator.generate(progressCallback, contexts, fullyQualifiedTables);
×
97

98
        } catch (XMLParserException e) {
×
99
            writeLine(getString("Progress.3")); //$NON-NLS-1$
×
100
            writeLine();
×
101
            for (String error : e.getErrors()) {
×
102
                writeLine(error);
×
103
            }
×
104

105
            return;
×
106
        } catch (SQLException | IOException e) {
×
107
            e.printStackTrace(System.out);
×
108
            return;
×
109
        } catch (InvalidConfigurationException e) {
×
110
            writeLine(getString("Progress.16")); //$NON-NLS-1$
×
111
            for (String error : e.getErrors()) {
×
112
                writeLine(error);
×
113
            }
×
114
            return;
×
115
        } catch (InterruptedException e) {
×
116
            Thread.currentThread().interrupt();
×
117
        }
×
118

119
        for (String warning : warnings) {
×
120
            writeLine(warning);
×
121
        }
×
122

123
        if (warnings.isEmpty()) {
×
124
            writeLine(getString("Progress.4")); //$NON-NLS-1$
×
125
        } else {
126
            writeLine();
×
127
            writeLine(getString("Progress.5")); //$NON-NLS-1$
×
128
        }
129
    }
×
130

131
    private static void usage() {
132
        writeLine(getString("Usage")); //$NON-NLS-1$
×
133
    }
×
134

135
    private static void writeLine(String message) {
136
        System.out.println(message);
×
137
    }
×
138

139
    private static void writeLine() {
140
        System.out.println();
×
141
    }
×
142

143
    private static Map<String, String> parseCommandLine(String[] args) {
144
        List<String> errors = new ArrayList<>();
×
145
        Map<String, String> arguments = new HashMap<>();
×
146

147
        for (int i = 0; i < args.length; i++) {
×
148
            if (CONFIG_FILE.equalsIgnoreCase(args[i])) {
×
149
                if ((i + 1) < args.length) {
×
150
                    arguments.put(CONFIG_FILE, args[i + 1]);
×
151
                } else {
152
                    errors.add(getString(
×
153
                            "RuntimeError.19", CONFIG_FILE)); //$NON-NLS-1$
154
                }
155
                i++;
×
156
            } else if (OVERWRITE.equalsIgnoreCase(args[i])) {
×
157
                arguments.put(OVERWRITE, "Y"); //$NON-NLS-1$
×
158
            } else if (VERBOSE.equalsIgnoreCase(args[i])) {
×
159
                arguments.put(VERBOSE, "Y"); //$NON-NLS-1$
×
160
            } else if (HELP_1.equalsIgnoreCase(args[i])) {
×
161
                arguments.put(HELP_1, "Y"); //$NON-NLS-1$
×
162
            } else if (HELP_2.equalsIgnoreCase(args[i])) {
×
163
                // put HELP_1 in the map here too - so we only
164
                // have to check for one entry in the mainline
165
                arguments.put(HELP_1, "Y"); //$NON-NLS-1$
×
166
            } else if (FORCE_JAVA_LOGGING.equalsIgnoreCase(args[i])) {
×
167
                LogFactory.forceJavaLogging();
×
168
            } else if (CONTEXT_IDS.equalsIgnoreCase(args[i])) {
×
169
                if ((i + 1) < args.length) {
×
170
                    arguments.put(CONTEXT_IDS, args[i + 1]);
×
171
                } else {
172
                    errors.add(getString(
×
173
                            "RuntimeError.19", CONTEXT_IDS)); //$NON-NLS-1$
174
                }
175
                i++;
×
176
            } else if (TABLES.equalsIgnoreCase(args[i])) {
×
177
                if ((i + 1) < args.length) {
×
178
                    arguments.put(TABLES, args[i + 1]);
×
179
                } else {
180
                    errors.add(getString("RuntimeError.19", TABLES)); //$NON-NLS-1$
×
181
                }
182
                i++;
×
183
            } else {
184
                errors.add(getString("RuntimeError.20", args[i])); //$NON-NLS-1$
×
185
            }
186
        }
187

188
        if (!errors.isEmpty()) {
×
189
            for (String error : errors) {
×
190
                writeLine(error);
×
191
            }
×
192

193
            System.exit(-1);
×
194
        }
195

196
        return arguments;
×
197
    }
198
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc