summaryrefslogtreecommitdiffstats
path: root/petascope/src/petascope/ConfigManager.java
blob: 0fa0481a53275a8777667144d739e516ffda35b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 * This file is part of rasdaman community.
 *
 * Rasdaman community is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Rasdaman community is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
 rasdaman GmbH.
 *
 * For more information please see <http://www.rasdaman.org>
 * or contact Peter Baumann via <baumann@rasdaman.com>.
 */
package petascope;

//~--- JDK imports ------------------------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import java.io.InputStream;
import java.net.URI;
import java.util.Properties;
import javax.servlet.ServletException;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import petascope.wcs2.server.templates.WcsNamespaceContext;

/**
 * Configuration Manager class: a single entry point for all server settings.
 * Implements the singleton design pattern.
 *
 * @author Andrei Aiordachioaie
 */
public class ConfigManager {

    private static Logger LOG = LoggerFactory.getLogger(ConfigManager.class);

    /* Major version number. This is the first release (1). */
    private final static String MAJOR = "1";
    /* Minor version number. v2 adds the reference implementation of WCS 2.0.
    v3 adds WGS84 handling in WCPS requests, and v4 adds a WPS implementation. */
    private final static String MINOR = "4";
    /* Bug-fix count. We have a hack: every WCPS response is written to disk. */
    private final static String BUGFIX = "11-hack";

    /* Petascope 1.2.0 contains WCS 1.1.0, WCS 2.0, WCS-T 1.0.0 and WCPS 1.0.0 */
    public final static String PETASCOPE_VERSION = MAJOR + "." + MINOR + "." + BUGFIX;
    /* Settings variables */
    public static String WCST_LANGUAGE;
    public static String WCST_VERSION;
    public static String RASDAMAN_URL;
    public static String RASDAMAN_DATABASE;
    public static String METADATA_DRIVER;
    public static String METADATA_URL;
    public static String METADATA_USER;
    public static String METADATA_PASS;
    public static boolean CCIP_HACK = false;

    /* WCS 2.0 variables */
    public static String WCS2_GET_CAPABILITIES_TEMPLATE;
    public static String WCS2_DESCRIBE_COVERAGE_TEMPLATE;
    public static String WCS2_GET_COVERAGE_TEMPLATE;
    public static String WCS2_SCHEMA_URL;

    /* This URL gets initialized automatically when the first request is received.
     * Its value is used in the Capabilities response */
    public static String PETASCOPE_SERVLET_URL;

    /* WCS-T Settings. Overridden by user-preferences in <code>settings.properties</code> */
    public static String WCST_DEFAULT_INTERPOLATION = "none";
    public static String WCST_DEFAULT_NULL_RESISTANCE = "none";
    public static String WCST_DEFAULT_DATATYPE = "unsigned char";
    /* Singleton instance */
    private static ConfigManager instance;
    private static Properties props;

    /**
     * Private constructor. Use <i>getInstance()</i>.
     *
     * @param settingsPath Path to the settings properties file
     * @param servletRoot Path to the root folder where the servlet is deployed
     */
    private ConfigManager(String settingsPath, String servletRoot) throws ServletException {
        props = new Properties();
        try {
            LOG.info("Loading settings from file: " + settingsPath);
            props.load(new FileInputStream(settingsPath));
            initSettings(servletRoot);
        } catch (IOException e) {
            LOG.error("Failed to load settings. Stack trace: " + e);
            throw new ServletException("Failed to load settings file.");
        }
    }

    /**
     * Returns the instance of the ConfigManager. If no such instance exists,
     * it creates one with the specified settings file.
     *
     * @param settingsPath Path to the settings file
     * @param servletRoot Path to the deployed servlet root
     * @return instance of the ConfigManager class
     */
    public static ConfigManager getInstance(String settingsPath, String servletRoot) throws ServletException {
        if (instance == null) {
            instance = new ConfigManager(settingsPath, servletRoot);
        }

        return instance;
    }

    public static ConfigManager getInstance() {
        if (instance == null) {
            throw new RuntimeException("Could not initialize the ConfigManager "
                    + "because no settings file path was provided.");
        }
        return instance;
    }

