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