You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by pa...@apache.org on 2013/11/26 09:09:35 UTC
[4/4] git commit: Fixed auto conversations
Fixed auto conversations
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/5133c758
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/5133c758
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/5133c758
Branch: refs/heads/wicket-6.x
Commit: 5133c7587143d53d3228ddb77f28f21d64810dfe
Parents: 6189a89
Author: Emond Papegaaij <em...@topicus.nl>
Authored: Tue Nov 26 07:50:37 2013 +0100
Committer: Emond Papegaaij <em...@topicus.nl>
Committed: Tue Nov 26 09:09:16 2013 +0100
----------------------------------------------------------------------
.../wicket/cdi/AutoConversationManager.java | 120 +++++++++++++++++++
.../org/apache/wicket/cdi/CdiConfiguration.java | 6 +-
.../wicket/cdi/ConversationPropagator.java | 81 +------------
3 files changed, 128 insertions(+), 79 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/5133c758/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/AutoConversationManager.java
----------------------------------------------------------------------
diff --git a/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/AutoConversationManager.java b/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/AutoConversationManager.java
new file mode 100644
index 0000000..5b88365
--- /dev/null
+++ b/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/AutoConversationManager.java
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.cdi;
+
+import javax.enterprise.context.Conversation;
+import javax.inject.Inject;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.Page;
+import org.apache.wicket.application.IComponentOnBeforeRenderListener;
+import org.apache.wicket.request.IRequestHandler;
+import org.apache.wicket.request.cycle.RequestCycle;
+import org.apache.wicket.util.visit.IVisit;
+import org.apache.wicket.util.visit.IVisitor;
+import org.apache.wicket.util.visit.Visits;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Automatically starts and ends conversations for pages with a
+ * {@link ConversationalComponent}.
+ *
+ * @author papegaaij
+ */
+public class AutoConversationManager implements IComponentOnBeforeRenderListener
+{
+ private static final Logger logger = LoggerFactory.getLogger(AutoConversationManager.class);
+
+ @Inject
+ private Conversation conversation;
+
+ @Inject
+ private AutoConversation autoConversation;
+
+ private IConversationPropagation propagation;
+
+ public AutoConversationManager(IConversationPropagation propagation)
+ {
+ NonContextual.of(AutoConversationManager.class).inject(this);
+ this.propagation = propagation;
+ }
+
+ @Override
+ public void onBeforeRender(Component component)
+ {
+ if (component instanceof Page)
+ {
+ Page page = (Page)component;
+ autoEndIfNecessary(page, RequestCycle.get().getActiveRequestHandler());
+ autoBeginIfNecessary(page, RequestCycle.get().getActiveRequestHandler());
+ }
+ }
+
+ protected void autoBeginIfNecessary(Page page, IRequestHandler handler)
+ {
+ if (conversation == null || !conversation.isTransient() || page == null
+ || !hasConversationalComponent(page) || !propagation.propagatesVia(handler, page))
+ {
+ return;
+ }
+
+ // auto activate conversation
+
+ conversation.begin();
+ autoConversation.setAutomatic(true);
+
+ logger.debug("Auto-began conversation {} for page {}", conversation.getId(), page);
+ }
+
+ protected void autoEndIfNecessary(Page page, IRequestHandler handler)
+ {
+ if (conversation == null || conversation.isTransient() || page == null
+ || hasConversationalComponent(page) || !propagation.propagatesVia(handler, page)
+ || autoConversation.isAutomatic() == false)
+ {
+ return;
+ }
+
+ // auto de-activate conversation
+
+ String cid = conversation.getId();
+
+ autoConversation.setAutomatic(false);
+ conversation.end();
+ ConversationPropagator.removeConversationIdFromPage(page);
+
+ logger.debug("Auto-ended conversation {} for page {}", cid, page);
+ }
+
+ protected boolean hasConversationalComponent(Page page)
+ {
+ Boolean hasConversational = Visits.visit(page, new IVisitor<Component, Boolean>()
+ {
+ @Override
+ public void component(Component object, IVisit<Boolean> visit)
+ {
+ if (object instanceof ConversationalComponent)
+ {
+ visit.stop(true);
+ }
+ }
+ });
+
+ return hasConversational == null ? false : hasConversational;
+ }
+}
http://git-wip-us.apache.org/repos/asf/wicket/blob/5133c758/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/CdiConfiguration.java
----------------------------------------------------------------------
diff --git a/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/CdiConfiguration.java b/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/CdiConfiguration.java
index 93675f7..37bfa33 100644
--- a/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/CdiConfiguration.java
+++ b/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/CdiConfiguration.java
@@ -33,7 +33,7 @@ public class CdiConfiguration
private static final long serialVersionUID = 1L;
};
- private IConversationPropagation propagation = ConversationPropagation.ALL;
+ private IConversationPropagation propagation = ConversationPropagation.NONBOOKMARKABLE;
/**
* Constructor
@@ -47,14 +47,12 @@ public class CdiConfiguration
return propagation;
}
-
public CdiConfiguration setPropagation(IConversationPropagation propagation)
{
this.propagation = propagation;
return this;
}
-
/**
* Configures the specified application
*
@@ -77,6 +75,8 @@ public class CdiConfiguration
{
listeners.add(new ConversationPropagator(application, getPropagation()));
application.getComponentPreOnBeforeRenderListeners().add(
+ new AutoConversationManager(getPropagation()));
+ application.getComponentPreOnBeforeRenderListeners().add(
new ConversationExpiryChecker());
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/5133c758/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/ConversationPropagator.java
----------------------------------------------------------------------
diff --git a/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/ConversationPropagator.java b/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/ConversationPropagator.java
index 9af8156..9bd0597 100644
--- a/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/ConversationPropagator.java
+++ b/wicket-experimental/wicket-cdi-1.1/src/main/java/org/apache/wicket/cdi/ConversationPropagator.java
@@ -21,7 +21,6 @@ import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import org.apache.wicket.Application;
-import org.apache.wicket.Component;
import org.apache.wicket.MetaDataKey;
import org.apache.wicket.Page;
import org.apache.wicket.core.request.handler.BufferedResponseRequestHandler;
@@ -37,9 +36,6 @@ import org.apache.wicket.request.handler.resource.ResourceReferenceRequestHandle
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.PackageResourceReference;
import org.apache.wicket.util.lang.Args;
-import org.apache.wicket.util.visit.IVisit;
-import org.apache.wicket.util.visit.IVisitor;
-import org.apache.wicket.util.visit.Visits;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -75,9 +71,6 @@ public class ConversationPropagator extends AbstractRequestCycleListener
@Inject
private Conversation conversation;
- @Inject
- private AutoConversation autoConversation;
-
/**
* Constructor
*
@@ -129,22 +122,6 @@ public class ConversationPropagator extends AbstractRequestCycleListener
}
@Override
- public void onRequestHandlerExecuted(RequestCycle cycle, IRequestHandler handler)
- {
- Page page = getPage(handler);
-
- if (page == null)
- {
- return;
- }
-
- // apply auto semantics
-
- autoEndIfNecessary(page, handler, conversation);
- autoBeginIfNecessary(page, handler, conversation);
- }
-
- @Override
public void onRequestHandlerScheduled(RequestCycle cycle, IRequestHandler handler)
{
// propagate current non-transient conversation to the newly scheduled
@@ -237,59 +214,6 @@ public class ConversationPropagator extends AbstractRequestCycleListener
return true;
}
- protected void autoBeginIfNecessary(Page page, IRequestHandler handler,
- Conversation conversation)
- {
- if (conversation == null || !conversation.isTransient() || page == null
- || !hasConversationalComponent(page) || !propagation.propagatesVia(handler, page))
- {
- return;
- }
-
- // auto activate conversation
-
- conversation.begin();
- autoConversation.setAutomatic(true);
-
- logger.debug("Auto-began conversation {} for page {}", conversation.getId(), page);
- }
-
- protected void autoEndIfNecessary(Page page, IRequestHandler handler, Conversation conversation)
- {
- if (conversation == null || conversation.isTransient() || page == null
- || hasConversationalComponent(page) || autoConversation.isAutomatic() == false)
- {
- return;
- }
-
- // auto de-activate conversation
-
- String cid = conversation.getId();
-
- autoConversation.setAutomatic(false);
- conversation.end();
-
- logger.debug("Auto-ended conversation {} for page {}", cid, page);
- }
-
-
- protected boolean hasConversationalComponent(Page page)
- {
- Boolean hasConversational = Visits.visit(page, new IVisitor<Component, Boolean>()
- {
- @Override
- public void component(Component object, IVisit<Boolean> visit)
- {
- if (object instanceof ConversationalComponent)
- {
- visit.stop(true);
- }
- }
- });
-
- return hasConversational == null ? false : hasConversational;
- }
-
public static void markPageWithConversationId(IRequestHandler handler, String cid)
{
Page page = getPage(handler);
@@ -304,6 +228,11 @@ public class ConversationPropagator extends AbstractRequestCycleListener
return page.getMetaData(CONVERSATION_ID_KEY);
}
+ public static void removeConversationIdFromPage(Page page)
+ {
+ page.setMetaData(CONVERSATION_ID_KEY, null);
+ }
+
/**
* Resolves a page instance from the request handler iff the page instance
* is already created