KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > custom > aliasbean > AliasBean


1 /*
2  * Copyright 2004 The Apache Software Foundation.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.myfaces.custom.aliasbean;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.UIComponentBase;
26 import javax.faces.context.FacesContext;
27 import javax.faces.el.ValueBinding;
28 import javax.faces.event.AbortProcessingException;
29 import javax.faces.event.FacesEvent;
30 import javax.faces.event.FacesListener;
31 import javax.faces.event.PhaseId;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35
36 /**
37  * The aliasBean tag allows you to link a fictive bean to a real bean.
38  *
39  * Let's suppose you have a subform you use often but with different beans.
40  * <br/>The aliasBean allows you to design the subform with a fictive bean and
41  * to include it in all the pages where you use it. You just need to make an
42  * alias to the real bean named after the fictive bean before invoking the
43  * fictive bean. <br/>This making it possible to have a library of reusable
44  * generic subforms.
45  *
46  * @author Sylvain Vieujot (latest modification by $Author: svieujot $)
47  * @version $Revision: 1.10 $ $Date: 2005/03/15 00:34:17 $
48  * $Log: AliasBean.java,v $
49  * Revision 1.10 2005/03/15 00:34:17 svieujot
50  * Close MYFACES-128, thanks to Mathias Broekelmann.
51  *
52  * Revision 1.9 2005/03/10 22:42:26 svieujot
53  * Close MYFACES-125 thanks to Mathias Broekelmann.
54  *
55  * Revision 1.8 2005/03/10 15:11:00 svieujot
56  * Fix MYFACES-125 thanks to Mathias Broekelmann.
57  *
58  * Revision 1.7 2005/01/27 16:00:30 svieujot
59  * *** empty log message ***
60  *
61  * Revision 1.6 2005/01/27 01:59:45 svieujot
62  * AliasBean : Change sourceBean attribute for value.
63  * Make it work with both beans references ( #{myBean} ), and fix strings as value.
64  * Document tld.
65  *
66  * Revision 1.5 2005/01/04 15:41:06 svieujot
67  * new x:buffer component.
68  *
69  * Revision 1.4 2004/11/23 11:03:35 svieujot
70  * Get ride of the x:aliasBean "permanent" attribute.
71  *
72  * Revision 1.3 2004/11/23 04:46:40 svieujot Add an ugly "permanent"
73  * tag to x:aliasBean to handle children events.
74  *
75  * Revision 1.2 2004/11/14 15:06:36 svieujot Improve AliasBean to make the alias
76  * effective only within the tag body
77  *
78  * Revision 1.1 2004/11/08 20:43:15 svieujot Add an x:aliasBean component
79  *
80  */

