View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.core.setup;
16  
17  import org.figure8.join.util.LogUtil;
18  
19  import org.apache.commons.logging.Log;
20  
21  import java.io.InputStream;
22  import java.io.IOException;
23  import java.util.Properties;
24  /**
25   * Helper for finding Join application home path.
26   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
27   * @version $Revision: 1.2 $
28   */
29  public class JoinHomeLocator{
30     
31     // Static -------------------------------------------------------------------
32     
33     /** Property denoting Join application home location. */
34     public static final String JOIN_HOME = "join.home";
35     /** Name of the file containing Join application home location property. */
36     public static final String PROPERTIES_FILE = "join-init.properties";
37     
38     
39     // Attributes ---------------------------------------------------------------
40     
41     /** Logger */
42     protected Log log;
43     
44     
45     // Constructors -------------------------------------------------------------
46     
47     /** Creates a new instance of JoinHomeLocator */
48     public JoinHomeLocator(){
49        log = LogUtil.getLog(this.getClass());
50     }
51     
52     
53     // Public -------------------------------------------------------------------
54     
55     /**
56      * Retrieve the path value of the JoinHome property. Try first,
57      * retrieving it from System property and then from properties file
58      * in classpath.
59      */
60     public String getHomePath(){
61        String home = getHomeFromSystemProperty();
62        if (home == null)
63           home = getHomeFromPropertiesFile();
64        log.info("Found join.home property with value: " + home);
65        // Return home.
66        return home;
67     }
68     
69     
70     // Private ------------------------------------------------------------------
71     
72     /**
73      * Try loading join.home from System properties. 
74      */
75     private String getHomeFromSystemProperty(){
76        log.debug("Trying to load join.home property from System parameters...");
77        String home = System.getProperty(JOIN_HOME);
78        if (home == null)
79           log.debug("Could not find join.home property as a System property.");
80        // Return home.
81        return home;
82     }
83     
84     /**
85      * Try loading join.home from properties file in classpath.
86      */
87     private String getHomeFromPropertiesFile(){
88        log.debug("Trying to load join.home property from properties file...");
89        String home = null;
90        try{
91           InputStream is = null;
92           // Try getting is from classpath.
93           is = getClass().getResourceAsStream(PROPERTIES_FILE);
94           // Try getting is from classloader.
95           if (is == null)
96              is = getClass().getClassLoader().getResourceAsStream(PROPERTIES_FILE);
97           // Try getting is from WEB-INF.
98           if (is == null)
99              is = getClass().getResourceAsStream("/WEB-INF/" + PROPERTIES_FILE);
100          // Try getting is from WEB-INF/classes
101          if (is == null)
102             is = getClass().getResourceAsStream("/WEB-INF/classes/" + PROPERTIES_FILE);
103          
104          if (is != null){
105             // Load properties.
106             Properties props = new Properties();
107             props.load(is);
108             try {is.close();}
109             catch (Exception e) {log.debug("Could not close InputStream: " + e);}
110             
111             if (props.getProperty(JOIN_HOME) != null)
112                home = props.getProperty(JOIN_HOME);
113             else
114                log.debug("Could not find join.home property from " + PROPERTIES_FILE);
115          }
116       }
117       catch (IOException ioe){
118          log.debug("Could not find join.home property from properties file.");
119       }
120       // Return home.
121       return home;
122    }
123 }