1 16 package net.sf.dozer.util.mapping.config; 17 18 import java.io.IOException ; 19 import java.net.URL ; 20 import java.util.Properties ; 21 22 import net.sf.dozer.util.mapping.MappingException; 23 import net.sf.dozer.util.mapping.util.InitLogger; 24 import net.sf.dozer.util.mapping.util.Loader; 25 import net.sf.dozer.util.mapping.util.MapperConstants; 26 import net.sf.dozer.util.mapping.util.MappingUtils; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 31 34 public class GlobalSettings { 35 36 private static final Log log = LogFactory.getLog(GlobalSettings.class); 37 private static GlobalSettings singleton; 38 39 private final Settings settings; 40 private String loadedByFileName; 41 42 public static synchronized GlobalSettings getInstance() { 43 if (singleton == null) { 44 singleton = new GlobalSettings(); 45 } 46 return singleton; 47 } 48 49 protected static GlobalSettings createNew() { 50 return new GlobalSettings(); 51 } 52 53 private GlobalSettings() { 54 settings = loadSettings(); 55 } 56 57 public Settings getSettings() { 58 return settings; 59 } 60 61 protected String getLoadedByFileName() { 62 return loadedByFileName; 63 } 64 65 private synchronized Settings loadSettings() { 66 Settings result = new Settings(); 67 MappingUtils utils = new MappingUtils(); 68 69 String propFileName = System.getProperty(MapperConstants.CONFIG_FILE_SYS_PROP); 71 if (utils.isBlankOrNull(propFileName)) { 72 propFileName = MapperConstants.DEFAULT_CONFIG_FILE; 73 } 74 75 InitLogger.log(log,"Trying to find configuration file: " + propFileName); 76 Loader loader = new Loader(); 78 URL url = loader.getResource(propFileName); 79 if (url == null) { 80 InitLogger.log(log,"Configuration file not found: " + propFileName + ". Using defaults for all global properties."); 81 return result; 82 } else { 83 InitLogger.log(log,"Using URL [" + url + "] for global property configuration"); 84 } 85 86 Properties props = new Properties (); 87 try { 88 InitLogger.log(log,"Reading properties from URL [" + url + "]"); 89 props.load(url.openStream()); 90 } catch (IOException e) { 91 throw new MappingException("Problem loading properties from URL [" + propFileName + "]", e); 92 } 93 94 SettingsHelper.populateSettingsFromProperties(result, props); 96 loadedByFileName = propFileName; 97 InitLogger.log(log,"Finished configuring global properties"); 98 99 return result; 100 } 101 102 } 103 | Popular Tags |