KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > config > GlobalSettings


1 /*
2  * Copyright 2005-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.dozer.util.mapping.config;
17
18 import java.io.IOException JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.Properties JavaDoc;
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 /**
32  * @author tierney.matt
33  */

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 JavaDoc 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 JavaDoc getLoadedByFileName() {
62     return loadedByFileName;
63   }
64
65   private synchronized Settings loadSettings() {
66     Settings result = new Settings();
67     MappingUtils utils = new MappingUtils();
68
69     //Determine prop file name
70
String JavaDoc 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     //Load prop file. Prop file is optional, so if it's not found just use defaults
77
Loader loader = new Loader();
78     URL JavaDoc 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 JavaDoc props = new Properties JavaDoc();
87     try {
88       InitLogger.log(log,"Reading properties from URL [" + url + "]");
89       props.load(url.openStream());
90     } catch (IOException JavaDoc e) {
91       throw new MappingException("Problem loading properties from URL [" + propFileName + "]", e);
92     }
93     
94     //Populate settings from loaded properties
95
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