cleanStreamVariable(owner, variableName);
}
} catch (Exception e) {
- session.getLock().lock();
+ session.lock();
try {
handleChangeVariablesError(session, (Component) owner, e,
new HashMap<String, Object>());
} finally {
- session.getLock().unlock();
+ session.unlock();
}
}
sendUploadResponse(request, response);
cleanStreamVariable(owner, variableName);
}
} catch (Exception e) {
- session.getLock().lock();
+ session.lock();
try {
handleChangeVariablesError(session, (Component) owner, e,
new HashMap<String, Object>());
} finally {
- session.getLock().unlock();
+ session.unlock();
}
}
sendUploadResponse(request, response);
filename, type, contentLength);
try {
boolean listenProgress;
- session.getLock().lock();
+ session.lock();
try {
streamVariable.streamingStarted(startedEvent);
out = streamVariable.getOutputStream();
listenProgress = streamVariable.listenProgress();
} finally {
- session.getLock().unlock();
+ session.unlock();
}
// Gets the output target stream
if (listenProgress) {
// update progress if listener set and contentLength
// received
- session.getLock().lock();
+ session.lock();
try {
StreamingProgressEventImpl progressEvent = new StreamingProgressEventImpl(
filename, type, contentLength, totalBytes);
streamVariable.onProgress(progressEvent);
} finally {
- session.getLock().unlock();
+ session.unlock();
}
}
if (streamVariable.isInterrupted()) {
out.close();
StreamingEndEvent event = new StreamingEndEventImpl(filename, type,
totalBytes);
- session.getLock().lock();
+ session.lock();
try {
streamVariable.streamingFinished(event);
} finally {
- session.getLock().unlock();
+ session.unlock();
}
} catch (UploadInterruptedException e) {
tryToCloseStream(out);
StreamingErrorEvent event = new StreamingErrorEventImpl(filename,
type, contentLength, totalBytes, e);
- session.getLock().lock();
+ session.lock();
try {
streamVariable.streamingFailed(event);
} finally {
- session.getLock().unlock();
+ session.unlock();
}
// Note, we are not throwing interrupted exception forward as it is
// not a terminal level error like all other exception.
} catch (final Exception e) {
tryToCloseStream(out);
- session.getLock().lock();
+ session.lock();
try {
StreamingErrorEvent event = new StreamingErrorEventImpl(
filename, type, contentLength, totalBytes, e);
// terminalErrorHandler)
throw new UploadException(e);
} finally {
- session.getLock().unlock();
+ session.unlock();
}
}
return startedEvent.isDisposed();
// The rest of the process is synchronized with the session
// in order to guarantee that no parallel variable handling is
// made
- session.getLock().lock();
+ session.lock();
try {
// Verify that there's an UI
outWriter, uI, analyzeLayouts);
postPaint(uI);
} finally {
- session.getLock().unlock();
+ session.unlock();
}
outWriter.close();
public void handleBrowserDetailsRequest(VaadinRequest request,
VaadinResponse response, VaadinSession session) throws IOException {
- session.getLock().lock();
+ session.lock();
try {
assert UI.getCurrent() == null;
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
- session.getLock().unlock();
+ session.unlock();
}
}
}
/**
- * Gets the lock that should be used to synchronize usage of data inside
- * this session.
+ * Gets the {@link Lock} instance that is used for protecting the data of
+ * this session from concurrent access.
+ * <p>
+ * The <code>Lock</code> can be used to gain more control than what is
+ * available only using {@link #lock()} and {@link #unlock()}. The returned
+ * instance is not guaranteed to support any other features of the
+ * <code>Lock</code> interface than {@link Lock#lock()} and
+ * {@link Lock#unlock()}.
+ *
+ * @return the <code>Lock</code> that is used for synchronization, never
+ * <code>null</code>
*
- * @return the lock that should be used for synchronization
+ * @see #lock()
+ * @see Lock
*/
- public Lock getLock() {
+ public Lock getLockInstance() {
return lock;
}
+ /**
+ * Locks this session to protect its data from concurrent access. Accessing
+ * the UI state from outside the normal request handling should always lock
+ * the session and unlock it when done. To ensure that the lock is always
+ * released, you should typically wrap the code in a <code>try</code> block
+ * and unlock the session in <code>finally</code>:
+ *
+ * <pre>
+ * session.lock();
+ * try {
+ * doSomething();
+ * } finally {
+ * session.unlock();
+ * }
+ * </pre>
+ * <p>
+ * This method will block until the lock can be retrieved.
+ * <p>
+ * {@link #getLockInstance()} can be used if more control over the locking
+ * is required.
+ *
+ * @see #unlock()
+ * @see #getLockInstance()
+ */
+ public void lock() {
+ getLockInstance().lock();
+ }
+
+ /**
+ * Unlocks this session. This method should always be used in a finally
+ * block after {@link #lock()} to ensure that the lock is always released.
+ *
+ * @see #unlock()
+ */
+ public void unlock() {
+ getLockInstance().unlock();
+ }
+
/**
* Stores a value in this service session. This can be used to associate
* data with the current user so that it can be retrieved at a later point