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.el.ValueExpression;
22  import javax.faces.component.UIComponent;
23  import javax.faces.context.FacesContext;
24  import javax.faces.el.ValueBinding;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  
29  /**
30   * A helper bean used by both AliasBean and AliasBeansScope components.
31   * <p>
32   * An Alias instance represents a single mapping from a "temporary" bean name
33   * to the real bean that temporary name should reference. When this alias
34   * is "activated" the temporary name is registered and when the alias is
35   * "deactivated" the temporary name is removed.
36   *
37   * @author Sylvain Vieujot (latest modification by $Author: skitching $)
38   * @version $Revision: 673833 $ $Date: 2008-07-03 16:58:05 -0500 (Thu, 03 Jul 2008) $
39   */
40  
41  class Alias {
42      static final Log log = LogFactory.getLog(Alias.class);
43      
44      private transient UIComponent _aliasComponent;
45      private String _aliasBeanExpression;
46      private String _valueExpression;
47      private transient boolean _active = false;
48      
49      private transient Object evaluatedExpression = null;
50  
51      Alias(AliasBean aliasComponent){
52          this._aliasComponent = aliasComponent;
53      }
54  
55      /**
56       * Define the temporary/transient name that will exist while this alias
57       * is "active" (in scope). This is usually a constant string.
58       */
59      void setAliasBeanExpression(String aliasBeanExpression){
60          this._aliasBeanExpression = aliasBeanExpression;
61      }
62      
63      /**
64       * Define the object that will be referenced by the temporary/transient
65       * name while it exists.
66       * <p>
67       * This can be a constant, but is more usually an EL expression. The value is
68       * recalculated each time this alias is "activated".
69       */
70      void setValueExpression(String valueExpression){
71          this._valueExpression = valueExpression;
72      }
73      
74      String getValueExpression(){
75          return _valueExpression;
76      }
77      
78      boolean isActive(){
79          return _active;
80      }
81      
82      String[] saveState(){
83          return new String[]{_aliasBeanExpression, _valueExpression};
84      }
85      
86      void restoreState(Object state){
87          String[] values = (String[]) state;
88          _aliasBeanExpression = values[0];
89          _valueExpression = values[1];
90      }
91      
92      private void computeEvaluatedExpression(FacesContext facesContext){
93          if( evaluatedExpression != null )
94              return;
95          
96          ValueExpression valueVB = null;
97          if (_valueExpression == null) {
98              valueVB = _aliasComponent.getValueExpression("value");
99              _valueExpression = valueVB.getExpressionString();
100         }
101 
102         if( valueVB == null ){
103             if( _valueExpression.startsWith("#{") ){
104                 valueVB = facesContext.getApplication().getExpressionFactory()
105                     .createValueExpression(facesContext.getELContext(),                            
106                             _valueExpression,
107                             Object.class);
108                 evaluatedExpression = valueVB.getValue(facesContext.getELContext());
109             }else{
110                 evaluatedExpression = _valueExpression;
111             }
112         }else{
113             evaluatedExpression = valueVB.getValue(facesContext.getELContext());
114         }
115     }
116 
117     /**
118      * Activate this alias (ie create the temporary name).
119      */
120     void make(FacesContext facesContext){
121         if( _active )
122             return;
123 
124         ValueExpression aliasVB;
125         if (_aliasBeanExpression == null) {
126             aliasVB = _aliasComponent.getValueExpression("alias");
127             if( aliasVB == null )
128                 return;
129             _aliasBeanExpression = aliasVB.getExpressionString();
130             if( _aliasBeanExpression == null )
131                 return;
132         } else {
133             aliasVB = facesContext.getApplication().getExpressionFactory().
134                 createValueExpression(facesContext.getELContext(),_aliasBeanExpression,Object.class);
135         }
136 
137         computeEvaluatedExpression( facesContext );
138         
139         aliasVB.setValue(facesContext.getELContext(), evaluatedExpression);
140         _active = true;
141 
142         log.debug("makeAlias: " + _valueExpression + " = " + _aliasBeanExpression);
143     }
144     
145     /**
146      * Deactivate this alias (ie remove the temporary name).
147      */
148     void remove(FacesContext facesContext){
149         _active = false;
150         if( evaluatedExpression == null )
151             return;
152         
153         evaluatedExpression = null;
154 
155         log.debug("removeAlias: " + _valueExpression + " != " + _aliasBeanExpression);
156         ValueExpression aliasVB = _aliasComponent.getValueExpression("alias");
157         if( aliasVB != null )
158             aliasVB.setValue(facesContext.getELContext(), null);
159     }
160 }