001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004
005 This file is part of Granite Data Services.
006
007 Granite Data Services is free software; you can redistribute it and/or modify
008 it under the terms of the GNU Library General Public License as published by
009 the Free Software Foundation; either version 2 of the License, or (at your
010 option) any later version.
011
012 Granite Data Services is distributed in the hope that it will be useful, but
013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015 for more details.
016
017 You should have received a copy of the GNU Library General Public License
018 along with this library; if not, see <http://www.gnu.org/licenses/>.
019 */
020
021 package org.granite.messaging.service;
022
023 import org.granite.config.flex.Destination;
024
025 import flex.messaging.messages.Message;
026
027 /**
028 * @author Marcelo SCHROEDER
029 */
030 public abstract class AbstractServiceExceptionHandler implements ServiceExceptionHandler {
031
032 private static final long serialVersionUID = 1L;
033
034 private final boolean logException;
035
036 public AbstractServiceExceptionHandler(boolean logException) {
037 this.logException = logException;
038 }
039
040 protected boolean getLogException() {
041 return logException;
042 }
043
044 protected ServiceException getServiceException(ServiceInvocationContext context, Throwable e) {
045 String method = (context.getMethod() != null ? context.getMethod().toString() : "null");
046 return getServiceException(context.getMessage(), context.getDestination(), method, e);
047 }
048
049 protected ServiceException getServiceException(Message request, Destination destination, String method, Throwable e) {
050
051 if (e instanceof ServiceException)
052 return (ServiceException)e;
053
054 String detail = "\n" +
055 "- destination: " + (destination != null ? destination.getId() : "") + "\n" +
056 "- method: " + method + "\n" +
057 "- exception: " + e.toString() + "\n";
058
059 return new ServiceException(getClass().getSimpleName() + ".Call.Failed", e.getMessage(), detail, e);
060 }
061 }