KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > io > support > PropertiesLoaderSupport


1 /*
2  * Copyright 2002-2006 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
17 package org.springframework.core.io.support;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.InputStreamReader JavaDoc;
22 import java.util.Enumeration JavaDoc;
23 import java.util.Properties JavaDoc;
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 /**
34  * Base class for JavaBean-style components that need to load properties
35  * from one or more resources. Supports local properties as well, with
36  * configurable overriding.
37  *
38  * @author Juergen Hoeller
39  * @since 1.2.2
40  */

41 public abstract class PropertiesLoaderSupport {
42
43     public static final String JavaDoc XML_FILE_EXTENSION = ".xml";
44
45
46     /** Logger available to subclasses */
47     protected final Log logger = LogFactory.getLog(getClass());
48
49     private Properties JavaDoc[] localProperties;
50
51     private Resource[] locations;
52
53     private boolean localOverride = false;
54
55     private boolean ignoreResourceNotFound = false;
56
57     private String JavaDoc fileEncoding;
58
59     private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
60
61
62     /**
63      * Set local properties, e.g. via the "props" tag in XML bean definitions.
64      * These can be considered defaults, to be overridden by properties
65      * loaded from files.
66      */

67     public void setProperties(Properties JavaDoc properties) {
68         this.localProperties = new Properties JavaDoc[] {properties};
69     }
70
71     /**
72      * Set local properties, e.g. via the "props" tag in XML bean definitions,
73      * allowing for merging multiple properties sets into one.
74      */

75     public void setPropertiesArray(Properties JavaDoc[] propertiesArray) {
76         this.localProperties = propertiesArray;
77     }
78
79     /**
80      * Set a location of a properties file to be loaded.
81      * <p>Can point to a classic properties file or to an XML file
82      * that follows JDK 1.5's properties XML format.
83      */

84     public void setLocation(Resource location) {
85         this.locations = new Resource[] {location};
86     }
87
88     /**
89      * Set locations of properties files to be loaded.
90      * <p>Can point to classic properties files or to XML files
91      * that follow JDK 1.5's properties XML format.
92      */

93     public void setLocations(Resource[] locations) {
94         this.locations = locations;
95     }
96
97     /**
98      * Set whether local properties override properties from files.
99      * Default is "false": properties from files override local defaults.
100      * Can be switched to "true" to let local properties override defaults
101      * from files.
102      */

103     public void setLocalOverride(boolean localOverride) {
104         this.localOverride = localOverride;
105     }
106
107     /**
108      * Set if failure to find the property resource should be ignored.
109      * True is appropriate if the properties file is completely optional.
110      * Default is "false".
111      */

112     public void setIgnoreResourceNotFound(boolean ignoreResourceNotFound) {
113         this.ignoreResourceNotFound = ignoreResourceNotFound;
114     }
115
116     /**
117      * Set the encoding to use for parsing properties files.
118      * <p>Default is none, using the <code>java.util.Properties</code>
119      * default encoding.
120      * <p>Only applies to classic properties files, not to XML files.
121      * @see org.springframework.util.PropertiesPersister#load
122      */

123     public void setFileEncoding(String JavaDoc encoding) {
124         this.fileEncoding = encoding;
125     }
126
127     /**
128      * Set the PropertiesPersister to use for parsing properties files.
129      * The default is DefaultPropertiesPersister.
130      * @see org.springframework.util.DefaultPropertiesPersister
131      */

132     public void setPropertiesPersister(PropertiesPersister propertiesPersister) {
133         this.propertiesPersister =
134                 (propertiesPersister != null ? propertiesPersister : new DefaultPropertiesPersister());
135     }
136
137
138     /**
139      * Return a merged Properties instance containing both the
140      * loaded properties and properties set on this FactoryBean.
141      */

142     protected Properties JavaDoc mergeProperties() throws IOException JavaDoc {
143         Properties JavaDoc result = new Properties JavaDoc();
144
145         if (this.localOverride) {
146             // Load properties from file upfront, to let local properties override.
147
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             // Load properties from file afterwards, to let those properties override.
158
loadProperties(result);
159         }
160
161         return result;
162     }
163
164     /**
165      * Load properties into the given instance.
166      * @param props the Properties instance to load into
167      * @throws java.io.IOException in case of I/O errors
168      * @see #setLocations
169      */

170     protected void loadProperties(Properties JavaDoc props) throws IOException JavaDoc {
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 JavaDoc 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 JavaDoc(is, this.fileEncoding));
186                         }
187                         else {
188                             this.propertiesPersister.load(props, is);
189                         }
190                     }
191                 }
192                 catch (IOException JavaDoc 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