1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.tag.composite;
20
21 import java.beans.BeanDescriptor;
22 import java.beans.BeanInfo;
23 import java.io.IOException;
24 import java.util.Collection;
25
26 import javax.faces.component.UIComponent;
27 import javax.faces.view.facelets.FaceletContext;
28 import javax.faces.view.facelets.FaceletHandler;
29
30 import org.apache.myfaces.view.facelets.AbstractFaceletContext;
31 import org.apache.myfaces.view.facelets.FaceletCompositionContext;
32 import org.apache.myfaces.view.facelets.el.CompositeComponentELUtils;
33 import org.apache.myfaces.view.facelets.tag.TagHandlerUtils;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public final class CompositeComponentDefinitionTagHandler implements FaceletHandler
54 {
55 private final FaceletHandler _nextHandler;
56
57 private boolean _cacheable;
58
59
60
61
62
63
64
65
66 private BeanInfo _cachedBeanInfo;
67
68 private InterfaceHandler _interfaceHandler;
69
70 private ImplementationHandler _implementationHandler;
71
72 public CompositeComponentDefinitionTagHandler(FaceletHandler next)
73 {
74 this._nextHandler = next;
75
76 _cacheable = true;
77
78 _interfaceHandler = TagHandlerUtils.findFirstNextByType(_nextHandler, InterfaceHandler.class);
79
80 _implementationHandler = TagHandlerUtils.findFirstNextByType(_nextHandler, ImplementationHandler.class);
81
82 Collection<InterfaceDescriptorCreator> metadataInterfaceHandlerList =
83 TagHandlerUtils.findNextByType( _nextHandler, InterfaceDescriptorCreator.class);
84
85 for (InterfaceDescriptorCreator handler : metadataInterfaceHandlerList)
86 {
87 if (!handler.isCacheable())
88 {
89 _cacheable = false;
90 break;
91 }
92 }
93 if (!_cacheable)
94 {
95 for (InterfaceDescriptorCreator handler : metadataInterfaceHandlerList)
96 {
97 handler.setCacheable(false);
98 }
99 }
100 }
101
102 public void apply(FaceletContext ctx, UIComponent parent)
103 throws IOException
104 {
105 FaceletCompositionContext mctx = FaceletCompositionContext.getCurrentInstance(ctx);
106 AbstractFaceletContext actx = (AbstractFaceletContext)ctx;
107 UIComponent compositeBaseParent = actx.isBuildingCompositeComponentMetadata() ? parent : parent.getParent();
108
109
110
111 if (_interfaceHandler != null)
112 {
113 compositeBaseParent.getAttributes()
114 .put(CompositeComponentELUtils.LOCATION_KEY, this._interfaceHandler.getLocation());
115 }
116 else if (_implementationHandler != null)
117 {
118 compositeBaseParent.getAttributes()
119 .put(CompositeComponentELUtils.LOCATION_KEY, this._implementationHandler.getLocation());
120 }
121
122
123
124 if ( actx.isBuildingCompositeComponentMetadata() )
125 {
126 CompositeComponentBeanInfo tempBeanInfo =
127 (CompositeComponentBeanInfo) compositeBaseParent.getAttributes()
128 .get(UIComponent.BEANINFO_KEY);
129
130 if (tempBeanInfo == null)
131 {
132 if (_cacheable)
133 {
134 if (_cachedBeanInfo == null)
135 {
136 _cachedBeanInfo = _createCompositeComponentMetadata(ctx, compositeBaseParent);
137 compositeBaseParent.getAttributes().put(
138 UIComponent.BEANINFO_KEY, _cachedBeanInfo);
139
140 try
141 {
142 mctx.pushCompositeComponentToStack(compositeBaseParent);
143
144 _nextHandler.apply(ctx, parent);
145 }
146 finally
147 {
148 mctx.popCompositeComponentToStack();
149 }
150 }
151 else
152 {
153
154
155 compositeBaseParent.getAttributes().put(
156 UIComponent.BEANINFO_KEY, _cachedBeanInfo);
157 }
158 }
159 else
160 {
161 tempBeanInfo = _createCompositeComponentMetadata(ctx, compositeBaseParent);
162 compositeBaseParent.getAttributes().put(
163 UIComponent.BEANINFO_KEY, tempBeanInfo);
164
165 try
166 {
167 mctx.pushCompositeComponentToStack(compositeBaseParent);
168
169 _nextHandler.apply(ctx, parent);
170
171 }
172 finally
173 {
174 mctx.popCompositeComponentToStack();
175 }
176 }
177 }
178 }
179 else
180 {
181 try
182 {
183 mctx.pushCompositeComponentToStack(compositeBaseParent);
184
185 _nextHandler.apply(ctx, parent);
186 }
187 finally
188 {
189 mctx.popCompositeComponentToStack();
190 }
191 }
192 }
193
194 private CompositeComponentBeanInfo _createCompositeComponentMetadata(
195 FaceletContext ctx, UIComponent parent)
196 {
197 BeanDescriptor descriptor = new BeanDescriptor(parent.getClass());
198 CompositeComponentBeanInfo beanInfo = new CompositeComponentBeanInfo(descriptor);
199 return beanInfo;
200 }
201 }