1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.services.impl; |
16 |
|
|
17 |
|
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; |
18 |
|
import org.apache.hivemind.Resource; |
19 |
|
import org.apache.hivemind.lib.chain.ChainBuilder; |
20 |
|
import org.apache.tapestry.IComponent; |
21 |
|
import org.apache.tapestry.INamespace; |
22 |
|
import org.apache.tapestry.engine.IPropertySource; |
23 |
|
import org.apache.tapestry.event.ResetEventListener; |
24 |
|
import org.apache.tapestry.services.ComponentPropertySource; |
25 |
|
import org.apache.tapestry.spec.IComponentSpecification; |
26 |
|
import org.apache.tapestry.util.PropertyHolderPropertySource; |
27 |
|
|
28 |
|
import java.util.ArrayList; |
29 |
|
import java.util.List; |
30 |
|
import java.util.Locale; |
31 |
|
import java.util.Map; |
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
0 |
public class ComponentPropertySourceImpl implements ComponentPropertySource, ResetEventListener |
42 |
|
{ |
43 |
|
private IPropertySource _globalProperties; |
44 |
|
|
45 |
|
private ChainBuilder _chainBuilder; |
46 |
|
|
47 |
0 |
private Map _componentSources = new ConcurrentHashMap(); |
48 |
|
|
49 |
0 |
private Map _localizedComponentSources = new ConcurrentHashMap(); |
50 |
|
|
51 |
0 |
private Map _namespaceSources = new ConcurrentHashMap(); |
52 |
|
|
53 |
0 |
private Map _localizedNamespaceSources = new ConcurrentHashMap(); |
54 |
|
|
55 |
|
public void resetEventDidOccur() |
56 |
|
{ |
57 |
0 |
_componentSources.clear(); |
58 |
0 |
_localizedComponentSources.clear(); |
59 |
0 |
_namespaceSources.clear(); |
60 |
0 |
_localizedNamespaceSources.clear(); |
61 |
0 |
} |
62 |
|
|
63 |
|
private IPropertySource getSourceForNamespace(INamespace namespace) |
64 |
|
{ |
65 |
0 |
Resource key = namespace.getSpecificationLocation(); |
66 |
|
|
67 |
0 |
IPropertySource result = (IPropertySource) _namespaceSources.get(key); |
68 |
|
|
69 |
0 |
if (result == null) |
70 |
|
{ |
71 |
0 |
result = createSourceForNamespace(namespace); |
72 |
|
|
73 |
0 |
_namespaceSources.put(key, result); |
74 |
|
} |
75 |
|
|
76 |
0 |
return result; |
77 |
|
} |
78 |
|
|
79 |
|
private IPropertySource getSourceForComponent(IComponent component) |
80 |
|
{ |
81 |
0 |
Resource key = component.getSpecification().getSpecificationLocation(); |
82 |
|
|
83 |
0 |
IPropertySource result = (IPropertySource) _componentSources.get(key); |
84 |
|
|
85 |
0 |
if (result == null) |
86 |
|
{ |
87 |
0 |
result = createSourceForComponent(component); |
88 |
0 |
_componentSources.put(key, result); |
89 |
|
} |
90 |
|
|
91 |
0 |
return result; |
92 |
|
} |
93 |
|
|
94 |
|
private LocalizedPropertySource getLocalizedSourceForComponent(IComponent component) |
95 |
|
{ |
96 |
0 |
Resource key = component.getSpecification().getSpecificationLocation(); |
97 |
|
|
98 |
0 |
LocalizedPropertySource result = (LocalizedPropertySource) _localizedComponentSources.get(key); |
99 |
|
|
100 |
0 |
if (result == null) |
101 |
|
{ |
102 |
0 |
result = new LocalizedPropertySource(getSourceForComponent(component)); |
103 |
|
|
104 |
0 |
_localizedComponentSources.put(key, result); |
105 |
|
} |
106 |
|
|
107 |
0 |
return result; |
108 |
|
} |
109 |
|
|
110 |
|
private LocalizedPropertySource getLocalizedSourceForNamespace(INamespace namespace) |
111 |
|
{ |
112 |
0 |
Resource key = namespace.getSpecificationLocation(); |
113 |
|
|
114 |
0 |
LocalizedPropertySource result = (LocalizedPropertySource) _localizedNamespaceSources.get(key); |
115 |
|
|
116 |
0 |
if (result == null) |
117 |
|
{ |
118 |
0 |
result = new LocalizedPropertySource(getSourceForNamespace(namespace)); |
119 |
|
|
120 |
0 |
_localizedNamespaceSources.put(key, result); |
121 |
|
} |
122 |
|
|
123 |
0 |
return result; |
124 |
|
} |
125 |
|
|
126 |
|
private IPropertySource createSourceForComponent(IComponent component) |
127 |
|
{ |
128 |
0 |
IComponentSpecification specification = component.getSpecification(); |
129 |
|
|
130 |
0 |
List sources = new ArrayList(); |
131 |
|
|
132 |
0 |
sources.add(new PropertyHolderPropertySource(specification)); |
133 |
0 |
sources.add(getSourceForNamespace(component.getNamespace())); |
134 |
|
|
135 |
0 |
return (IPropertySource) _chainBuilder.buildImplementation( |
136 |
0 |
IPropertySource.class, |
137 |
|
sources, |
138 |
|
ImplMessages.componentPropertySourceDescription(specification)); |
139 |
|
} |
140 |
|
|
141 |
|
private IPropertySource createSourceForNamespace(INamespace namespace) |
142 |
|
{ |
143 |
0 |
List sources = new ArrayList(); |
144 |
|
|
145 |
0 |
sources.add(new PropertyHolderPropertySource(namespace.getSpecification())); |
146 |
0 |
sources.add(_globalProperties); |
147 |
|
|
148 |
0 |
return (IPropertySource) _chainBuilder.buildImplementation(IPropertySource.class, |
149 |
|
sources, |
150 |
|
ImplMessages.namespacePropertySourceDescription(namespace)); |
151 |
|
} |
152 |
|
|
153 |
|
public String getComponentProperty(IComponent component, String propertyName) |
154 |
|
{ |
155 |
0 |
return getSourceForComponent(component).getPropertyValue(propertyName); |
156 |
|
} |
157 |
|
|
158 |
|
public String getLocalizedComponentProperty(IComponent component, Locale locale, String propertyName) |
159 |
|
{ |
160 |
0 |
return getLocalizedSourceForComponent(component).getPropertyValue(propertyName, locale); |
161 |
|
} |
162 |
|
|
163 |
|
public String getNamespaceProperty(INamespace namespace, String propertyName) |
164 |
|
{ |
165 |
0 |
return getSourceForNamespace(namespace).getPropertyValue(propertyName); |
166 |
|
} |
167 |
|
|
168 |
|
public String getLocalizedNamespaceProperty(INamespace namespace, Locale locale, String propertyName) |
169 |
|
{ |
170 |
0 |
return getLocalizedSourceForNamespace(namespace).getPropertyValue(propertyName, locale); |
171 |
|
} |
172 |
|
|
173 |
|
public void setChainBuilder(ChainBuilder chainBuilder) |
174 |
|
{ |
175 |
0 |
_chainBuilder = chainBuilder; |
176 |
0 |
} |
177 |
|
|
178 |
|
public void setGlobalProperties(IPropertySource globalProperties) |
179 |
|
{ |
180 |
0 |
_globalProperties = globalProperties; |
181 |
0 |
} |
182 |
|
} |