1 16 package org.apache.myfaces.context.servlet; 17 18 import org.apache.myfaces.util.NullIterator; 19 20 import javax.faces.FactoryFinder; 21 import javax.faces.application.Application; 22 import javax.faces.application.ApplicationFactory; 23 import javax.faces.application.FacesMessage; 24 import javax.faces.component.UIViewRoot; 25 import javax.faces.context.ExternalContext; 26 import javax.faces.context.FacesContext; 27 import javax.faces.context.ResponseStream; 28 import javax.faces.context.ResponseWriter; 29 import javax.faces.render.RenderKit; 30 import javax.faces.render.RenderKitFactory; 31 import javax.servlet.ServletContext ; 32 import javax.servlet.ServletRequest ; 33 import javax.servlet.ServletResponse ; 34 import java.util.*; 35 import javax.portlet.PortletContext; 36 import javax.portlet.PortletRequest; 37 import javax.portlet.PortletResponse; 38 import org.apache.myfaces.context.ReleaseableExternalContext; 39 import org.apache.myfaces.context.portlet.PortletExternalContextImpl; 40 41 42 66 public class ServletFacesContextImpl 67 extends FacesContext 68 { 69 71 protected static final Object NULL_DUMMY = new Object (); 72 73 75 List _messageClientIds = null; 76 private List _messages = null; 77 private Application _application; 78 private ReleaseableExternalContext _externalContext; 79 private ResponseStream _responseStream = null; 80 private ResponseWriter _responseWriter = null; 81 private FacesMessage.Severity _maximumSeverity = FacesMessage.SEVERITY_INFO; 82 private UIViewRoot _viewRoot; 83 private boolean _renderResponse = false; 84 private boolean _responseComplete = false; 85 private RenderKitFactory _renderKitFactory; 86 87 89 public ServletFacesContextImpl(PortletContext portletContext, 91 PortletRequest portletRequest, 92 PortletResponse portletResponse) 93 { 94 this(new PortletExternalContextImpl(portletContext, 95 portletRequest, 96 portletResponse)); 97 } 98 99 public ServletFacesContextImpl(ServletContext servletContext, 100 ServletRequest servletRequest, 101 ServletResponse servletResponse) 102 { 103 this(new ServletExternalContextImpl(servletContext, 104 servletRequest, 105 servletResponse)); 106 } 107 108 private ServletFacesContextImpl(ReleaseableExternalContext externalContext) 109 { 110 _application = ((ApplicationFactory)FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY)) 111 .getApplication(); 112 _renderKitFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); 113 _externalContext = externalContext; 114 FacesContext.setCurrentInstance(this); } 116 117 119 public ExternalContext getExternalContext() 120 { 121 return (ExternalContext)_externalContext; 122 } 123 124 public FacesMessage.Severity getMaximumSeverity() 125 { 126 return _maximumSeverity; 127 } 128 129 public Iterator getMessages() 130 { 131 return (_messages != null) ? _messages.iterator() : Collections.EMPTY_LIST.iterator(); 132 } 133 134 public Application getApplication() 135 { 136 return _application; 137 } 138 139 public Iterator getClientIdsWithMessages() 140 { 141 if (_messages == null || _messages.isEmpty()) 142 { 143 return NullIterator.instance(); 144 } 145 146 return new Iterator() 147 { 148 private int _next; 149 boolean _nextFound; 150 151 public void remove() 152 { 153 throw new UnsupportedOperationException (this.getClass().getName() + " UnsupportedOperationException"); 154 } 155 156 public boolean hasNext() 157 { 158 if (!_nextFound) 159 { 160 for (int len = _messageClientIds.size(); _next < len; _next++) 161 { 162 if (_messageClientIds.get(_next) != NULL_DUMMY) 163 { 164 _nextFound = true; 165 break; 166 } 167 } 168 } 169 return _nextFound; 170 } 171 172 public Object next() 173 { 174 if (hasNext()) 175 { 176 _nextFound = false; 177 return _messageClientIds.get(_next++); 178 } 179 180 throw new NoSuchElementException(); 181 } 182 }; 183 } 184 185 public Iterator getMessages(String clientId) 186 { 187 if (_messages == null) 188 { 189 return NullIterator.instance(); 190 } 191 192 List lst = new ArrayList(); 193 for (int i = 0; i < _messages.size(); i++) 194 { 195 Object savedClientId = _messageClientIds.get(i); 196 if (clientId == null) 197 { 198 if (savedClientId == NULL_DUMMY) lst.add(_messages.get(i)); 199 } 200 else 201 { 202 if (clientId.equals(savedClientId)) lst.add(_messages.get(i)); 203 } 204 } 205 return lst.iterator(); 206 } 207 208 public RenderKit getRenderKit() 209 { 210 if (getViewRoot() == null) 211 { 212 return null; 213 } 214 215 String renderKitId = getViewRoot().getRenderKitId(); 216 217 if (renderKitId == null) 218 { 219 return null; 220 } 221 222 return _renderKitFactory.getRenderKit(this, renderKitId); 223 } 224 225 public boolean getRenderResponse() 226 { 227 return _renderResponse; 228 } 229 230 public boolean getResponseComplete() 231 { 232 return _responseComplete; 233 } 234 235 public void setResponseStream(ResponseStream responseStream) 236 { 237 if (responseStream == null) 238 { 239 throw new NullPointerException ("responseStream"); 240 } 241 _responseStream = responseStream; 242 } 243 244 public ResponseStream getResponseStream() 245 { 246 return _responseStream; 247 } 248 249 public void setResponseWriter(ResponseWriter responseWriter) 250 { 251 if (responseWriter == null) 252 { 253 throw new NullPointerException ("responseWriter"); 254 } 255 _responseWriter = responseWriter; 256 } 257 258 public ResponseWriter getResponseWriter() 259 { 260 return _responseWriter; 261 } 262 263 public void setViewRoot(UIViewRoot viewRoot) 264 { 265 if (viewRoot == null) 266 { 267 throw new NullPointerException ("viewRoot"); 268 } 269 _viewRoot = viewRoot; 270 } 271 272 public UIViewRoot getViewRoot() 273 { 274 return _viewRoot; 275 } 276 277 public void addMessage(String clientId, FacesMessage message) 278 { 279 if (message == null) 280 { 281 throw new NullPointerException ("message"); 282 } 283 284 if (_messages == null) 285 { 286 _messages = new ArrayList(); 287 _messageClientIds = new ArrayList(); 288 } 289 _messages.add(message); 290 _messageClientIds.add((clientId != null) ? clientId : NULL_DUMMY); 291 FacesMessage.Severity serSeverity = message.getSeverity(); 292 if (serSeverity != null && serSeverity.compareTo(_maximumSeverity) > 0) 293 { 294 _maximumSeverity = message.getSeverity(); 295 } 296 } 297 298 public void release() 299 { 300 if (_externalContext != null) 301 { 302 _externalContext.release(); 303 _externalContext = null; 304 } 305 306 _messageClientIds = null; 307 _messages = null; 308 _application = null; 309 _responseStream = null; 310 _responseWriter = null; 311 _viewRoot = null; 312 313 FacesContext.setCurrentInstance(null); 314 } 315 316 public void renderResponse() 317 { 318 _renderResponse = true; 319 } 320 321 public void responseComplete() 322 { 323 _responseComplete = true; 324 } 325 326 public void setExternalContext(ReleaseableExternalContext extContext) 329 { 330 _externalContext = extContext; 331 FacesContext.setCurrentInstance(this); } 333 } 334 | Popular Tags |