View Javadoc
1   /**
2    *    Copyright 2006-2016 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    *       http://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.HashSet;
26  import java.util.List;
27  import java.util.Map;
28  import java.util.Set;
29  import java.util.StringTokenizer;
30  
31  import org.mybatis.generator.config.Configuration;
32  import org.mybatis.generator.config.xml.ConfigurationParser;
33  import org.mybatis.generator.exception.InvalidConfigurationException;
34  import org.mybatis.generator.exception.XMLParserException;
35  import org.mybatis.generator.internal.DefaultShellCallback;
36  import org.mybatis.generator.logging.LogFactory;
37  
38  /**
39   * This class allows the code generator to be run from the command line.
40   * 
41   * @author Jeff Butler
42   */
43  public class ShellRunner {
44      private static final String CONFIG_FILE = "-configfile"; //$NON-NLS-1$
45      private static final String OVERWRITE = "-overwrite"; //$NON-NLS-1$
46      private static final String CONTEXT_IDS = "-contextids"; //$NON-NLS-1$
47      private static final String TABLES = "-tables"; //$NON-NLS-1$
48      private static final String VERBOSE = "-verbose"; //$NON-NLS-1$
49      private static final String FORCE_JAVA_LOGGING = "-forceJavaLogging"; //$NON-NLS-1$
50      private static final String HELP_1 = "-?"; //$NON-NLS-1$
51      private static final String HELP_2 = "-h"; //$NON-NLS-1$
52  
53      public static void main(String[] args) {
54          if (args.length == 0) {
55              usage();
56              System.exit(0);
57              return; // only to satisfy compiler, never returns
58          }
59  
60          Map<String, String> arguments = parseCommandLine(args);
61  
62          if (arguments.containsKey(HELP_1)) {
63              usage();
64              System.exit(0);
65              return; // only to satisfy compiler, never returns
66          }
67  
68          if (!arguments.containsKey(CONFIG_FILE)) {
69              writeLine(getString("RuntimeError.0")); //$NON-NLS-1$
70              return;
71          }
72  
73          List<String> warnings = new ArrayList<String>();
74  
75          String configfile = arguments.get(CONFIG_FILE);
76          File configurationFile = new File(configfile);
77          if (!configurationFile.exists()) {
78              writeLine(getString("RuntimeError.1", configfile)); //$NON-NLS-1$
79              return;
80          }
81  
82          Set<String> fullyqualifiedTables = new HashSet<String>();
83          if (arguments.containsKey(TABLES)) {
84              StringTokenizer st = new StringTokenizer(arguments.get(TABLES), ","); //$NON-NLS-1$
85              while (st.hasMoreTokens()) {
86                  String s = st.nextToken().trim();
87                  if (s.length() > 0) {
88                      fullyqualifiedTables.add(s);
89                  }
90              }
91          }
92  
93          Set<String> contexts = new HashSet<String>();
94          if (arguments.containsKey(CONTEXT_IDS)) {
95              StringTokenizer st = new StringTokenizer(
96                      arguments.get(CONTEXT_IDS), ","); //$NON-NLS-1$
97              while (st.hasMoreTokens()) {
98                  String s = st.nextToken().trim();
99                  if (s.length() > 0) {
100                     contexts.add(s);
101                 }
102             }
103         }
104 
105         try {
106             ConfigurationParser cp = new ConfigurationParser(warnings);
107             Configuration config = cp.parseConfiguration(configurationFile);
108 
109             DefaultShellCallback shellCallback = new DefaultShellCallback(
110                     arguments.containsKey(OVERWRITE));
111 
112             MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, shellCallback, warnings);
113 
114             ProgressCallback progressCallback = arguments.containsKey(VERBOSE) ? new VerboseProgressCallback()
115                     : null;
116 
117             myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables);
118 
119         } catch (XMLParserException e) {
120             writeLine(getString("Progress.3")); //$NON-NLS-1$
121             writeLine();
122             for (String error : e.getErrors()) {
123                 writeLine(error);
124             }
125 
126             return;
127         } catch (SQLException e) {
128             e.printStackTrace();
129             return;
130         } catch (IOException e) {
131             e.printStackTrace();
132             return;
133         } catch (InvalidConfigurationException e) {
134             writeLine(getString("Progress.16")); //$NON-NLS-1$
135             for (String error : e.getErrors()) {
136                 writeLine(error);
137             }
138             return;
139         } catch (InterruptedException e) {
140             // ignore (will never happen with the DefaultShellCallback)
141         }
142 
143         for (String warning : warnings) {
144             writeLine(warning);
145         }
146 
147         if (warnings.size() == 0) {
148             writeLine(getString("Progress.4")); //$NON-NLS-1$
149         } else {
150             writeLine();
151             writeLine(getString("Progress.5")); //$NON-NLS-1$
152         }
153     }
154 
155     private static void usage() {
156         String lines = getString("Usage.Lines"); //$NON-NLS-1$
157         int iLines = Integer.parseInt(lines);
158         for (int i = 0; i < iLines; i++) {
159             String key = "Usage." + i; //$NON-NLS-1$
160             writeLine(getString(key));
161         }
162     }
163 
164     private static void writeLine(String message) {
165         System.out.println(message);
166     }
167 
168     private static void writeLine() {
169         System.out.println();
170     }
171 
172     private static Map<String, String> parseCommandLine(String[] args) {
173         List<String> errors = new ArrayList<String>();
174         Map<String, String> arguments = new HashMap<String, String>();
175 
176         for (int i = 0; i < args.length; i++) {
177             if (CONFIG_FILE.equalsIgnoreCase(args[i])) {
178                 if ((i + 1) < args.length) {
179                     arguments.put(CONFIG_FILE, args[i + 1]);
180                 } else {
181                     errors.add(getString(
182                             "RuntimeError.19", CONFIG_FILE)); //$NON-NLS-1$
183                 }
184                 i++;
185             } else if (OVERWRITE.equalsIgnoreCase(args[i])) {
186                 arguments.put(OVERWRITE, "Y"); //$NON-NLS-1$
187             } else if (VERBOSE.equalsIgnoreCase(args[i])) {
188                 arguments.put(VERBOSE, "Y"); //$NON-NLS-1$
189             } else if (HELP_1.equalsIgnoreCase(args[i])) {
190                 arguments.put(HELP_1, "Y"); //$NON-NLS-1$
191             } else if (HELP_2.equalsIgnoreCase(args[i])) {
192                 // put HELP_1 in the map here too - so we only
193                 // have to check for one entry in the mainline
194                 arguments.put(HELP_1, "Y"); //$NON-NLS-1$
195             } else if (FORCE_JAVA_LOGGING.equalsIgnoreCase(args[i])) {
196                 LogFactory.forceJavaLogging();
197             } else if (CONTEXT_IDS.equalsIgnoreCase(args[i])) {
198                 if ((i + 1) < args.length) {
199                     arguments.put(CONTEXT_IDS, args[i + 1]);
200                 } else {
201                     errors.add(getString(
202                             "RuntimeError.19", CONTEXT_IDS)); //$NON-NLS-1$
203                 }
204                 i++;
205             } else if (TABLES.equalsIgnoreCase(args[i])) {
206                 if ((i + 1) < args.length) {
207                     arguments.put(TABLES, args[i + 1]);
208                 } else {
209                     errors.add(getString("RuntimeError.19", TABLES)); //$NON-NLS-1$
210                 }
211                 i++;
212             } else {
213                 errors.add(getString("RuntimeError.20", args[i])); //$NON-NLS-1$
214             }
215         }
216 
217         if (!errors.isEmpty()) {
218             for (String error : errors) {
219                 writeLine(error);
220             }
221 
222             System.exit(-1);
223         }
224 
225         return arguments;
226     }
227 }