View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  
18  package ch.elca.el4j.util.codingsupport;
19  
20  import java.io.File;
21  import java.io.FileNotFoundException;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.util.Properties;
27  
28  import org.springframework.core.io.Resource;
29  import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
30  
31  import ch.elca.el4j.core.io.support.ListResourcePatternResolverDecorator;
32  import ch.elca.el4j.core.io.support.ManifestOrderedConfigLocationProvider;
33  import ch.elca.el4j.core.io.support.OrderedPathMatchingResourcePatternResolver;
34  import ch.elca.el4j.services.monitoring.notification.CoreNotificationHelper;
35  
36  /**
37   * A helper class which handles the loading and storing of Properties to/from
38   * files including Spring path resolving.
39   *
40   * <p>
41   * The files can be indicated absolutely or via classpath, i.e. either by
42   * "file:C:/folder/..." or by "classpath:folder/...".
43   *
44   * @svnLink $Revision: 3883 $;$Date: 2009-08-04 15:35:01 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/core/src/main/java/ch/elca/el4j/util/codingsupport/PropertiesHelper.java $
45   *
46   * @author Raphael Boog (RBO)
47   */
48  public class PropertiesHelper {
49  	
50  	/**
51  	 * Loads properties from given resources. Properties declared in late resources overwrite properties.
52  	 * declared in earlier resources. Invalid resources just get skipped.
53  	 * @param resources    the properties files
54  	 * @return             the merged properties
55  	 */
56  	public static Properties loadPropertiesFromResources(Resource[] resources) {
57  		Properties properties = new Properties();
58  		InputStream in = null;
59  		
60  		for (Resource resource : resources) {
61  			try {
62  				in = resource.getInputStream();
63  				properties.load(in);
64  			} catch (IOException e) {
65  				// ignore
66  			} finally {
67  				if (in != null) {
68  					try {
69  						in.close();
70  					} catch (IOException e) {
71  						CoreNotificationHelper.notifyMisconfiguration(
72  							"An IOException was thrown while loading properties from '"
73  							+ resource + "'.", e);
74  					}
75  				}
76  			}
77  		}
78  		
79  		return properties;
80  	}
81  	/**
82  	 * Resolves the given file name to an absolute file name and then loads the
83  	 * properties from this file to a Properties Object.
84  	 *
85  	 * @param inputFileName
86  	 *            The file(s) which will be loaded
87  	 * @return the Properties Object
88  	 */
89  	public Properties loadProperties(String inputFileName) {
90  		// downwards compatibility: append "file:" if it is missing and filename exists
91  		String resourceName = inputFileName;
92  		if (new File(inputFileName).exists() && !inputFileName.startsWith("file:")) {
93  			resourceName = "file:" + resourceName; 
94  		}
95  
96  		ListResourcePatternResolverDecorator resolver = new ListResourcePatternResolverDecorator(
97  			new ManifestOrderedConfigLocationProvider(),
98  			new OrderedPathMatchingResourcePatternResolver());
99  		// most specific resource has to be last, because later properties will overwrite previously set ones.
100 		resolver.setMostSpecificResourceLast(true);
101 		resolver.setMergeWithOuterResources(true);
102 		
103 		try {
104 			return loadPropertiesFromResources(resolver.getResources(resourceName));
105 		} catch (IOException e) {
106 			CoreNotificationHelper.notifyMisconfiguration(
107 				"An IOException was thrown. The responsible file is '"
108 				+ inputFileName + "'.", e);
109 			return null;
110 		}
111 	}
112 
113 	/**
114 	 * Resolves the given file name to an absolute file name and then stores the
115 	 * properties from the Properties Object to this file.
116 	 *
117 	 * @param props
118 	 *            The Properties Object
119 	 * @param outputFileName
120 	 *            The file where the data is stored
121 	 */
122 	public void storeProperties(Properties props, String outputFileName) {
123 
124 		PathMatchingResourcePatternResolver pmrpr
125 			= new PathMatchingResourcePatternResolver();
126 
127 		String fileName = null;
128 
129 		Resource res = pmrpr.getResource(outputFileName);
130 		OutputStream out = null;
131 		try {
132 			try {
133 				// Resolve the resource into an absolute file path
134 				fileName = res.getURL().getFile();
135 			} catch (FileNotFoundException e) {
136 				// The file is new
137 				File file = new File(outputFileName);
138 				fileName = file.getAbsolutePath();
139 			}
140 			out = new FileOutputStream(fileName);
141 			props.store(out, "Title");
142 		} catch (FileNotFoundException e) {
143 			CoreNotificationHelper.notifyMisconfiguration(
144 					"The file '" + outputFileName + "' could not be found.", e);
145 		} catch (IOException e) {
146 			CoreNotificationHelper.notifyMisconfiguration(
147 					"An IOException was thrown. The responsible file is '"
148 					+ outputFileName + "'.", e);
149 		} finally {
150 			if (out != null) {
151 				try {
152 					out.close();
153 				} catch (IOException e) {
154 					CoreNotificationHelper.notifyMisconfiguration(
155 						"The file '" + outputFileName
156 						+ "' could not be close.", e);
157 				}
158 			}
159 		}
160 
161 	}
162 
163 }