Coverage Report - ca.uhn.hl7v2.util.PropertyLoader
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertyLoader
0%
0/40
0%
0/18
3.5
 
 1  
 package ca.uhn.hl7v2.util;
 2  
 
 3  
 import java.io.BufferedReader;
 4  
 import java.io.FileReader;
 5  
 import java.io.IOException;
 6  
 import java.util.Enumeration;
 7  
 import java.util.HashSet;
 8  
 import java.util.Properties;
 9  
 import java.util.Set;
 10  
 import java.util.StringTokenizer;
 11  
 
 12  
 import org.slf4j.Logger;
 13  
 import org.slf4j.LoggerFactory;
 14  
 
 15  
 /**
 16  
  * Loads system properties from a file.  This is intended as a convenient way 
 17  
  * of setting multiple system properties. 
 18  
  * @deprecated
 19  
  */
 20  
 public class PropertyLoader {
 21  
     
 22  0
     private static final Logger log = LoggerFactory.getLogger(PropertyLoader.class);
 23  0
     private static Set<String> files = new HashSet<String>();
 24  
     
 25  0
     private PropertyLoader() {
 26  0
     }
 27  
     
 28  
     /**
 29  
      * Calls <code>loadProperties()</code> if it has not been called before for
 30  
      * the given file.  If the given property file has already been loaded, this
 31  
      * method does nothing.
 32  
      */
 33  
     public static void loadOnce(String propertyFileName) throws IOException {
 34  0
         if (!files.contains(propertyFileName)) {
 35  0
             loadProperties(propertyFileName);
 36  0
             files.add(propertyFileName);
 37  
         }
 38  0
     }
 39  
     
 40  
     /**
 41  
      * Reads given "property file" and sets system properties accordingly.  In the property file,
 42  
      * there should be one property per line.  A line should consist of 1) the fully qualified property name,
 43  
      * 2) one or more tabs, and 3) the value (everything after the first group of tabs and before any subsequent
 44  
      * groups will be considered "the value").
 45  
      * Lines in the file are consdidered comments if they begin with "%".
 46  
      */
 47  
     public static void loadProperties(String propertyFileName) throws IOException {
 48  
         
 49  
         //open stream from given property file
 50  0
         BufferedReader in = null;
 51  0
         in = new BufferedReader(new FileReader(propertyFileName));
 52  
         
 53  0
         String line, key, value, delim = "\t";
 54  
         StringTokenizer tok;
 55  0
         while ((line = in.readLine()) != null) {
 56  
             //ignore comments
 57  0
             if (!line.startsWith("%")) {
 58  0
                 key = null; value = null;
 59  
                 
 60  
                 //get property key and value
 61  0
                 tok = new StringTokenizer(line, delim, false);
 62  0
                 if (tok.hasMoreTokens()) key = tok.nextToken();
 63  0
                 if (tok.hasMoreTokens()) value = tok.nextToken();
 64  
                 
 65  
                 //set property
 66  0
                 if (key != null && value != null) {
 67  0
                     System.setProperty(key, value);
 68  0
                     log.debug("Setting system property {} to {}", key, value);
 69  
                 }
 70  
             }
 71  
         }
 72  0
         in.close();
 73  0
     }
 74  
     
 75  
     /** Test harness */
 76  
     public static void main(String args[]) {
 77  0
         if (args.length != 1) {
 78  0
             System.out.println("Usage: PropertyLoader file");
 79  0
             System.exit(1);
 80  
         }
 81  
         
 82  
         try {
 83  0
             System.setProperty("ca.uhn.hl7v2.util.status.level", "VERBOSE");
 84  0
             System.out.println("Loading properties in file " + args[0]);
 85  0
             loadOnce(args[0]);
 86  0
             System.out.println("Loading properties in file " + args[0] + " again");
 87  0
             loadOnce(args[0]);
 88  0
         } catch (Exception e) {
 89  0
             e.printStackTrace();
 90  0
         }
 91  
         
 92  0
         Properties p = System.getProperties();
 93  0
         Enumeration<?> en = p.propertyNames();
 94  0
         while (en.hasMoreElements()) {
 95  0
             String key = (String) en.nextElement();
 96  0
             System.out.println("Property: " + key + " Value: " + System.getProperty(key));
 97  0
         }
 98  0
     }
 99  
 }