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 org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * This handler gives you access to properties stored in
33   * <code>/net/fckeditor/handlers/default.properties</code> and
34   * <code>/fckeditor.properties</code>.<br />
35   * This class loads the properties files as follows:
36   * <ol>
37   * <li>Load <code>default.properties</code></li>
38   * <li>Load <code>fckeditor.properties</code> if present.</li>
39   * </ol>
40   * <em>Attention</em>: Properties specified in
41   * <code>fckeditor.properties</code> will override properties loaded from
42   * <code>default.properties</code>. (intended behavior)<br />
43   * <em>Hint</em>: You may set properties programmatically with
44   * {@link #setProperty(String, String)} instead or additionally.
45   * 
46   * @version $Id: PropertiesLoader.java 2101 2008-06-22 22:00:48Z mosipov $
47   */
48  public class PropertiesLoader {
49  	private static final Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
50  	private static Properties properties = new Properties();
51  
52  	static {
53  		try {
54  			// 1. load library defaults
55  			properties.load(new BufferedInputStream(PropertiesLoader.class
56  			        .getResourceAsStream("default.properties")));
57  
58  			// 2. load user defaults
59  			InputStream in = PropertiesLoader.class.getResourceAsStream("/fckeditor.properties");
60  			if (in == null)
61  				logger.warn("Can't find fckeditor.properties!");
62  			else {
63  				try {
64  					properties.load(new BufferedInputStream(in));
65  					logger.info("fckeditor.properties loaded successfully!");
66  				} catch (IOException e) {
67  					logger.error("Error while loading fckeditor.properties!", e);
68  					throw new RuntimeException("Can't load fckeditor.properties!", e);
69  				}
70  			}
71  		} catch (IOException e) {
72  			logger.error("Error while loading default.properties!", e);
73  			throw new RuntimeException("Can't load default.properties!", e);
74  		}
75  	}
76  
77  	/**
78  	 * Getter for a property.
79  	 * 
80  	 * @param key
81  	 *            The property key.
82  	 * @return The value for the specified key.
83  	 * @see Properties#getProperty(String)
84  	 */
85  	public static String getProperty(final String key) {
86  		return properties.getProperty(key);
87  	}
88  
89  	/**
90  	 * Setter for a property. If the property already exists, the value will be
91  	 * overridden.<br />
92  	 * <em>Hint</em>: This method is intended as an alternative way to set
93  	 * properties programmatically instead of using the
94  	 * <code>fckeditor.properties</code>. It should never used inside
95  	 * FCKeditor.Java!!!
96  	 * 
97  	 * @param key
98  	 *            The property key.
99  	 * @param value
100 	 *            The property value.
101 	 * @see Properties#setProperty(String, String)
102 	 */
103 	public static void setProperty(final String key, final String value) {
104 		properties.setProperty(key, value);
105 	}
106 }