You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltaspike.apache.org by st...@apache.org on 2013/04/18 22:36:47 UTC
git commit: DELTASPIKE-289 WindowContext cleanup
Updated Branches:
refs/heads/master 32b5ef9df -> ac93bc54a
DELTASPIKE-289 WindowContext cleanup
Project: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/commit/ac93bc54
Tree: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/tree/ac93bc54
Diff: http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/diff/ac93bc54
Branch: refs/heads/master
Commit: ac93bc54a2c1a7b17d3a0b57fc9a24ec9d334c78
Parents: 32b5ef9
Author: Mark Struberg <st...@apache.org>
Authored: Thu Apr 18 22:36:14 2013 +0200
Committer: Mark Struberg <st...@apache.org>
Committed: Thu Apr 18 22:36:14 2013 +0200
----------------------------------------------------------------------
.../core/spi/scope/window/WindowContext.java | 18 ++----
.../core/util/context/AbstractContext.java | 6 +-
.../core/impl/scope/window/WindowBeanHolder.java | 17 +++++
.../core/impl/scope/window/WindowContextImpl.java | 52 +++++++--------
.../scope/window/DefaultWindowContextTest.java | 8 +-
5 files changed, 54 insertions(+), 47 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/ac93bc54/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/scope/window/WindowContext.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/scope/window/WindowContext.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/scope/window/WindowContext.java
index fbd585c..14663c5 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/scope/window/WindowContext.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/scope/window/WindowContext.java
@@ -30,7 +30,8 @@ package org.apache.deltaspike.core.spi.scope.window;
* session as @SessionScoped bean.
* </p>
* <p>Every WindowContext is uniquely identified via a
- * 'windowId'. Each Thread is associated with at most
+ * 'windowId' inside the current Session.
+ * Each Thread is associated with at most
* one single windowId at a time. The {@link WindowContext}
* is the interface which allows resolving the current <i>windowId</i>
* associated with this very Thread.</p>
@@ -47,21 +48,12 @@ public interface WindowContext
* If no WindowContext exists with the very windowId we will create a new one.
* @param windowId
*/
- void activateWindowContext(String windowId);
+ void activateWindow(String windowId);
/**
- * close the WindowContext with the currently activated windowId for the very Thread.
+ * close the WindowContext with the given windowId.
* @return <code>true</code> if any did exist, <code>false</code> otherwise
*/
- boolean closeCurrentWindowContext();
-
-
- /**
- * Close all WindowContexts which are managed by the WindowContextManager.
- * This is necessary when the session gets closed down or the application closes.
- * @return
- */
- void destroy();
-
+ boolean closeWindow(String windowId);
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/ac93bc54/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/context/AbstractContext.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/context/AbstractContext.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/context/AbstractContext.java
index 583faf8..e30931c 100644
--- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/context/AbstractContext.java
+++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/util/context/AbstractContext.java
@@ -154,9 +154,11 @@ public abstract class AbstractContext implements Context
}
/**
- * destroys all the Contextual Instances in the specified ContextualStorage.
+ * Destroys all the Contextual Instances in the specified ContextualStorage.
+ * This is a static method to allow various holder objects to cleanup
+ * properly in @PreDestroy.
*/
- public void destroyAllActive(ContextualStorage storage)
+ public static void destroyAllActive(ContextualStorage storage)
{
Map<Object, ContextualInstanceInfo<?>> contextMap = storage.getStorage();
for (Map.Entry<Object, ContextualInstanceInfo<?>> entry : contextMap.entrySet())
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/ac93bc54/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/scope/window/WindowBeanHolder.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/scope/window/WindowBeanHolder.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/scope/window/WindowBeanHolder.java
index d51342f..8acd0e6 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/scope/window/WindowBeanHolder.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/scope/window/WindowBeanHolder.java
@@ -18,12 +18,14 @@
*/
package org.apache.deltaspike.core.impl.scope.window;
+import javax.annotation.PreDestroy;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.spi.BeanManager;
import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
+import org.apache.deltaspike.core.util.context.AbstractContext;
import org.apache.deltaspike.core.util.context.ContextualStorage;
/**
@@ -41,6 +43,7 @@ public class WindowBeanHolder implements Serializable
*/
private volatile Map<String, ContextualStorage> storageMap = new ConcurrentHashMap<String, ContextualStorage>();
+ //X TODO review usage
public Map<String, ContextualStorage> getStorageMap()
{
return storageMap;
@@ -86,4 +89,18 @@ public class WindowBeanHolder implements Serializable
storageMap = new ConcurrentHashMap<String, ContextualStorage>();
return oldStorageMap;
}
+
+ @PreDestroy
+ public void destroyBeans()
+ {
+ // we replace the old windowBeanHolder beans with a new storage Map
+ // an afterwards destroy the old Beans without having to care about any syncs.
+ Map<String, ContextualStorage> oldWindowContextStorages = forceNewStorage();
+
+ for (ContextualStorage contextualStorage : oldWindowContextStorages.values())
+ {
+ AbstractContext.destroyAllActive(contextualStorage);
+ }
+
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/ac93bc54/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/scope/window/WindowContextImpl.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/scope/window/WindowContextImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/scope/window/WindowContextImpl.java
index f20d02f..e9aa326 100644
--- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/scope/window/WindowContextImpl.java
+++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/scope/window/WindowContextImpl.java
@@ -23,8 +23,6 @@ import javax.enterprise.inject.Typed;
import javax.enterprise.inject.spi.BeanManager;
import java.lang.annotation.Annotation;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
import org.apache.deltaspike.core.api.scope.WindowScoped;
import org.apache.deltaspike.core.spi.scope.window.WindowContext;
@@ -38,13 +36,18 @@ import org.apache.deltaspike.core.util.context.ContextualStorage;
public class WindowContextImpl extends AbstractContext implements WindowContext
{
/**
- * all the {@link WindowContext}s which are active in this very Session.
+ * Holds the currently active windowId of each Request
*/
- private Map<String, WindowContext> windowContexts = new ConcurrentHashMap<String, WindowContext>();
-
private WindowIdHolder windowIdHolder;
+
+ /**
+ * Contains the stored WindowScoped contextual instances.
+ */
private WindowBeanHolder windowBeanHolder;
+ /**
+ * needed for serialisation and passivationId
+ */
private BeanManager beanManager;
@@ -55,8 +58,20 @@ public class WindowContextImpl extends AbstractContext implements WindowContext
this.beanManager = beanManager;
}
+ /**
+ * We need to pass the session scoped windowbean holder and the
+ * requestscoped windowIdHolder in a later phase because
+ * getBeans is only allowed from AfterDeploymentValidation onwards.
+ */
+ void initWindowContext(WindowBeanHolder windowBeanHolder, WindowIdHolder windowIdHolder)
+ {
+ this.windowBeanHolder = windowBeanHolder;
+ this.windowIdHolder = windowIdHolder;
+ }
+
+
@Override
- public void activateWindowContext(String windowId)
+ public void activateWindow(String windowId)
{
windowIdHolder.setWindowId(windowId);
}
@@ -68,16 +83,15 @@ public class WindowContextImpl extends AbstractContext implements WindowContext
}
@Override
- public boolean closeCurrentWindowContext()
+ public boolean closeWindow(String windowId)
{
- String windowId = windowIdHolder.getWindowId();
if (windowId == null)
{
return false;
}
- WindowContext windowContext = windowContexts.get(windowId);
- if (windowContext == null)
+ ContextualStorage windowStorage = windowBeanHolder.getContextualStorage(beanManager, windowId);
+ if (windowStorage == null)
{
return false;
}
@@ -86,19 +100,6 @@ public class WindowContextImpl extends AbstractContext implements WindowContext
}
@Override
- public synchronized void destroy()
- {
- // we replace the old windowBeanHolder beans with a new storage Map
- // an afterwards destroy the old Beans without having to care about any syncs.
- Map<String, ContextualStorage> oldWindowContextStorages = windowBeanHolder.forceNewStorage();
-
- for (ContextualStorage contextualStorage : oldWindowContextStorages.values())
- {
- destroyAllActive(contextualStorage);
- }
- }
-
- @Override
protected ContextualStorage getContextualStorage(boolean createIfNotExist)
{
String windowId = getCurrentWindowId();
@@ -127,9 +128,4 @@ public class WindowContextImpl extends AbstractContext implements WindowContext
return windowId != null;
}
- void initWindowContext(WindowBeanHolder windowBeanHolder, WindowIdHolder windowIdHolder)
- {
- this.windowBeanHolder = windowBeanHolder;
- this.windowIdHolder = windowIdHolder;
- }
}
http://git-wip-us.apache.org/repos/asf/incubator-deltaspike/blob/ac93bc54/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/scope/window/DefaultWindowContextTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/scope/window/DefaultWindowContextTest.java b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/scope/window/DefaultWindowContextTest.java
index 68408fd..39640b8 100644
--- a/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/scope/window/DefaultWindowContextTest.java
+++ b/deltaspike/core/impl/src/test/java/org/apache/deltaspike/test/core/impl/scope/window/DefaultWindowContextTest.java
@@ -68,14 +68,14 @@ public class DefaultWindowContextTest
Assert.assertNotNull(someWindowScopedBean);
{
- windowContext.activateWindowContext("window1");
+ windowContext.activateWindow("window1");
someWindowScopedBean.setValue("Hans");
Assert.assertEquals("Hans", someWindowScopedBean.getValue());
}
// now we switch it away to another 'window'
{
- windowContext.activateWindowContext("window2");
+ windowContext.activateWindow("window2");
Assert.assertNull(someWindowScopedBean.getValue());
someWindowScopedBean.setValue("Karl");
Assert.assertEquals("Karl", someWindowScopedBean.getValue());
@@ -83,7 +83,7 @@ public class DefaultWindowContextTest
// and now back to the first window
{
- windowContext.activateWindowContext("window1");
+ windowContext.activateWindow("window1");
// which must still contain the old value
Assert.assertEquals("Hans", someWindowScopedBean.getValue());
@@ -91,7 +91,7 @@ public class DefaultWindowContextTest
// and again back to the second window
{
- windowContext.activateWindowContext("window2");
+ windowContext.activateWindow("window2");
// which must still contain the old value of the 2nd window
Assert.assertEquals("Karl", someWindowScopedBean.getValue());