001package gu.sql2java.generator; 002 003import static gu.sql2java.generator.GeneratorConfig.*; 004import static com.google.common.base.Preconditions.*; 005 006import java.io.File; 007import java.io.FileInputStream; 008import java.io.IOException; 009import java.io.InputStream; 010import java.util.List; 011import java.util.Properties; 012 013import org.slf4j.Logger; 014import org.slf4j.LoggerFactory; 015 016import gu.sql2java.generator.CodeWriter; 017import gu.sql2java.generator.Database; 018import net.gdface.utils.FaceUtilits; 019 020public class Generator { 021 private static final Logger logger = LoggerFactory.getLogger(Generator.class); 022 023 private static final Properties DEFAULT_PROP; 024 static { 025 DEFAULT_PROP = new Properties(); 026 try (InputStream stream=Generator.class.getResourceAsStream("/sql2java.properties")){ 027 DEFAULT_PROP.load(stream); 028 } catch (IOException e) { 029 throw new ExceptionInInitializerError(e); 030 } 031 } 032 private static Properties prop; 033 public static void main(String ...argv) { 034 CONFIG.parseCommandLine(argv); 035 prop = new Properties(DEFAULT_PROP); 036 File propFile = CONFIG.getPropFile(); 037 if(!propFile.isFile()){ 038 logger.error("NOT FOUND file {}", propFile); 039 System.exit(-1); 040 } 041 try (InputStream is = new FileInputStream(propFile)){ 042 prop.load(is); 043 CodeWriter cw = new CodeWriter(null, prop); 044 cw.log("database properties initialization"); 045 Database db = new Database(); 046 db.setDriver(getPropertyRequired("jdbc.driver")); 047 db.setUrl(getPropertyRequired("jdbc.url")); 048 db.setUsername(getProperty("jdbc.username")); 049 db.setPassword(getProperty("jdbc.password")); 050 db.setCatalog(getProperty("jdbc.catalog")); 051 db.setSchema(getProperty("jdbc.schema")); 052 db.setTableNamePattern(getProperty("jdbc.tablenamepattern")); 053 CodeWriter writer = new CodeWriter(db, prop); 054 055 List<String> tableTypes = FaceUtilits.elementsOf(getProperty("jdbc.tabletypes", "TABLE")); 056 db.setTableTypes(tableTypes.toArray(new String[tableTypes.size()])); 057 db.load(); 058 writer.process(); 059 } catch (Exception e) { 060 e.printStackTrace(); 061 System.exit(1); 062 } 063 } 064 065 public static String getProperty(String property) { 066 String s = prop.getProperty(property); 067 return s != null ? s.trim() : s; 068 } 069 070 public static String getProperty(String property, String defaultVal) { 071 String s = prop.getProperty(property,defaultVal); 072 return s != null ? s.trim() : s; 073 } 074 public static String getPropertyRequired(String property) { 075 return checkNotNull(getProperty(property),"Missing property %s",property).trim(); 076 } 077 078}