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

Last change on this file since 27868 was 27868, checked in by davidb, 11 years ago

Additional fallback code added that tries to use System property gsdl3.writablehome to locate global.properties

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