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 | */
|
---|
19 | package org.greenstone.util;
|
---|
20 |
|
---|
21 | import java.util.Properties;
|
---|
22 | import java.io.File;
|
---|
23 | import java.io.InputStream;
|
---|
24 |
|
---|
25 | import org.apache.log4j.*;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * holds some global properties for the application. Read from a properties file
|
---|
29 | */
|
---|
30 | public class GlobalProperties
|
---|
31 | {
|
---|
32 | static Logger logger = Logger.getLogger(org.greenstone.util.GlobalProperties.class.getName());
|
---|
33 | private static Properties properties = null;
|
---|
34 | private static String properties_filename = "global.properties";
|
---|
35 | private static String gsdl3_home = null;
|
---|
36 | private static String gsdl3_web_address = null;
|
---|
37 |
|
---|
38 | // Note, that if the servlet is reloadable, then it is reloaded each time the file is changed.
|
---|
39 | static
|
---|
40 | {
|
---|
41 | //load in the properties
|
---|
42 | properties = new Properties();
|
---|
43 | reload();
|
---|
44 | }
|
---|
45 |
|
---|
46 | /** get the value of the property 'key'. returns null if not found */
|
---|
47 | public static String getProperty(String key)
|
---|
48 | {
|
---|
49 | return properties.getProperty(key);
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * get the value of the property 'key'. returns default_value if not found
|
---|
54 | */
|
---|
55 | public static String getProperty(String key, String default_value)
|
---|
56 | {
|
---|
57 | return properties.getProperty(key, default_value);
|
---|
58 | }
|
---|
59 |
|
---|
60 | /** some special ones */
|
---|
61 | public static String getGSDL3Home()
|
---|
62 | {
|
---|
63 | return gsdl3_home;
|
---|
64 | }
|
---|
65 |
|
---|
66 | public static String getGSDL3WebAddress()
|
---|
67 | {
|
---|
68 | return gsdl3_web_address;
|
---|
69 | }
|
---|
70 |
|
---|
71 | public static void reload()
|
---|
72 | {
|
---|
73 | try
|
---|
74 | {
|
---|
75 |
|
---|
76 | InputStream in = Class.forName("org.greenstone.util.GlobalProperties").getClassLoader().getResourceAsStream(properties_filename);
|
---|
77 | if (in != null)
|
---|
78 | {
|
---|
79 | logger.debug("loading global properties");
|
---|
80 | properties.load(in);
|
---|
81 | in.close();
|
---|
82 | }
|
---|
83 | else
|
---|
84 | {
|
---|
85 | logger.error("couldn't load global properties!");
|
---|
86 | }
|
---|
87 | gsdl3_home = properties.getProperty("gsdl3.home");
|
---|
88 | // make sure the path separators are correct
|
---|
89 | File gs3_file = new File(gsdl3_home);
|
---|
90 | gsdl3_home = gs3_file.getPath();
|
---|
91 |
|
---|
92 | //build the gsdl3 web address, in a way resilient to errors and ommisions in global.properties, simplifying where possible
|
---|
93 | //aiming for a string with no trailing slash, eg "http://localhost:8080/greenstone3" or "http://www.mygreenstonelibrary.com"
|
---|
94 | String protocolSpecifier = null, hostSpecifier = null, portSpecifier = null, contextSpecifier = null;
|
---|
95 |
|
---|
96 | //protocol
|
---|
97 | if (properties.getProperty("tomcat.protocol") == null || properties.getProperty("tomcat.protocol").equals(""))
|
---|
98 | {
|
---|
99 | protocolSpecifier = "http://";
|
---|
100 | }
|
---|
101 | else
|
---|
102 | {
|
---|
103 | if (properties.getProperty("tomcat.protocol").endsWith("://"))
|
---|
104 | {
|
---|
105 | protocolSpecifier = properties.getProperty("tomcat.protocol");
|
---|
106 | }
|
---|
107 | else
|
---|
108 | {
|
---|
109 | protocolSpecifier = properties.getProperty("tomcat.protocol") + "://";
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | //hostname
|
---|
114 | if (properties.getProperty("tomcat.server") == null)
|
---|
115 | {
|
---|
116 | hostSpecifier = "localhost";
|
---|
117 | }
|
---|
118 | else
|
---|
119 | {
|
---|
120 | hostSpecifier = properties.getProperty("tomcat.server");
|
---|
121 | while (hostSpecifier.endsWith("/"))
|
---|
122 | {
|
---|
123 | hostSpecifier = hostSpecifier.substring(0, hostSpecifier.length() - 1);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | //port
|
---|
128 | 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")))
|
---|
129 | {
|
---|
130 | portSpecifier = "";
|
---|
131 | }
|
---|
132 | else
|
---|
133 | {
|
---|
134 | portSpecifier = ":" + properties.getProperty("tomcat.port");
|
---|
135 | }
|
---|
136 |
|
---|
137 | //context path
|
---|
138 | if (properties.getProperty("tomcat.context") == null || properties.getProperty("tomcat.context").equals("") || properties.getProperty("tomcat.context").equals("/"))
|
---|
139 | {
|
---|
140 | contextSpecifier = "";
|
---|
141 | }
|
---|
142 | else
|
---|
143 | {
|
---|
144 | contextSpecifier = properties.getProperty("tomcat.context");
|
---|
145 | if (!contextSpecifier.startsWith("/"))
|
---|
146 | {
|
---|
147 | contextSpecifier = "/" + contextSpecifier;
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | //string it all together
|
---|
152 | gsdl3_web_address = protocolSpecifier + hostSpecifier + portSpecifier + contextSpecifier;
|
---|
153 |
|
---|
154 | }
|
---|
155 | catch (Exception e)
|
---|
156 | {
|
---|
157 | logger.error("Exception trying to reload global.properties: " + e);
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|