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  
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 (jue, 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          ValueExpression valueVB = null;
96          if (_valueExpression == null) {
97              valueVB = _aliasComponent.getValueExpression("value");
98              _valueExpression = valueVB.getExpressionString();
99          }
100 
101         if( valueVB == null ){
102             if( _valueExpression.startsWith("#{") ){
103                 valueVB = facesContext.getApplication().getExpressionFactory()
104                     .createValueExpression(facesContext.getELContext(),                            
105                             _valueExpression,
106                             Object.class);
107                 evaluatedExpression = valueVB.getValue(facesContext.getELContext());
108             }else{
109                 evaluatedExpression = _valueExpression;
110             }
111         }else{
112             evaluatedExpression = valueVB.getValue(facesContext.getELContext());
113         }
114     }
115 
116     /**
117      * Activate this alias (ie create the temporary name).
118      */
119     void make(FacesContext facesContext){
120         if( _active )
121             return;
122 
123         ValueExpression aliasVB;
124         if (_aliasBeanExpression == null) {
125             aliasVB = _aliasComponent.getValueExpression("alias");
126             if( aliasVB == null )
127                 return;
128             _aliasBeanExpression = aliasVB.getExpressionString();
129             if( _aliasBeanExpression == null )
130                 return;
131         } else {
132             aliasVB = facesContext.getApplication().getExpressionFactory().
133                 createValueExpression(facesContext.getELContext(),_aliasBeanExpression,Object.class);
134         }
135 
136         computeEvaluatedExpression( facesContext );
137         
138         aliasVB.setValue(facesContext.getELContext(), evaluatedExpression);
139         _active = true;
140 
141         log.debug("makeAlias: " + _valueExpression + " = " + _aliasBeanExpression);
142     }
143     
144     /**
145      * Deactivate this alias (ie remove the temporary name).
146      */
147     void remove(FacesContext facesContext){
148         _active = false;
149         if( evaluatedExpression == null )
150             return;
151         
152         evaluatedExpression = null;
153 
154         log.debug("removeAlias: " + _valueExpression + " != " + _aliasBeanExpression);
155         ValueExpression aliasVB = _aliasComponent.getValueExpression("alias");
156         if( aliasVB != null )
157             aliasVB.setValue(facesContext.getELContext(), null);
158     }
159 }