1 16 package org.apache.cocoon.faces.context; 17 18 import javax.faces.FactoryFinder; 19 import javax.faces.application.Application; 20 import javax.faces.application.ApplicationFactory; 21 import javax.faces.application.FacesMessage; 22 import javax.faces.application.FacesMessage.Severity; 23 import javax.faces.component.UIViewRoot; 24 import javax.faces.context.ExternalContext; 25 import javax.faces.context.FacesContext; 26 import javax.faces.context.ResponseStream; 27 import javax.faces.context.ResponseWriter; 28 import javax.faces.render.RenderKit; 29 import javax.faces.render.RenderKitFactory; 30 import java.util.ArrayList ; 31 import java.util.Collections ; 32 import java.util.HashMap ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 import java.util.Map ; 36 37 43 public class FacesContextImpl extends FacesContext { 44 private ExternalContextImpl extContext; 45 46 private boolean released; 47 private boolean renderResponse; 48 private boolean responseComplete; 49 50 private Application application; 51 private UIViewRoot viewRoot; 52 private Map messages; 53 54 private ResponseStream responseStream; 55 private ResponseWriter responseWriter; 56 57 58 FacesContextImpl(ExternalContextImpl extContext) { 59 this.extContext = extContext; 60 FacesContext.setCurrentInstance(this); 61 } 62 63 private void checkReleased() { 64 if (released) { 65 throw new IllegalStateException ("Context is released."); 66 } 67 } 68 69 public Application getApplication() { 70 checkReleased(); 71 72 if (application == null) { 73 ApplicationFactory aFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); 74 this.application = aFactory.getApplication(); 75 } 76 77 return this.application; 78 } 79 80 public Iterator getClientIdsWithMessages() { 81 checkReleased(); 82 83 if (this.messages == null) { 84 return Collections.EMPTY_LIST.iterator(); 85 } else { 86 return this.messages.keySet().iterator(); 87 } 88 } 89 90 public ExternalContext getExternalContext() { 91 checkReleased(); 92 return this.extContext; 93 } 94 95 public Severity getMaximumSeverity() { 96 throw new UnsupportedOperationException (); 97 } 98 99 public Iterator getMessages() { 100 checkReleased(); 101 if (this.messages == null) { 102 return Collections.EMPTY_LIST.iterator(); 103 } 104 105 List messages = new ArrayList (); 106 for (Iterator i = this.messages.values().iterator(); i.hasNext();) { 107 final List list = (List ) i.next(); 108 messages.addAll(list); 109 } 110 111 if (messages.size() > 0) { 112 return messages.iterator(); 113 } 114 115 return Collections.EMPTY_LIST.iterator(); 116 } 117 118 public Iterator getMessages(String clientID) { 119 checkReleased(); 120 if (this.messages != null) { 121 final List list = (List ) this.messages.get(clientID); 122 if (list != null) { 123 return list.iterator(); 124 } 125 } 126 127 return Collections.EMPTY_LIST.iterator(); 128 } 129 130 public RenderKit getRenderKit() { 131 checkReleased(); 132 133 UIViewRoot viewRoot = getViewRoot(); 134 if (viewRoot == null) { 135 return null; 136 } 137 138 String renderKitId = viewRoot.getRenderKitId(); 139 if (renderKitId == null) { 140 return null; 141 } else { 142 RenderKitFactory rkFactory = (RenderKitFactory) FactoryFinder.getFactory(FactoryFinder.RENDER_KIT_FACTORY); 143 return rkFactory.getRenderKit(this, renderKitId); 144 } 145 } 146 147 public boolean getRenderResponse() { 148 checkReleased(); 149 return this.renderResponse; 150 } 151 152 public boolean getResponseComplete() { 153 checkReleased(); 154 return this.responseComplete; 155 } 156 157 public ResponseStream getResponseStream() { 158 checkReleased(); 159 return this.responseStream; 160 } 161 162 public void setResponseStream(ResponseStream responseStream) { 163 checkReleased(); 164 if (responseStream == null) { 165 throw new NullPointerException ("ResponseStream can't be null."); 166 } 167 168 this.responseStream = responseStream; 169 } 170 171 public ResponseWriter getResponseWriter() { 172 checkReleased(); 173 return this.responseWriter; 174 } 175 176 public void setResponseWriter(ResponseWriter responseWriter) { 177 checkReleased(); 178 if (responseWriter == null) { 179 throw new NullPointerException ("ResponseWriter can't be null."); 180 } 181 182 this.responseWriter = responseWriter; 183 } 184 185 public UIViewRoot getViewRoot() { 186 checkReleased(); 187 return this.viewRoot; 188 } 189 190 public void setViewRoot(UIViewRoot viewRoot) { 191 checkReleased(); 192 this.viewRoot = viewRoot; 193 } 194 195 public void addMessage(String clientID, FacesMessage message) { 196 checkReleased(); 197 if (message == null) { 198 throw new NullPointerException ("Message can't be null"); 199 } 200 201 if (messages == null) { 202 messages = new HashMap (); 203 } 204 205 List list = (List ) messages.get(clientID); 206 if (list == null) { 207 list = new ArrayList (); 208 messages.put(clientID, list); 209 } 210 211 list.add(message); 212 } 213 214 public void release() { 215 this.released = true; 216 this.extContext = null; 217 218 FacesContext.setCurrentInstance(null); 219 220 this.application = null; 221 this.viewRoot = null; 222 this.messages = null; 223 224 this.responseStream = null; 225 this.responseWriter = null; 226 } 227 228 public void renderResponse() { 229 checkReleased(); 230 this.renderResponse = true; 231 } 232 233 public void responseComplete() { 234 checkReleased(); 235 this.responseComplete = true; 236 } 237 } 238
| Popular Tags
|