@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();
  }
  // 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();
    }
  }
  /**
   * @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());
 }