KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlet > faces > lifecycle > ExoLifecycle


1 package org.exoplatform.portlet.faces.lifecycle;
2
3 import java.util.*;
4 import javax.faces.FacesException;
5 import javax.faces.context.FacesContext;
6 import javax.faces.event.*;
7 import javax.faces.lifecycle.Lifecycle;
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9
10 import org.apache.commons.logging.Log;
11 import org.exoplatform.services.log.LogUtil;
12 import com.sun.faces.lifecycle.* ;
13
14 import org.exoplatform.portlet.faces.context.* ;
15
16 public class ExoLifecycle extends Lifecycle {
17     private static Log log_ = LogUtil.getLog("org.exoplatform.portlet.faces.faces");
18     
19   private List listeners;
20   private Phase phases[] = {
21      null, new RestoreViewPhase(), new ApplyRequestValuesPhase(),
22      new ProcessValidationsPhase(), new UpdateModelValuesPhase(),
23      new InvokeApplicationPhase()
24   };
25   private Phase response;
26
27   public ExoLifecycle() {
28     listeners = new ArrayList();
29     response = new RenderResponsePhase();
30   }
31
32   public void execute(FacesContext context) throws FacesException {
33     //log_.debug("##########################Call excute in lifecycle");
34
if(context == null) throw new NullPointerException JavaDoc("Faces context cannot be null");
35     FacesPortletContextImpl contextImpl = (FacesPortletContextImpl) context ;
36     boolean portletRenderPhase = contextImpl.isPortletRenderPhase() ;
37     for(int i = 1; i < phases.length; i++) {
38       if(context.getRenderResponse() || context.getResponseComplete()) {
39         break;
40       }
41       //log_.debug("phase: " + phases[i]);
42
phase((PhaseId)PhaseId.VALUES.get(i), phases[i], context);
43       if(reload((PhaseId)PhaseId.VALUES.get(i), context)) {
44         context.renderResponse();
45       }
46       if(portletRenderPhase) return ;
47     }
48     //log_.debug("##########################End Call excute in lifecycle");
49
}
50
51   public void render(FacesContext context) throws FacesException {
52     if(context == null) throw new NullPointerException JavaDoc("Faces context cannot be null");
53     FacesPortletContextImpl contextImpl = (FacesPortletContextImpl) context ;
54     if (!contextImpl.isPortletRenderPhase()) return ;
55     if(!context.getResponseComplete()) {
56       phase(PhaseId.RENDER_RESPONSE, response, context);
57     }
58   }
59
60   public void addPhaseListener(PhaseListener listener) {
61     if(listener == null)
62       throw new NullPointerException JavaDoc("Faces Context cannot be null");
63     synchronized(listeners) {
64       listeners.add(listener);
65     }
66   }
67
68   public PhaseListener[] getPhaseListeners() {
69     List list = listeners;
70     PhaseListener results[] = new PhaseListener[listeners.size()];
71     return (PhaseListener[])listeners.toArray(results);
72   }
73
74   public void removePhaseListener(PhaseListener listener) {
75     if(listener == null) throw new NullPointerException JavaDoc("Faces Context cannot be null");
76     synchronized(listeners) {
77       listeners.remove(listener);
78     }
79   }
80
81   private void phase(PhaseId phaseId, Phase phase, FacesContext context) throws FacesException {
82     synchronized(listeners) {
83       if(listeners.size() > 0) {
84         PhaseEvent event = new PhaseEvent(context, phaseId, this);
85         for(int i = 0; i < listeners.size(); i++) {
86           PhaseListener listener = (PhaseListener)listeners.get(i);
87           if(phaseId.equals(listener.getPhaseId()) || PhaseId.ANY_PHASE.equals(listener.getPhaseId())) {
88             listener.beforePhase(event);
89           }
90         }
91
92       }
93     }
94
95     if(!skipping(phaseId, context)) phase.execute(context);
96     synchronized(listeners) {
97       if(listeners.size() > 0) {
98         PhaseEvent event = new PhaseEvent(context, phaseId, this);
99         for(int i = listeners.size() - 1; i >= 0; i--) {
100           PhaseListener listener = (PhaseListener)listeners.get(i);
101           if(phaseId.equals(listener.getPhaseId()) || PhaseId.ANY_PHASE.equals(listener.getPhaseId())) {
102             listener.afterPhase(event);
103           }
104         }
105
106       }
107     }
108   }
109
110   private boolean reload(PhaseId phaseId, FacesContext context) {
111     if(!phaseId.equals(PhaseId.RESTORE_VIEW)) return false;
112     if(!(context.getExternalContext().getRequest() instanceof HttpServletRequest JavaDoc)) {
113       log_.debug("request instance is not HttpServletRequest ");
114       return false;
115     }
116
117     HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)context.getExternalContext().getRequest();
118     String JavaDoc method = request.getMethod();
119     log_.debug("method is " + method);
120     if("GET".equals(method)) {
121       Iterator names = context.getExternalContext().getRequestParameterNames();
122       if(names.hasNext()) return false;
123     }
124     return !"POST".equals(method) && !"PUT".equals(method);
125   }
126
127   private boolean skipping(PhaseId phaseId, FacesContext context) {
128     if(context.getResponseComplete()) return true;
129     return context.getRenderResponse() && !phaseId.equals(PhaseId.RENDER_RESPONSE);
130   }
131
132 }
133
Popular Tags