Author: taher
Date: Tue Dec 20 19:26:11 2016 New Revision: 1775340 URL: http://svn.apache.org/viewvc?rev=1775340&view=rev Log: Improved: Refactored Config.java to simplify fields initialization Improvements remove in total roughly 70 lines of code and includes: - moving all default values to the constructor to make it clear - refactor getProperty to use the Optional monad - remove FileInputStream from getPropertiesFile as it is not used - properly indent conditions in determineOfbizPropertiesFileName - simplify locale creation - remove dependency on base (StringUtil) - other minor renamings and formatting Modified: ofbiz/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Config.java Modified: ofbiz/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Config.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Config.java?rev=1775340&r1=1775339&r2=1775340&view=diff ============================================================================== --- ofbiz/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Config.java (original) +++ ofbiz/trunk/framework/start/src/main/java/org/apache/ofbiz/base/start/Config.java Tue Dec 20 19:26:11 2016 @@ -18,20 +18,17 @@ *******************************************************************************/ package org.apache.ofbiz.base.start; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.InetAddress; import java.net.UnknownHostException; -import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Locale; +import java.util.Optional; import java.util.Properties; import java.util.TimeZone; -import org.apache.ofbiz.base.util.StringUtil; - /** * OFBiz server parameters needed on system startup and retrieved from * one of the properties files in the start component @@ -55,16 +52,17 @@ public final class Config { Properties props = getPropertiesFile(ofbizCommands);; // set this class fields - ofbizHome = getOfbizHome(props); - adminAddress = getAdminAddress(props); + ofbizHome = getOfbizHome(getProperty(props, "ofbiz.home", ".")); + adminAddress = getAdminAddress(getProperty(props, "ofbiz.admin.host", "127.0.0.1")); adminKey = getProperty(props, "ofbiz.admin.key", "so3du5kasd5dn"); - portOffset = getPortOffsetValue(ofbizCommands); - adminPort = getAdminPort(props, portOffset); - containerConfig = getAbsolutePath(props, "ofbiz.container.config", "framework/base/config/ofbiz-containers.xml", ofbizHome); - loaders = getLoaders(props); + portOffset = getPortOffsetValue(ofbizCommands, "0"); + adminPort = getAdminPort(props, 10523, portOffset); // see INFRA-6790 + containerConfig = getAbsolutePath(props, "ofbiz.container.config", + "framework/base/config/ofbiz-containers.xml", ofbizHome); + loaders = Arrays.asList(getProperty(props, "ofbiz.start.loaders", "").split(",")); logDir = getAbsolutePath(props, "ofbiz.log.dir", "runtime/logs", ofbizHome); - shutdownAfterLoad = isShutdownAfterLoad(props); - useShutdownHook = isUseShutdownHook(props); + shutdownAfterLoad = getProperty(props, "ofbiz.auto.shutdown", "false").equalsIgnoreCase("true"); + useShutdownHook = getProperty(props, "ofbiz.enable.hook", "true").equalsIgnoreCase("true"); System.out.println("Set OFBIZ_HOME to - " + ofbizHome); @@ -74,29 +72,24 @@ public final class Config { System.setProperty("derby.system.home", getProperty(props, "derby.system.home", "runtime/data/derby")); // set the default locale - setDefaultLocale(props); + setDefaultLocale(getProperty(props, "ofbiz.locale.default", "")); // set the default timezone String tzString = props.getProperty("ofbiz.timeZone.default", TimeZone.getDefault().getID()); TimeZone.setDefault(TimeZone.getTimeZone(tzString)); } - private String getAbsolutePath(Properties props, String key, String def, String ofbizHome) { - String value = System.getProperty(key); - if (value != null) { - return value; - } else { - return ofbizHome + "/" + props.getProperty(key, def); - } + private String getProperty(Properties props, String key, String defaultValue) { + return Optional.ofNullable(System.getProperty(key)) + .orElse(props.getProperty(key, defaultValue)); } - private String getProperty(Properties props, String key, String defaultValue) { - String value = System.getProperty(key); - if (value != null) { - return value; - } else { - return props.getProperty(key, defaultValue); - } + private String getOfbizHome(String homeProp) { + return homeProp.equals(".") ? System.getProperty("user.dir").replace('\\', '/') : homeProp; + } + + private String getAbsolutePath(Properties props, String key, String def, String ofbizHome) { + return getProperty(props, key, ofbizHome + "/" + props.getProperty(key, def)); } private Properties getPropertiesFile(List<StartupCommand> ofbizCommands) throws StartupException { @@ -105,37 +98,23 @@ public final class Config { Properties props = new Properties(); try (InputStream propsStream = getClass().getClassLoader().getResourceAsStream(fullyQualifiedFileName)) { - // first try classpath - if (propsStream != null) { - props.load(propsStream); - } else { - // next try file location - try(FileInputStream fis = new FileInputStream(new File(fullyQualifiedFileName))) { - if (fis != null) { - props.load(fis); - } - } - } + props.load(propsStream); } catch (IOException e) { throw new StartupException(e); } - // check for empty properties - if (props.isEmpty()) { - throw new StartupException("Cannot load configuration properties : " + fullyQualifiedFileName); - } - System.out.println("Start.java using configuration file " + fullyQualifiedFileName); + System.out.println("Config.java using configuration file " + fileName); return props; } private String determineOfbizPropertiesFileName(List<StartupCommand> ofbizCommands) { String fileName = null; if (ofbizCommands.stream().anyMatch(command -> - command.getName().equals(StartupCommandUtil.StartupOption.START.getName()) - || command.getName().equals(StartupCommandUtil.StartupOption.SHUTDOWN.getName()) - || command.getName().equals(StartupCommandUtil.StartupOption.STATUS.getName())) - || ofbizCommands.isEmpty() - || ofbizCommands.stream().allMatch(command -> + command.getName().equals(StartupCommandUtil.StartupOption.START.getName()) + || command.getName().equals(StartupCommandUtil.StartupOption.SHUTDOWN.getName()) + || command.getName().equals(StartupCommandUtil.StartupOption.STATUS.getName())) + || ofbizCommands.isEmpty() + || ofbizCommands.stream().allMatch(command -> command.getName().equals(StartupCommandUtil.StartupOption.PORTOFFSET.getName()))) { fileName = "start.properties"; } else if(ofbizCommands.stream().anyMatch( @@ -148,39 +127,12 @@ public final class Config { return fileName; } - private List<String> getLoaders(Properties props) { - String loadersProp = getProperty(props, "ofbiz.start.loaders", ""); - List<String> loaders = new ArrayList<String>(); - for(String loader : StringUtil.split(loadersProp, ",")) { - loaders.add(loader); - } - return loaders; - } - - private void setDefaultLocale(Properties props) { - String localeString = props.getProperty("ofbiz.locale.default"); - if (localeString != null && localeString.length() > 0) { - String locales[] = localeString.split("_"); - switch (locales.length) { - case 1: - Locale.setDefault(new Locale(locales[0])); - break; - case 2: - Locale.setDefault(new Locale(locales[0], locales[1])); - break; - case 3: - Locale.setDefault(new Locale(locales[0], locales[1], locales[2])); - } - System.setProperty("user.language", localeString); - } - } - - private int getPortOffsetValue(List<StartupCommand> ofbizCommands) throws StartupException { + private int getPortOffsetValue(List<StartupCommand> ofbizCommands, String defaultOffset) throws StartupException { String extractedPortOffset = ofbizCommands.stream() .filter(command -> command.getName().equals(StartupCommandUtil.StartupOption.PORTOFFSET.getName())) .findFirst() .map(ofbizCommand -> ofbizCommand.getProperties().keySet().iterator().next()) - .orElse("0"); + .orElse(defaultOffset); try { return Integer.parseInt(extractedPortOffset); } catch(NumberFormatException e) { @@ -188,55 +140,36 @@ public final class Config { } } - private InetAddress getAdminAddress(Properties props) throws StartupException { - String serverHost = getProperty(props, "ofbiz.admin.host", "127.0.0.1"); + private int getAdminPort(Properties props, int defaultAdminPort, int portOffsetValue) { + String adminPortStr = getProperty(props, "ofbiz.admin.port", String.valueOf(defaultAdminPort)); try { - return InetAddress.getByName(serverHost); - } catch (UnknownHostException e) { - throw new StartupException(e); + return Integer.parseInt(adminPortStr) + portOffsetValue; + } catch (NumberFormatException e) { + System.out.println("Error parsing admin port: " + adminPortStr + " -- " + e.getMessage()); + return defaultAdminPort + portOffsetValue; } } - private int getAdminPort(Properties props, int portOffsetValue) { - String adminPortStr = getProperty(props, "ofbiz.admin.port", "0"); - int calculatedAdminPort; + private InetAddress getAdminAddress(String serverHost) throws StartupException { try { - calculatedAdminPort = Integer.parseInt(adminPortStr); - calculatedAdminPort = calculatedAdminPort != 0 ? calculatedAdminPort : 10523; // This is necessary because the ASF machines don't allow ports 1 to 3, see INFRA-6790 - calculatedAdminPort += portOffsetValue; - } catch (Exception e) { - System.out.println("Error while parsing admin port number (so default to 10523) = " + e); - calculatedAdminPort = 10523; - } - return calculatedAdminPort; - } - - private String getOfbizHome(Properties props) { - String extractedOfbizHome = props.getProperty("ofbiz.home", "."); - if (extractedOfbizHome.equals(".")) { - extractedOfbizHome = System.getProperty("user.dir"); - extractedOfbizHome = extractedOfbizHome.replace('\\', '/'); - } - return extractedOfbizHome; - } - - private boolean isShutdownAfterLoad(Properties props) { - if (System.getProperty("ofbiz.auto.shutdown") != null && System.getProperty("ofbiz.auto.shutdown").length() > 0) { - return "true".equalsIgnoreCase(System.getProperty("ofbiz.auto.shutdown")); - } else if (props.getProperty("ofbiz.auto.shutdown") != null && props.getProperty("ofbiz.auto.shutdown").length() > 0) { - return "true".equalsIgnoreCase(props.getProperty("ofbiz.auto.shutdown")); - } else { - return false; + return InetAddress.getByName(serverHost); + } catch (UnknownHostException e) { + throw new StartupException(e); } } - private boolean isUseShutdownHook(Properties props) { - if (System.getProperty("ofbiz.enable.hook") != null && System.getProperty("ofbiz.enable.hook").length() > 0) { - return "true".equalsIgnoreCase(System.getProperty("ofbiz.enable.hook")); - } else if (props.getProperty("ofbiz.enable.hook") != null && props.getProperty("ofbiz.enable.hook").length() > 0) { - return "true".equalsIgnoreCase(props.getProperty("ofbiz.enable.hook")); - } else { - return true; + private void setDefaultLocale(String localeString) { + String locales[] = localeString.split("_"); + switch (locales.length) { + case 1: + Locale.setDefault(new Locale(locales[0])); + break; + case 2: + Locale.setDefault(new Locale(locales[0], locales[1])); + break; + case 3: + Locale.setDefault(new Locale(locales[0], locales[1], locales[2])); } + System.setProperty("user.language", localeString); } } |
Free forum by Nabble | Edit this page |