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.gsdl3.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 | /** holds some global properties for the application. Read from a properties file */
|
---|
28 | public class GlobalProperties {
|
---|
29 |
|
---|
30 |
|
---|
31 | static Logger logger = Logger.getLogger(org.greenstone.gsdl3.util.GlobalProperties.class.getName());
|
---|
32 | private static Properties properties = null;
|
---|
33 | private static String properties_filename = "global.properties";
|
---|
34 | private static String gsdl3_home = null;
|
---|
35 | private static String gsdl3_web_address = null;
|
---|
36 |
|
---|
37 | // Note, that if the servlet is reloadable, then it is reloaded each time the file is changed.
|
---|
38 | static {
|
---|
39 | //load in the properties
|
---|
40 | properties = new Properties();
|
---|
41 | reload();
|
---|
42 | }
|
---|
43 |
|
---|
44 | /** get the value of the property 'key'. returns null if not found */
|
---|
45 | public static String getProperty(String key) {
|
---|
46 | return properties.getProperty(key);
|
---|
47 | }
|
---|
48 |
|
---|
49 | /** get the value of the property 'key'.
|
---|
50 | * returns default_value if not found */
|
---|
51 | public static String getProperty(String key, String default_value) {
|
---|
52 | return properties.getProperty(key, default_value);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /** some special ones */
|
---|
56 | public static String getGSDL3Home() {
|
---|
57 | return gsdl3_home;
|
---|
58 | }
|
---|
59 |
|
---|
60 | public static String getGSDL3WebAddress() {
|
---|
61 | return gsdl3_web_address;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public static void reload() {
|
---|
65 | try {
|
---|
66 | InputStream in = Class.forName("org.greenstone.gsdl3.util.GlobalProperties").getClassLoader().getResourceAsStream(properties_filename);
|
---|
67 | if (in != null) {
|
---|
68 | logger.error("loading global properties");
|
---|
69 | properties.load(in);
|
---|
70 | in.close();
|
---|
71 | } else {
|
---|
72 | logger.error("couldn't load global properties!");
|
---|
73 | }
|
---|
74 | gsdl3_home = properties.getProperty("gsdl3.home");
|
---|
75 | // make sure the path separators are correct
|
---|
76 | File gs3_file = new File(gsdl3_home);
|
---|
77 | gsdl3_home = gs3_file.getPath();
|
---|
78 | gsdl3_web_address = "http://"+ properties.getProperty("tomcat.server")+":"+ properties.getProperty("tomcat.port")+"/"+properties.getProperty("tomcat.context");
|
---|
79 | } catch (Exception e) {
|
---|
80 | logger.error("Exception trying to reload global.properties: "+e);
|
---|
81 | }
|
---|
82 |
|
---|
83 | }
|
---|
84 | }
|
---|