source: main/trunk/greenstone3/src/java/org/greenstone/util/GlobalProperties.java@ 24897

Last change on this file since 24897 was 24897, checked in by sjm84, 12 years ago

Added in a method to get the gs2build folder

  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 KB
Line 
1/*
2 * GlobalProperties.java
3 * Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19package org.greenstone.util;
20
21import java.util.Properties;
22import java.io.File;
23import java.io.InputStream;
24
25import org.apache.log4j.*;
26
27/**
28 * holds some global properties for the application. Read from a properties file
29 */
30public class GlobalProperties
31{
32
33 static Logger logger = Logger.getLogger(org.greenstone.util.GlobalProperties.class.getName());
34 private static Properties properties = null;
35 private static String properties_filename = "global.properties";
36 private static String gsdl3_home = null;
37 private static String gsdl3_web_address = null;
38
39 // Note, that if the servlet is reloadable, then it is reloaded each time the file is changed.
40 static
41 {
42 //load in the properties
43 properties = new Properties();
44 reload();
45 }
46
47 /** get the value of the property 'key'. returns null if not found */
48 public static String getProperty(String key)
49 {
50 return properties.getProperty(key);
51 }
52
53 /**
54 * get the value of the property 'key'. returns default_value if not found
55 */
56 public static String getProperty(String key, String default_value)
57 {
58 return properties.getProperty(key, default_value);
59 }
60
61 /** some special ones */
62 public static String getGSDL3Home()
63 {
64 return gsdl3_home;
65 }
66
67 public static String getGS2Build()
68 {
69 return gsdl3_home + File.separator + ".." + File.separator + "gs2build";
70 }
71
72 public static String getGSDL3WebAddress()
73 {
74 return gsdl3_web_address;
75 }
76
77 public static void reload()
78 {
79 try
80 {
81
82 InputStream in = Class.forName("org.greenstone.util.GlobalProperties").getClassLoader().getResourceAsStream(properties_filename);
83 if (in != null)
84 {
85 logger.debug("loading global properties");
86 properties.load(in);
87 in.close();
88 }
89 else
90 {
91 logger.error("couldn't load global properties!");
92 }
93 gsdl3_home = properties.getProperty("gsdl3.home");
94 // make sure the path separators are correct
95 File gs3_file = new File(gsdl3_home);
96 gsdl3_home = gs3_file.getPath();
97
98 //build the gsdl3 web address, in a way resilient to errors and ommisions in global.properties, simplifying where possible
99 //aiming for a string with no trailing slash, eg "http://localhost:8080/greenstone3" or "http://www.mygreenstonelibrary.com"
100 String protocolSpecifier = null, hostSpecifier = null, portSpecifier = null, contextSpecifier = null;
101
102 //protocol
103 if (properties.getProperty("tomcat.protocol") == null || properties.getProperty("tomcat.protocol").equals(""))
104 {
105 protocolSpecifier = "http://";
106 }
107 else
108 {
109 if (properties.getProperty("tomcat.protocol").endsWith("://"))
110 {
111 protocolSpecifier = properties.getProperty("tomcat.protocol");
112 }
113 else
114 {
115 protocolSpecifier = properties.getProperty("tomcat.protocol") + "://";
116 }
117 }
118
119 //hostname
120 if (properties.getProperty("tomcat.server") == null)
121 {
122 hostSpecifier = "localhost";
123 }
124 else
125 {
126 hostSpecifier = properties.getProperty("tomcat.server");
127 while (hostSpecifier.endsWith("/"))
128 {
129 hostSpecifier = hostSpecifier.substring(0, hostSpecifier.length() - 1);
130 }
131 }
132
133 //port
134 if (properties.getProperty("tomcat.port") == null || properties.getProperty("tomcat.port").equals("") || (protocolSpecifier.equals("http://") && properties.getProperty("tomcat.port").equals("80")) || (protocolSpecifier.equals("https://") && properties.getProperty("tomcat.port").equals("443")))
135 {
136 portSpecifier = "";
137 }
138 else
139 {
140 portSpecifier = ":" + properties.getProperty("tomcat.port");
141 }
142
143 //context path
144 if (properties.getProperty("tomcat.context") == null || properties.getProperty("tomcat.context").equals("") || properties.getProperty("tomcat.context").equals("/"))
145 {
146 contextSpecifier = "";
147 }
148 else
149 {
150 contextSpecifier = properties.getProperty("tomcat.context");
151 if (!contextSpecifier.startsWith("/"))
152 {
153 contextSpecifier = "/" + contextSpecifier;
154 }
155 }
156
157 //string it all together
158 gsdl3_web_address = protocolSpecifier + hostSpecifier + portSpecifier + contextSpecifier;
159
160 }
161 catch (Exception e)
162 {
163 logger.error("Exception trying to reload global.properties: " + e);
164 }
165 }
166}
Note: See TracBrowser for help on using the repository browser.