@Override public boolean remove(String resourceKey) throws UnsupportedOperationException { ObjectUtils.argumentNotNull(resourceKey, "Resource key must be not null"); final VaadinSession session = VaadinSession.getCurrent(); if (session == null) { throw new IllegalStateException("Current VaadinSession not available"); } Object exist = session.getAttribute(resourceKey); session.setAttribute(resourceKey, null); return exist != null; }
/** * Get the session registry for VaadinSession. If no SessionRegistry exists * then one will be created for given VaadinSession. * * @param session * vaadin session to get registry for * @return session registry for given session */ public static RouteRegistry getSessionRegistry(VaadinSession session) { Objects.requireNonNull(session, "Null session is not supported for session route registry"); SessionRouteRegistry registry = session .getAttribute(SessionRouteRegistry.class); if (registry == null) { registry = new SessionRouteRegistry(session, session.getService()); session.setAttribute(SessionRouteRegistry.class, registry); } if (!registry.session.equals(session)) { throw new IllegalStateException( "Session has as the attribute a route registered to another session"); } return registry; }
/** * Returns whether the given session is active or whether it can be closed. * <p> * A session is active if and only if its {@link VaadinSession#getState()} * returns {@link VaadinSessionState#OPEN} and * {@link #getUidlRequestTimeout(VaadinSession) getUidlRequestTimeout} is * negative or has not yet expired. * * @param session * The session whose status to check * @return true if the session is active, false if it could be closed. */ private boolean isSessionActive(VaadinSession session) { if (session.getState() != VaadinSessionState.OPEN || session.getSession() == null) { return false; } else { long now = System.currentTimeMillis(); int timeout = 1000 * getUidlRequestTimeout(session); return timeout < 0 || now - session.getLastRequestTimestamp() < timeout; } }
/** * Checks whether there might be some {@link VaadinSession} other than the * provided one for which the current thread holds a lock. This method might * not detect all cases where some other session is locked, but it should * cover the most typical situations. * * @param session * the session that is expected to be locked * @return <code>true</code> if another session is also locked by the * current thread; <code>false</code> if no such session was found */ public static boolean isOtherSessionLocked(VaadinSession session) { VaadinSession otherSession = VaadinSession.getCurrent(); if (otherSession == null || otherSession == session) { return false; } return otherSession.hasLock(); }
private void cleanStreamVariable(VaadinSession session, StreamReceiver streamReceiver) { session.lock(); try { session.getResourceRegistry().unregisterResource(streamReceiver); } finally { session.unlock(); } }
@Override protected BeanStore getBeanStore() { final VaadinSession session = getVaadinSession(); session.lock(); try { BeanStore beanStore = session.getAttribute(BeanStore.class); if (beanStore == null) { beanStore = new SessionBeanStore(session); session.setAttribute(BeanStore.class, beanStore); } return beanStore; } finally { session.unlock(); } }
@Override protected ContextualStorage getContextualStorage(Contextual<?> contextual, boolean createIfNotExist) { VaadinSession session = VaadinSession.getCurrent(); ContextualStorage storage = findContextualStorage(session); if (storage == null && createIfNotExist) { storage = new ContextualStorage(beanManager, false, true); session.setAttribute(ATTRIBUTE_NAME, storage); } return storage; }
getLogger() .warn("A VaadinSession instance not associated to any service is getting unbound. " + "Session destroy events will not be fired and UIs in the session will not get detached. " + "This might happen if a session is deserialized but never used before it expires."); } else if (VaadinService.getCurrentRequest() != null && getCurrent() == this) { checkHasLock(); if (getAttribute(VaadinService.PRESERVE_UNBOUND_SESSION_ATTRIBUTE) == Boolean.TRUE) { return; if (getState() == VaadinSessionState.OPEN) { close();
@Override public boolean isActive() { return VaadinSession.getCurrent() != null; }
/** * Get the {@link DeviceInfoRegistry} associated with current session, if available. * @return Optional session registry */ static Optional<DeviceInfoRegistry> getSessionRegistry() { VaadinSession session = VaadinSession.getCurrent(); if (session != null) { return Optional.ofNullable(session.getAttribute(DeviceInfoRegistry.class)); } return Optional.empty(); }
private void destroy() { session.lock(); try { session.setAttribute(UIStoreWrapper.class, null); uiStores.values().forEach(BeanStore::destroy); uiStores.clear(); } finally { session.unlock(); if (sessionDestroyListenerRegistration != null) { sessionDestroyListenerRegistration.remove(); } } }
/** * Called after the framework has handled a request and the response has * been written. * * @param request * The request object * @param response * The response object * @param session * The session which was used during the request or null if the * request did not use a session */ public void requestEnd(VaadinRequest request, VaadinResponse response, VaadinSession session) { if (session != null) { assert VaadinSession.getCurrent() == session; session.lock(); try { cleanupSession(session); final long duration = (System.nanoTime() - (Long) request .getAttribute(REQUEST_START_TIME_ATTRIBUTE)) / 1000000; session.setLastRequestDuration(duration); } finally { session.unlock(); } } CurrentInstance.clearAll(); }
@Override public void serviceInit(ServiceInitEvent event) { // Session event.getSource().addSessionInitListener(e -> { // DeviceInfo registry is available if (e.getSession().getAttribute(DeviceInfoRegistry.class) == null) { e.getSession().setAttribute(DeviceInfoRegistry.class, new DefaultDeviceInfoRegistry()); } }); // UI init event.getSource().addUIInitListener(e -> { if (e.getUI().getSession() != null) { // configure DeviceInfo final DeviceInfoRegistry registry = e.getUI().getSession().getAttribute(DeviceInfoRegistry.class); if (registry != null && !registry.hasDeviceInfo(e.getUI())) { final VaadinRequest request = VaadinRequest.getCurrent(); if (request != null) { registry.setDeviceInfo(e.getUI(), DeviceInfo.create(e.getUI(), request)); // log LOGGER.debug(() -> "A DeviceInfo was configured for UI with id [" + e.getUI().getUIId() + "]"); } } } }); }
/** * Gets current Vaadin session. * <p> * Throws {@link IllegalStateException} if there is no current Vaadin * session scope or it's not opened. * * @return the current Vaadin session */ protected VaadinSession getVaadinSession() { VaadinSession session = VaadinSession.getCurrent(); if (session == null) { throw new IllegalStateException( "No VaadinSession bound to current thread"); } if (session.getState() != VaadinSessionState.OPEN) { throw new IllegalStateException( "Current VaadinSession is not open"); } return session; } }
serviceSession.setAttribute(PRESERVE_UNBOUND_SESSION_ATTRIBUTE, Boolean.TRUE); VaadinService service = serviceSession.getService(); serviceSession.getLockInstance()); .setAttribute(PRESERVE_UNBOUND_SESSION_ATTRIBUTE, null);
private static ContextualStorage findContextualStorage(VaadinSession session) { // session lock is checked inside return (ContextualStorage) session.getAttribute(ATTRIBUTE_NAME); }
public void setLayoutToSession() { UI.getCurrent().getSession().setAttribute(SESSION_ATTRIBUTE_APP_LAYOUT, appLayout); }
private static String getSessionDetails(VaadinSession session) { if (session == null) { return null; } else { return session + " for " + session.getService().getServiceName(); } }