de.fhg.igd.vaadin.util.servlets.AutowiringApplicationServlet.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.igd.vaadin.util.servlets.AutowiringApplicationServlet.java

Source

// This file is part of Vaadin Utils
//
// Copyright (c) 2012 Fraunhofer IGD
//
// MongoMVCC is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// MongoMVCC 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with Vaadin Utils. If not, see <http://www.gnu.org/licenses/>.
package de.fhg.igd.vaadin.util.servlets;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.ApplicationServlet;

/**
 * This servlet connects a Vaadin Application to the Web Container
 * * 
 * @author Robert Gregor
 */
public class AutowiringApplicationServlet extends ApplicationServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 7956838397103989242L;

    private WebApplicationContext webApplicationContext;

    /**
     * Get the {@link AutowireCapableBeanFactory} associated with the containing Spring {@link WebApplicationContext}.
     * This only works after the servlet has been initialized (via {@link #init init()}).
     *
     * @throws ServletException if the operation fails
     */
    protected final AutowireCapableBeanFactory getAutowireCapableBeanFactory() throws ServletException {
        try {
            return getWebApplicationContext().getAutowireCapableBeanFactory();
        } catch (final IllegalStateException e) {
            throw new ServletException(
                    "containing context " + getWebApplicationContext() + " is not autowire-capable", e);
        }
    }

    /**
     * Create and configure a new instance of the configured application class.
     *
     * <p>
     * The implementation in {@link AutowiringApplicationServlet} delegates to
     * {@link #getAutowireCapableBeanFactory getAutowireCapableBeanFactory()}, then invokes
     * {@link AutowireCapableBeanFactory#createBean AutowireCapableBeanFactory.createBean()}
     * using the configured {@link Application} class.
     * </p>
     *
     * @param request the triggering {@link HttpServletRequest}
     * @throws ServletException if creation or autowiring fails
     */
    @Override
    protected Application getNewApplication(HttpServletRequest request) throws ServletException {
        final Class<? extends Application> cl;
        try {
            cl = getApplicationClass();
        } catch (final ClassNotFoundException e) {
            throw new ServletException("failed to determine ApplicationClass", e);
        }
        try {
            final AutowireCapableBeanFactory beanFactory = getAutowireCapableBeanFactory();
            return beanFactory.createBean(cl);
        } catch (final BeansException e) {
            throw new ServletException("failed to create new instance of " + cl, e);
        }
    }

    /**
     * Get the containing Spring {@link WebApplicationContext}
     * This only works after the servlet has been initialized (via {@link #init init()}).
     *
     * @throws ServletException if the operation fails
     */
    protected final WebApplicationContext getWebApplicationContext() throws ServletException {
        if (webApplicationContext == null)
            throw new ServletException("can't retrieve WebApplicationContext before init() is invoked");
        return webApplicationContext;
    }

    /**
    * Initialize this servlet.
    *
    * @throws ServletException if there is no {@link WebApplicationContext} associated with this servlet's context
    */
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        // initApplicationClass(servletConfig);
        try {
            webApplicationContext = WebApplicationContextUtils
                    .getRequiredWebApplicationContext(servletConfig.getServletContext());
        } catch (final IllegalStateException e) {
            throw new ServletException("could not locate containing WebApplicationContext");
        }
    }
}