1 16 17 package org.springframework.core.io.support; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.io.InputStreamReader ; 22 import java.util.Enumeration ; 23 import java.util.Properties ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 28 import org.springframework.core.io.Resource; 29 import org.springframework.util.DefaultPropertiesPersister; 30 import org.springframework.util.PropertiesPersister; 31 import org.springframework.util.CollectionUtils; 32 33 41 public abstract class PropertiesLoaderSupport { 42 43 public static final String XML_FILE_EXTENSION = ".xml"; 44 45 46 47 protected final Log logger = LogFactory.getLog(getClass()); 48 49 private Properties [] localProperties; 50 51 private Resource[] locations; 52 53 private boolean localOverride = false; 54 55 private boolean ignoreResourceNotFound = false; 56 57 private String fileEncoding; 58 59 private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister(); 60 61 62 67 public void setProperties(Properties properties) { 68 this.localProperties = new Properties [] {properties}; 69 } 70 71 75 public void setPropertiesArray(Properties [] propertiesArray) { 76 this.localProperties = propertiesArray; 77 } 78 79 84 public void setLocation(Resource location) { 85 this.locations = new Resource[] {location}; 86 } 87 88 93 public void setLocations(Resource[] locations) { 94 this.locations = locations; 95 } 96 97 103 public void setLocalOverride(boolean localOverride) { 104 this.localOverride = localOverride; 105 } 106 107 112 public void setIgnoreResourceNotFound(boolean ignoreResourceNotFound) { 113 this.ignoreResourceNotFound = ignoreResourceNotFound; 114 } 115 116 123 public void setFileEncoding(String encoding) { 124 this.fileEncoding = encoding; 125 } 126 127 132 public void setPropertiesPersister(PropertiesPersister propertiesPersister) { 133 this.propertiesPersister = 134 (propertiesPersister != null ? propertiesPersister : new DefaultPropertiesPersister()); 135 } 136 137 138 142 protected Properties mergeProperties() throws IOException { 143 Properties result = new Properties (); 144 145 if (this.localOverride) { 146 loadProperties(result); 148 } 149 150 if (this.localProperties != null) { 151 for (int i = 0; i < this.localProperties.length; i++) { 152 CollectionUtils.mergePropertiesIntoMap(this.localProperties[i], result); 153 } 154 } 155 156 if (!this.localOverride) { 157 loadProperties(result); 159 } 160 161 return result; 162 } 163 164 170 protected void loadProperties(Properties props) throws IOException { 171 if (this.locations != null) { 172 for (int i = 0; i < this.locations.length; i++) { 173 Resource location = this.locations[i]; 174 if (logger.isInfoEnabled()) { 175 logger.info("Loading properties file from " + location); 176 } 177 InputStream is = null; 178 try { 179 is = location.getInputStream(); 180 if (location.getFilename().endsWith(XML_FILE_EXTENSION)) { 181 this.propertiesPersister.loadFromXml(props, is); 182 } 183 else { 184 if (this.fileEncoding != null) { 185 this.propertiesPersister.load(props, new InputStreamReader (is, this.fileEncoding)); 186 } 187 else { 188 this.propertiesPersister.load(props, is); 189 } 190 } 191 } 192 catch (IOException ex) { 193 if (this.ignoreResourceNotFound) { 194 if (logger.isWarnEnabled()) { 195 logger.warn("Could not load properties from " + location + ": " + ex.getMessage()); 196 } 197 } 198 else { 199 throw ex; 200 } 201 } 202 finally { 203 if (is != null) { 204 is.close(); 205 } 206 } 207 } 208 } 209 } 210 211 } 212 | Popular Tags |