View Javadoc

1   package ca.uhn.hl7v2.util;
2   
3   import java.io.File;
4   
5   import org.slf4j.Logger;
6   import org.slf4j.LoggerFactory;
7   
8   /**
9    * Used to access the hapi.home system property.  Note that the property 
10   * is only checked (at most) once per session.  
11   * @author Bryan Tripp
12   */
13  public class Home {
14      
15      private static File home;
16      private static Logger log = LoggerFactory.getLogger(Home.class);
17      
18      /** Creates a new instance of Home */
19      public Home() {
20      }
21      
22      /**
23       * Returns a File object corresponding to the directory specified in 
24       * the system property hapi.home.  The property is only checked the 
25       * first time this method is called, so changes will not take place 
26       * until a new VM is started.  
27       * This method is guaranteed to return a directory.  If hapi.home is 
28       * not set, or is set to a non-directory path, the current working directory will 
29       * be used.  
30       */
31      public static File getHomeDirectory() {
32          if (home == null) 
33              setHomeDirectory();
34          
35          return home;
36      }
37      
38      private synchronized static void setHomeDirectory() {
39          String dir = System.getProperty("hapi.home", ".");
40          home = new File(dir);
41          
42          if (!home.isDirectory()) {
43              home = new File("."); 
44              log.warn("The system property hapi.home is not a valid directory: {}.  Using . instead", dir);
45          }
46           
47          log.info("hapi.home is set to " + home.getAbsolutePath());
48      }
49      
50      public static void main(String args[]) {
51          System.out.println("HOME: " + getHomeDirectory().getAbsolutePath());
52      }
53          
54  }