1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.seasar.teeda.core.el.impl; |
17 | |
|
18 | |
import java.util.Collections; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.Map; |
21 | |
|
22 | |
import javax.faces.application.Application; |
23 | |
import javax.faces.component.StateHolder; |
24 | |
import javax.faces.context.FacesContext; |
25 | |
import javax.faces.el.ValueBinding; |
26 | |
|
27 | |
import org.seasar.framework.exception.EmptyRuntimeException; |
28 | |
import org.seasar.framework.util.StringUtil; |
29 | |
import org.seasar.teeda.core.el.ELParser; |
30 | |
import org.seasar.teeda.core.el.ValueBindingFactory; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
public class ValueBindingFactoryImpl implements ValueBindingFactory { |
36 | |
|
37 | 3 | private Map cache = Collections.synchronizedMap(new HashMap()); |
38 | |
|
39 | |
private ELParser parser; |
40 | |
|
41 | 3 | public ValueBindingFactoryImpl() { |
42 | 3 | this.parser = ELParserFactory.createELParser(); |
43 | 3 | } |
44 | |
|
45 | |
public void setELParser(ELParser parser) { |
46 | 2 | this.parser = parser; |
47 | 2 | } |
48 | |
|
49 | |
public ELParser getELParser() { |
50 | 2 | return parser; |
51 | |
} |
52 | |
|
53 | |
public ValueBinding createValueBinding(Application application, |
54 | |
String expression) { |
55 | 2 | if (StringUtil.isEmpty(expression)) { |
56 | 0 | throw new EmptyRuntimeException("Expression string"); |
57 | |
} |
58 | 2 | if (application == null) { |
59 | 0 | throw new EmptyRuntimeException("Application"); |
60 | |
} |
61 | 2 | ValueBinding vb = getValueBindingFromCache(application, expression); |
62 | 2 | if (vb != null) { |
63 | 0 | return vb; |
64 | |
} |
65 | 2 | vb = new ValueBindingImpl(application, expression, getELParser()); |
66 | 2 | cache.put(expression, vb); |
67 | 2 | return vb; |
68 | |
} |
69 | |
|
70 | |
public void clearCache() { |
71 | 0 | cache.clear(); |
72 | 0 | } |
73 | |
|
74 | |
protected ValueBinding getRestoredValueBinding(StateHolder holder, |
75 | |
String expression) { |
76 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
77 | 0 | holder |
78 | |
.restoreState(context, |
79 | |
new Object[] { expression, getELParser() }); |
80 | 0 | return (ValueBinding) holder; |
81 | |
} |
82 | |
|
83 | |
protected ValueBinding getValueBindingFromCache(Application application, |
84 | |
String expression) { |
85 | 2 | ValueBinding vb = (ValueBinding) cache.get(expression); |
86 | 2 | if (vb != null && vb instanceof StateHolder) { |
87 | 0 | vb = getRestoredValueBinding((StateHolder) vb, expression); |
88 | |
} |
89 | 2 | return vb; |
90 | |
} |
91 | |
} |