Coverage Report - javax.faces.internal.scope.VariableScope
 
Classes in this File Line Coverage Branch Coverage Complexity
VariableScope
80%
35/44
83%
5/6
1.333
 
 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 javax.faces.internal.scope;
 17  
 
 18  
 import java.util.Collections;
 19  
 import java.util.HashMap;
 20  
 import java.util.Map;
 21  
 
 22  
 import javax.faces.FacesException;
 23  
 import javax.faces.context.ExternalContext;
 24  
 import javax.faces.context.FacesContext;
 25  
 import javax.faces.internal.WindowIdUtil;
 26  
 
 27  
 import org.seasar.framework.util.LruHashMap;
 28  
 
 29  
 /**
 30  
  * @author higa
 31  
  *
 32  
  */
 33  
 public class VariableScope {
 34  
 
 35  
     private String key;
 36  
 
 37  3
     private int windowSize = 20;
 38  
 
 39  3
     public VariableScope(String key) {
 40  3
         this.key = key;
 41  3
     }
 42  
 
 43  0
     public VariableScope(String key, int windowSize) {
 44  0
         this.key = key;
 45  0
         this.windowSize = windowSize;
 46  0
     }
 47  
 
 48  
     public Map getContext(FacesContext context) throws FacesException {
 49  2
         ExternalContext extCtx = context.getExternalContext();
 50  2
         Map contexts = getContexts(extCtx);
 51  2
         String wid = WindowIdUtil.getWindowId(extCtx);
 52  2
         return (Map) contexts.get(wid);
 53  
     }
 54  
 
 55  
     public synchronized Map getOrCreateContext(FacesContext context)
 56  
             throws FacesException {
 57  6
         ExternalContext extCtx = context.getExternalContext();
 58  6
         Map contexts = getContexts(extCtx);
 59  6
         String wid = WindowIdUtil.getWindowId(extCtx);
 60  6
         Map ctx = (Map) contexts.get(wid);
 61  6
         if (ctx == null) {
 62  4
             ctx = new HashMap();
 63  4
             contexts.put(wid, ctx);
 64  
         }
 65  6
         return ctx;
 66  
     }
 67  
 
 68  
     public void removeContext(FacesContext context) throws FacesException {
 69  0
         ExternalContext extCtx = context.getExternalContext();
 70  0
         String wid = WindowIdUtil.getWindowId(extCtx);
 71  0
         removeContext(context, wid);
 72  0
     }
 73  
 
 74  
     public void removeContext(FacesContext context, String wid)
 75  
             throws FacesException {
 76  1
         ExternalContext extCtx = context.getExternalContext();
 77  1
         Map contexts = getContexts(extCtx);
 78  1
         contexts.remove(wid);
 79  1
     }
 80  
 
 81  
     public void clearContext(FacesContext context) throws FacesException {
 82  2
         ExternalContext extCtx = context.getExternalContext();
 83  2
         String wid = WindowIdUtil.getWindowId(extCtx);
 84  2
         clearContext(context, wid);
 85  2
     }
 86  
 
 87  
     public void clearContext(FacesContext context, String wid)
 88  
             throws FacesException {
 89  3
         ExternalContext extCtx = context.getExternalContext();
 90  3
         Map contexts = getContexts(extCtx);
 91  3
         Map m = (Map) contexts.get(wid);
 92  3
         if (m != null) {
 93  0
             m.clear();
 94  
         }
 95  3
     }
 96  
 
 97  
     protected synchronized Map getContexts(ExternalContext externalContext)
 98  
             throws FacesException {
 99  12
         Map sessionMap = externalContext.getSessionMap();
 100  12
         Map contexts = (Map) sessionMap.get(key);
 101  12
         if (contexts == null) {
 102  6
             contexts = Collections.synchronizedMap(new LruHashMap(windowSize));
 103  6
             sessionMap.put(key, contexts);
 104  
         }
 105  12
         return contexts;
 106  
     }
 107  
 }