    /**
     * Return a setting value from the settings file
     *
     * @param key Key of the setting
     * @return String value, or the empty string in case the key does not exist
     */
    private String get(String key) {
        String result = "";

        if (props.containsKey(key)) {
            result = props.getProperty(key);
        }

        return result;
    }

    private void initSettings(String servletRoot) {
        WCST_LANGUAGE = get("wcst_language");
        WCST_VERSION = get("wcst_version");
        RASDAMAN_DATABASE = get("rasdaman_database");
        RASDAMAN_URL = get("rasdaman_url");
        METADATA_DRIVER = get("metadata_driver");
        METADATA_URL = get("metadata_url");
        METADATA_USER = get("metadata_user");
        METADATA_PASS = get("metadata_pass");

        CCIP_HACK = Boolean.parseBoolean(get("ccip_version"));

        try {
            URI desc = WcsNamespaceContext.class.getResource("DescribeCoverageTemplate.xml").toURI();
            URI getcov = WcsNamespaceContext.class.getResource("GetCoverageTemplate.xml").toURI();
            URI getcap = WcsNamespaceContext.class.getResource("GetCapabilitiesTemplate.xml").toURI();
            WCS2_GET_CAPABILITIES_TEMPLATE = loadFile(getcap);
            WCS2_DESCRIBE_COVERAGE_TEMPLATE = loadFile(desc);
            WCS2_GET_COVERAGE_TEMPLATE = loadFile(getcov);
            WCS2_SCHEMA_URL = get("wcs2_schema_url");

        } catch (Exception e) {
            LOG.warn("Could not read XML template files for WCS 2.0. Therefore, WCS 2.0 will be unable to start.");
        }

        /* User preferences override default values for WCS-T */
        String tmp = get("default_interpolation");
        if (tmp.length() > 0) {
            WCST_DEFAULT_INTERPOLATION = tmp;
        }
        tmp = get("default_null_resistance");
        if (tmp.length() > 0) {
            WCST_DEFAULT_NULL_RESISTANCE = tmp;
        }
        tmp = get("default_datatype");
        if (tmp.length() > 0) {
            WCST_DEFAULT_DATATYPE = tmp;
        }

        LOG.info("---------------------------");
        if (CCIP_HACK) {
            LOG.info("-----------CCIP------------");
        }
        LOG.info("---------------------------");

//        log("Print Log: " + PRINT_LOG);
        LOG.info("       *** PETASCOPE ***      ");
        LOG.info("Rasdaman URL: " + RASDAMAN_URL);
        LOG.info("Rasdaman DB: " + RASDAMAN_DATABASE);
        LOG.info("Metadata Driver: " + METADATA_DRIVER);
        LOG.info("Metadata URL: " + METADATA_URL);
        LOG.info("Metadata Username: " + METADATA_USER);
//        LOG.info("Metadata Password: " + METADATA_PASS);
        LOG.info("       *** WCS-T ***       ");
        LOG.info("WCS-T Language: " + WCST_LANGUAGE);
        LOG.info("WCS-T Version: " + WCST_VERSION);
        LOG.info("WCS-T Default Interpolation: " + WCST_DEFAULT_INTERPOLATION);
        LOG.info("WCS-T Default Null Resistance: " + WCST_DEFAULT_NULL_RESISTANCE);
        LOG.info("WCS-T Default Datatype: " + WCST_DEFAULT_DATATYPE);
        LOG.info("       *** WCS 2.0 ***     ");
        LOG.trace("Get Capabilities Template: " + WCS2_GET_CAPABILITIES_TEMPLATE.substring(0, 100));
        LOG.trace("Describe Coverage Template: " + WCS2_DESCRIBE_COVERAGE_TEMPLATE.substring(0, 100));
        LOG.trace("Get Capabilities Template: " + WCS2_GET_COVERAGE_TEMPLATE.substring(0, 100));
        LOG.info("---------------------------");
    }

    private String loadFile(URI fileUri) throws IOException {
        InputStream is = null;
        String contents = null;
        try {
            LOG.debug("Loading file: " + fileUri);
            File f = new File(fileUri);
            is = new FileInputStream(f);
            contents = IOUtils.toString(is);
        } finally {
            try {
                is.close();
            } catch (IOException ex) {
            }
        }
        return contents;
    }
}