001: /*
002: * Copyright 2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package javax.faces.component;
018:
019: import javax.faces.FacesException;
020: import javax.faces.component.StateHolder;
021: import javax.faces.context.FacesContext;
022: import javax.faces.el.EvaluationException;
023: import javax.faces.el.MethodBinding;
024: import javax.faces.event.AbortProcessingException;
025: import javax.faces.event.FacesEvent;
026:
027: /**
028: * Common base class for converting a MethodBinding to a FacesListener
029: *
030: * @author Stan Silvert
031: */
032: abstract class _MethodBindingToListener implements StateHolder {
033:
034: protected MethodBinding methodBinding;
035:
036: public _MethodBindingToListener() {
037: }
038:
039: /**
040: * Creates a new instance of MethodBindingToListener
041: */
042: public _MethodBindingToListener(MethodBinding methodBinding) {
043: if (methodBinding == null)
044: throw new NullPointerException(
045: "methodBinding can not be null");
046: if (!(methodBinding instanceof StateHolder))
047: throw new IllegalArgumentException(
048: "methodBinding must implement the StateHolder interface");
049:
050: this .methodBinding = methodBinding;
051: }
052:
053: private FacesContext getFacesContext() {
054: return FacesContext.getCurrentInstance();
055: }
056:
057: protected void invokeMethodBinding(FacesEvent event)
058: throws AbortProcessingException {
059: try {
060: methodBinding.invoke(getFacesContext(),
061: new Object[] { event });
062: } catch (EvaluationException e) {
063: Throwable cause = e.getCause();
064: if (cause != null
065: && cause instanceof AbortProcessingException) {
066: throw (AbortProcessingException) cause;
067: }
068:
069: throw e;
070: }
071: }
072:
073: public MethodBinding getMethodBinding() {
074: return methodBinding;
075: }
076:
077: public void restoreState(FacesContext context, Object state) {
078: Object[] stateArray = (Object[]) state;
079: try {
080: methodBinding = (MethodBinding) Thread.currentThread()
081: .getContextClassLoader().loadClass(
082: (String) stateArray[0]).newInstance();
083: } catch (Exception e) {
084: throw new FacesException(e);
085: }
086:
087: ((StateHolder) methodBinding).restoreState(context,
088: stateArray[1]);
089: }
090:
091: public Object saveState(FacesContext context) {
092: Object[] stateArray = new Object[2];
093: stateArray[0] = methodBinding.getClass().getName();
094: stateArray[1] = ((StateHolder) methodBinding)
095: .saveState(context);
096: return stateArray;
097: }
098:
099: public void setTransient(boolean newTransientValue) {
100: ((StateHolder) methodBinding).setTransient(newTransientValue);
101: }
102:
103: public boolean isTransient() {
104: return ((StateHolder) methodBinding).isTransient();
105: }
106:
107: }
|