Ignore:
Timestamp:
2009-02-27T14:29:52+13:00 (15 years ago)
Author:
oranfry
Message:

*create the gsdl3 web address in a smarter way, including honouring a new property 'tomcat.protocol'
*whitespace fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • greenstone3/trunk/src/java/org/greenstone/gsdl3/util/GlobalProperties.java

    r16869 r18599  
    2929
    3030
    31      static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GlobalProperties.class.getName());
     31    static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GlobalProperties.class.getName());
    3232    private static Properties properties = null;
    3333    private static String properties_filename = "global.properties";
     
    3737    // Note, that if the servlet is reloadable, then it is reloaded each time the file is changed.
    3838    static {
    39     //load in the properties
    40     properties = new Properties();
    41     reload();   
     39        //load in the properties
     40        properties = new Properties();
     41        reload();
    4242    }
    4343   
    4444    /** get the value of the property 'key'. returns null if not found */
    4545    public static String getProperty(String key) {
    46     return properties.getProperty(key);
     46        return properties.getProperty(key);
    4747    }
    4848
     
    5050     * returns default_value if not found */
    5151    public static String getProperty(String key, String default_value) {
    52     return properties.getProperty(key, default_value);
     52        return properties.getProperty(key, default_value);
    5353    }
    5454
    5555    /** some special ones */
    5656    public static String getGSDL3Home() {
    57     return gsdl3_home;
     57        return gsdl3_home;
    5858    }
    5959
    6060    public static String getGSDL3WebAddress() {
    61     return gsdl3_web_address;
     61        return gsdl3_web_address;
    6262    }
    6363   
    6464    public static void reload() {
    65     try {
    66         InputStream in = Class.forName("org.greenstone.gsdl3.util.GlobalProperties").getClassLoader().getResourceAsStream(properties_filename);
    67         if (in != null) {
    68         logger.error("loading global properties");
    69         properties.load(in);
    70         in.close();
    71         } else {
    72         logger.error("couldn't load global properties!");
    73         }
    74         gsdl3_home = properties.getProperty("gsdl3.home");
    75         // make sure the path separators are correct
    76         File gs3_file = new File(gsdl3_home);
    77         gsdl3_home = gs3_file.getPath();
    78         gsdl3_web_address = "http://"+ properties.getProperty("tomcat.server")+":"+ properties.getProperty("tomcat.port")+"/"+properties.getProperty("tomcat.context");
    79     } catch (Exception e) {
    80         logger.error("Exception trying to reload global.properties: "+e);
    81     }
    82    
     65        try {
     66
     67
     68            InputStream in = Class.forName("org.greenstone.gsdl3.util.GlobalProperties").getClassLoader().getResourceAsStream(properties_filename);
     69            if (in != null) {
     70            logger.error("loading global properties");
     71            properties.load(in);
     72            in.close();
     73            } else {
     74            logger.error("couldn't load global properties!");
     75            }
     76            gsdl3_home = properties.getProperty("gsdl3.home");
     77            // make sure the path separators are correct
     78            File gs3_file = new File(gsdl3_home);
     79            gsdl3_home = gs3_file.getPath();
     80
     81            //build the gsdl3 web address, in a way resilient to errors and ommisions in global.properties, simplifying where possible
     82            //aiming for a string with no trailing slash, eg "http://localhost:8080/greenstone3" or "http://www.mygreenstonelibrary.com"
     83            String protocolSpecifier = null, hostSpecifier = null, portSpecifier = null, contextSpecifier = null;
     84
     85            //protocol
     86            if ( properties.getProperty("tomcat.protocol") == null || properties.getProperty("tomcat.protocol").equals("") ) {
     87                protocolSpecifier = "http://";
     88            } else {
     89                if ( properties.getProperty("tomcat.protocol").endsWith("://") ) {
     90                    protocolSpecifier = properties.getProperty("tomcat.protocol");
     91                } else {
     92                    protocolSpecifier = properties.getProperty("tomcat.protocol") + "://";
     93                }
     94            }
     95
     96            //hostname
     97            if ( properties.getProperty("tomcat.server") == null ) {
     98                hostSpecifier = "localhost";
     99            } else {
     100                hostSpecifier = properties.getProperty("tomcat.server");
     101                while ( hostSpecifier.endsWith("/") ) {
     102                    hostSpecifier = hostSpecifier.substring( 0, hostSpecifier.length()-1 );
     103                }
     104            }
     105
     106            //port
     107            if (
     108                properties.getProperty("tomcat.port") == null ||
     109                properties.getProperty("tomcat.port").equals("") ||
     110                ( protocolSpecifier.equals("http://") && properties.getProperty("tomcat.port").equals("80") ) ||
     111                ( protocolSpecifier.equals("https://") && properties.getProperty("tomcat.port").equals("443") )
     112                ) {
     113                portSpecifier = "";
     114            } else {
     115                portSpecifier = ":" + properties.getProperty("tomcat.port");
     116            }
     117
     118            //context path
     119            if ( properties.getProperty("tomcat.context") == null || properties.getProperty("tomcat.context").equals("") || properties.getProperty("tomcat.context").equals("/") ) {
     120                contextSpecifier = "";
     121            } else {
     122                contextSpecifier = properties.getProperty("tomcat.context");
     123                if ( !contextSpecifier.startsWith("/") ) {
     124                    contextSpecifier = "/" + contextSpecifier;
     125                }
     126            }
     127
     128            //string it all together
     129            gsdl3_web_address = protocolSpecifier + hostSpecifier + portSpecifier + contextSpecifier;
     130
     131        } catch (Exception e) {
     132            logger.error( "Exception trying to reload global.properties: " + e );
     133        }
    83134    }
    84135}
Note: See TracChangeset for help on using the changeset viewer.