01: package com.avaje.util.codegen;
02:
03: import java.util.logging.Level;
04: import java.util.logging.Logger;
05:
06: import com.avaje.ebean.Ebean;
07: import com.avaje.ebean.server.core.EbeanServerInternal;
08: import com.avaje.ebean.server.plugin.Plugin;
09: import com.avaje.lib.SystemProperties;
10: import com.avaje.lib.log.LogFactory;
11: import com.avaje.lib.util.StringHelper;
12:
13: /**
14: * <pre>
15: * <code>
16: *
17: * ## Mode = test the table types
18: * #ebean.codegen.mode=test
19: *
20: * ## Mode = generate code
21: * ebean.codegen.mode=generate
22: *
23: * ebean.codegen.regex
24: *
25: * ## patterns for database dictionary loading
26: * ebean.codegen.catalog
27: * ebean.codegen.schema
28: * ebean.codegen.table
29: * </code>
30: * </pre>
31: *
32: */
33: public class MainCodeGenerator {
34:
35: private static final Logger logger = LogFactory
36: .get(MainCodeGenerator.class);
37:
38: public static void main(String[] args) throws Exception {
39:
40: try {
41: try {
42: // make sure SystemProperties is initialised before
43: // loading the codegen.properties
44: SystemProperties.getProperty("default.datasource");
45: SystemProperties.loadProperties("codegen.properties",
46: false);
47:
48: } catch (Exception ex) {
49: String msg = "codegen.properties was not loaded.";
50: logger.log(Level.SEVERE, msg, ex);
51: }
52:
53: EbeanServerInternal server = (EbeanServerInternal) Ebean
54: .getServer(null);
55: Plugin plugin = server.getPlugin();
56:
57: GenerateConfiguration config = new GenerateConfiguration(
58: plugin);
59:
60: GenerateController gen = new GenerateController(config);
61:
62: String regex = null;
63: if (args.length > 0) {
64: regex = args[0];
65: } else {
66: // pattern to match against tables to determine
67: // if we should generate the code
68: regex = config.getProperty("codegen.regex", null);
69: }
70: if (regex == null) {
71: String match = config
72: .getProperty("codegen.match", null);
73: if (match != null) {
74: // use wildcard matching converting
75: // * & % into appropriate regex expression
76: String regexAll = "[a-z_]*";
77: match = StringHelper.replaceString(match, "*",
78: regexAll);
79: match = StringHelper.replaceString(match, "%",
80: regexAll);
81:
82: regex = "^" + match + "$";
83: }
84: }
85:
86: gen.generate(regex);
87:
88: } catch (Exception ex) {
89: ex.printStackTrace();
90: logger.log(Level.SEVERE, null, ex);
91: }
92: }
93: }
|