From b25aa231abd7aeae1d600572352304ce3fa3096a Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Fri, 15 Jun 2012 16:29:17 +0200 Subject: [PATCH] OnlineGlomServiceImpl: Improve the OnlineGlomProperties class. * src/main/java/org/glom/web/server/OnlineGlomServiceImpl.java init(): Move knowledge of the config file format into the OnlineGlomProperties inner class. --- ChangeLog | 8 +++ .../org/glom/web/server/OnlineGlomServiceImpl.java | 83 +++++++++++++++++----- 2 files changed, 75 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index d3971f0..2387818 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,13 @@ 2012-06-15 Murray Cumming + OnlineGlomServiceImpl: Improve the OnlineGlomProperties class. + + * src/main/java/org/glom/web/server/OnlineGlomServiceImpl.java + init(): Move knowledge of the config file format into the + OnlineGlomProperties inner class. + +2012-06-15 Murray Cumming + SelfHostExampleTest: Make sure we cleanup on failure. * src/test/java/org/glom/web/server/SelfHostExampleTest.java: Move diff --git a/src/main/java/org/glom/web/server/OnlineGlomServiceImpl.java b/src/main/java/org/glom/web/server/OnlineGlomServiceImpl.java index ab3b6ec..3662e9a 100644 --- a/src/main/java/org/glom/web/server/OnlineGlomServiceImpl.java +++ b/src/main/java/org/glom/web/server/OnlineGlomServiceImpl.java @@ -63,9 +63,15 @@ import com.mchange.v2.c3p0.DataSources; public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService { // convenience class for dealing with the Online Glom configuration file - // TODO: Turn this into a class with specific getters, and test it. + // TODO: test this. private static class OnlineGlomProperties extends Properties { - public String getKey(final String value) { + + /** Get the whole line that has a key with this value. + * + * @param value + * @return + */ + private String getKey(final String value) { for (final String key : stringPropertyNames()) { if (getProperty(key).trim().equals(value)) { return key; @@ -73,6 +79,54 @@ public class OnlineGlomServiceImpl extends RemoteServiceServlet implements Onlin } return null; } + + public static class Credentials { + public String userName = ""; + public String password = ""; + }; + + public Credentials getCredentials(final String filename) { + Credentials result = null; + + //TODO: This could fail if a username or password has the same string as a filename. + //TODO: Check for ".filename =" in getKey(). + final String key = getKey(filename); + if (key == null) { + return result; + } + + //Split the line at the . separators, + final String[] keyArray = key.split("\\."); + + //Check that the third item is "filename", as expected: + if (keyArray.length == 3 && "filename".equals(keyArray[2])) { + result = new Credentials(); + + //Get the username and password for this file: + final String usernameKey = key.replaceAll(keyArray[2], "username"); + final String passwordKey = key.replaceAll(keyArray[2], "password"); + result.userName = getProperty(usernameKey).trim(); + result.password = getProperty(passwordKey); + } + + return result; + } + + public String getGlobalUsername() { + return getProperty("glom.document.username").trim(); + } + + public String getGlobalPassword() { + return getProperty("glom.document.password"); + } + + public String getGlobalLocale() { + return getProperty("glom.document.locale"); + } + + public String getDocumentsDirectory() { + return getProperty("glom.document.directory"); + } } private static final String GLOM_FILE_EXTENSION = "glom"; @@ -432,7 +486,7 @@ public class OnlineGlomServiceImpl extends RemoteServiceServlet implements Onlin config.load(is); // can throw an IOException // check if we can read the configured glom file directory - final String documentDirName = config.getProperty("glom.document.directory"); + final String documentDirName = config.getDocumentsDirectory(); final File documentDir = new File(documentDirName); if (!documentDir.isDirectory()) { final String errorMessage = documentDirName + " is not a directory."; @@ -462,7 +516,7 @@ public class OnlineGlomServiceImpl extends RemoteServiceServlet implements Onlin // Check for a specified default locale, // for table titles, field titles, etc: - final String globalLocaleID = StringUtils.defaultString(config.getProperty("glom.document.locale")); + final String globalLocaleID = StringUtils.defaultString(config.getGlobalLocale()); for (final File glomFile : glomFiles) { final Document document = new Document(); @@ -478,24 +532,21 @@ public class OnlineGlomServiceImpl extends RemoteServiceServlet implements Onlin final ConfiguredDocument configuredDocument = new ConfiguredDocument(document); // can throw a // PropertyVetoException + final String globalUserName = config.getGlobalUsername(); + final String globalPassword = config.getGlobalPassword(); + // check if a username and password have been set and work for the current document final String filename = glomFile.getName(); - final String key = config.getKey(filename); - if (key != null) { - final String[] keyArray = key.split("\\."); - if (keyArray.length == 3 && "filename".equals(keyArray[2])) { - // username/password could be set, let's check to see if it works - final String usernameKey = key.replaceAll(keyArray[2], "username"); - final String passwordKey = key.replaceAll(keyArray[2], "password"); - configuredDocument.setUsernameAndPassword(config.getProperty(usernameKey).trim(), - config.getProperty(passwordKey)); // can throw an SQLException - } + + // Username/password could be set. Let's check to see if it works. + final OnlineGlomProperties.Credentials docCredentials = config.getCredentials(filename); + if(docCredentials != null) { + configuredDocument.setUsernameAndPassword(docCredentials.userName, docCredentials.password); // can throw an SQLException } // check the if the global username and password have been set and work with this document if (!configuredDocument.isAuthenticated()) { - configuredDocument.setUsernameAndPassword(config.getProperty("glom.document.username").trim(), - config.getProperty("glom.document.password")); // can throw an SQLException + configuredDocument.setUsernameAndPassword(globalUserName, globalPassword); // can throw an SQLException } if (!StringUtils.isEmpty(globalLocaleID)) { -- 2.1.4