/** * Creates a new VaadinSession tied to a VaadinService. * * @param service * the Vaadin service for the new session */ public VaadinSession(VaadinService service) { this.service = service; resourceRegistry = new StreamResourceRegistry(this); }
@Override public AbstractStreamResource getResource() { Optional<AbstractStreamResource> resource = registry .getResource(getResourceUri()); return resource.isPresent() ? resource.get() : null; } }
/** * Unregister a stream receiver resource. * * @param resource * stream receiver resource to unregister */ public void unregisterResource(StreamReceiver resource) { res.remove(getURI(resource)); }
private void cleanStreamVariable(VaadinSession session, StreamReceiver streamReceiver) { session.lock(); try { session.getResourceRegistry().unregisterResource(streamReceiver); } finally { session.unlock(); } }
private void registerResource(String attribute, AbstractStreamResource resource) { ensureResourceRegistrations(); ensurePendingRegistrations(); assert !resourceRegistrations.containsKey(attribute); StreamRegistration registration = getSession().getResourceRegistry() .registerResource(resource); resourceRegistrations.put(attribute, registration); Registration handle = pendingRegistrations.remove(attribute); if (handle != null) { handle.remove(); } pendingRegistrations.put(attribute, getNode().addDetachListener( // This explicit class instantiation is the workaround // which fixes a JVM optimization+serialization bug. // Do not convert to lambda // Detected under Win7_64 /JDK 1.8.0_152, 1.8.0_172 // see ElementAttributeMap#deferRegistration new Command() { @Override public void execute() { ElementAttributeMap.this.unsetResource(attribute); } })); }
/** * Gets the URI for the given {@code resource}. * <p> * The URI won't be handled (and won't work) if {@code resource} is not * registered in the session. * * @see #registerResource(AbstractStreamResource) * * @param resource * stream resource * @return resource URI */ public static URI getURI(AbstractStreamResource resource) { return getURI(resource.getName(), resource.getId()); }
/** * Sets the given attribute to the given {@link StreamResource} value. * * @param attribute * the attribute name * @param resource * the value */ public void setResource(String attribute, AbstractStreamResource resource) { set(attribute, StreamResourceRegistry.getURI(resource).toASCIIString()); if (getNode().isAttached()) { registerResource(attribute, resource); } else { deferRegistration(attribute, resource); } }