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

Last change on this file since 32549 was 32433, checked in by ak19, 6 years ago

Oversight. If httpPort is 80, don't append colon and 80 to form base URL.

  • Property svn:keywords set to Author Date Id Revision
File size: 8.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.io.File;
22import java.io.InputStream;
23import java.io.FileInputStream;
24import java.util.Properties;
25
26import org.apache.log4j.Logger;
27
28/**
29 * holds some global properties for the application. Read from a properties file
30 */
31public class GlobalProperties
32{
33
34 static Logger logger = Logger.getLogger(org.greenstone.util.GlobalProperties.class.getName());
35 private static Properties properties = new Properties();
36 private static String properties_filename = "global.properties";
37 private static String gsdl3_home = null;
38 private static String gsdl3_writablehome = null;
39 private static String gsdl3_web_address = null;
40 private static String full_gsdl3_web_address = null; // for the default protocol
41
42 private static String http_full_gsdl3_web_address = null; // if http or both protocols supported
43 private static String https_full_gsdl3_web_address = null; // if https or both protocols supported
44
45 // The locally accessible url such as for solr is always available at http://127.0.0.1:<httpPort>
46 // regardless of whether http is listed as one of the server protocols
47 private static String localhost_http_web_address = null;
48
49 /** get the value of the property 'key'. returns null if not found */
50 public static String getProperty(String key)
51 {
52 return properties.getProperty(key);
53 }
54
55 /**
56 * get the value of the property 'key'. returns default_value if not found
57 */
58 public static String getProperty(String key, String default_value)
59 {
60 return properties.getProperty(key, default_value);
61 }
62
63 /** some special ones */
64 public static String getGSDL3Home()
65 {
66 return gsdl3_home;
67 }
68
69 public static String getGSDL3WritableHome()
70 {
71 return gsdl3_writablehome;
72 }
73
74 public static String getGS2Build()
75 {
76 return gsdl3_home + File.separator + ".." + File.separator + "gs2build";
77 }
78
79 public static String getGSDL3WebAddress()
80 {
81 return gsdl3_web_address;
82 }
83
84 public static String getFullGSDL3WebAddress()
85 {
86 return full_gsdl3_web_address;
87 }
88
89 public static String getLocalHttpBaseAddress()
90 {
91 return localhost_http_web_address;
92 }
93
94 public static void loadGlobalProperties(String optionalGS3Home)
95 {
96 try
97 {
98 InputStream in = Class.forName("org.greenstone.util.GlobalProperties").getClassLoader().getResourceAsStream(properties_filename);
99
100 if (in==null)
101 {
102 // Try to find it through gsdl3.writablehome
103 logger.info("Couldn't find '" + properties_filename + "' through ClassLoader");
104 logger.info("Trying alternative path through System property gsdl3.writablehome");
105 gsdl3_writablehome = System.getProperty("gsdl3.writablehome");
106 if (gsdl3_writablehome!=null) {
107
108 String gp_full_filename = gsdl3_writablehome + File.separator + "WEB-INF"
109 + File.separator + "classes"
110 + File.separator + properties_filename;
111
112 try {
113 in = new FileInputStream(gp_full_filename);
114 logger.info("Found '" + properties_filename + "'");
115 }
116 catch (Exception e) {
117 logger.info("Unable to locate '" + gp_full_filename + "'");
118 }
119 }
120 else {
121 logger.info("Java property gsdl3.writablehome was not set");
122 }
123 }
124
125 if (in != null)
126 {
127 logger.debug("Loading global properties");
128 properties.load(in);
129 in.close();
130 }
131 else {
132 logger.error("Failed to find '" + properties_filename + "'");
133 }
134
135 gsdl3_home = properties.getProperty("gsdl3.home");
136 if ((gsdl3_home == null || gsdl3_home.length() == 0)
137 && optionalGS3Home != null && optionalGS3Home.length() > 0)
138 {
139 gsdl3_home = optionalGS3Home;
140 }
141
142 // if gsdl3_home is still null, fall back to default: gsdl3srchome/web
143 if (gsdl3_home == null) {
144 gsdl3_home = System.getenv("GSDL3SRCHOME") + File.separator + "web";
145 logger.warn("** Note: falling back to using GSDL3SRCHOME to set gsdl3.home to: " + gsdl3_home);
146 }
147
148 // make sure the path separators are correct
149 // gsdl3_home may be null, e.g., when we are loading properties from Server3
150 if (gsdl3_home != null)
151 {
152 File gs3_file = new File(gsdl3_home);
153 gsdl3_home = gs3_file.getPath();
154 }
155
156 gsdl3_writablehome = properties.getProperty("gsdl3.writablehome");
157 // if gsdl3_writablehome is null, then defaults to gsdl3_home
158 if (gsdl3_writablehome == null) {
159 gsdl3_writablehome = gsdl3_home;
160 }
161
162 //build the gsdl3 web address, in a way resilient to errors and ommisions in global.properties, simplifying where possible
163 //aiming for a string with no trailing slash, eg "http://localhost:8080/greenstone3" or "http://www.mygreenstonelibrary.com"
164 String protocolSpecifier = null, hostSpecifier = null, portSpecifier = null, contextSpecifier = null;
165
166 // default protocol
167 protocolSpecifier = properties.getProperty("server.protocol");
168 if (protocolSpecifier == null || protocolSpecifier.equals(""))
169 {
170 protocolSpecifier = "http://";
171 }
172 else if (!protocolSpecifier.endsWith("://"))
173 {
174 protocolSpecifier = protocolSpecifier + "://";
175 }
176
177 //hostname
178 hostSpecifier = properties.getProperty("tomcat.server");
179 if (hostSpecifier == null)
180 {
181 hostSpecifier = "localhost";
182 }
183 else
184 {
185 while (hostSpecifier.endsWith("/"))
186 {
187 hostSpecifier = hostSpecifier.substring(0, hostSpecifier.length() - 1);
188 }
189 }
190
191 //default port (port for the default protocol)
192 portSpecifier = properties.getProperty("tomcat.port"); // support for legacy tomcat.port?
193 if (portSpecifier == null || portSpecifier.equals("")
194 || (protocolSpecifier.equals("http://") && portSpecifier.equals("80"))
195 || (protocolSpecifier.equals("https://") && portSpecifier.equals("443")))
196 {
197 portSpecifier = "";
198 }
199 else
200 {
201 portSpecifier = ":" + portSpecifier;
202 }
203
204 //context path
205 contextSpecifier = properties.getProperty("tomcat.context");
206 if (contextSpecifier == null || contextSpecifier.equals("") || contextSpecifier.equals("/"))
207 {
208 contextSpecifier = "";
209 }
210 else
211 {
212 if (!contextSpecifier.startsWith("/"))
213 {
214 contextSpecifier = "/" + contextSpecifier;
215 }
216 }
217
218 //string it all together
219 full_gsdl3_web_address = protocolSpecifier + hostSpecifier + portSpecifier + contextSpecifier;
220 gsdl3_web_address = contextSpecifier;
221
222 // Set the always available internal root address that is locally accessible (http://127.0.0.1:<httpPort>)
223 // Used by solr.
224 String httpPort = properties.getProperty("localhost.port.http");
225 localhost_http_web_address = properties.getProperty("localhost.protocol.http") + "://"
226 + properties.getProperty("localhost.server.http", "127.0.0.1"); // 127.0.0.1 better as fallback rather than localhost, since localhost can be modified and is therefore unsafe
227 if(httpPort != null && !httpPort.equals("") && !httpPort.equals("80")) { // no need to check 443, *http*Port can't be 443
228 localhost_http_web_address = localhost_http_web_address + ":" + httpPort;
229 }
230
231 // set the http and https variants of the full web address, if they're meant to be available
232 // First check the default protocol, then set the web address for the other protocol too
233 String supportedProtocols = properties.getProperty("server.protocols", "http");
234 String isHttpRestrictedToLocal = properties.getProperty("restrict.http.to.local", "true");
235
236 if(protocolSpecifier.startsWith("https")) {
237 https_full_gsdl3_web_address = full_gsdl3_web_address;
238
239 // and set http version of URL, if http is made public (if http is in server.protocols list)
240 if(isHttpRestrictedToLocal.equals("false")) {
241 http_full_gsdl3_web_address = "http://" + hostSpecifier + httpPort + contextSpecifier;
242 }
243 } else { // default protocol was http
244 http_full_gsdl3_web_address = full_gsdl3_web_address;
245
246 // and set https version, if https enabled
247 if(supportedProtocols.contains("https")) {
248 String httpsPort = properties.getProperty("tomcat.port.https");
249 https_full_gsdl3_web_address = "https://" + hostSpecifier + httpsPort + contextSpecifier;
250 }
251 }
252 }
253 catch (Exception e)
254 {
255 logger.error("Exception trying to reload global.properties: " + e);
256 e.printStackTrace();
257 }
258 }
259}
Note: See TracBrowser for help on using the repository browser.