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

Last change on this file since 25983 was 25951, checked in by ak19, 12 years ago

gsdl3.home no longer exists as a property in global.properties. This had broken gs3-server.sh, but now it defaults to the usual GSDL3SRCHOME/web folder if there is no gsdl3.home property.

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