001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *   http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.myfaces.tobago.application;
021    
022    /*
023     * Created 22.11.2004 18:33:44.
024     * $Id: ActionListenerImpl.java 1368577 2012-08-02 16:20:31Z lofwyr $
025     */
026    
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    
030    import javax.faces.application.Application;
031    import javax.faces.application.FacesMessage;
032    import javax.faces.application.NavigationHandler;
033    import javax.faces.component.ActionSource;
034    import javax.faces.component.UIComponent;
035    import javax.faces.context.FacesContext;
036    import javax.faces.el.MethodBinding;
037    import javax.faces.event.AbortProcessingException;
038    import javax.faces.event.ActionEvent;
039    import javax.faces.event.ActionListener;
040    import javax.faces.FacesException;
041    
042    public class ActionListenerImpl implements ActionListener {
043    
044      private static final Log LOG = LogFactory.getLog(ActionListenerImpl.class);
045    
046      private ActionListener base;
047    
048      private String errorOutcome = "error";
049    
050      public ActionListenerImpl(ActionListener base) {
051        this.base = base;
052      }
053    
054      public void processAction(ActionEvent event) throws AbortProcessingException {
055        try {
056          base.processAction(event);
057        } catch (Throwable e) {
058          if (e instanceof FacesException) {
059            Throwable fe = e;
060            while (fe != null) {
061              if (fe instanceof AbortProcessingException) {
062                throw (FacesException) e;
063              }
064              fe = fe.getCause();
065            }
066          }
067          LOG.error("Processing failed. Forwarding to error page. errorOutcome="
068              + errorOutcome, e.getCause());
069          FacesContext facesContext = FacesContext.getCurrentInstance();
070          FacesMessage facesMessage
071              = new FacesMessage(e.getCause().toString());
072          facesContext.addMessage(null, facesMessage);
073          UIComponent source = event.getComponent();
074          ActionSource actionSource = (ActionSource) source;
075          Application application = facesContext.getApplication();
076          MethodBinding binding = actionSource.getAction();
077          // Retrieve the NavigationHandler instance..
078          NavigationHandler navHandler = application.getNavigationHandler();
079          // Invoke nav handling..
080          String navBinding =
081              (null != binding) ? binding.getExpressionString() : null;
082          navHandler.handleNavigation(facesContext, navBinding,
083              errorOutcome);
084          // Trigger a switch to Render Response if needed
085          facesContext.renderResponse();
086        }
087      }
088    
089      public String getErrorOutcome() {
090        return errorOutcome;
091      }
092    
093      public void setErrorOutcome(String errorOutcome) {
094        this.errorOutcome = errorOutcome;
095      }
096    }