Coverage Report - net.fckeditor.handlers.PropertiesLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesLoader
39%
9/23
0%
0/2
1.5
 
 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  0
 public class PropertiesLoader {
 44  2
         private static final Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
 45  1
         private static Properties properties = new Properties();
 46  
 
 47  
         static {
 48  
                 try {
 49  
                         // 1. load system defaults
 50  1
                         properties.load(new BufferedInputStream(PropertiesLoader.class
 51  
                                 .getResourceAsStream("default.properties")));
 52  
 
 53  
                         // 2. load user defaults
 54  1
                         InputStream in = PropertiesLoader.class.getResourceAsStream("/fckeditor.properties");
 55  1
                         if (in == null)
 56  1
                                 logger.warn("Can't find user properties!");
 57  
                         else {
 58  
                                 try {
 59  0
                                         properties.load(new BufferedInputStream(in));
 60  0
                                         logger.info("User's properties loaded successfully!");
 61  0
                                 } catch (IOException e) {
 62  0
                                         logger.error("Error while loading user properties!", e);
 63  0
                                         throw new RuntimeException("Can't load user properties!", e);
 64  0
                                 }
 65  
                         }
 66  0
                 } catch (IOException e) {
 67  0
                         logger.error("Error while loading default properties!", e);
 68  0
                         throw new RuntimeException("Can't load default properties!", e);
 69  1
                 }
 70  1
         }
 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  17
                 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  0
                 if (Utils.isEmpty(key))
 98  0
                         new IllegalArgumentException("The 'key' of a property schouldn't be null!");
 99  0
                 properties.setProperty(key, value);
 100  0
         }
 101  
 }