View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2009 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.InputStream;
25  import java.util.Properties;
26  
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * This handler gives you access to properties stored in
32   * <code>/net/fckeditor/handlers/default.properties</code> and
33   * <code>/fckeditor.properties</code>.<br />
34   * This class loads the properties files as follows:
35   * <ol>
36   * <li>Load <code>default.properties</code></li>
37   * <li>Load <code>fckeditor.properties</code> if present.</li>
38   * </ol>
39   * <em>Attention</em>: Properties specified in
40   * <code>fckeditor.properties</code> will override properties loaded from
41   * <code>default.properties</code>. (intended behavior)<br />
42   * <em>Hint</em>: You may set properties programmatically with
43   * {@link #setProperty(String, String)} instead or additionally.
44   * 
45   * @version $Id: PropertiesLoader.java 3840 2009-07-08 20:29:46Z mosipov $
46   */
47  public class PropertiesLoader {
48  	private static final Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
49  	private static Properties properties = new Properties();
50  
51  	static {
52  
53  		// 1. load library defaults
54  		InputStream in = PropertiesLoader.class
55  				.getResourceAsStream("default.properties");
56  
57  		if (in == null) {
58  			logger.error("default.properties not found");
59  			throw new RuntimeException("default.properties not found");
60  		} else {
61  			if (!(in instanceof BufferedInputStream))
62  				in = new BufferedInputStream(in);
63  
64  			try {
65  				properties.load(in);
66  				in.close();
67  				logger.debug("default.properties loaded");
68  			} catch (Exception e) {
69  				logger.error("Error while processing default.properties");
70  				throw new RuntimeException(
71  						"Error while processing default.properties", e);
72  			}
73  		}
74  
75  		// 2. load user defaults if present
76  		InputStream in2 = PropertiesLoader.class
77  				.getResourceAsStream("/fckeditor.properties");
78  
79  		if (in2 == null) {
80  			logger.info("fckeditor.properties not found");
81  		} else {
82  
83  			if (!(in2 instanceof BufferedInputStream))
84  				in2 = new BufferedInputStream(in2);
85  
86  			try {
87  				properties.load(in2);
88  				in2.close();
89  				logger.debug("fckeditor.properties loaded");
90  			} catch (Exception e) {
91  				logger.error("Error while processing fckeditor.properties");
92  				throw new RuntimeException(
93  						"Error while processing fckeditor.properties", e);
94  			}
95  
96  		}		
97  	}
98  
99  	/**
100 	 * Getter for a property.
101 	 * 
102 	 * @param key
103 	 *            The property key.
104 	 * @return The value for the specified key.
105 	 * @see Properties#getProperty(String)
106 	 */
107 	public static String getProperty(final String key) {
108 		return properties.getProperty(key);
109 	}
110 
111 	/**
112 	 * Setter for a property. If the property already exists, the value will be
113 	 * overridden.<br />
114 	 * <em>Hint</em>: This method is intended as an alternative way to set
115 	 * properties programmatically instead of using the
116 	 * <code>fckeditor.properties</code>. It should never used inside
117 	 * FCKeditor.Java!!!
118 	 * 
119 	 * @param key
120 	 *            The property key.
121 	 * @param value
122 	 *            The property value.
123 	 * @see Properties#setProperty(String, String)
124 	 */
125 	public static void setProperty(final String key, final String value) {
126 		properties.setProperty(key, value);
127 	}
128 }