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

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

Removed debugging output

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