View Javadoc

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      private static final Logger log = LoggerFactory.getLogger(PropertyLoader.class);
23      private static Set<String> files = new HashSet<String>();
24      
25      private PropertyLoader() {
26      }
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          if (!files.contains(propertyFileName)) {
35              loadProperties(propertyFileName);
36              files.add(propertyFileName);
37          }
38      }
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          BufferedReader in = null;
51          in = new BufferedReader(new FileReader(propertyFileName));
52          
53          String line, key, value, delim = "\t";
54          StringTokenizer tok;
55          while ((line = in.readLine()) != null) {
56              //ignore comments
57              if (!line.startsWith("%")) {
58                  key = null; value = null;
59                  
60                  //get property key and value
61                  tok = new StringTokenizer(line, delim, false);
62                  if (tok.hasMoreTokens()) key = tok.nextToken();
63                  if (tok.hasMoreTokens()) value = tok.nextToken();
64                  
65                  //set property
66                  if (key != null && value != null) {
67                      System.setProperty(key, value);
68                      log.debug("Setting system property {} to {}", key, value);
69                  }
70              }
71          }
72          in.close();
73      }
74      
75      /** Test harness */
76      public static void main(String args[]) {
77          if (args.length != 1) {
78              System.out.println("Usage: PropertyLoader file");
79              System.exit(1);
80          }
81          
82          try {
83              System.setProperty("ca.uhn.hl7v2.util.status.level", "VERBOSE");
84              System.out.println("Loading properties in file " + args[0]);
85              loadOnce(args[0]);
86              System.out.println("Loading properties in file " + args[0] + " again");
87              loadOnce(args[0]);
88          } catch (Exception e) {
89              e.printStackTrace();
90          }
91          
92          Properties p = System.getProperties();
93          Enumeration<?> en = p.propertyNames();
94          while (en.hasMoreElements()) {
95              String key = (String) en.nextElement();
96              System.out.println("Property: " + key + " Value: " + System.getProperty(key));
97          }
98      }
99  }