001    package ca.uhn.hl7v2.util;
002    
003    import java.io.File;
004    import ca.uhn.log.*;
005    
006    /**
007     * Used to access the hapi.home system property.  Note that the property 
008     * is only checked (at most) once per session.  
009     * @author Bryan Tripp
010     */
011    public class Home {
012        
013        private static File home;
014        private static HapiLog log = HapiLogFactory.getHapiLog(Home.class);
015        
016        /** Creates a new instance of Home */
017        public Home() {
018        }
019        
020        /**
021         * Returns a File object corresponding to the directory specified in 
022         * the system property hapi.home.  The property is only checked the 
023         * first time this method is called, so changes will not take place 
024         * until a new VM is started.  
025         * This method is guaranteed to return a directory.  If hapi.home is 
026         * not set, or is set to a non-directory path, the current directory will 
027         * be used.  
028         */
029        public static File getHomeDirectory() {
030            if (home == null) 
031                setHomeDirectory();
032            
033            return home;
034        }
035        
036        private synchronized static void setHomeDirectory() {
037            String dir = System.getProperty("hapi.home", ".");
038            home = new File(dir);
039            
040            if (!home.isDirectory()) {
041                home = new File("."); 
042                log.warn("The system property hapi.home is not a valid directory: " 
043                    + dir + ".  Using . instead");
044            }
045             
046            log.info("hapi.home is set to " + home.getAbsolutePath());
047        }
048        
049        public static void main(String args[]) {
050            System.out.println("HOME: " + getHomeDirectory().getAbsolutePath());
051        }
052            
053    }