001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.faces.util;
023:
024: import org.apache.log4j.Logger;
025:
026: import javax.faces.component.UICommand;
027: import javax.faces.context.FacesContext;
028: import javax.faces.el.EvaluationException;
029: import javax.faces.event.AbortProcessingException;
030: import javax.faces.event.ActionEvent;
031: import javax.faces.event.ActionListener;
032: import java.util.ArrayList;
033: import java.util.Arrays;
034: import java.util.Iterator;
035:
036: /**
037: * The purpose of this class is to dispatch an event to the action listeners attached to an instance of
038: * <code>UICommand</code> object. It will iterate the set of listeners returned by the method
039: * <code>UICommand.getActionListeners()</code> (SunRI) and the listener evaluated by the method binding returned by the
040: * method <code>getActionListener()</code> (MyFaces). So overally we try to encapsulate the delivery of an action event
041: * in a portable way accross JSF implementations.
042: *
043: * @author <a href="mailto:julien@jboss.org">Julien Viet</a>
044: * @version $Revision: 8784 $
045: */
046: public class ActionEventDispatcher {
047:
048: /** . */
049: private static final ActionListener[] EMPTY_LISTENERS = new ActionListener[0];
050:
051: /** . */
052: private static final Logger log = Logger
053: .getLogger(ActionEventDispatcher.class);
054:
055: /** . */
056: private final UICommand command;
057:
058: public ActionEventDispatcher(UICommand command) {
059: this .command = command;
060: }
061:
062: public void dispatch(final FacesContext faces,
063: final ActionEvent event) throws AbortProcessingException {
064: //
065: ArrayList listeners = new ArrayList();
066:
067: if (command.getActionListeners() != null) {
068: listeners.addAll(Arrays
069: .asList(command.getActionListeners()));
070: }
071:
072: //
073: if (command.getActionListener() != null) {
074: ActionListener listener = new ActionListener() {
075: public void processAction(ActionEvent actionEvent)
076: throws AbortProcessingException {
077: try {
078: command.getActionListener().invoke(faces,
079: new Object[] { event });
080: } catch (EvaluationException e) {
081: Throwable cause = e.getCause();
082: if (cause != null
083: && cause instanceof AbortProcessingException) {
084: throw (AbortProcessingException) cause;
085: } else {
086: log
087: .error(
088: "Was not able to evaluate the method binding",
089: e);
090: }
091: }
092: }
093: };
094: listeners.add(listener);
095: }
096:
097: //
098: for (Iterator i = listeners.iterator(); i.hasNext();) {
099: ActionListener listener = (ActionListener) i.next();
100: listener.processAction(event);
101: }
102: }
103: }
|