@Override public void init(PortletConfig config) throws PortletException { CurrentInstance.clearAll(); super.init(config); Properties initParameters = new Properties(); // Read default parameters from the context final PortletContext context = config.getPortletContext(); for (final Enumeration<String> e = context.getInitParameterNames(); e.hasMoreElements(); ) { final String name = e.nextElement(); initParameters.setProperty(name, context.getInitParameter(name)); } // Override with application settings from portlet.xml for (final Enumeration<String> e = config.getInitParameterNames(); e.hasMoreElements(); ) { final String name = e.nextElement(); initParameters.setProperty(name, config.getInitParameter(name)); } DeploymentConfiguration deploymentConfiguration = createDeploymentConfiguration(initParameters); try { vaadinService = createPortletService(deploymentConfiguration); } catch (ServiceException e) { throw new PortletException("Could not initialized VaadinPortlet", e); } // Sets current service even though there are no request and response vaadinService.setCurrentInstances(null, null); portletInitialized(); CurrentInstance.clearAll(); }
@Override public UI createInstance(UICreateEvent uiCreateEvent) { Class<? extends UI> type = uiCreateEvent.getUIClass(); int uiId = uiCreateEvent.getUiId(); VaadinRequest request = uiCreateEvent.getRequest(); Bean<?> bean = scanForBeans(type); String uiMapping = ""; if (bean == null) { if (type.isAnnotationPresent(CDIUI.class)) { uiMapping = parseUIMapping(request); bean = getUIBeanWithMapping(uiMapping); } else { throw new IllegalStateException( "UI class: " + type.getName() + " with mapping: " + uiMapping + " is not annotated with CDIUI!"); } } UIBean uiBean = new UIBean(bean, uiId); try { // Make the UIBean available to UIScopedContext when creating nested // injected objects CurrentInstance.set(UIBean.class, uiBean); return (UI) getBeanManager() .getReference(uiBean, type, getBeanManager().createCreationalContext(bean)); } finally { CurrentInstance.set(UIBean.class, null); } }
// Generic method that creates necessary VaadinSessin mock environment to be // able to test the BeanStoreRetrievalStrategy class. // To use it, call this method and write your test logic inside the // test method implemented from BeanStoreRetrievalStrategyTest interface. private synchronized void beanStoreTest( BeanStoreRetrievalStrategyTest test, boolean openVaadinSession) { WrappedSession wrappedSession = mock(WrappedSession.class); VaadinService vaadinService = mock(VaadinService.class); VaadinSession session = mock(VaadinSession.class); if (openVaadinSession) { when(session.getState()).thenReturn(VaadinSession.State.OPEN); } else { when(session.getState()).thenReturn(VaadinSession.State.CLOSED); } when(session.getSession()).thenReturn(wrappedSession); when(session.getService()).thenReturn(vaadinService); when(session.getSession().getId()).thenReturn(TEST_SESSION_ID); UIID uiid = new UIID(TEST_UIID); BeanStore beanStore = new BeanStore(TEST_BEAN_NAME); UIStore uiStore = mock(UIStore.class); when(session.getAttribute(UIStore.class)).thenReturn(uiStore); when(uiStore.getBeanStore(uiid)).thenReturn(beanStore); try { CurrentInstance.set(VaadinSession.class, session); CurrentInstance.set(UIID.class, uiid); test.test(session, uiStore, uiid); } finally { CurrentInstance.clearAll(); } }
@Override public UI createInstance(UICreateEvent event) { final Class<VaadinUIIdentifier> key = VaadinUIIdentifier.class; final VaadinUIIdentifier identifier = new VaadinUIIdentifier(event); CurrentInstance.set(key, identifier); try { logger.debug( "Creating a new UI bean of class [{}] with identifier [{}]", event.getUIClass().getCanonicalName(), identifier); return webApplicationContext.getBean(event.getUIClass()); } finally { CurrentInstance.set(key, null); } }
/** * Gets the currently used Vaadin portlet. The current portlet is automatically defined when * initializing the portlet and when processing requests to the server and in threads started at a * point when the current portlet is defined (see {@link InheritableThreadLocal}). In other cases, * (e.g. from background threads started in some other way), the current portlet is not * automatically defined. * * <p>The current portlet is derived from the current service using {@link * VaadinService#getCurrent()} * * @return the current vaadin portlet instance if available, otherwise <code>null</code> * @since 7.0 */ public static VaadinPortlet getCurrent() { VaadinService vaadinService = CurrentInstance.get(VaadinService.class); if (vaadinService instanceof VaadinPortletService) { VaadinPortletService vps = (VaadinPortletService) vaadinService; return vps.getPortlet(); } else { return null; } }
/** * @param request * @param response * @throws PortletException * @throws IOException * @deprecated As of 7.0. Will likely change or be removed in a future version */ @Deprecated protected void handleRequest(PortletRequest request, PortletResponse response) throws PortletException, IOException { CurrentInstance.clearAll(); try { getService().handleRequest(createVaadinRequest(request), createVaadinResponse(response)); } catch (ServiceException e) { throw new PortletException(e); } }
@Test public void threadLocalsAfterSessionDestroy() { final AtomicBoolean detachCalled = new AtomicBoolean(false); ui.addDetachListener( new DetachListener() { @Override public void detach(DetachEvent event) { detachCalled.set(true); Assert.assertEquals(ui, UI.getCurrent()); Assert.assertEquals(ui.getPage(), Page.getCurrent()); Assert.assertEquals(session, VaadinSession.getCurrent()); Assert.assertEquals(mockService, VaadinService.getCurrent()); Assert.assertEquals(mockServlet, VaadinServlet.getCurrent()); } }); CurrentInstance.clearAll(); session.close(); mockService.cleanupSession(session); Assert.assertTrue(detachCalled.get()); }
/** * Gets the currently used UI. The current UI is automatically defined when processing requests to * the server. In other cases, (e.g. from background threads), the current UI is not automatically * defined. * * @return the current UI instance if available, otherwise <code>null</code> * @see #setCurrent(UI) */ public static UI getCurrent() { return CurrentInstance.get(UI.class); }
/** * Sets the thread local for the current UI. This method is used by the framework to set the * current application whenever a new request is processed and it is cleared when the request has * been processed. * * <p>The application developer can also use this method to define the current UI outside the * normal request handling, e.g. when initiating custom background threads. * * @param uI the UI to register as the current UI * @see #getCurrent() * @see ThreadLocal */ public static void setCurrent(UI ui) { CurrentInstance.setInheritable(UI.class, ui); }