Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright ${year}, Red Hat, Inc. and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.richfaces.event;
 
 import  javax.faces.component.StateHolder;
 import  javax.faces.component.UIComponentBase;
 import  javax.faces.context.FacesContext;
 import  javax.faces.event.AbortProcessingException;
 import  javax.faces.event.FacesEvent;
 import  javax.faces.event.FacesListener;

MethodExpressionEventListener is a FacesListener that wraps a MethodExpression. When it receives a FacesEvent, it executes a method on an object identified by the MethodExpression.

Author(s):
akolonitsky
Version:
1.0
 
 public abstract class MethodExpressionEventListener implements FacesListener, StateHolder {
     private static final Class<?>[] EVENT_LISTENER_ZERO_ARG_SIG = new Class[] {};
     private static final Object[] NO_PARAMS = new Object[0];
 
     // ------------------------------------------------------ Instance Variables
     private MethodExpression methodExpressionOneArg = null;
     private MethodExpression methodExpressionZeroArg = null;
     private boolean isTransient;
 
     protected MethodExpressionEventListener() {
     }

    

Construct a FacesListener that contains a MethodExpression .To accomodate method expression targets that take no arguments instead of taking a FacesEvent argument, the implementation of this class must take the argument methodExpressionOneArg, extract its expression string, and create another MethodExpression whose expected param types match those of a zero argument method. The usage requirements for both of these MethodExpression instances are described in processEvent.

Parameters:
methodExpressionOneArg a MethodExpression that points to a method that returns void and takes a single argument of type FacesEvent.
 
     protected MethodExpressionEventListener(MethodExpression methodExpressionOneArg) {
 
         super();
         this. = methodExpressionOneArg;
         FacesContext context = FacesContext.getCurrentInstance();
         ELContext elContext = context.getELContext();
         this. = context
             .getApplication()
             .getExpressionFactory()
             .createMethodExpression(elContextmethodExpressionOneArg.getExpressionString(), Void.class,
                 );
     }

    

Construct a FacesListener that contains a MethodExpression.

Parameters:
methodExprOneArg
methodExprZeroArg
 
     protected MethodExpressionEventListener(MethodExpression methodExprOneArgMethodExpression methodExprZeroArg) {
 
         super();
         this. = methodExprOneArg;
         this. = methodExprZeroArg;
     }
 
     // ------------------------------------------------------- Event Method

    

Call through to the MethodExpression passed in our constructor. First, try to invoke the MethodExpression passed to the constructor of this instance, passing the argument FacesEvent as the argument. If a MethodNotFoundException is thrown, call to the zero argument MethodExpression derived from the MethodExpression passed to the constructor of this instance. If that fails for any reason, throw an AbortProcessingException, including the cause of the failure.

Throws:
NullPointerException
AbortProcessingException
    public void processEvent(FacesEvent eventthrows AbortProcessingException {
        if (event == null) {
            throw new NullPointerException();
        }
        FacesContext context = FacesContext.getCurrentInstance();
        ELContext elContext = context.getELContext();
        // PENDING: The corresponding code in MethodExpressionActionListener
        // has an elaborate message capture, logging, and rethrowing block.
        // Why not here?
        try {
            .invoke(elContextnew Object[] { event });
        } catch (MethodNotFoundException mnf) {
            if (null != ) {
                try {
                    // try to invoke a no-arg version
                    .invoke(elContext);
                } catch (ELException e) {
                    throw new AbortProcessingException(e.getMessage(), e.getCause());
                }
            }
        } catch (ELException e) {
            throw new AbortProcessingException(e.getMessage(), e.getCause());
        }
    }
    // ------------------------------------------------ Methods from StateHolder

    

Both MethodExpression instances described in the constructor must be saved.

    public Object saveState(FacesContext context) {
        if (context == null) {
            throw new NullPointerException();
        }
        return new Object[] { UIComponentBase.saveAttachedState(context),
                UIComponentBase.saveAttachedState(context) };
    }

    

Both MethodExpression instances described in the constructor must be restored.

    public void restoreState(FacesContext contextObject state) {
        if (context == null) {
            throw new NullPointerException();
        }
        if (state == null) {
            return;
        }
         = (MethodExpression) UIComponentBase.restoreAttachedState(context, ((Object[]) state)[0]);
         = (MethodExpression) UIComponentBase.restoreAttachedState(context, ((Object[]) state)[1]);
    }
    public boolean isTransient() {
        return ;
    }
    public void setTransient(boolean newTransientValue) {
         = newTransientValue;
    }
New to GrepCode? Check out our FAQ X