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