Java tutorial
// 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; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.google.common.base.Throwables; import com.vaadin.Application; import com.vaadin.terminal.ExternalResource; import com.vaadin.terminal.Resource; import com.vaadin.terminal.gwt.server.WebApplicationContext; import com.vaadin.ui.Component; public final class VaadinUtil { private static final ThreadLocal<Application> threadLocalApp = new ThreadLocal<Application>(); /** * same as {@link VaadinUtil#closeApplication(Application)} but tries to retrieve * application from ThreadLocal variable, that can be set up by * {@link VaadinUtil#setApplication()} */ public static void closeApplication() { final Application app = getApplicationInstance(); final HttpSession session = getHttpSession(app); app.close(); session.invalidate(); } /** * closes the application and in addition invalidates the HttpSession, * which is crucial for using session scoped spring beans * in conjunction with vaadin applications */ public static void closeApplication(Application application) { final Application app = getApplicationInstance(); final HttpSession session = getHttpSession(app); app.close(); session.invalidate(); } @SuppressWarnings("unchecked") public static <T extends Application> T getApplicationInstance() { final Application app = threadLocalApp.get(); assert app != null; return (T) app; } /** * can only be used if the Vaadin Application has been set with {@link VaadinUtil#setApplication()} * during a still running request * this does not work outside of the request thread * */ public static HttpSession getHttpSession() { return getHttpSession(getApplicationInstance()); } public static HttpSession getHttpSession(Application app) { return getWebApplicationContext(app).getHttpSession(); } /** * can only be used if the Vaadin Application has been set with {@link VaadinUtil#setApplication()} * during a still running request * this does not work outside of the request thread * */ public static ServletContext getServletContext() { return getHttpSession(getApplicationInstance()).getServletContext(); } public static ServletContext getServletContext(Application app) { return getHttpSession(app).getServletContext(); } /** * can only be used if the Vaadin Application has been set with {@link VaadinUtil#setApplication()} * during a still running request * this does not work outside of the request thread * */ public static ApplicationContext getSpringApplicationContext() { return WebApplicationContextUtils .getRequiredWebApplicationContext(getServletContext(getApplicationInstance())); } public static ApplicationContext getSpringApplicationContext(Application app) { return WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext(app)); } @SuppressWarnings("unchecked") public static <BT> BT getSpringBean(Class<?> beanClass) { try { return (BT) getSpringApplicationContext().getBean(beanClass); } catch (final BeansException e) { throw Throwables.propagate(e); } } @SuppressWarnings("unchecked") public static <BT> BT getSpringBean(String springBeanRef) { try { return (BT) getSpringApplicationContext().getBean(springBeanRef); } catch (final BeansException e) { throw Throwables.propagate(e); } } /** * same as {@link VaadinUtil#isProductionMode(Application)} but tries to retrieve application from ThreadLocal variable, that can be set up by * {@link VaadinUtil#setApplication()} * @param application * @return true if the application is currently running in Vaadin Production Mode */ public static boolean isProductionMode() { return Boolean.parseBoolean(getServletContext(getApplicationInstance()).getInitParameter("productionMode")); } /** * checks whether the application is currently running in Vaadin Production Mode, * this should be the case if the ServletContext has an init Parameter "productionMode" set to "true" * @param application * @return */ public static boolean isProductionMode(Application application) { return Boolean.parseBoolean(getServletContext(application).getInitParameter("productionMode")); } public static WebApplicationContext getWebApplicationContext() { return (WebApplicationContext) getApplicationInstance().getContext(); } public static WebApplicationContext getWebApplicationContext(Application app) { return (WebApplicationContext) app.getContext(); } public static String getWidgetsetName() { return getApplicationInstance().getProperty("widgetset"); } public static String getWidgetsetPath() { // XXX test if this is still working, is this generic enough for different setups? return getServletContext().getContextPath() + "/VAADIN/widgetsets/" + getWidgetsetName(); } public static Resource getWidgetsetResource(String path) { return new ExternalResource(VaadinUtil.getWidgetsetPath() + path); } /** * convenience method that stores the passed application reference inside a static ThreadLocal reference * best used in conjunction with Vaadin Application implementing {@link com.vaadin.terminal.gwt.server.HttpServletRequestListener} * @see {@link https://vaadin.com/wiki/-/wiki/Main/ThreadLocal%20Pattern} * @param app */ public static void setApplication(Application app) { threadLocalApp.set(app); } /** * checks whether the passed in component is attached to an application, this is true exactly if * <li>the application is initialized * <li>HttpSession is set up * <li>the passed in component is an ancestor of the mainWindow of the application * * this implies that * @param c * @return */ public static boolean isAttached(Component c) { return c.getApplication() != null; } }