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 474273 2006-11-13 12:15:36Z lofwyr $
023     */
024    
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
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    
039    public class ActionListenerImpl implements ActionListener {
040    
041      private static final Log LOG = LogFactory.getLog(ActionListenerImpl.class);
042    
043      private ActionListener base;
044    
045      private String errorOutcome = "error";
046    
047      public ActionListenerImpl(ActionListener base) {
048        this.base = base;
049      }
050    
051      public void processAction(ActionEvent event) throws AbortProcessingException {
052        try {
053          base.processAction(event);
054        } catch (Throwable e) {
055          LOG.error("Processing failed. Forwarding to error page. errorOutcome="
056              + errorOutcome, e.getCause());
057          FacesContext facesContext = FacesContext.getCurrentInstance();
058          FacesMessage facesMessage
059              = new FacesMessage(e.getCause().toString());
060          facesContext.addMessage(null, facesMessage);
061          UIComponent source = event.getComponent();
062          ActionSource actionSource = (ActionSource) source;
063          Application application = facesContext.getApplication();
064          MethodBinding binding = actionSource.getAction();
065          // Retrieve the NavigationHandler instance..
066          NavigationHandler navHandler = application.getNavigationHandler();
067          // Invoke nav handling..
068          String navBinding =
069              (null != binding) ? binding.getExpressionString() : null;
070          navHandler.handleNavigation(facesContext, navBinding,
071              errorOutcome);
072          // Trigger a switch to Render Response if needed
073          facesContext.renderResponse();
074        }
075      }
076    
077      public String getErrorOutcome() {
078        return errorOutcome;
079      }
080    
081      public void setErrorOutcome(String errorOutcome) {
082        this.errorOutcome = errorOutcome;
083      }
084    }