View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2003-2008 Frederico Caldeira Knabben
4    * 
5    * == BEGIN LICENSE ==
6    * 
7    * Licensed under the terms of any of the following licenses at your
8    * choice:
9    * 
10   *  - GNU General Public License Version 2 or later (the "GPL")
11   *    http://www.gnu.org/licenses/gpl.html
12   * 
13   *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   *    http://www.gnu.org/licenses/lgpl.html
15   * 
16   *  - Mozilla Public License Version 1.1 or later (the "MPL")
17   *    http://www.mozilla.org/MPL/MPL-1.1.html
18   * 
19   * == END LICENSE ==
20   */
21  package net.fckeditor.handlers;
22  
23  import java.io.BufferedInputStream;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.Properties;
27  
28  import net.fckeditor.tool.Utils;
29  
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   * Handler to hold the basic properties.<br>
35   * The main default file is 'default.properties' in the deepth of the classpath and should be
36   * untouched. If there is a file named 'fckeditor.properties' in the root of the classpath, it will
37   * be loaded. Values which are loaded before, will be overwritten.<br>
38   * If you won't use an extra properties file to adjust the defaults, you can use
39   * {@link #setProperty(String, String)} instead.
40   * 
41   * @version $Id: PropertiesLoader.java 1685 2008-03-09 13:13:40Z th-schwarz $
42   */
43  public class PropertiesLoader {
44  	private static final Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
45  	private static Properties properties = new Properties();
46  
47  	static {
48  		try {
49  			// 1. load system defaults
50  			properties.load(new BufferedInputStream(PropertiesLoader.class
51  			        .getResourceAsStream("default.properties")));
52  
53  			// 2. load user defaults
54  			InputStream in = PropertiesLoader.class.getResourceAsStream("/fckeditor.properties");
55  			if (in == null)
56  				logger.warn("Can't find user properties!");
57  			else {
58  				try {
59  					properties.load(new BufferedInputStream(in));
60  					logger.info("User's properties loaded successfully!");
61  				} catch (IOException e) {
62  					logger.error("Error while loading user properties!", e);
63  					throw new RuntimeException("Can't load user properties!", e);
64  				}
65  			}
66  		} catch (IOException e) {
67  			logger.error("Error while loading default properties!", e);
68  			throw new RuntimeException("Can't load default properties!", e);
69  		}
70  	}
71  
72  	/**
73  	 * Getter for a property of 'key'.
74  	 * 
75  	 * @param key
76  	 *            the propery key
77  	 * @return the value in this property list with the specified key value.
78  	 * @see Properties#getProperty(String)
79  	 */
80  	public static String getProperty(final String key) {
81  		return properties.getProperty(key);
82  	}
83  
84  	/**
85  	 * Setter for a property. If the property already exists, the value will be overwritten.<br>
86  	 * Hint: This method is intended for an alternative way to set user defaults programmatically
87  	 * instead of using the 'fckeditor.properties'. It should never used inside FCKeditor.Java !!!
88  	 * 
89  	 * @param key
90  	 *            key the propery key
91  	 * @param value
92  	 * @throws IllegalArgumentException
93  	 *             if 'key' is empty.
94  	 * @see Properties#setProperty(String, String)
95  	 */
96  	public static void setProperty(final String key, final String value) {
97  		if (Utils.isEmpty(key))
98  			new IllegalArgumentException("The 'key' of a property schouldn't be null!");
99  		properties.setProperty(key, value);
100 	}
101 }