KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URL JavaDoc;
22 import java.net.URLConnection JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Properties JavaDoc;
25
26 import org.springframework.core.io.Resource;
27 import org.springframework.util.Assert;
28 import org.springframework.util.ClassUtils;
29
30 /**
31  * Convenient utility methods for loading of <code>java.util.Properties</code>,
32  * performing standard handling of input streams.
33  *
34  * <p>For more configurable properties loading, including the option of a
35  * customized encoding, consider using the PropertiesLoaderSupport class.
36  *
37  * @author Juergen Hoeller
38  * @author Rob Harrop
39  * @since 2.0
40  * @see PropertiesLoaderSupport
41  */

42 public abstract class PropertiesLoaderUtils {
43
44     /**
45      * Load properties from the given resource.
46      * @param resource the resource to load from
47      * @return the populated Properties instance
48      * @throws IOException if loading failed
49      */

50     public static Properties JavaDoc loadProperties(Resource resource) throws IOException JavaDoc {
51         Properties JavaDoc props = new Properties JavaDoc();
52         fillProperties(props, resource);
53         return props;
54     }
55
56     /**
57      * Fill the given properties from the given resource.
58      * @param props the Properties instance to fill
59      * @param resource the resource to load from
60      * @throws IOException if loading failed
61      */

62     public static void fillProperties(Properties JavaDoc props, Resource resource) throws IOException JavaDoc {
63         InputStream JavaDoc is = resource.getInputStream();
64         try {
65             props.load(is);
66         }
67         finally {
68             is.close();
69         }
70     }
71
72     /**
73      * Load all properties from the given class path resource,
74      * using the default class loader.
75      * <p>Merges properties if more than one resource of the same name
76      * found in the class path.
77      * @param resourceName the name of the class path resource
78      * @return the populated Properties instance
79      * @throws IOException if loading failed
80      */

81     public static Properties JavaDoc loadAllProperties(String JavaDoc resourceName) throws IOException JavaDoc {
82         return loadAllProperties(resourceName, null);
83     }
84
85     /**
86      * Load all properties from the given class path resource,
87      * using the given class loader.
88      * <p>Merges properties if more than one resource of the same name
89      * found in the class path.
90      * @param resourceName the name of the class path resource
91      * @param classLoader the ClassLoader to use for loading
92      * (or <code>null</code> to use the default class loader)
93      * @return the populated Properties instance
94      * @throws IOException if loading failed
95      */

96     public static Properties JavaDoc loadAllProperties(String JavaDoc resourceName, ClassLoader JavaDoc classLoader) throws IOException JavaDoc {
97         Assert.notNull(resourceName, "Resource name must not be null");
98         ClassLoader JavaDoc clToUse = classLoader;
99         if (clToUse == null) {
100             clToUse = ClassUtils.getDefaultClassLoader();
101         }
102         Properties JavaDoc properties = new Properties JavaDoc();
103         Enumeration JavaDoc urls = clToUse.getResources(resourceName);
104         while (urls.hasMoreElements()) {
105             URL JavaDoc url = (URL JavaDoc) urls.nextElement();
106             InputStream JavaDoc is = null;
107             try {
108                 URLConnection JavaDoc con = url.openConnection();
109                 con.setUseCaches(false);
110                 is = con.getInputStream();
111                 properties.load(is);
112             }
113             finally {
114                 if (is != null) {
115                     is.close();
116                 }
117             }
118         }
119         return properties;
120     }
121
122 }
123
Popular Tags