001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service;
016    
017    import java.util.Map;
018    
019    /**
020     * @author Matthew Tambara
021     */
022    public class ExceptionRetryAcceptor implements RetryAcceptor {
023    
024            public static final String EXCEPTION_NAME = "EXCEPTION_NAME";
025    
026            @Override
027            public boolean acceptException(
028                    Throwable t, Map<String, String> propertyMap) {
029    
030                    String name = propertyMap.get(EXCEPTION_NAME);
031    
032                    if (name == null) {
033                            throw new IllegalArgumentException(
034                                    "Missing property " + EXCEPTION_NAME);
035                    }
036    
037                    while (true) {
038                            Class<?> clazz = t.getClass();
039    
040                            ClassLoader classLoader = clazz.getClassLoader();
041    
042                            try {
043                                    Class<?> exceptionClass = classLoader.loadClass(name);
044    
045                                    if (exceptionClass.isInstance(t)) {
046                                            return true;
047                                    }
048                            }
049                            catch (ClassNotFoundException cnfe) {
050                            }
051    
052                            Throwable cause = t.getCause();
053    
054                            if ((t == cause) || (cause == null)) {
055                                    break;
056                            }
057    
058                            t = cause;
059                    }
060    
061                    return false;
062            }
063    
064            @Override
065            public boolean acceptResult(
066                    Object returnValue, Map<String, String> propertyMap) {
067    
068                    return false;
069            }
070    
071    }