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