edu.jhu.pha.vospace.SettingsServlet.java Source code

Java tutorial

Introduction

Here is the source code for edu.jhu.pha.vospace.SettingsServlet.java

Source

/*******************************************************************************
 * Copyright 2013 Johns Hopkins University
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package edu.jhu.pha.vospace;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.DefaultConfigurationBuilder;
import org.apache.log4j.Logger;

/**
 * Initialises the settings object in application context from the settingsFileName parameter of the servlet
 * @author Dmitry Mishin
 *
 */
public class SettingsServlet extends HttpServlet {

    private static final long serialVersionUID = -5938376645952514807L;
    private static final Logger logger = Logger.getLogger(SettingsServlet.class);
    private static Configuration config;

    @Override
    public void init() throws ServletException {
        try {
            DefaultConfigurationBuilder factory = new DefaultConfigurationBuilder(
                    getServletConfig().getInitParameter("settingsFileName"));
            Configuration config = factory.getConfiguration();
            getServletContext().setAttribute("configuration", config);
            SettingsServlet.config = config;
        } catch (ConfigurationException e) {
            logger.error("Error loading configuration", e);
            throw new ServletException(e);
        }

    }

    public static Configuration getConfig() {
        return config;
    }
}