Coverage Report - org.seasar.teeda.core.context.portlet.PortletFacesContextImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
PortletFacesContextImpl
23%
23/98
3%
1/32
1.833
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package org.seasar.teeda.core.context.portlet;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.Collections;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 
 23  
 import javax.faces.application.Application;
 24  
 import javax.faces.application.FacesMessage;
 25  
 import javax.faces.application.FacesMessage.Severity;
 26  
 import javax.faces.component.UIViewRoot;
 27  
 import javax.faces.context.ExternalContext;
 28  
 import javax.faces.context.FacesContext;
 29  
 import javax.faces.context.ResponseStream;
 30  
 import javax.faces.context.ResponseWriter;
 31  
 import javax.faces.internal.FactoryFinderUtil;
 32  
 import javax.faces.render.RenderKit;
 33  
 
 34  
 import org.seasar.framework.util.AssertionUtil;
 35  
 import org.seasar.teeda.core.context.Releaseable;
 36  
 import org.seasar.teeda.core.util.ApplicationUtil;
 37  
 
 38  
 /**
 39  
  * @author shot
 40  
  * @author shinsuke
 41  
  *
 42  
  */
 43  
 public class PortletFacesContextImpl extends FacesContext {
 44  
 
 45  
     private ExternalContext externalContext;
 46  
 
 47  
     private Application application;
 48  
 
 49  1
     private boolean released = false;
 50  
 
 51  1
     private boolean renderResponse = false;
 52  
 
 53  1
     private boolean responseComplete = false;
 54  
 
 55  
     private ResponseWriter responseWriter;
 56  
 
 57  
     private UIViewRoot root;
 58  
 
 59  
     private List messages;
 60  
 
 61  
     private List messageClientIds;
 62  
 
 63  
     private FacesMessage.Severity maxSeverity;
 64  
 
 65  
     private ResponseStream responseStream;
 66  
 
 67  1
     public PortletFacesContextImpl(ExternalContext externalContext) {
 68  1
         this.externalContext = externalContext;
 69  1
         this.application = ApplicationUtil.getApplicationFromFactory();
 70  1
         FacesContext.setCurrentInstance(this);
 71  1
     }
 72  
 
 73  
     public Application getApplication() {
 74  1
         assertNotReleased();
 75  1
         return application;
 76  
     }
 77  
 
 78  
     public Iterator getClientIdsWithMessages() {
 79  0
         assertNotReleased();
 80  0
         if (messages == null) {
 81  0
             return Collections.EMPTY_LIST.iterator();
 82  
         }
 83  0
         return messageClientIds.iterator();
 84  
     }
 85  
 
 86  
     public ExternalContext getExternalContext() {
 87  1
         assertNotReleased();
 88  1
         return externalContext;
 89  
     }
 90  
 
 91  
     public Severity getMaximumSeverity() {
 92  0
         assertNotReleased();
 93  0
         return maxSeverity;
 94  
     }
 95  
 
 96  
     public Iterator getMessages() {
 97  0
         assertNotReleased();
 98  0
         return (messages != null) ? messages.iterator()
 99  
                 : Collections.EMPTY_LIST.iterator();
 100  
     }
 101  
 
 102  
     public Iterator getMessages(String clientId) {
 103  0
         assertNotReleased();
 104  0
         if (messages == null) {
 105  0
             return Collections.EMPTY_LIST.iterator();
 106  
         }
 107  0
         if (messages.size() != messageClientIds.size()) {
 108  0
             throw new IllegalStateException();
 109  
         }
 110  0
         List list = new ArrayList();
 111  0
         for (int i = 0; i < messages.size(); i++) {
 112  0
             String savedClientId = (String) messageClientIds.get(i);
 113  0
             if (stringEquals(clientId, savedClientId)) {
 114  0
                 list.add(messages.get(i));
 115  
             }
 116  
         }
 117  0
         return list.iterator();
 118  
     }
 119  
 
 120  
     private boolean stringEquals(String a, String b) {
 121  0
         if (a == null) {
 122  0
             return b == null;
 123  
         }
 124  0
         return a.equals(b);
 125  
     }
 126  
 
 127  
     public RenderKit getRenderKit() {
 128  0
         assertNotReleased();
 129  0
         UIViewRoot root = getViewRoot();
 130  0
         if (root == null) {
 131  0
             return null;
 132  
         }
 133  0
         String renderKitId = root.getRenderKitId();
 134  0
         if (renderKitId == null) {
 135  0
             return null;
 136  
         }
 137  0
         return FactoryFinderUtil.getRenderKitFactory().getRenderKit(this,
 138  
                 renderKitId);
 139  
     }
 140  
 
 141  
     public boolean getRenderResponse() {
 142  0
         assertNotReleased();
 143  0
         return renderResponse;
 144  
     }
 145  
 
 146  
     public boolean getResponseComplete() {
 147  0
         assertNotReleased();
 148  0
         return responseComplete;
 149  
     }
 150  
 
 151  
     public ResponseStream getResponseStream() {
 152  0
         assertNotReleased();
 153  0
         return responseStream;
 154  
     }
 155  
 
 156  
     public void setResponseStream(ResponseStream responseStream) {
 157  0
         assertNotReleased();
 158  0
         AssertionUtil.assertNotNull("responseStream", responseStream);
 159  0
         this.responseStream = responseStream;
 160  0
     }
 161  
 
 162  
     public ResponseWriter getResponseWriter() {
 163  0
         assertNotReleased();
 164  0
         return responseWriter;
 165  
     }
 166  
 
 167  
     public void setResponseWriter(ResponseWriter responseWriter) {
 168  0
         assertNotReleased();
 169  0
         AssertionUtil.assertNotNull("responseWriter", responseWriter);
 170  0
         this.responseWriter = responseWriter;
 171  0
     }
 172  
 
 173  
     public UIViewRoot getViewRoot() {
 174  2
         assertNotReleased();
 175  2
         return root;
 176  
     }
 177  
 
 178  
     public void setViewRoot(UIViewRoot root) {
 179  2
         assertNotReleased();
 180  2
         AssertionUtil.assertNotNull("root", root);
 181  2
         this.root = root;
 182  2
     }
 183  
 
 184  
     public void addMessage(String clientId, FacesMessage message) {
 185  0
         assertNotReleased();
 186  0
         AssertionUtil.assertNotNull("message", message);
 187  0
         if (messages == null) {
 188  0
             messages = new ArrayList();
 189  0
             messageClientIds = new ArrayList();
 190  
         }
 191  0
         messages.add(message);
 192  0
         messageClientIds.add(clientId);
 193  0
         FacesMessage.Severity severity = message.getSeverity();
 194  0
         setSeverity(severity);
 195  0
     }
 196  
 
 197  
     public void release() {
 198  0
         released = true;
 199  0
         application = null;
 200  0
         if (externalContext instanceof Releaseable) {
 201  0
             ((Releaseable) externalContext).release();
 202  
         }
 203  0
         externalContext = null;
 204  0
         FacesContext.setCurrentInstance(null);
 205  0
     }
 206  
 
 207  
     public void renderResponse() {
 208  1
         assertNotReleased();
 209  1
         renderResponse = true;
 210  1
     }
 211  
 
 212  
     public void responseComplete() {
 213  0
         assertNotReleased();
 214  0
         responseComplete = true;
 215  0
     }
 216  
 
 217  
     private void setSeverity(FacesMessage.Severity severity) {
 218  0
         if (severity != null) {
 219  0
             if (maxSeverity == null || severity.compareTo(maxSeverity) > 0) {
 220  0
                 maxSeverity = severity;
 221  
             }
 222  
         }
 223  0
     }
 224  
 
 225  
     private void assertNotReleased() {
 226  7
         if (released) {
 227  0
             throw new IllegalStateException("already released");
 228  
         }
 229  7
     }
 230  
 
 231  
     public void setExternalContext(ExternalContext externalContext) {
 232  0
         assertNotReleased();
 233  0
         this.externalContext = externalContext;
 234  
         //TODO this code is needed Portlet render method. needs to review FacesContext handling..
 235  0
         FacesContext.setCurrentInstance(this);
 236  0
     }
 237  
 
 238  
 }