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 | */
|
---|
19 | package org.greenstone.util;
|
---|
20 |
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.InputStream;
|
---|
23 | import java.io.FileInputStream;
|
---|
24 | import java.util.Properties;
|
---|
25 |
|
---|
26 | import org.apache.log4j.Logger;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * holds some global properties for the application. Read from a properties file
|
---|
30 | */
|
---|
31 | public 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");
|
---|
184 | if (portSpecifier == null || portSpecifier.equals("")
|
---|
185 | || (protocolSpecifier.equals("http://") && portSpecifier.equals("80"))
|
---|
186 | || (protocolSpecifier.equals("https://") && portSpecifier.equals("443")))
|
---|
187 | {
|
---|
188 | portSpecifier = "";
|
---|
189 | }
|
---|
190 | else
|
---|
191 | {
|
---|
192 | portSpecifier = ":" + portSpecifier;
|
---|
193 | }
|
---|
194 |
|
---|
195 | //context path
|
---|
196 | contextSpecifier = properties.getProperty("tomcat.context");
|
---|
197 | if (contextSpecifier == null || contextSpecifier.equals("") || contextSpecifier.equals("/"))
|
---|
198 | {
|
---|
199 | contextSpecifier = "";
|
---|
200 | }
|
---|
201 | else
|
---|
202 | {
|
---|
203 | if (!contextSpecifier.startsWith("/"))
|
---|
204 | {
|
---|
205 | contextSpecifier = "/" + contextSpecifier;
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | //string it all together
|
---|
210 | full_gsdl3_web_address = protocolSpecifier + hostSpecifier + portSpecifier + contextSpecifier;
|
---|
211 | gsdl3_web_address = contextSpecifier;
|
---|
212 |
|
---|
213 | // set the http and https variants of the full web address, if they're meant to be available
|
---|
214 | if(protocolSpecifier.startsWith("https")) { // check the default protocol, then set the address for the other protocol too
|
---|
215 | https_full_gsdl3_web_address = full_gsdl3_web_address;
|
---|
216 |
|
---|
217 | // and set http version, if sufficient properties are available
|
---|
218 | String httpPort = properties.getProperty("tomcat.port.http");
|
---|
219 | if(httpPort != null && !httpPort.equals("")) {
|
---|
220 | http_full_gsdl3_web_address = "http://" + hostSpecifier + httpPort + contextSpecifier;
|
---|
221 | }
|
---|
222 | } else { // default protocol was http
|
---|
223 | http_full_gsdl3_web_address = full_gsdl3_web_address;
|
---|
224 |
|
---|
225 | // and set https version, if sufficient properties are available
|
---|
226 | String httpsPort = properties.getProperty("tomcat.port.https");
|
---|
227 | if(httpsPort != null && !httpsPort.equals("")) {
|
---|
228 | https_full_gsdl3_web_address = "https://" + hostSpecifier + httpsPort + contextSpecifier;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | }
|
---|
232 | catch (Exception e)
|
---|
233 | {
|
---|
234 | logger.error("Exception trying to reload global.properties: " + e);
|
---|
235 | e.printStackTrace();
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|