Coverage Report - org.melati.util.PropertiesUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertiesUtils
57%
31/54
91%
11/12
4.222
 
 1  
 /*
 2  
  * $Source: /usr/cvsroot/melati/melati/src/site/resources/withWebmacro/org.melati.util.PropertiesUtils.html,v $
 3  
  * $Revision: 1.1 $
 4  
  *
 5  
  * Copyright (C) 2000 William Chesters
 6  
  *
 7  
  * Part of Melati (http://melati.org), a framework for the rapid
 8  
  * development of clean, maintainable web applications.
 9  
  *
 10  
  * Melati is free software; Permission is granted to copy, distribute
 11  
  * and/or modify this software under the terms either:
 12  
  *
 13  
  * a) the GNU General Public License as published by the Free Software
 14  
  *    Foundation; either version 2 of the License, or (at your option)
 15  
  *    any later version,
 16  
  *
 17  
  *    or
 18  
  *
 19  
  * b) any version of the Melati Software License, as published
 20  
  *    at http://melati.org
 21  
  *
 22  
  * You should have received a copy of the GNU General Public License and
 23  
  * the Melati Software License along with this program;
 24  
  * if not, write to the Free Software Foundation, Inc.,
 25  
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the
 26  
  * GNU General Public License and visit http://melati.org to obtain the
 27  
  * Melati Software License.
 28  
  *
 29  
  * Feel free to contact the Developers of Melati (http://melati.org),
 30  
  * if you would like to work out a different arrangement than the options
 31  
  * outlined here.  It is our intention to allow Melati to be used by as
 32  
  * wide an audience as possible.
 33  
  *
 34  
  * This program is distributed in the hope that it will be useful,
 35  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 36  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 37  
  * GNU General Public License for more details.
 38  
  *
 39  
  * Contact details for copyright holder:
 40  
  *
 41  
  *     William Chesters <williamc AT paneris.org>
 42  
  *     http://paneris.org/~williamc
 43  
  *     Obrechtstraat 114, 2517VX Den Haag, The Netherlands
 44  
  */
 45  
 
 46  
 package org.melati.util;
 47  
 
 48  
 import java.util.Hashtable;
 49  
 import java.util.Properties;
 50  
 import java.io.File;
 51  
 import java.io.InputStream;
 52  
 import java.io.FileInputStream;
 53  
 import java.io.IOException;
 54  
 import java.io.FileNotFoundException;
 55  
 
 56  
 /**
 57  
  * Useful things to do with properties files.
 58  
  */
 59  
 public final class PropertiesUtils {
 60  
 
 61  0
   private PropertiesUtils() {}
 62  
 
 63  
   /**
 64  
    * Get a {@link Properties} object from a file.
 65  
    * 
 66  
    * @param path a {@link File} path name
 67  
    * @return a {@link Properties} object
 68  
    * @throws IOException if there is a problem loading the file
 69  
    */
 70  
   public static Properties fromFile(File path) throws IOException {
 71  0
     InputStream data = new FileInputStream(path);
 72  0
     Properties them = new Properties();
 73  
     try {
 74  0
       them.load(data);
 75  0
     } catch (IOException e) {
 76  0
       throw new IOException("Corrupt properties file `" + path + "': " +
 77  
       e.getMessage());
 78  0
     }
 79  
 
 80  0
     return them;
 81  
   }
 82  
 
 83  
   /**
 84  
    * Get a {@link Properties} object from a {@link Class}.
 85  
    * 
 86  
    * 
 87  
    * @param clazz the {@link Class} to look up
 88  
    * @param name the property file name
 89  
    * @return a {@link Properties} object
 90  
    * @throws IOException if the file cannot load or is not found
 91  
    */
 92  
   public static Properties fromResource(Class<?> clazz, String name)
 93  
       throws IOException {
 94  606
     InputStream is = clazz.getResourceAsStream(name);
 95  
 
 96  606
     if (is == null)
 97  2
       throw new FileNotFoundException(name + ": is it in CLASSPATH?");
 98  
 
 99  604
     Properties them = new Properties();
 100  
     try {
 101  604
       them.load(is);
 102  0
     } catch (IOException e) {
 103  0
       throw new IOException("Corrupt properties file `" + name + "': " +
 104  
       e.getMessage());
 105  604
     }
 106  
 
 107  604
     return them;
 108  
   }
 109  
 
 110  
   /**
 111  
    * Return a property.
 112  
    * 
 113  
    * @param properties the {@link Properties} object to look in 
 114  
    * @param propertyName the property to get 
 115  
    * @return the property value
 116  
    * @throws NoSuchPropertyException if the property is not set
 117  
    */
 118  
   public static String getOrDie(Properties properties, String propertyName)
 119  
       throws NoSuchPropertyException {
 120  296
     String value = properties.getProperty(propertyName);
 121  296
     if (value == null)
 122  6
       throw new NoSuchPropertyException(properties, propertyName);
 123  290
     return value;
 124  
   }
 125  
 
 126  
   /**
 127  
    * Get a property or return the supplied default.
 128  
    * 
 129  
    * @param properties the {@link Properties} object to look in 
 130  
    * @param propertyName the property to get 
 131  
    * @param def the default to return if not found
 132  
    * @return the property value
 133  
    */
 134  
   public static String getOrDefault(Properties properties, 
 135  
                                     String propertyName, String def) {
 136  4420
     String value = properties.getProperty(propertyName);
 137  4420
     if (value == null) return def;
 138  2620
     return value;
 139  
   }
 140  
 
 141  
   /**
 142  
    * Get an Integer property.
 143  
    * 
 144  
    * @param properties the {@link Properties} object to look in 
 145  
    * @param propertyName the property to get 
 146  
    * @return the int property value 
 147  
    * @throws NoSuchPropertyException if it is not found
 148  
    * @throws FormatPropertyException if it is not an Integer 
 149  
    */
 150  
   public static int getOrDie_int(Properties properties, String propertyName)
 151  
       throws NoSuchPropertyException, FormatPropertyException {
 152  0
     String string = getOrDie(properties, propertyName);
 153  
     try {
 154  0
       return Integer.parseInt(string);
 155  
     }
 156  0
     catch (NumberFormatException e) {
 157  0
       throw new FormatPropertyException(properties, propertyName, string,
 158  
       "an integer", e);
 159  
     }
 160  
   }
 161  
 
 162  
   /**
 163  
    * Get an Integer property from a {@link Properties} object or make a fuss. 
 164  
    * 
 165  
    * @param properties a {@link Properties} 
 166  
    * @param propertyName the name of the property
 167  
    * @param def cater for multiple definitions, with increment numbers
 168  
    * @return the property as an int 
 169  
    * @throws FormatPropertyException if it is not an Integer 
 170  
    */
 171  
   public static int getOrDefault_int(Properties properties, 
 172  
                                      String propertyName, int def)
 173  
       throws FormatPropertyException {
 174  58
     String string = getOrDefault(properties, propertyName, ""+def);
 175  
     try {
 176  58
       return Integer.parseInt(string);
 177  
     }
 178  0
     catch (NumberFormatException e) {
 179  0
       throw new FormatPropertyException(properties, propertyName, string,
 180  
       "an integer", e);
 181  
     }
 182  
   }
 183  
 
 184  2
   private static Hashtable<String, Object> instantiatedClassesCache = new Hashtable<String, Object>();
 185  
   /**
 186  
    * Instantiate an interface.
 187  
    * 
 188  
    * @param className the name of the class
 189  
    * @param interfaceClassName the interface Class name
 190  
    * @return a new object
 191  
    * @throws InstantiationPropertyException 
 192  
    *   if the named class does not descend from the interface
 193  
    */
 194  
   public static Object instanceOfNamedClass(String className, String interfaceClassName)
 195  
       throws InstantiationPropertyException {
 196  1794
     Object cached = instantiatedClassesCache.get(className);
 197  1794
     if (cached != null)
 198  1786
       return cached;
 199  
     try {
 200  8
       Class<?> interfaceClass = Class.forName(interfaceClassName);
 201  8
       Class<?> clazz = Class.forName(className);
 202  8
       if (!interfaceClass.isAssignableFrom(clazz))
 203  0
         throw new ClassCastException(
 204  
                 clazz + " is not descended from " + interfaceClass);
 205  8
       Object it = clazz.newInstance();
 206  8
       instantiatedClassesCache.put(className, it);
 207  8
       return it;
 208  0
     } catch (Exception e) {
 209  0
       throw new InstantiationPropertyException(className, e);
 210  
     }
 211  
   }
 212  
 
 213  
   /**
 214  
    * Instantiate a Class.
 215  
    * 
 216  
    * @param properties a {@link Properties} 
 217  
    * @param propertyName the name of the property
 218  
    * @param interfaceClassName     the interface name
 219  
    * @param defaultName  a default concrete class if the property is undefined
 220  
    * @return a new Object
 221  
    * @throws InstantiationPropertyException if there is a problem
 222  
    */
 223  
   public static Object instanceOfNamedClass(Properties properties, 
 224  
                                             String propertyName,
 225  
                                             String interfaceClassName, 
 226  
                                             String defaultName)
 227  
   throws InstantiationPropertyException {
 228  1800
     String className =  (String)properties.get(propertyName);
 229  1800
     if (className == null)
 230  
       try {
 231  6
         Class<?> defaultClass = Class.forName(defaultName);
 232  6
         return defaultClass.newInstance();
 233  0
       } catch (Exception e) {
 234  0
         throw new RuntimeException("Problem creating new instance of " + 
 235  
                 defaultName + " :" + e.toString());
 236  0
       } catch (Error e) { 
 237  0
         throw new RuntimeException("Problem creating new instance of " + 
 238  
                 defaultName + " :" + e.toString());
 239  
       }
 240  
       
 241  1794
     return instanceOfNamedClass(className, interfaceClassName);
 242  
   }
 243  
 }