001    package ca.uhn.hl7v2.util;
002    
003    import java.io.BufferedReader;
004    import java.io.FileReader;
005    import java.util.StringTokenizer;
006    import java.util.*;
007    import java.io.FileNotFoundException;
008    import java.io.IOException;
009    import ca.uhn.log.HapiLog;
010    import ca.uhn.log.HapiLogFactory;
011    
012    /**
013     * Loads system properties from a file.  This is intended as a convenient way 
014     * of setting multiple system properties. 
015     */
016    public class PropertyLoader {
017        
018        private static final HapiLog log = HapiLogFactory.getHapiLog(PropertyLoader.class);
019        private static HashSet files = new HashSet();
020        
021        private PropertyLoader() {
022        }
023        
024        /**
025         * Calls <code>loadProperties()</code> if it has not been called before for
026         * the given file.  If the given property file has already been loaded, this
027         * method does nothing.
028         */
029        public static void loadOnce(String propertyFileName) throws IOException {
030            if (!files.contains(propertyFileName)) {
031                loadProperties(propertyFileName);
032                files.add(propertyFileName);
033            }
034        }
035        
036        /**
037         * Reads given "property file" and sets system properties accordingly.  In the property file,
038         * there should be one property per line.  A line should consist of 1) the fully qualified property name,
039         * 2) one or more tabs, and 3) the value (everything after the first group of tabs and before any subsequent
040         * groups will be considered "the value").
041         * Lines in the file are consdidered comments if they begin with "%".
042         */
043        public static void loadProperties(String propertyFileName) throws IOException {
044            
045            //open stream from given property file
046            BufferedReader in = null;
047            in = new BufferedReader(new FileReader(propertyFileName));
048            
049            String line, key, value, delim = "\t";
050            StringTokenizer tok;
051            while ((line = in.readLine()) != null) {
052                //ignore comments
053                if (!line.startsWith("%")) {
054                    key = null; value = null;
055                    
056                    //get property key and value
057                    tok = new StringTokenizer(line, delim, false);
058                    if (tok.hasMoreTokens()) key = tok.nextToken();
059                    if (tok.hasMoreTokens()) value = tok.nextToken();
060                    
061                    //set property
062                    if (key != null && value != null) {
063                        System.setProperty(key, value);
064                        log.debug("Setting system property " + key + " to " + value);
065                    }
066                }
067            }
068            in.close();
069        }
070        
071        /** Test harness */
072        public static void main(String args[]) {
073            if (args.length != 1) {
074                System.out.println("Usage: PropertyLoader file");
075                System.exit(1);
076            }
077            
078            try {
079                System.setProperty("ca.uhn.hl7v2.util.status.level", "VERBOSE");
080                System.out.println("Loading properties in file " + args[0]);
081                loadOnce(args[0]);
082                System.out.println("Loading properties in file " + args[0] + " again");
083                loadOnce(args[0]);
084            } catch (Exception e) {
085                e.printStackTrace();
086            }
087            
088            Properties p = System.getProperties();
089            Enumeration en = p.propertyNames();
090            while (en.hasMoreElements()) {
091                String key = (String) en.nextElement();
092                System.out.println("Property: " + key + " Value: " + System.getProperty(key));
093            }
094        }
095    }