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