Coverage Report - org.webmacro.util.Settings
 
Classes in this File Line Coverage Branch Coverage Complexity
Settings
63%
69/108
73%
41/56
2.375
Settings$ListSettingHandler
100%
1/1
N/A
2.375
 
 1  
 /*
 2  
  * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
 3  
  *
 4  
  * Redistribution and use in source and binary forms, with or without
 5  
  * modification, are permitted under the terms of either of the following
 6  
  * Open Source licenses:
 7  
  *
 8  
  * The GNU General Public License, version 2, or any later version, as
 9  
  * published by the Free Software Foundation
 10  
  * (http://www.fsf.org/copyleft/gpl.html);
 11  
  *
 12  
  *  or
 13  
  *
 14  
  * The Semiotek Public License (http://webmacro.org/LICENSE.)
 15  
  *
 16  
  * This software is provided "as is", with NO WARRANTY, not even the
 17  
  * implied warranties of fitness to purpose, or merchantability. You
 18  
  * assume all risks and liabilities associated with its use.
 19  
  *
 20  
  * See www.webmacro.org for more information on the WebMacro project.
 21  
  */
 22  
 
 23  
 
 24  
 package org.webmacro.util;
 25  
 
 26  
 import org.webmacro.InitException;
 27  
 
 28  
 import java.io.IOException;
 29  
 import java.io.InputStream;
 30  
 import java.net.MalformedURLException;
 31  
 import java.net.URL;
 32  
 import java.util.Enumeration;
 33  
 import java.util.Properties;
 34  
 import java.util.StringTokenizer;
 35  
 
 36  
 /**
 37  
  * Models the settings from a property object.
 38  
  */
 39  
 public class Settings
 40  
 {
 41  
 
 42  
     /**
 43  
      * Handler for a setting whose value may be a list.
 44  
      */
 45  567
     abstract public static class ListSettingHandler
 46  
     {
 47  
 
 48  
         abstract public void processSetting (String settingKey,
 49  
                                              String settingValue);
 50  
     }
 51  
 
 52  41
     protected static final String[] stringArray = new String[0];
 53  
 
 54  
     private Properties _props;
 55  
 
 56  
     /**
 57  
      * Create an empty Settings object
 58  
      */
 59  
     public Settings ()
 60  992
     {
 61  992
         _props = new Properties();
 62  992
     }
 63  
 
 64  
     /**
 65  
      * Search for the named settingsFile on the classpath
 66  
      * and instantiate a Settings object based on its values
 67  
      */
 68  
     public Settings (String settingsFile)
 69  
             throws InitException, IOException
 70  
     {
 71  40
         this();
 72  40
         load(settingsFile);
 73  40
     }
 74  
 
 75  
     /**
 76  
      * Search for the named settingsFile from the supplied URL
 77  
      * and instantiate a Settings object based on its values
 78  
      */
 79  
     public Settings (URL settingsFile)
 80  
             throws InitException, IOException
 81  
     {
 82  0
         this();
 83  0
         load(settingsFile);
 84  0
     }
 85  
 
 86  
 
 87  
     /**
 88  
      * Instantiate a new Settings object using the properties
 89  
      * supplied as the settings values.
 90  
      */
 91  
     protected Settings (Properties values)
 92  
     {
 93  0
         this();
 94  0
         load(values);
 95  0
     }
 96  
 
 97  
     /**
 98  
      * Instantiate a new Settings object using the supplied
 99  
      * Settings as the defaults.
 100  
      */
 101  
     public Settings (Settings defaults)
 102  
     {
 103  0
         this();
 104  0
         load(defaults);
 105  0
     }
 106  
 
 107  
     /**
 108  
      * Load settings from the supplied fileName, searching for
 109  
      * the file along the classpath, and then search for the settings
 110  
      * file as a file: URL.
 111  
      */
 112  
     public void load (String fileName) throws InitException, IOException
 113  
     {
 114  44
         ClassLoader cl = this.getClass().getClassLoader();
 115  44
         URL u = (cl == null) ? null : cl.getResource(fileName);
 116  44
         if (u == null)
 117  0
             u = ClassLoader.getSystemResource(fileName);
 118  44
         if (u == null)
 119  
             try
 120  
             {
 121  0
                 u = new URL("file:" + fileName);
 122  
             }
 123  0
             catch (MalformedURLException e)
 124  
             {
 125  
                 // Yes, ignore this exception
 126  0
             }
 127  
         ;
 128  
 
 129  44
         if (u == null)
 130  
         {
 131  0
             StringBuffer error = new StringBuffer();
 132  0
             error.append("Unable to load the properties file: ");
 133  0
             error.append(fileName);
 134  0
             error.append(" via the class path\n");
 135  0
             error.append("This indicates a configuration error.");
 136  0
             throw new InitException(error.toString());
 137  
         }
 138  44
         load(u);
 139  44
     }
 140  
 
 141  
     /**
 142  
      * Load settings from the supplied URL
 143  
      */
 144  
     public void load (URL u) throws IOException
 145  
     {
 146  55
         InputStream in = u.openStream();
 147  55
         _props.load(in);
 148  55
         in.close();
 149  55
     }
 150  
 
 151  
     /**
 152  
      * Load settings from the supplied input stream
 153  
      */
 154  
     public void load (InputStream in) throws IOException
 155  
     {
 156  0
         _props.load(in);
 157  0
     }
 158  
 
 159  
     /**
 160  
      * Load settings from a Settings
 161  
      */
 162  
     public void load (Settings defaults)
 163  
     {
 164  63
         String keys[] = defaults.getKeys();
 165  6363
         for (int i = 0; i < keys.length; i++)
 166  
         {
 167  6300
             _props.setProperty(keys[i], defaults.getSetting(keys[i]));
 168  
         }
 169  63
     }
 170  
 
 171  
     /**
 172  
      * Load settings from a Properties, only extracting properties
 173  
      * which have the specified prefix
 174  
      */
 175  
     public void load (Properties props, String prefix)
 176  
     {
 177  73
         Enumeration e = props.propertyNames();
 178  73
         String dotPrefix = (prefix == null) ? "" : prefix + ".";
 179  3360
         while (e.hasMoreElements())
 180  
         {
 181  3287
             String key = (String) e.nextElement();
 182  3287
             if (prefix == null)
 183  9
                     setProperty(_props,key, props.getProperty(key));
 184  3278
             else if (key.startsWith(dotPrefix)) {
 185  65
                     setProperty(_props,key.substring(dotPrefix.length()),
 186  
                         props.getProperty(key));
 187  
             }
 188  3287
         }
 189  73
     }
 190  
 
 191  
     private static void setProperty(Properties props, String key, String value) {
 192  
             //System.out.println("Setting " + key + " to " + value);
 193  74
             props.setProperty(key, value);
 194  74
     }
 195  
     /**
 196  
      * Load settings from a Properties
 197  
      */
 198  
     public void load (Properties props)
 199  
     {
 200  6
         load(props, null);
 201  6
     }
 202  
 
 203  
     /**
 204  
      * Find out if a setting is defined
 205  
      */
 206  
     public boolean containsKey (String key)
 207  
     {
 208  2890
         return _props.containsKey(key);
 209  
     }
 210  
 
 211  
     /**
 212  
      * Get a setting.  We trim leading and trailing spaces (since the
 213  
      * property file loader doesn't) and, if the result is a quoted string,
 214  
      * remove the quotes.
 215  
      */
 216  
     public String getSetting (String key)
 217  
     {
 218  15288
         String prop = _props.getProperty(key);
 219  15288
         if (prop != null)
 220  13567
             prop = prop.trim();
 221  15288
         if (prop != null
 222  
                 && prop.length() > 2
 223  
                 && prop.charAt(0) == '"'
 224  
                 && prop.charAt(prop.length() - 1) == '"')
 225  4
             prop = prop.substring(1, prop.length() - 1);
 226  
 
 227  15288
         return prop;
 228  
     }
 229  
 
 230  
     /**
 231  
      * Get a setting with a default value in case it is not set
 232  
      */
 233  
     public String getSetting (String key, String defaultValue)
 234  
     {
 235  1394
         String ret = getSetting(key);
 236  1394
         return (ret != null) ? ret : defaultValue;
 237  
     }
 238  
 
 239  
     /**
 240  
      * Get a setting and convert it to an int
 241  
      */
 242  
     public int getIntegerSetting (String key)
 243  
     {
 244  4
         return getIntegerSetting(key, 0);
 245  
     }
 246  
 
 247  
     /**
 248  
      * Get a setting with a default value in case it is not set
 249  
      */
 250  
     public int getIntegerSetting (String key, int defaultValue)
 251  
     {
 252  2386
         if (containsKey(key))
 253  
         {
 254  
             try
 255  
             {
 256  1881
                 return Integer.parseInt(getSetting(key));
 257  
             }
 258  0
             catch (Exception e)
 259  
             {
 260  0
                 return defaultValue;
 261  
             }
 262  
         }
 263  
         else
 264  
         {
 265  505
             return defaultValue;
 266  
         }
 267  
     }
 268  
 
 269  
     /**
 270  
      * Get a setting and convert it to a boolean. The
 271  
      * values "on", "true", and "yes" are considered to
 272  
      * be TRUE values, everything else is FALSE.
 273  
      */
 274  
     public boolean getBooleanSetting (String key)
 275  
     {
 276  260
         String setting = getSetting(key);
 277  260
         return ((setting != null) &&
 278  
                 ((setting.equalsIgnoreCase("on"))
 279  
                 || (setting.equalsIgnoreCase("true"))
 280  
                 || (setting.equalsIgnoreCase("yes"))));
 281  
     }
 282  
 
 283  
     /**
 284  
      * Get a setting with a default value in case it is not set
 285  
      */
 286  
     public boolean getBooleanSetting (String key, boolean defaultValue)
 287  
     {
 288  0
         if (containsKey(key))
 289  
         {
 290  0
             return getBooleanSetting(key);
 291  
         }
 292  
         else
 293  
         {
 294  0
             return defaultValue;
 295  
         }
 296  
     }
 297  
 
 298  
     /**
 299  
      * Get the keys for this settings object as an array
 300  
      */
 301  
     public String[] getKeys ()
 302  
     {
 303  696
         return (String[]) _props.keySet().toArray(stringArray);
 304  
     }
 305  
 
 306  
     /**
 307  
      * Get the values from this settings object as a properties
 308  
      */
 309  
     public Properties getAsProperties ()
 310  
     {
 311  2
         String[] keys = getKeys();
 312  2
         Properties p = new Properties();
 313  
 
 314  202
         for (int i = 0; i < keys.length; i++)
 315  200
             p.setProperty(keys[i], getSetting(keys[i]));
 316  
 
 317  2
         return p;
 318  
     }
 319  
 
 320  
     /**
 321  
      * Iterate through a list of settings.  If the settingName is
 322  
      * "foo", then the SettingHandler will be called once for each
 323  
      * element of the space-separated list setting "foo", plus once
 324  
      * for each setting of the form "foo.x".  In the case of "foo.x",
 325  
      * settingName will be passed as "x"; when processing the list setting,
 326  
      * settingName will be passed as "".
 327  
      * This is designed to support settings of the form
 328  
      *   Directives.a: ADirective
 329  
      *   Directives.b: BDirective
 330  
      *   Directives:   CDirective DDirective
 331  
      */
 332  
     public void processListSetting (String settingName, ListSettingHandler h)
 333  
     {
 334  
         // First, the list setting, if any
 335  567
         String listNames = getSetting(settingName);
 336  567
         if (listNames != null)
 337  
         {
 338  316
             Enumeration denum = new StringTokenizer(listNames);
 339  317
             while (denum.hasMoreElements())
 340  1
                 h.processSetting("", ((String) denum.nextElement()));
 341  
         }
 342  
 
 343  567
         Settings s = new SubSettings(this, settingName);
 344  567
         String[] keys = s.getKeys();
 345  3151
         for (int i = 0; i < keys.length; i++)
 346  
         {
 347  2584
             String value = s.getSetting(keys[i]).trim();
 348  2584
             if (value.equals(""))
 349  0
                 continue;
 350  2584
             h.processSetting(keys[i], value);
 351  
         }
 352  567
     }
 353  
 
 354  
 
 355  
     /**
 356  
      * Brief test
 357  
      */
 358  
     public static void main (String arg[]) throws Exception
 359  
     {
 360  
 
 361  0
         Settings s = new Settings();
 362  0
         s.load("Test.properties");
 363  
 
 364  0
         Settings sb = new SubSettings(s, "b");
 365  0
         String[] keys = sb.getKeys();
 366  0
         for (int i = 0; i < keys.length; i++)
 367  
         {
 368  0
             System.out.println("sub-prop " + keys[i] + " = " + sb.getSetting(keys[i]));
 369  
         }
 370  
 
 371  0
         Settings ssb = new SubSettings(sb, "b");
 372  0
         keys = ssb.getKeys();
 373  0
         for (int i = 0; i < keys.length; i++)
 374  
         {
 375  0
             System.out.println("sub-sub-prop " + keys[i] + " = " + ssb.getSetting(keys[i]));
 376  
         }
 377  
 
 378  0
         System.out.println("LogTraceExceptions is: " + s.getBooleanSetting("LogTraceExceptions"));
 379  
 
 380  0
     }
 381  
 }
 382