* @since 7.0.0
*/
public class CurrentInstance implements Serializable {
+ private static final Object NULL_OBJECT = new Object();
+
private final WeakReference<Object> instance;
private final boolean inheritable;
Object instance = entry.getValue().instance.get();
if (instance == null) {
iterator.remove();
- getLogger().log(Level.WARNING,
+ getLogger().log(Level.FINE,
"CurrentInstance for {0} has been garbage collected.",
entry.getKey());
}
Object v = ci.instance.get();
if (v == null) {
removeStale = true;
+ } else if (v == NULL_OBJECT) {
+ set(c, null, ci.inheritable);
} else {
set(c, v, ci.inheritable);
}
boolean removeStale = false;
for (Class<?> c : map.keySet()) {
CurrentInstance ci = map.get(c);
- if (ci.instance.isEnqueued()) {
+ if (ci.instance.get() == null) {
removeStale = true;
} else if (ci.inheritable || !onlyInheritable) {
copy.put(c, ci);
*/
public static Map<Class<?>, CurrentInstance> setCurrent(UI ui) {
Map<Class<?>, CurrentInstance> old = new HashMap<Class<?>, CurrentInstance>();
- old.put(UI.class, new CurrentInstance(UI.getCurrent(), true));
+ old.put(UI.class,
+ new CurrentInstance(getSameOrNullObject(UI.getCurrent()), true));
UI.setCurrent(ui);
old.putAll(setCurrent(ui.getSession()));
return old;
public static Map<Class<?>, CurrentInstance> setCurrent(
VaadinSession session) {
Map<Class<?>, CurrentInstance> old = new HashMap<Class<?>, CurrentInstance>();
- old.put(VaadinSession.class,
- new CurrentInstance(VaadinSession.getCurrent(), true));
- old.put(VaadinService.class,
- new CurrentInstance(VaadinService.getCurrent(), true));
+ old.put(VaadinSession.class, new CurrentInstance(
+ getSameOrNullObject(VaadinSession.getCurrent()), true));
+ old.put(VaadinService.class, new CurrentInstance(
+ getSameOrNullObject(VaadinService.getCurrent()), true));
VaadinService service = null;
if (session != null) {
service = session.getService();
return old;
}
+ /**
+ * Returns {@code object} unless it is null, in which case #NULL_OBJECT is
+ * returned.
+ *
+ * @param object
+ * The instance to return if non-null.
+ * @return {@code object} or #NULL_OBJECT if {@code object} is null.
+ */
+ private static Object getSameOrNullObject(Object object) {
+ return object == null ? NULL_OBJECT : object;
+ }
+
private static Logger getLogger() {
return Logger.getLogger(CurrentInstance.class.getName());
}
*/
package com.vaadin.util;
+import static org.junit.Assert.assertNull;
+
import java.lang.reflect.Field;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
-import junit.framework.Assert;
-
+import org.easymock.EasyMock;
+import org.junit.Assert;
import org.junit.Test;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.server.VaadinService;
+import com.vaadin.server.VaadinSession;
+import com.vaadin.ui.UI;
+
public class TestCurrentInstance {
@Test
public void testInheritedClearedAfterRemove() {
}
+
+ private static class UIStoredInCurrentInstance extends UI {
+ @Override
+ protected void init(VaadinRequest request) {
+ }
+ }
+
+ private static class SessionStoredInCurrentInstance extends VaadinSession {
+ public SessionStoredInCurrentInstance(VaadinService service) {
+ super(service);
+ }
+ }
+
+ @Test
+ public void testRestoringNullUIWorks() throws Exception {
+ // First make sure current instance is empty
+ CurrentInstance.clearAll();
+
+ // Then store a new UI in there
+ Map<Class<?>, CurrentInstance> old = CurrentInstance
+ .setCurrent(new UIStoredInCurrentInstance());
+
+ // Restore the old values and assert that the UI is null again
+ CurrentInstance.restoreInstances(old);
+ assertNull(CurrentInstance.get(UI.class));
+ }
+
+ @Test
+ public void testRestoringNullSessionWorks() throws Exception {
+ // First make sure current instance is empty
+ CurrentInstance.clearAll();
+
+ // Then store a new session in there
+ Map<Class<?>, CurrentInstance> old = CurrentInstance
+ .setCurrent(new SessionStoredInCurrentInstance(EasyMock
+ .createNiceMock(VaadinService.class)));
+
+ // Restore the old values and assert that the session is null again
+ CurrentInstance.restoreInstances(old);
+ assertNull(CurrentInstance.get(VaadinSession.class));
+ assertNull(CurrentInstance.get(VaadinService.class));
+ }
}