001package gu.sql2java.generator;
002
003import static gu.sql2java.generator.GeneratorConfig.CONFIG;
004import static com.google.common.base.Preconditions.checkNotNull;
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.MiscellaneousUtils;
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 = MiscellaneousUtils.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                        if(CONFIG.isTrace()){
061                                e.printStackTrace();
062                        }else{
063                                System.out.println(e.getMessage());
064                        }
065                        System.exit(1);
066                }
067        }
068
069        public static String getProperty(String property) {
070                String s = prop.getProperty(property);
071                return s != null ? s.trim() : s;
072        }
073
074        public static String getProperty(String property, String defaultVal) {
075                String s = prop.getProperty(property,defaultVal);
076                return s != null ? s.trim() : s;
077        }
078        public static String getPropertyRequired(String property) {
079                return checkNotNull(getProperty(property),"Missing property %s",property).trim();
080        }
081
082}