Java tutorial
/* * Copyright 2015 52North Initiative for Geospatial Open Source * Software GmbH * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 as published * by the Free Software Foundation. * * If the program is linked with libraries which are licensed under one of * the following licenses, the combination of the program with the linked * library is not considered a "derivative work" of the program: * * - Apache License, version 2.0 * - Apache Software License, version 1.0 * - GNU Lesser General Public License, version 3 * - Mozilla Public License, versions 1.0, 1.1 and 2.0 * - Common Development and Distribution License (CDDL), version 1.0 * * Therefore the distribution of the program linked with libraries licensed * under the aforementioned licenses, is permitted by the copyright holders * if the distribution is compliant with both the GNU General Public * License version 2 and the aforementioned licenses. * * This program 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. */ package org.n52.wfs; import javax.servlet.ServletContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.aop.target.HotSwappableTargetSource; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.XmlWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; import org.n52.iceland.service.DatabaseSettingsHandler; import org.n52.sos.context.ContextSwitcher; public class WfsDispatcherServlet extends DispatcherServlet implements ContextSwitcher { private static final long serialVersionUID = -5393279536808296529L; private static final Logger LOG = LoggerFactory.getLogger(WfsDispatcherServlet.class); private static final String CONTEXT_SWITCHER_BEAN_NAME = "contextSwitcherSwapper"; public static final String UNCONFIGURED_CONFIG_LOCATION_PARAM = "unconfiguredConfigLocations"; public static final String CONFIGURED_CONFIG_LOCATION_PARAM = "configuredConfigLocations"; public static final String COMMON_CONFIG_LOCATION_PARAM = "commonConfigLocation"; private boolean configured; @Override public String getContextConfigLocation() { return getConfigLocation(getServletContext()); } private String getConfigLocation(ServletContext servletContext) { String def = getDefaultConfigLocations(); String com = getCommonConfigLocation(servletContext); String add = isConfigured(servletContext) ? getConfiguredConfigLocation(servletContext) : getUnconfiguredConfigLocation(servletContext); return def + " " + com + " " + add; } protected String getDefaultConfigLocations() { // -> /WEB-INF/dispatcher-servlet.xml return XmlWebApplicationContext.DEFAULT_CONFIG_LOCATION_PREFIX + getNamespace() + XmlWebApplicationContext.DEFAULT_CONFIG_LOCATION_SUFFIX; } private String getConfigLocation(ServletContext servletContext, String initParamName) { String initParam = servletContext.getInitParameter(initParamName); return (initParam == null || initParam.isEmpty()) ? "" : initParam; } private String getCommonConfigLocation(ServletContext servletContext) { return getConfigLocation(servletContext, COMMON_CONFIG_LOCATION_PARAM); } private String getUnconfiguredConfigLocation(ServletContext servletContext) { return getConfigLocation(servletContext, UNCONFIGURED_CONFIG_LOCATION_PARAM); } private String getConfiguredConfigLocation(ServletContext servletContext) { return getConfigLocation(servletContext, CONFIGURED_CONFIG_LOCATION_PARAM); } protected boolean isConfigured(ServletContext servletContext) { DatabaseSettingsHandler handler = new DatabaseSettingsHandler(); handler.setServletContext(servletContext); return this.configured = handler.exists(); } @Override protected void postProcessWebApplicationContext(ConfigurableWebApplicationContext wac) { super.postProcessWebApplicationContext(wac); ServletContext servletContext = wac.getServletContext(); wac.setConfigLocation(getConfigLocation(servletContext)); } @Override protected void onRefresh(ApplicationContext context) { super.onRefresh(context); try { Object targetBean = context.getBean(CONTEXT_SWITCHER_BEAN_NAME); if (targetBean instanceof HotSwappableTargetSource) { ((HotSwappableTargetSource) targetBean).swap(this); } } catch (NoSuchBeanDefinitionException e) { // ignore } } @Override public void reloadContext() { WebApplicationContext applicationContext = getWebApplicationContext(); if (!(applicationContext instanceof ConfigurableWebApplicationContext)) { throw new IllegalStateException( "WebApplicationContext does not support refresh: " + applicationContext); } LOG.info("Reloading context"); this.configureAndRefreshWebApplicationContext((ConfigurableWebApplicationContext) applicationContext); } @Override public boolean isConfigured() { return this.configured; } }