KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlet > faces > context > FacesPortletContextImpl


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.portlet.faces.context;
6
7
8 import javax.faces.FactoryFinder;
9 import javax.faces.render.* ;
10 import javax.faces.application.* ;
11 import javax.faces.context.ExternalContext;
12 import javax.faces.context.FacesContext;
13 import javax.faces.context.ResponseStream;
14 import javax.faces.context.ResponseWriter;
15 import javax.faces.event.FacesEvent;
16 import javax.faces.component.UIViewRoot ;
17 import javax.portlet.RenderRequest;
18 import javax.portlet.RenderResponse;
19 import org.exoplatform.faces.context.HtmlResponseWriter;
20 import org.exoplatform.faces.context.PortletFacesContext;
21 import org.exoplatform.portlet.faces.application.PortletFacesData;
22
23 import java.util.* ;
24 import java.io.PrintWriter JavaDoc ;
25 import java.io.IOException JavaDoc ;
26 /**
27  * The JSR-127 specification chapter 6.1 outlines the requirements of the faces factory implementation.
28  *
29  * @author Ove Ranheim (oranheim@users.sourceforge.net)
30  * @since Nov 6, 2003 5:29:38 PM
31  */

32 public class FacesPortletContextImpl extends FacesContext implements PortletFacesContext {
33
34   private ExternalContext externalContext_;
35   private Locale locale_ = null;
36   private UIViewRoot viewRoot_ = null;
37   private List p_facesEvents = null;
38   private Map facesMessages_ = null;
39   private int p_maximumSeverity = 0;
40   private ResponseStream responseStream_ = null;
41   private ResponseWriter responseWriter_ = null;
42   private boolean renderResponse_ = false;
43   private boolean responseComplete_ = false;
44   private Application application_ ;
45   private boolean renderPhase_ ;
46  
47   private FacesContext portalFacesContext;
48
49   public FacesPortletContextImpl() { throw new UnsupportedOperationException JavaDoc(); }
50
51   public FacesPortletContextImpl(Object JavaDoc config, Object JavaDoc request, Object JavaDoc response ) throws IOException JavaDoc {
52     this.portalFacesContext = FacesContext.getCurrentInstance();
53     renderPhase_ = false ;
54     if ( request instanceof RenderRequest ) {
55       RenderResponse rr = (RenderResponse) response ;
56       PrintWriter JavaDoc writer = rr.getWriter() ;
57       /*
58       if (portalFacesContext != null) {
59         RenderKit rkit = portalFacesContext.getRenderKit();
60         responseWriter_ = rkit.createResponseWriter(writer, null, null) ;
61       } else {
62         responseWriter_ = new HtmlResponseWriter(writer) ;
63       }
64       */

65       renderPhase_ = true ;
66       responseWriter_ = new HtmlResponseWriter(writer) ;
67     }
68     externalContext_ = new ExternalContextImpl(config, request, response, renderPhase_);
69     FacesContext.setCurrentInstance( this );
70   }
71
72   public static void setCurrentInstance( FacesContext context ) {
73     FacesContext.setCurrentInstance( context );
74   }
75
76   public void release() {
77     externalContext_ = null;
78     locale_ = null;
79     viewRoot_ = null;
80     p_facesEvents = null;
81     facesMessages_ = null;
82     responseStream_ = null;
83     responseWriter_ = null;
84     application_ = null ;
85     FacesContext.setCurrentInstance(portalFacesContext);
86   }
87
88   // JSR-127 6.1.1
89
public ExternalContext getExternalContext() { return externalContext_; }
90
91   // JSR-127 6.1.2
92
public Locale getLocale() {
93     if (locale_ == null) {
94       locale_ = getExternalContext().getRequestLocale();
95     }
96     return locale_;
97   }
98
99   public void setLocale( Locale locale ) {
100     locale_ = locale;
101   }
102
103   // JSR-127 6.1.3
104
public UIViewRoot getViewRoot() {
105     return viewRoot_ ;
106   }
107   public void setViewRoot(UIViewRoot viewRoot) {
108     viewRoot_ = viewRoot;
109   }
110
111
112   public void addMessage(String JavaDoc clientId, FacesMessage message) {
113     if(null == message)
114       throw new NullPointerException JavaDoc("message cannot be null");
115     if(facesMessages_ == null) facesMessages_ = new HashMap();
116     List list = (List) facesMessages_.get(clientId);
117     if(list == null) {
118       list = new ArrayList();
119       facesMessages_.put(clientId, list);
120     }
121     list.add(message);
122   }
123
124   public Iterator getMessages() {
125     if(null == facesMessages_) return Collections.EMPTY_LIST.iterator();
126     List messages = new ArrayList() ;
127     Iterator i = facesMessages_.values().iterator() ;
128     while (i.hasNext()) {
129       List list = (List) i.next() ;
130       for (int j = 0; j < list.size() ; j++) {
131         messages.add(list.get(j));
132       }
133     }
134     if(messages.size() > 0) return messages.iterator();
135     return Collections.EMPTY_LIST.iterator();
136   }
137
138
139   public Iterator getMessages(String JavaDoc clientId) {
140     if(null == facesMessages_) return Collections.EMPTY_LIST.iterator();
141     List list = (List) facesMessages_.get(clientId);
142     if(list == null) return Collections.EMPTY_LIST.iterator();
143     return list.iterator();
144   }
145
146   // JSR-127 6.1.5
147
public Iterator getFacesEvents() {
148     if (p_facesEvents == null) p_facesEvents = new ArrayList();
149     return p_facesEvents.iterator();
150   }
151
152   // JSR-127 6.1.5
153
public void addFacesEvent( FacesEvent event ) {
154     if (p_facesEvents == null) p_facesEvents = new ArrayList();
155     p_facesEvents.add( event );
156   }
157
158   // JSR-127 6.1.6
159
public ResponseStream getResponseStream() {
160     return responseStream_;
161   }
162
163   public void setResponseStream( ResponseStream responseStream ) {
164     responseStream_ = responseStream;
165   }
166
167   public ResponseWriter getResponseWriter() {
168     return responseWriter_;
169   }
170
171   public void setResponseWriter( ResponseWriter responseWriter ) {
172     responseWriter_ = responseWriter;
173   }
174
175
176   public FacesMessage.Severity getMaximumSeverity() {
177     int max = 0;
178     FacesMessage.Severity result = null;
179     if(null == facesMessages_ ) return null;
180     Iterator i = facesMessages_.values().iterator() ;
181     while (i.hasNext()) {
182       List list = (List) i.next() ;
183       for (int j = 0; j < list.size() ; j++) {
184         FacesMessage.Severity temp = ((FacesMessage) list.get(j)).getSeverity();
185         if(temp.getOrdinal() > max) result = temp;
186         if(result == FacesMessage.SEVERITY_FATAL) return result ;
187       }
188     }
189
190     return result;
191   }
192
193   public RenderKit getRenderKit() {
194     UIViewRoot vr = getViewRoot();
195     if(vr == null) return null;
196     String JavaDoc renderKitId = vr.getRenderKitId();
197     if(renderKitId == null) {
198       return null;
199     }
200     RenderKitFactory rkFactory =
201         (RenderKitFactory)FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
202     return rkFactory.getRenderKit(this, renderKitId);
203   }
204
205   public Iterator getClientIdsWithMessages() {
206     Iterator result = null;
207     if(null == facesMessages_) result = Collections.EMPTY_LIST.iterator();
208     else result = facesMessages_.keySet().iterator();
209     return result;
210   }
211
212   
213   public Application getApplication() {
214     if(null != application_) {
215       return application_ ;
216     }
217     ApplicationFactory aFactory = (ApplicationFactory)FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY);
218     application_ = aFactory.getApplication();
219     return application_;
220   }
221   // JSR-127 6.1.7
222
public void renderResponse() { renderResponse_ = true; }
223
224   public boolean getRenderResponse() { return renderResponse_; }
225
226   public void responseComplete() { responseComplete_ = true; }
227
228   public boolean getResponseComplete() { return responseComplete_; }
229
230   public FacesContext getPortalFacesContext() { return portalFacesContext ; }
231
232   public boolean isPortletRenderPhase() { return renderPhase_ ; }
233   
234   public void destroy() {
235     PortletFacesData.destroy(this) ;
236   }
237 }
238
Popular Tags