Author: alexsmirnov
Date: 2007-10-04 16:58:27 -0400 (Thu, 04 Oct 2007)
New Revision: 3264
Modified:
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/resource/ResourceLifecycle.java
Log:
fix for a
http://jira.jboss.com/jira/browse/RF-1064
Modified:
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/resource/ResourceLifecycle.java
===================================================================
---
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/resource/ResourceLifecycle.java 2007-10-04
19:02:45 UTC (rev 3263)
+++
branches/3.1.x/framework/impl/src/main/java/org/ajax4jsf/resource/ResourceLifecycle.java 2007-10-04
20:58:27 UTC (rev 3264)
@@ -54,7 +54,7 @@
public class ResourceLifecycle extends Lifecycle {
private Lifecycle lifecycle;
-
+
private static final Log _log = LogFactory.getLog(ResourceLifecycle.class);
/*
@@ -122,77 +122,107 @@
phaseListeners = facesLifecycle.getPhaseListeners();
PhaseEvent restoreViewEvent = new PhaseEvent(facesContext,
PhaseId.RESTORE_VIEW, this);
- // Invoke before restore view phase listeners
- for (int i = 0; i < phaseListeners.length; i++) {
- PhaseListener phaseListener = phaseListeners[i];
- if (PhaseId.RESTORE_VIEW.equals(phaseListener.getPhaseId())
- || PhaseId.ANY_PHASE.equals(phaseListener.getPhaseId())) {
- try {
- phaseListener.beforePhase(restoreViewEvent);
+ processPhaseListeners(phaseListeners, restoreViewEvent, true);
+ // Fix for a
http://jira.jboss.org/jira/browse/RF-1056
+ if (facesContext.getResponseComplete())
+ return;
+ // fix for a
http://jira.jboss.com/jira/browse/RF-1064 .
+ // viewRoot can be created outside.
+ UIViewRoot savedViewRoot = facesContext.getViewRoot();
+ try {
+ // create "dummy" viewRoot, to avoid problems in phase
+ // listeners.
+ UIViewRoot root = new UIViewRoot();
+ root.setViewId(resource.getKey());
+ root.setLocale(Locale.getDefault());
+ root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
+ facesContext.setViewRoot(root);
+ // Invoke after restore view phase listeners
+ processPhaseListeners(phaseListeners, restoreViewEvent, false);
+ // Fix for a
http://jira.jboss.org/jira/browse/RF-1056
+ if (!facesContext.getResponseComplete()) {
+ // Invoke before render view phase listeners
+ renderViewEvent = new PhaseEvent(facesContext,
+ PhaseId.RENDER_RESPONSE, this);
+ processPhaseListeners(phaseListeners, renderViewEvent, true);
+ sendResource(resourceContext, resource);
+ processPhaseListeners(phaseListeners, renderViewEvent,
+ false);
+ }
- } catch (Exception e) {
- _log.error("Exception in PhaseListener, restore view : beforePhase", e);
- }
+ } finally {
+ if (null != savedViewRoot) {
+ facesContext.setViewRoot(savedViewRoot);
}
}
- // Fix for a
http://jira.jboss.org/jira/browse/RF-1056
- if(facesContext.getResponseComplete()) return;
- // create "dummy" viewRoot, to avoid problems in phase listeners.
- UIViewRoot root = new UIViewRoot();
- root.setViewId(resource.getKey());
- root.setLocale(Locale.getDefault());
- root.setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT);
- facesContext.setViewRoot(root);
- // Invoke after restore view phase listeners
- for (int i = phaseListeners.length - 1; i > 0; i--) {
- PhaseListener phaseListener = phaseListeners[i];
- if (PhaseId.RESTORE_VIEW.equals(phaseListener.getPhaseId())
- || PhaseId.ANY_PHASE.equals(phaseListener.getPhaseId())) {
- try {
- phaseListener.afterPhase(restoreViewEvent);
+ } else {
+ sendResource(resourceContext, resource);
+ }
+ }
- } catch (Exception e) {
- _log.error("Exception in PhaseListener, restore view : afterPhase", e);
- }
- }
- }
- // Fix for a
http://jira.jboss.org/jira/browse/RF-1056
- if(facesContext.getResponseComplete()) return;
- // Invoke before render view phase listeners
- renderViewEvent = new PhaseEvent(facesContext,
- PhaseId.RENDER_RESPONSE, this);
+ /**
+ * Send phase event to all apropriate PhaseListener's
+ *
+ * @param phaseListeners
+ * @param phaseEvent
+ * @param beforePhase
+ * TODO
+ */
+ private void processPhaseListeners(PhaseListener[] phaseListeners,
+ PhaseEvent phaseEvent, boolean beforePhase) {
+ if (beforePhase) {
+ // Invoke before phase listeners
for (int i = 0; i < phaseListeners.length; i++) {
PhaseListener phaseListener = phaseListeners[i];
- if (PhaseId.RENDER_RESPONSE.equals(phaseListener.getPhaseId())
- || PhaseId.ANY_PHASE.equals(phaseListener.getPhaseId())) {
- try {
- phaseListener.beforePhase(renderViewEvent);
+ invokePhaseListener(phaseListener, phaseEvent, beforePhase);
+ }
- } catch (Exception e) {
- _log.error("Exception in PhaseListener, render view : beforePhase", e);
- }
- }
- }
- }
- resource.sendHeaders(resourceContext);
- resource.send(resourceContext);
- if (null != facesContext) {
- // Invoke after restore view phase listeners
+ } else {
+ // Invoke after phase listeners, in reverse order.
for (int i = phaseListeners.length - 1; i > 0; i--) {
PhaseListener phaseListener = phaseListeners[i];
- if (PhaseId.RENDER_RESPONSE.equals(phaseListener.getPhaseId())
- || PhaseId.ANY_PHASE.equals(phaseListener.getPhaseId())) {
- try {
- phaseListener.afterPhase(renderViewEvent);
+ invokePhaseListener(phaseListener, phaseEvent, beforePhase);
+ }
- } catch (Exception e) {
- _log.error("Exception in PhaseListener, render view : afterPhase", e);
- }
+ }
+ }
+
+ /**
+ * @param phaseListener
+ * @param phaseEvent
+ * @param beforePhase
+ */
+ private void invokePhaseListener(PhaseListener phaseListener,
+ PhaseEvent phaseEvent, boolean beforePhase) {
+ if (phaseEvent.getPhaseId().equals(phaseListener.getPhaseId())
+ || PhaseId.ANY_PHASE.equals(phaseListener.getPhaseId())) {
+ try {
+ if (beforePhase) {
+ phaseListener.beforePhase(phaseEvent);
+ } else {
+ phaseListener.afterPhase(phaseEvent);
}
+ } catch (Exception e) {
+ _log
+ .error("Exception in PhaseListener, phase :"
+ + phaseEvent.getPhaseId().toString()
+ + (beforePhase ? " : beforePhase"
+ : " : afterPhase"), e);
}
}
}
+ /**
+ * @param resourceContext
+ * @param resource
+ * @throws IOException
+ */
+ private void sendResource(ResourceContext resourceContext,
+ InternetResource resource) throws IOException {
+ resource.sendHeaders(resourceContext);
+ resource.send(resourceContext);
+ }
+
protected synchronized Lifecycle getFacesLifecycle() {
if (lifecycle == null) {
// Acquire our Lifecycle instance