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

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

Simplifying the code.

  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 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 protocolSpecifier = properties.getProperty("server.protocol");
156 if (protocolSpecifier == null || protocolSpecifier.equals(""))
157 {
158 protocolSpecifier = "http://";
159 }
160 else if (!protocolSpecifier.endsWith("://"))
161 {
162 protocolSpecifier = protocolSpecifier + "://";
163 }
164
165 //hostname
166 hostSpecifier = properties.getProperty("tomcat.server");
167 if (hostSpecifier == null)
168 {
169 hostSpecifier = "localhost";
170 }
171 else
172 {
173 while (hostSpecifier.endsWith("/"))
174 {
175 hostSpecifier = hostSpecifier.substring(0, hostSpecifier.length() - 1);
176 }
177 }
178
179 //port
180 portSpecifier = properties.getProperty("tomcat.port");
181 if (portSpecifier == null || portSpecifier.equals("")
182 || (protocolSpecifier.equals("http://") && portSpecifier.equals("80"))
183 || (protocolSpecifier.equals("https://") && portSpecifier.equals("443")))
184 {
185 portSpecifier = "";
186 }
187 else
188 {
189 portSpecifier = ":" + portSpecifier;
190 }
191
192 //context path
193 contextSpecifier = properties.getProperty("tomcat.context");
194 if (contextPath == null || contextPath.equals("") || contextPath.equals("/"))
195 {
196 contextSpecifier = "";
197 }
198 else
199 {
200 if (!contextSpecifier.startsWith("/"))
201 {
202 contextSpecifier = "/" + contextSpecifier;
203 }
204 }
205
206 //string it all together
207 full_gsdl3_web_address = protocolSpecifier + hostSpecifier + portSpecifier + contextSpecifier;
208 gsdl3_web_address = contextSpecifier;
209 }
210 catch (Exception e)
211 {
212 logger.error("Exception trying to reload global.properties: " + e);
213 e.printStackTrace();
214 }
215 }
216}
Note: See TracBrowser for help on using the repository browser.