1 16 17 package org.springframework.core.io.support; 18 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 import java.net.URL ; 22 import java.net.URLConnection ; 23 import java.util.Enumeration ; 24 import java.util.Properties ; 25 26 import org.springframework.core.io.Resource; 27 import org.springframework.util.Assert; 28 import org.springframework.util.ClassUtils; 29 30 42 public abstract class PropertiesLoaderUtils { 43 44 50 public static Properties loadProperties(Resource resource) throws IOException { 51 Properties props = new Properties (); 52 fillProperties(props, resource); 53 return props; 54 } 55 56 62 public static void fillProperties(Properties props, Resource resource) throws IOException { 63 InputStream is = resource.getInputStream(); 64 try { 65 props.load(is); 66 } 67 finally { 68 is.close(); 69 } 70 } 71 72 81 public static Properties loadAllProperties(String resourceName) throws IOException { 82 return loadAllProperties(resourceName, null); 83 } 84 85 96 public static Properties loadAllProperties(String resourceName, ClassLoader classLoader) throws IOException { 97 Assert.notNull(resourceName, "Resource name must not be null"); 98 ClassLoader clToUse = classLoader; 99 if (clToUse == null) { 100 clToUse = ClassUtils.getDefaultClassLoader(); 101 } 102 Properties properties = new Properties (); 103 Enumeration urls = clToUse.getResources(resourceName); 104 while (urls.hasMoreElements()) { 105 URL url = (URL ) urls.nextElement(); 106 InputStream is = null; 107 try { 108 URLConnection 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 |