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.orchestra.flow.config;
20  
21  import java.io.Serializable;
22  
23  import javax.el.MethodExpression;
24  import javax.faces.context.FacesContext;
25  
26  import org.apache.myfaces.orchestra.flow.utils._ExpressionFactory;
27  import org.apache.myfaces.orchestra.lib.OrchestraException;
28  
29  
30  /**
31   * Defines an action to execute in the caller environment after
32   * a flow has committed.
33   * <p>
34   * The invoked method is effectively running at the end of a postback cycle.
35   * <p>
36   * This option gives the caller a chance to process returned values.
37   * <p>
38   * Normally, the EL expression will map to a method with void return type.
39   * However if the EL expression invokes a method that returns a String, then
40   * that value is used as a navigation outcome; this allows a calling page to
41   * cause some other page to be rendered on completion of a flow. And as the
42   * values returned from the flow are available, the navigation can be dynamic
43   * depending upon those return values.
44   */
45  public class FlowOnCommit implements Serializable
46  {
47      // For serialization. IMPORTANT; update this when changing the
48      // binary format of this class (eg adding fields).
49      private static final long serialVersionUID = 1L;
50  
51      private String action;
52      private MethodExpression actionExpr;
53      
54      /** Constructor. */
55      public FlowOnCommit()
56      {
57      }
58  
59      /**
60       * Check that all the properties of this object have valid values, ie
61       * whether the configuration specified by the user is valid.
62       */
63      public void validate()
64      {
65          if (action == null)
66          {
67              throw new OrchestraException("action is null");
68          }
69      }
70  
71      /**
72       * An EL expression which defines what method to call.
73       * <p>
74       * Null is never returned.
75       */
76      public String getAction()
77      {
78          return action;
79      }
80  
81      /** For use only during object initialization. */
82      public void setAction(String action)
83      {
84          this.action = action;
85          
86  
87          FacesContext fc = FacesContext.getCurrentInstance();
88          actionExpr = _ExpressionFactory.createMethodExpression(fc, action);
89      }
90  
91      /**
92       * Execute the associated action, and return whatever the called method returns.
93       */
94      public Object execute(FacesContext facesContext)
95      {
96          Object o = actionExpr.invoke(facesContext.getELContext(), null);
97          return o;
98      }
99  }