Coverage Report - org.seasar.teeda.core.context.servlet.ServletFacesContextImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
ServletFacesContextImpl
54%
51/94
78%
25/32
1.87
 
 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.servlet;
 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  
  */
 41  
 public class ServletFacesContextImpl extends FacesContext {
 42  
 
 43  
     private ExternalContext externalContext;
 44  
 
 45  
     private Application application;
 46  
 
 47  12
     private boolean released = false;
 48  
 
 49  12
     private boolean renderResponse = false;
 50  
 
 51  12
     private boolean responseComplete = false;
 52  
 
 53  
     private ResponseWriter responseWriter;
 54  
 
 55  
     private UIViewRoot root;
 56  
 
 57  
     private List messages;
 58  
 
 59  
     private List messageClientIds;
 60  
 
 61  
     private FacesMessage.Severity maxSeverity;
 62  
 
 63  
     private ResponseStream responseStream;
 64  
 
 65  12
     public ServletFacesContextImpl(ExternalContext externalContext) {
 66  12
         this.externalContext = externalContext;
 67  12
         this.application = ApplicationUtil.getApplicationFromFactory();
 68  12
         FacesContext.setCurrentInstance(this);
 69  12
     }
 70  
 
 71  
     public Application getApplication() {
 72  0
         assertNotReleased();
 73  0
         return application;
 74  
     }
 75  
 
 76  
     public Iterator getClientIdsWithMessages() {
 77  3
         assertNotReleased();
 78  2
         if (messages == null) {
 79  1
             return Collections.EMPTY_LIST.iterator();
 80  
         }
 81  1
         return messageClientIds.iterator();
 82  
     }
 83  
 
 84  
     public ExternalContext getExternalContext() {
 85  0
         assertNotReleased();
 86  0
         return externalContext;
 87  
     }
 88  
 
 89  
     public Severity getMaximumSeverity() {
 90  0
         assertNotReleased();
 91  0
         return maxSeverity;
 92  
     }
 93  
 
 94  
     public Iterator getMessages() {
 95  3
         assertNotReleased();
 96  2
         return (messages != null) ? messages.iterator()
 97  
                 : Collections.EMPTY_LIST.iterator();
 98  
     }
 99  
 
 100  
     public Iterator getMessages(String clientId) {
 101  6
         assertNotReleased();
 102  5
         if (messages == null) {
 103  1
             return Collections.EMPTY_LIST.iterator();
 104  
         }
 105  4
         if (messages.size() != messageClientIds.size()) {
 106  0
             throw new IllegalStateException();
 107  
         }
 108  4
         List list = new ArrayList();
 109  28
         for (int i = 0; i < messages.size(); i++) {
 110  24
             String savedClientId = (String) messageClientIds.get(i);
 111  24
             if (stringEquals(clientId, savedClientId)) {
 112  6
                 list.add(messages.get(i));
 113  
             }
 114  
         }
 115  4
         return list.iterator();
 116  
     }
 117  
 
 118  
     private boolean stringEquals(String a, String b) {
 119  24
         if (a == null) {
 120  6
             return b == null;
 121  
         }
 122  18
         return a.equals(b);
 123  
     }
 124  
 
 125  
     public RenderKit getRenderKit() {
 126  0
         assertNotReleased();
 127  0
         UIViewRoot root = getViewRoot();
 128  0
         if (root == null) {
 129  0
             return null;
 130  
         }
 131  0
         String renderKitId = root.getRenderKitId();
 132  0
         if (renderKitId == null) {
 133  0
             return null;
 134  
         }
 135  0
         return FactoryFinderUtil.getRenderKitFactory().getRenderKit(this,
 136  
                 renderKitId);
 137  
     }
 138  
 
 139  
     public boolean getRenderResponse() {
 140  0
         assertNotReleased();
 141  0
         return renderResponse;
 142  
     }
 143  
 
 144  
     public boolean getResponseComplete() {
 145  0
         assertNotReleased();
 146  0
         return responseComplete;
 147  
     }
 148  
 
 149  
     public ResponseStream getResponseStream() {
 150  0
         assertNotReleased();
 151  0
         return responseStream;
 152  
     }
 153  
 
 154  
     public void setResponseStream(ResponseStream responseStream) {
 155  0
         assertNotReleased();
 156  0
         AssertionUtil.assertNotNull("responseStream", responseStream);
 157  0
         this.responseStream = responseStream;
 158  0
     }
 159  
 
 160  
     public ResponseWriter getResponseWriter() {
 161  0
         assertNotReleased();
 162  0
         return responseWriter;
 163  
     }
 164  
 
 165  
     public void setResponseWriter(ResponseWriter responseWriter) {
 166  0
         assertNotReleased();
 167  0
         AssertionUtil.assertNotNull("responseWriter", responseWriter);
 168  0
         this.responseWriter = responseWriter;
 169  0
     }
 170  
 
 171  
     public UIViewRoot getViewRoot() {
 172  0
         assertNotReleased();
 173  0
         return root;
 174  
     }
 175  
 
 176  
     public void setViewRoot(UIViewRoot root) {
 177  0
         assertNotReleased();
 178  0
         AssertionUtil.assertNotNull("root", root);
 179  0
         this.root = root;
 180  0
     }
 181  
 
 182  
     public void addMessage(String clientId, FacesMessage message) {
 183  18
         assertNotReleased();
 184  18
         AssertionUtil.assertNotNull("message", message);
 185  18
         if (messages == null) {
 186  3
             messages = new ArrayList();
 187  3
             messageClientIds = new ArrayList();
 188  
         }
 189  18
         messages.add(message);
 190  18
         messageClientIds.add(clientId);
 191  18
         FacesMessage.Severity severity = message.getSeverity();
 192  18
         setSeverity(severity);
 193  18
     }
 194  
 
 195  
     public void release() {
 196  14
         released = true;
 197  14
         application = null;
 198  14
         if (externalContext instanceof Releaseable) {
 199  1
             ((Releaseable) externalContext).release();
 200  
         }
 201  14
         externalContext = null;
 202  14
         FacesContext.setCurrentInstance(null);
 203  14
     }
 204  
 
 205  
     public void renderResponse() {
 206  0
         assertNotReleased();
 207  0
         renderResponse = true;
 208  0
     }
 209  
 
 210  
     public void responseComplete() {
 211  0
         assertNotReleased();
 212  0
         responseComplete = true;
 213  0
     }
 214  
 
 215  
     private void setSeverity(FacesMessage.Severity severity) {
 216  18
         if (severity != null) {
 217  18
             if (maxSeverity == null || severity.compareTo(maxSeverity) > 0) {
 218  3
                 maxSeverity = severity;
 219  
             }
 220  
         }
 221  18
     }
 222  
 
 223  
     private void assertNotReleased() {
 224  30
         if (released) {
 225  3
             throw new IllegalStateException("already released");
 226  
         }
 227  27
     }
 228  
 
 229  
 }