View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.custom.aliasbean;
20  
21  import javax.faces.component.UIComponent;
22  import javax.faces.context.FacesContext;
23  import javax.faces.el.ValueBinding;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /**
29   * A helper bean used by both AliasBean and AliasBeansScope components.
30   * <p>
31   * An Alias instance represents a single mapping from a "temporary" bean name
32   * to the real bean that temporary name should reference. When this alias
33   * is "activated" the temporary name is registered and when the alias is
34   * "deactivated" the temporary name is removed.
35   *
36   * @author Sylvain Vieujot (latest modification by $Author: skitching $)
37   * @version $Revision: 673833 $ $Date: 2008-07-03 16:58:05 -0500 (Thu, 03 Jul 2008) $
38   */
39  
40  class Alias {
41      static final Log log = LogFactory.getLog(Alias.class);
42      
43      private transient UIComponent _aliasComponent;
44      private String _aliasBeanExpression;
45      private String _valueExpression;
46      private transient boolean _active = false;
47      
48      private transient Object evaluatedExpression = null;
49  
50      Alias(AliasBean aliasComponent){
51          this._aliasComponent = aliasComponent;
52      }
53  
54      /**
55       * Define the temporary/transient name that will exist while this alias
56       * is "active" (in scope). This is usually a constant string.
57       */
58      void setAliasBeanExpression(String aliasBeanExpression){
59          this._aliasBeanExpression = aliasBeanExpression;
60      }
61      
62      /**
63       * Define the object that will be referenced by the temporary/transient
64       * name while it exists.
65       * <p>
66       * This can be a constant, but is more usually an EL expression. The value is
67       * recalculated each time this alias is "activated".
68       */
69      void setValueExpression(String valueExpression){
70          this._valueExpression = valueExpression;
71      }
72      
73      String getValueExpression(){
74          return _valueExpression;
75      }
76      
77      boolean isActive(){
78          return _active;
79      }
80      
81      String[] saveState(){
82          return new String[]{_aliasBeanExpression, _valueExpression};
83      }
84      
85      void restoreState(Object state){
86          String[] values = (String[]) state;
87          _aliasBeanExpression = values[0];
88          _valueExpression = values[1];
89      }
90      
91      private void computeEvaluatedExpression(FacesContext facesContext){
92          if( evaluatedExpression != null )
93              return;
94          
95          ValueBinding valueVB = null;
96          if (_valueExpression == null) {
97              valueVB = _aliasComponent.getValueBinding("value");
98              _valueExpression = valueVB.getExpressionString();
99          }
100 
101         if( valueVB == null ){
102             if( _valueExpression.startsWith("#{") ){
103                 valueVB = facesContext.getApplication().createValueBinding(_valueExpression);
104                 evaluatedExpression = valueVB.getValue(facesContext);
105             }else{
106                 evaluatedExpression = _valueExpression;
107             }
108         }else{
109             evaluatedExpression = valueVB.getValue(facesContext);
110         }
111     }
112 
113     /**
114      * Activate this alias (ie create the temporary name).
115      */
116     void make(FacesContext facesContext){
117         if( _active )
118             return;
119 
120         ValueBinding aliasVB;
121         if (_aliasBeanExpression == null) {
122             aliasVB = _aliasComponent.getValueBinding("alias");
123             if( aliasVB == null )
124                 return;
125             _aliasBeanExpression = aliasVB.getExpressionString();
126             if( _aliasBeanExpression == null )
127                 return;
128         } else {
129             aliasVB = facesContext.getApplication().createValueBinding(_aliasBeanExpression);
130         }
131 
132         computeEvaluatedExpression( facesContext );
133         
134         aliasVB.setValue(facesContext, evaluatedExpression);
135         _active = true;
136 
137         log.debug("makeAlias: " + _valueExpression + " = " + _aliasBeanExpression);
138     }
139     
140     /**
141      * Deactivate this alias (ie remove the temporary name).
142      */
143     void remove(FacesContext facesContext){
144         _active = false;
145         if( evaluatedExpression == null )
146             return;
147         
148         evaluatedExpression = null;
149 
150         log.debug("removeAlias: " + _valueExpression + " != " + _aliasBeanExpression);
151         ValueBinding aliasVB = _aliasComponent.getValueBinding("alias");
152         if( aliasVB != null )
153             aliasVB.setValue(facesContext, null);
154     }
155 }