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

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

'writabe' version of gsdl3_home introduced to help support Greenstone running off a read-only medium, such as a DVD

  • Property svn:keywords set to Author Date Id Revision
File size: 5.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.util.Properties;
24
25import org.apache.log4j.Logger;
26
27/**
28 * holds some global properties for the application. Read from a properties file
29 */
30public class GlobalProperties
31{
32
33 static Logger logger = Logger.getLogger(org.greenstone.util.GlobalProperties.class.getName());
34 private static Properties properties = new Properties();
35 private static String properties_filename = "global.properties";
36 private static String gsdl3_home = null;
37 private static String gsdl3_writablehome = null;
38 private static String gsdl3_web_address = null;
39 private static String full_gsdl3_web_address = null;
40
41 /** get the value of the property 'key'. returns null if not found */
42 public static String getProperty(String key)
43 {
44 return properties.getProperty(key);
45 }
46
47 /**
48 * get the value of the property 'key'. returns default_value if not found
49 */
50 public static String getProperty(String key, String default_value)
51 {
52 return properties.getProperty(key, default_value);
53 }
54
55 /** some special ones */
56 public static String getGSDL3Home()
57 {
58 return gsdl3_home;
59 }
60
61 public static String getGSDL3WritableHome()
62 {
63 return gsdl3_writablehome;
64 }
65
66 public static String getGS2Build()
67 {
68 return gsdl3_home + File.separator + ".." + File.separator + "gs2build";
69 }
70
71 public static String getGSDL3WebAddress()
72 {
73 return gsdl3_web_address;
74 }
75
76 public static String getFullGSDL3WebAddress()
77 {
78 return full_gsdl3_web_address;
79 }
80
81 public static void loadGlobalProperties(String optionalGS3Home)
82 {
83 try
84 {
85 InputStream in = Class.forName("org.greenstone.util.GlobalProperties").getClassLoader().getResourceAsStream(properties_filename);
86 if (in != null)
87 {
88 logger.debug("Loading global properties");
89 properties.load(in);
90 in.close();
91 }
92 else
93 {
94 logger.error("Couldn't load global properties: " + properties_filename);
95 }
96
97 gsdl3_home = properties.getProperty("gsdl3.home");
98 if ((gsdl3_home == null || gsdl3_home.length() == 0)
99 && optionalGS3Home != null && optionalGS3Home.length() > 0)
100 {
101 gsdl3_home = optionalGS3Home;
102 }
103
104 // if gsdl3_home is still null, fall back to default: gsdl3srchome/web
105 if (gsdl3_home == null) {
106 gsdl3_home = System.getenv("GSDL3SRCHOME") + File.separator + "web";
107 logger.warn("** Note: falling back to using GSDL3SRCHOME to set gsdl3.home to: " + gsdl3_home);
108 }
109
110 // make sure the path separators are correct
111 // gsdl3_home may be null, e.g., when we are loading properties from Server3
112 if (gsdl3_home != null)
113 {
114 File gs3_file = new File(gsdl3_home);
115 gsdl3_home = gs3_file.getPath();
116 }
117
118 gsdl3_writablehome = properties.getProperty("gsdl3.writablehome");
119 // if gsdl3_writablehome is null, then defaults to gsdl3_home
120 if (gsdl3_writablehome == null) {
121 gsdl3_writablehome = gsdl3_home;
122 }
123
124 //build the gsdl3 web address, in a way resilient to errors and ommisions in global.properties, simplifying where possible
125 //aiming for a string with no trailing slash, eg "http://localhost:8080/greenstone3" or "http://www.mygreenstonelibrary.com"
126 String protocolSpecifier = null, hostSpecifier = null, portSpecifier = null, contextSpecifier = null;
127
128 //protocol
129 if (properties.getProperty("tomcat.protocol") == null || properties.getProperty("tomcat.protocol").equals(""))
130 {
131 protocolSpecifier = "http://";
132 }
133 else
134 {
135 if (properties.getProperty("tomcat.protocol").endsWith("://"))
136 {
137 protocolSpecifier = properties.getProperty("tomcat.protocol");
138 }
139 else
140 {
141 protocolSpecifier = properties.getProperty("tomcat.protocol") + "://";
142 }
143 }
144
145 //hostname
146 if (properties.getProperty("tomcat.server") == null)
147 {
148 hostSpecifier = "localhost";
149 }
150 else
151 {
152 hostSpecifier = properties.getProperty("tomcat.server");
153 while (hostSpecifier.endsWith("/"))
154 {
155 hostSpecifier = hostSpecifier.substring(0, hostSpecifier.length() - 1);
156 }
157 }
158
159 //port
160 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")))
161 {
162 portSpecifier = "";
163 }
164 else
165 {
166 portSpecifier = ":" + properties.getProperty("tomcat.port");
167 }
168
169 //context path
170 if (properties.getProperty("tomcat.context") == null || properties.getProperty("tomcat.context").equals("") || properties.getProperty("tomcat.context").equals("/"))
171 {
172 contextSpecifier = "";
173 }
174 else
175 {
176 contextSpecifier = properties.getProperty("tomcat.context");
177 if (!contextSpecifier.startsWith("/"))
178 {
179 contextSpecifier = "/" + contextSpecifier;
180 }
181 }
182
183 //string it all together
184 full_gsdl3_web_address = protocolSpecifier + hostSpecifier + portSpecifier + contextSpecifier;
185 gsdl3_web_address = contextSpecifier;
186 }
187 catch (Exception e)
188 {
189 logger.error("Exception trying to reload global.properties: " + e);
190 e.printStackTrace();
191 }
192 }
193}
Note: See TracBrowser for help on using the repository browser.