1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
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 | |
|
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 | |
} |