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

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

GS3 Java code has moved away from using tomcat.port to using tomcat.port.<protocol>.

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