001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2014 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.seam;
023
024import javax.faces.application.FacesMessage;
025import javax.faces.context.FacesContext;
026
027import org.granite.logging.Logger;
028import org.granite.messaging.service.DefaultServiceExceptionHandler;
029import org.granite.messaging.service.ServiceException;
030import org.granite.messaging.service.ServiceInvocationContext;
031import org.granite.messaging.service.security.SecurityServiceException;
032import org.hibernate.validator.InvalidStateException;
033import org.hibernate.validator.InvalidValue;
034
035/**
036 * @author Venkat DANDA
037 * @author Cameron INGRAM
038 *
039 * Update services-config.xml to use the seam service exception handler
040 * <factory id="seamFactory" class="org.granite.seam.SeamServiceFactory" >
041 *      <properties>
042 *              <service-exception-handler>org.granite.seam.SeamServiceExceptionHandler</service-exception-handler>
043 *      </properties>
044 * </factory>
045 */
046public class SeamServiceExceptionHandler extends DefaultServiceExceptionHandler {
047
048    private static final long serialVersionUID = -929771583032427716L;
049        private static final Logger log = Logger.getLogger(SeamServiceExceptionHandler.class);
050
051    public SeamServiceExceptionHandler() {
052        this(true);
053    }
054
055    public SeamServiceExceptionHandler(boolean logException) {
056                super(logException);
057        }
058
059    @Override
060    public ServiceException handleInvocationException(ServiceInvocationContext context, Throwable t) {
061        try {
062            while ((!(t instanceof InvalidStateException) && (t.getCause() != null)))
063                t = t.getCause();
064
065            if (t instanceof InvalidStateException) {
066                InvalidStateException ise = (InvalidStateException)t;
067                InvalidValue[] temp = ise.getInvalidValues();
068                if (temp != null && temp.length > 0) {
069                                        for (int i = 0; i < temp.length; i++)
070                                                FacesContext.getCurrentInstance().addMessage(temp[i].getPropertyName(), new FacesMessage(temp[i].getMessage()));
071                                }
072            }
073        }
074        catch(Exception e) {
075            log.error(e, "Could not add FacesMessage for exception: %s", t);
076        }
077
078        if (t instanceof SecurityServiceException)
079            log.debug(t, "Could not process remoting message: %s", context.getMessage());
080        else
081            log.error(t, "Could not process remoting message: %s", context.getMessage());
082
083        
084        return getServiceException(context, t);
085    }
086
087}