Coverage Report - net.fckeditor.handlers.PropertiesLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesLoader
44%
14/32
N/A
1
 
 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  0
 public class PropertiesLoader {
 48  2
         private static final Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
 49  1
         private static Properties properties = new Properties();
 50  
 
 51  
         static {
 52  
 
 53  
                 // 1. load library defaults
 54  1
                 InputStream in = PropertiesLoader.class
 55  
                                 .getResourceAsStream("default.properties");
 56  
 
 57  1
                 if (in == null) {
 58  0
                         logger.error("default.properties not found");
 59  0
                         throw new RuntimeException("default.properties not found");
 60  
                 } else {
 61  1
                         if (!(in instanceof BufferedInputStream))
 62  0
                                 in = new BufferedInputStream(in);
 63  
 
 64  
                         try {
 65  1
                                 properties.load(in);
 66  1
                                 in.close();
 67  1
                                 logger.debug("default.properties loaded");
 68  0
                         } catch (Exception e) {
 69  0
                                 logger.error("Error while processing default.properties");
 70  0
                                 throw new RuntimeException(
 71  
                                                 "Error while processing default.properties", e);
 72  1
                         }
 73  
                 }
 74  
 
 75  
                 // 2. load user defaults if present
 76  1
                 InputStream in2 = PropertiesLoader.class
 77  
                                 .getResourceAsStream("/fckeditor.properties");
 78  
 
 79  1
                 if (in2 == null) {
 80  1
                         logger.info("fckeditor.properties not found");
 81  
                 } else {
 82  
 
 83  0
                         if (!(in2 instanceof BufferedInputStream))
 84  0
                                 in2 = new BufferedInputStream(in2);
 85  
 
 86  
                         try {
 87  0
                                 properties.load(in2);
 88  0
                                 in2.close();
 89  0
                                 logger.debug("fckeditor.properties loaded");
 90  0
                         } catch (Exception e) {
 91  0
                                 logger.error("Error while processing fckeditor.properties");
 92  0
                                 throw new RuntimeException(
 93  
                                                 "Error while processing fckeditor.properties", e);
 94  0
                         }
 95  
 
 96  
                 }                
 97  1
         }
 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  17
                 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  0
                 properties.setProperty(key, value);
 127  0
         }
 128  
 }