From b5ed81c932db5a409f916d736fc9a8e600f8b15e Mon Sep 17 00:00:00 2001 From: Chris Jaekl Date: Sun, 13 Dec 2015 20:05:37 +0900 Subject: [PATCH] Store db parameters in Config object, not in CFB object. --- prod/net/jaekl/cfb/CFB.java | 43 +++++++++++++++++------------- prod/net/jaekl/cfb/Config.java | 32 ++++++++++++++++++++++ test/net/jaekl/cfb/ConfigTest.java | 20 +++++++++++++- 3 files changed, 76 insertions(+), 19 deletions(-) diff --git a/prod/net/jaekl/cfb/CFB.java b/prod/net/jaekl/cfb/CFB.java index c02aa54..63271ec 100644 --- a/prod/net/jaekl/cfb/CFB.java +++ b/prod/net/jaekl/cfb/CFB.java @@ -48,13 +48,8 @@ public class CFB { // Command-line parameters File m_configFile; - String m_dbName; // db name File m_fbp; // FindBugsProject file File m_fbDir; // Directory where FindBugs is installed - String m_host; // db host - int m_port; // db port - String m_user; // db user - String m_pass; // db password String m_projName; // project (module) name String m_buildNum; // build number (version) boolean m_removeSchema; // purge DB schema @@ -68,13 +63,8 @@ public class CFB { m_config = new Config(); m_configFile = new File("config.properties"); - m_dbName = "CFB"; m_fbp = null; m_fbDir = null; - m_host = "localhost"; - m_port = 5432; - m_pass = ""; - m_user = "user"; m_projName = null; m_buildNum = null; m_removeSchema = false; @@ -108,13 +98,13 @@ public class CFB { m_configFile = new File(line.getOptionValue("c")); } if (line.hasOption("d")) { - m_dbName = line.getOptionValue("d"); + m_config.setDbName(line.getOptionValue("d")); } if (line.hasOption("f")) { m_fbp = new File(line.getOptionValue("f")); } if (line.hasOption("h")) { - m_host = line.getOptionValue("h"); + m_config.setDbHost(line.getOptionValue("h")); } if (line.hasOption("j")) { m_projName = line.getOptionValue("j"); @@ -126,14 +116,14 @@ public class CFB { m_output = new File(line.getOptionValue("o")); } if (line.hasOption("p")) { - m_pass = line.getOptionValue("p"); + m_config.setDbPass(line.getOptionValue("p")); } m_removeSchema = line.hasOption("drop-tables"); if (line.hasOption("t")) { - m_port = Integer.parseInt(line.getOptionValue("t")); + m_config.setDbPort(Integer.parseInt(line.getOptionValue("t"))); } if (line.hasOption("u")) { - m_user = line.getOptionValue("u"); + m_config.setDbUser(line.getOptionValue("u")); } } catch (ParseException exc) { @@ -196,7 +186,12 @@ public class CFB { MessageMap messageMap = new MessageMap(); messageMap.load(findBugsDir, Locale.getDefault(Category.DISPLAY)); - try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) { + try (Connection con = m_driver.connect( + m_config.getDbHost(), m_config.getDbPort(), + m_config.getDbName(), + m_config.getDbUser(), m_config.getDbPass()) + ) + { m_schema.setMessageMap(messageMap); if (m_removeSchema) { @@ -218,7 +213,14 @@ public class CFB { return; } - try (Connection con = m_driver.connect(m_host, m_port, m_dbName, m_user, m_pass)) { + try ( + Connection con = m_driver.connect( + m_config.getDbHost(), m_config.getDbPort(), + m_config.getDbName(), + m_config.getDbUser(), m_config.getDbPass() + ) + ) + { DbStore store = new DbStore(con, m_driver, messageMap.getColl()); store.put(analysis); @@ -241,7 +243,12 @@ public class CFB { private void reportUnableToConnect(PrintWriter pw, SQLException exc) { String cannotConnectFormat = trans(CfbBundle.CANNOT_CONNECT); - String cannotConnect = MessageFormat.format(cannotConnectFormat, m_host, ""+m_port, m_dbName, m_user); + String cannotConnect = MessageFormat.format(cannotConnectFormat, + m_config.getDbHost(), + ""+m_config.getDbPort(), + m_config.getDbName(), + m_config.getDbUser() + ); exc.printStackTrace(pw); SQLException next = exc.getNextException(); while (null != next) { diff --git a/prod/net/jaekl/cfb/Config.java b/prod/net/jaekl/cfb/Config.java index 8aa4234..76151a7 100644 --- a/prod/net/jaekl/cfb/Config.java +++ b/prod/net/jaekl/cfb/Config.java @@ -17,6 +17,11 @@ import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; public class Config { + private static final String DB_HOST = "db.host"; + private static final String DB_NAME = "db.name"; + private static final String DB_PASS = "db.pass"; + private static final String DB_PORT = "db.port"; + private static final String DB_USER = "db.user"; private static final String FINDBUGS_HOME = "FindBugsHome"; private static final String MAIL_FROM = "mail.from"; private static final String MAIL_SMTP_HOST = "mail.smtp.host"; @@ -64,6 +69,18 @@ public class Config { public String getMailSmtpHost() { return m_mailSmtpHost; } public ArrayList getNotify() { return new ArrayList(m_notify); } + public String getDbHost() { return m_host; } + public int getDbPort() { return m_port; } + public String getDbName() { return m_dbName; } + public String getDbUser() { return m_user; } + public String getDbPass() { return m_pass; } + + public void setDbHost(String value) { m_host = value; } + public void setDbPort(int value) { m_port = value; } + public void setDbName(String value) { m_dbName = value; } + public void setDbUser(String value) { m_user = value; } + public void setDbPass(String value) { m_pass = value; } + public void readFile(File configProperties) throws IOException { Properties props = new Properties(); @@ -79,6 +96,21 @@ public class Config { } } + if (props.containsKey(DB_HOST)) { + m_host = props.getProperty(DB_HOST); + } + if (props.containsKey(DB_NAME)) { + m_dbName = props.getProperty(DB_NAME); + } + if (props.containsKey(DB_PASS)) { + m_pass = props.getProperty(DB_PASS); + } + if (props.containsKey(DB_PORT)) { + m_port = Integer.parseInt(props.getProperty(DB_PORT)); + } + if (props.containsKey(DB_USER)) { + m_user = props.getProperty(DB_USER); + } if (props.containsKey(FINDBUGS_HOME)) { m_fbDir = new File(props.getProperty(FINDBUGS_HOME)); } diff --git a/test/net/jaekl/cfb/ConfigTest.java b/test/net/jaekl/cfb/ConfigTest.java index e7ca44c..11bd5b2 100644 --- a/test/net/jaekl/cfb/ConfigTest.java +++ b/test/net/jaekl/cfb/ConfigTest.java @@ -15,6 +15,11 @@ import org.junit.Test; public class ConfigTest { private static final String CHRIS = "chris@localhost"; + private static final String DB_HOST = "dbserver"; + private static final int DB_PORT = 1234; + private static final String DB_NAME = "Fred"; + private static final String DB_PASS = "Pebbles"; + private static final String DB_USER = "Wilma"; private static final String HUDSON = "hudson@jenkins.org"; private static final String MAIL_FROM = "findbugs@jaekl.net"; private static final String MAIL_TO = CHRIS + "," + HUDSON; @@ -28,7 +33,14 @@ public class ConfigTest { + "\n" + "; Mail server setup\n" + "mail.smtp.host=" + LOCALHOST + "\n" - + "mail.from=" + MAIL_FROM + "\n"; + + "mail.from=" + MAIL_FROM + "\n" + + "\n" + + "; Database\n" + + "db.host=" + DB_HOST + "\n" + + "db.port=" + DB_PORT + "\n" + + "db.name=" + DB_NAME + "\n" + + "db.user=" + DB_USER + "\n" + + "db.pass=" + DB_PASS + "\n"; @BeforeClass public static void beforeClass() { @@ -55,6 +67,12 @@ public class ConfigTest { List notify = config.getNotify(); assertTrue(notify.contains(CHRIS)); assertTrue(notify.contains(HUDSON)); + + assertEquals(DB_HOST, config.getDbHost()); + assertEquals(DB_PORT, config.getDbPort()); + assertEquals(DB_NAME, config.getDbName()); + assertEquals(DB_USER, config.getDbUser()); + assertEquals(DB_PASS, config.getDbPass()); } @Test -- 2.39.2