summaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
Diffstat (limited to 'server')
-rw-r--r--server/src/com/vaadin/server/VaadinSession.java109
1 files changed, 109 insertions, 0 deletions
diff --git a/server/src/com/vaadin/server/VaadinSession.java b/server/src/com/vaadin/server/VaadinSession.java
index c6b2d91f29..a91c011ddf 100644
--- a/server/src/com/vaadin/server/VaadinSession.java
+++ b/server/src/com/vaadin/server/VaadinSession.java
@@ -204,6 +204,8 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable {
private transient WrappedSession session;
+ private final Map<String, Object> attributes = new HashMap<String, Object>();
+
/**
* @see javax.servlet.http.HttpSessionBindingListener#valueBound(HttpSessionBindingEvent)
*/
@@ -1306,4 +1308,111 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable {
return lock;
}
+ /**
+ * Stores a value in this vaadin session. This can be used to associate data
+ * with the current user so that it can be retrieved at a later point from
+ * some other part of the application. Setting the value to
+ * <code>null</code> clears the stored value.
+ *
+ * @see #getAttribute(String)
+ *
+ * @param name
+ * the name to associate the value with, can not be
+ * <code>null</code>
+ * @param value
+ * the value to associate with the name, or <code>null</code> to
+ * remove a previous association.
+ */
+ public void setAttribute(String name, Object value) {
+ if (name == null) {
+ throw new IllegalArgumentException("name can not be null");
+ }
+ if (value != null) {
+ attributes.put(name, value);
+ } else {
+ attributes.remove(name);
+ }
+ }
+
+ /**
+ * Stores a value in this vaadin session. This can be used to associate data
+ * with the current user so that it can be retrieved at a later point from
+ * some other part of the application. Setting the value to
+ * <code>null</code> clears the stored value.
+ * <p>
+ * The fully qualified name of the type is used as the name when storing the
+ * value. The outcome of calling this method is thus the same as if calling<br />
+ * <br />
+ * <code>setAttribute(type.getName(), value);</code>
+ *
+ * @see #getAttribute(Class)
+ * @see #setAttribute(String, Object)
+ *
+ * @param type
+ * the type that the stored value represents, can not be null
+ * @param value
+ * the value to associate with the type, or <code>null</code> to
+ * remove a previous association.
+ */
+ public <T> void setAttribute(Class<T> type, T value) {
+ if (type == null) {
+ throw new IllegalArgumentException("type can not be null");
+ }
+ if (value != null && !type.isInstance(value)) {
+ throw new IllegalArgumentException("value of type "
+ + type.getName() + " expected but got "
+ + value.getClass().getName());
+ }
+ setAttribute(type.getName(), value);
+ }
+
+ /**
+ * Gets a stored attribute value. If a value has been stored for the
+ * session, that value is returned. If no value is stored for the name,
+ * <code>null</code> is returned.
+ *
+ * @see #setAttribute(String, Object)
+ *
+ * @param name
+ * the name of the value to get, can not be <code>null</code>.
+ * @return the value, or <code>null</code> if no value has been stored or if
+ * it has been set to null.
+ */
+ public Object getAttribute(String name) {
+ if (name == null) {
+ throw new IllegalArgumentException("name can not be null");
+ }
+ return attributes.get(name);
+ }
+
+ /**
+ * Gets a stored attribute value. If a value has been stored for the
+ * session, that value is returned. If no value is stored for the name,
+ * <code>null</code> is returned.
+ * <p>
+ * The fully qualified name of the type is used as the name when getting the
+ * value. The outcome of calling this method is thus the same as if calling<br />
+ * <br />
+ * <code>getAttribute(type.getName());</code>
+ *
+ * @see #setAttribute(Class, Object)
+ * @see #getAttribute(String)
+ *
+ * @param type
+ * the type of the value to get, can not be <code>null</code>.
+ * @return the value, or <code>null</code> if no value has been stored or if
+ * it has been set to null.
+ */
+ public <T> T getAttribute(Class<T> type) {
+ if (type == null) {
+ throw new IllegalArgumentException("type can not be null");
+ }
+ Object value = getAttribute(type.getName());
+ if (value == null) {
+ return null;
+ } else {
+ return type.cast(value);
+ }
+ }
+
}