81 public class AliasBean extends UIComponentBase {
82     private static final Log log = LogFactory.getLog(AliasBean.class);
83
84     public static final String JavaDoc COMPONENT_TYPE = "org.apache.myfaces.AliasBean";
85     public static final String JavaDoc COMPONENT_FAMILY = "javax.faces.Data";
86     private static final String JavaDoc DEFAULT_RENDERER_TYPE = "org.apache.myfaces.AliasBean";
87
88     private String JavaDoc _valueExpression = null;
89
90     private String JavaDoc _aliasBeanExpression = null;
91
92     private transient FacesContext _context = null;
93
94     public AliasBean() {
95         setRendererType(DEFAULT_RENDERER_TYPE);
96     }
97
98     public String JavaDoc getFamily() {
99         return COMPONENT_FAMILY;
100     }
101
102     public void setAlias(String JavaDoc aliasBeanExpression){
103         this._aliasBeanExpression = aliasBeanExpression;
104     }
105     
106     public String JavaDoc getValue(){
107         if (_valueExpression != null)
108             return _valueExpression;
109         ValueBinding vb = getValueBinding("value");
110         return vb != null ? (String JavaDoc)vb.getValue(getFacesContext()) : null;
111     }
112     public void setValue(String JavaDoc valueExpression){
113         this._valueExpression = valueExpression;
114     }
115
116     public Object JavaDoc saveState(FacesContext context) {
117         log.debug("saveState");
118
119         _context = context;
120
121         Object JavaDoc values[] = new Object JavaDoc[3];
122         values[0] = super.saveState(context);
123         values[1] = _valueExpression;
124         values[2] = _aliasBeanExpression;
125         return values;
126     }
127
128     public void restoreState(FacesContext context, Object JavaDoc state) {
129         log.debug("restoreState");
130
131         _context = context;
132
133         Object JavaDoc values[] = (Object JavaDoc[]) state;
134         super.restoreState(context, values[0]);
135         _valueExpression = (String JavaDoc) values[1];
136         _aliasBeanExpression = (String JavaDoc) values[2];
137     }
138
139     public Object JavaDoc processSaveState(FacesContext context) {
140         if (context == null)
141             throw new NullPointerException JavaDoc("context");
142         if (isTransient())
143             return null;
144         
145         makeAlias(context);
146         
147         Map JavaDoc facetMap = null;
148         for (Iterator JavaDoc it = getFacets().entrySet().iterator(); it.hasNext();) {
149             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
150             if (facetMap == null)
151                 facetMap = new HashMap JavaDoc();
152             UIComponent component = (UIComponent) entry.getValue();
153             if (!component.isTransient()) {
154                 facetMap.put(entry.getKey(), component.processSaveState(context));
155             }
156         }
157         List JavaDoc childrenList = null;
158         if (getChildCount() > 0) {
159             for (Iterator JavaDoc it = getChildren().iterator(); it.hasNext();) {
160                 UIComponent child = (UIComponent) it.next();
161                 if (!child.isTransient()) {
162                     if (childrenList == null)
163                         childrenList = new ArrayList JavaDoc(getChildCount());
164                     childrenList.add(child.processSaveState(context));
165                 }
166             }
167         }
168         
169         removeAlias(context);
170         
171         return new Object JavaDoc[] { saveState(context), facetMap, childrenList };
172     }
173
174     public void processRestoreState(FacesContext context, Object JavaDoc state) {
175         if (context == null)
176             throw new NullPointerException JavaDoc("context");
177         Object JavaDoc myState = ((Object JavaDoc[]) state)[0];
178
179         restoreState(context, myState);
180         makeAlias(context);
181
182         Map JavaDoc facetMap = (Map JavaDoc) ((Object JavaDoc[]) state)[1];
183         List JavaDoc childrenList = (List JavaDoc) ((Object JavaDoc[]) state)[2];
184         for (Iterator JavaDoc it = getFacets().entrySet().iterator(); it.hasNext();) {
185             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
186             Object JavaDoc facetState = facetMap.get(entry.getKey());
187             if (facetState != null) {
188                 ((UIComponent) entry.getValue()).processRestoreState(context, facetState);
189             } else {
190                 context.getExternalContext().log("No state found to restore facet " + entry.getKey());
191             }
192         }
193         if (getChildCount() > 0) {
194             int idx = 0;
195             for (Iterator JavaDoc it = getChildren().iterator(); it.hasNext();) {
196                 UIComponent child = (UIComponent) it.next();
197                 Object JavaDoc childState = childrenList.get(idx++);
198                 if (childState != null) {
199                     child.processRestoreState(context, childState);
200                 } else {
201                     context.getExternalContext().log("No state found to restore child of component " + getId());
202                 }
203             }
204         }
205
206         removeAlias(context);
207     }
208
209     public void processValidators(FacesContext context) {
210         log.debug("processValidators");
211         makeAlias(context);
212         super.processValidators(context);
213         removeAlias(context);
214     }
215
216     public void processDecodes(FacesContext context) {
217         log.debug("processDecodes");
218         makeAlias(context);
219         super.processDecodes(context);
220         removeAlias(context);
221     }
222     
223     public void processUpdates(FacesContext context) {
224         log.debug("processUpdates");
225         makeAlias(context);
226         super.processUpdates(context);
227         removeAlias(context);
228     }
229
230     public void queueEvent(FacesEvent event) {
231         super.queueEvent(new FacesEventWrapper(event, this));
232     }
233
234     public void broadcast(FacesEvent event) throws AbortProcessingException {
235         makeAlias();
236
237         if (event instanceof FacesEventWrapper) {
238             FacesEvent originalEvent = ((FacesEventWrapper) event).getWrappedFacesEvent();
239             originalEvent.getComponent().broadcast(originalEvent);
240         } else {
241             super.broadcast(event);
242         }
243
244         removeAlias();
245     }
246
247     void makeAlias(FacesContext context) {
248         _context = context;
249         makeAlias();
250     }
251
252     private void makeAlias() {
253         // First, compute the value or reference
254
Object JavaDoc value;
255         
256         ValueBinding valueVB = null;
257         if (_valueExpression == null) {
258             valueVB = getValueBinding("value");
259             _valueExpression = valueVB.getExpressionString();
260         }
261
262         if( valueVB == null ){
263             if( _valueExpression.startsWith("#{") ){
264                 valueVB = _context.getApplication().createValueBinding(_valueExpression);
265                 value = valueVB.getValue(_context);
266             }else{
267                 value = _valueExpression;
268             }
269         }else{
270             value = valueVB.getValue(_context);
271         }
272
273         // Then set the alias to this value
274
ValueBinding aliasVB;
275         if (_aliasBeanExpression == null) {
276             aliasVB = getValueBinding("alias");
277             _aliasBeanExpression = aliasVB.getExpressionString();
278         } else {
279             aliasVB = _context.getApplication().createValueBinding(_aliasBeanExpression);
280         }
281
282         aliasVB.setValue(_context, value);
283
284         log.debug("makeAlias: " + _valueExpression + " = " + _aliasBeanExpression);
285     }
286
287     void removeAlias(FacesContext context) {
288         _context = context;
289         removeAlias();
290     }
291
292     private void removeAlias() {
293         log.debug("removeAlias: " + _valueExpression + " != " + _aliasBeanExpression);
294
295         ValueBinding aliasVB = getValueBinding("alias");
296         aliasVB.setValue(_context, null);
297     }
298
299     private static class FacesEventWrapper extends FacesEvent {
300         private FacesEvent _wrappedFacesEvent;
301
302         public FacesEventWrapper(FacesEvent facesEvent, AliasBean redirectComponent) {
303             super(redirectComponent);
304             _wrappedFacesEvent = facesEvent;
305         }
306
307         public PhaseId getPhaseId() {
308             return _wrappedFacesEvent.getPhaseId();
309         }
310
311         public void setPhaseId(PhaseId phaseId) {
312             _wrappedFacesEvent.setPhaseId(phaseId);
313         }
314
315         public void queue() {
316             _wrappedFacesEvent.queue();
317         }
318
319         public String JavaDoc toString() {
320             return _wrappedFacesEvent.toString();
321         }
322
323         public boolean isAppropriateListener(FacesListener faceslistener) {
324             return _wrappedFacesEvent.isAppropriateListener(faceslistener);
325         }
326
327         public void processListener(FacesListener faceslistener) {
328             _wrappedFacesEvent.processListener(faceslistener);
329         }
330
331         public FacesEvent getWrappedFacesEvent() {
332             return _wrappedFacesEvent;
333         }
334     }
335 }
Popular Tags