/****************************************************************************** * * Copyright: Intellectual Property of Four Elements Capital Pte Ltd, Singapore. * All rights reserved. * ******************************************************************************/ package com.fourelementscapital.db; import java.io.File; import java.io.FileInputStream; import java.util.MissingResourceException; import java.util.Properties; import java.util.ResourceBundle; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; /** * This class populates the configuration value from property file */ class Config { /** * Configuration file */ private static final String BUNDLE_NAME = "config_db"; //$NON-NLS-1$ /** * windows peer configuration */ private static final String PROPERTY_FILE_WIN = "peer_db_windows.properties"; //$NON-NLS-1$ /** * unix peer/server configuration file */ private static final String PROPERTY_FILE_UNIX = "peer_db_unix.properties"; //$NON-NLS-1$ public static String CONFIG_PROPERTY_LOCATION=null; private static Properties confpro=null; /** * Private constructor */ private Config() { } /** * Get property value of specified key. Return '!key!' if not found. * @param key key * @return property value */ protected static String getString(String key) { if(CONFIG_PROPERTY_LOCATION==null){ try { //return RESOURCE_BUNDLE.getString(key); return getResourceBuddle().getString(key); } catch (MissingResourceException e) { return '!' + key + '!'; } }else{ try { String ky= getPeerProperty(key); if(ky==null) throw new Exception(); else return ky; } catch (Exception e) { return '!' + key + '!'; } } } private static ResourceBundle resourceBundle=null; /** * Get resources bundle * @return resources bundle */ private synchronized static ResourceBundle getResourceBuddle(){ if(resourceBundle==null){ resourceBundle=ResourceBundle.getBundle(BUNDLE_NAME); LogManager.getLogger(Config.class.getName()).info("resourceBundle:"+resourceBundle); } return resourceBundle; } /** * Get property value of specified key. Return null if not found. * @param key key * @return property value */ protected static String getValue(String key) { if(CONFIG_PROPERTY_LOCATION==null){ try { return getResourceBuddle().getString(key); } catch (MissingResourceException e) { return null; } }else{ try { return getPeerProperty(key); } catch (Exception e) { return null; } } } /** * Get peer property by key * @param key key * @return peer property */ private static String getPeerProperty(String key) throws Exception { try{ if(confpro==null){ String propertyfilename=PROPERTY_FILE_WIN; if(System.getProperty("os.name").toLowerCase().equals("freebsd")) propertyfilename=PROPERTY_FILE_UNIX; if(System.getProperty("os.name").toLowerCase().contains("linux")) propertyfilename=PROPERTY_FILE_UNIX; String folder=CONFIG_PROPERTY_LOCATION; folder=folder.endsWith(File.separator)?folder:folder+File.separator; confpro=new Properties(); String filename=folder+"conf"+File.separator+propertyfilename; confpro.load(new FileInputStream(filename)); } return confpro.getProperty(key); }catch(Exception e){ e.printStackTrace(); throw e; } } }