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  
20  package javax.faces.component;
21  
22  import javax.faces.FacesException;
23  import javax.faces.context.FacesContext;
24  import javax.faces.el.EvaluationException;
25  import javax.faces.el.MethodBinding;
26  import javax.faces.event.AbortProcessingException;
27  import javax.faces.event.FacesEvent;
28  
29  /**
30   * Common base class for converting a MethodBinding to a FacesListener
31   */
32  abstract class _MethodBindingToListener implements StateHolder
33  {
34  
35      protected MethodBinding methodBinding;
36  
37      public _MethodBindingToListener()
38      {
39      }
40  
41      /**
42       * Creates a new instance of MethodBindingToListener
43       */
44      public _MethodBindingToListener(MethodBinding methodBinding)
45      {
46          if (methodBinding == null)
47          {
48              throw new NullPointerException("methodBinding can not be null");
49          }
50          if (!(methodBinding instanceof StateHolder))
51          {
52              throw new IllegalArgumentException("methodBinding must implement the StateHolder interface");
53          }
54  
55          this.methodBinding = methodBinding;
56      }
57  
58      private FacesContext getFacesContext()
59      {
60          return FacesContext.getCurrentInstance();
61      }
62  
63      protected void invokeMethodBinding(FacesEvent event) throws AbortProcessingException
64      {
65          try
66          {
67              methodBinding.invoke(getFacesContext(), new Object[]{event});
68          }
69          catch (EvaluationException e)
70          {
71              Throwable cause = e.getCause();
72              if (cause != null && cause instanceof AbortProcessingException)
73              {
74                  throw (AbortProcessingException) cause;
75              }
76  
77              throw e;
78          }
79      }
80  
81      public MethodBinding getMethodBinding()
82      {
83          return methodBinding;
84      }
85  
86      public void restoreState(FacesContext context, Object state)
87      {
88          Object[] stateArray = (Object[]) state;
89          try
90          {
91              methodBinding = (MethodBinding) _ClassUtils.getContextClassLoader()
92                      .loadClass((String) stateArray[0])
93                      .newInstance();
94          }
95          catch (Exception e)
96          {
97              throw new FacesException(e);
98          }
99  
100         ((StateHolder) methodBinding).restoreState(context, stateArray[1]);
101     }
102 
103     public Object saveState(FacesContext context)
104     {
105         Object[] stateArray = new Object[2];
106         stateArray[0] = methodBinding.getClass().getName();
107         stateArray[1] = ((StateHolder) methodBinding).saveState(context);
108         return stateArray;
109     }
110 
111     public void setTransient(boolean newTransientValue)
112     {
113         ((StateHolder) methodBinding).setTransient(newTransientValue);
114     }
115 
116     public boolean isTransient()
117     {
118         return ((StateHolder) methodBinding).isTransient();
119     }
120 
121 }