1 package net.myvietnam.mvncore.configuration; 2 3 56 57 import java.io.IOException ; 58 import java.io.InputStream ; 59 import java.net.MalformedURLException ; 60 import java.net.URL ; 61 62 import org.apache.commons.logging.Log; 63 import org.apache.commons.logging.LogFactory; 64 65 import org.apache.commons.lang.StringUtils; 66 67 96 public class PropertiesConfiguration 97 extends BasePropertiesConfiguration 98 implements Configuration 99 { 100 101 Log log = LogFactory.getLog(PropertiesConfiguration.class); 102 103 104 protected String fileSeparator = System.getProperty("file.separator"); 105 106 109 protected String fileName = null; 110 111 118 public PropertiesConfiguration() 119 { 120 setIncludesAllowed(false); 121 } 122 123 130 public PropertiesConfiguration(Configuration defaults) throws IOException 131 { 132 this(); 133 this.defaults = defaults; 134 } 135 136 144 public PropertiesConfiguration(String fileName) throws IOException 145 { 146 147 load(fileName); 148 } 149 150 155 public void load() throws IOException 156 { 157 load(getFileName()); 158 } 159 160 166 public void load(String fileName) throws IOException 167 { 168 load(getPropertyStream(fileName)); 169 } 170 171 178 public PropertiesConfiguration(String file, Configuration defaults) throws IOException 179 { 180 this(file); 181 this.defaults = defaults; 182 } 183 184 192 public PropertiesConfiguration(String file, String defaultFile) throws IOException 193 { 194 this(file); 195 if (StringUtils.isNotEmpty(defaultFile)) 196 { 197 this.defaults = new PropertiesConfiguration(defaultFile); 198 } 199 } 200 201 209 public InputStream getPropertyStream(String resourceName) throws IOException 210 { 211 InputStream resource = null; 212 URL url = null; 213 214 try 215 { 216 url = ConfigurationUtils.getURL(getBasePath(), resourceName); 217 } 218 catch(MalformedURLException uex) 219 { 220 throw new IOException ("Cannot obtain URL for resource " 221 + resourceName); 222 } 223 224 resource = url.openStream(); 225 226 setBasePath(url.toString()); 227 setIncludesAllowed(true); 228 229 return resource; 230 } 231 232 236 public String getFileName() 237 { 238 return fileName; 239 } 240 241 245 public void setFileName(String fileName) 246 { 247 this.fileName = fileName; 248 } 249 250 256 public void setBasePath(String basePath) 257 { 258 super.setBasePath(basePath); 259 setIncludesAllowed(StringUtils.isNotEmpty(basePath)); 260 } 261 } 262 | Popular Tags |