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.velocity;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ProxyUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    import org.apache.velocity.app.event.MethodExceptionEventHandler;
028    
029    /**
030     * @author Raymond Aug??
031     * @author Peter Shin
032     */
033    public class LiferayMethodExceptionEventHandler
034            implements MethodExceptionEventHandler {
035    
036            @Override
037            public Object methodException(
038                            @SuppressWarnings("rawtypes") Class clazz, String method,
039                            Exception e)
040                    throws Exception {
041    
042                    StringBundler sb = new StringBundler(9);
043    
044                    sb.append("Unable to execute method ");
045                    sb.append(method);
046                    sb.append(StringPool.SPACE);
047                    sb.append(StringPool.OPEN_CURLY_BRACE);
048                    sb.append("exception=");
049                    sb.append(e);
050                    sb.append(StringPool.COMMA_AND_SPACE);
051                    sb.append(getKeyValuePair(clazz));
052                    sb.append(StringPool.CLOSE_CURLY_BRACE);
053    
054                    _log.error(sb.toString(), e);
055    
056                    return null;
057            }
058    
059            protected String getKeyValuePair(Class<?> clazz) {
060                    if (clazz == null) {
061                            return "class=null";
062                    }
063    
064                    if (!ProxyUtil.isProxyClass(clazz)) {
065                            return "className=" + clazz.getName();
066                    }
067    
068                    Class<?>[] interfaceClasses = clazz.getInterfaces();
069    
070                    if (interfaceClasses == null) {
071                            return "className=" + clazz.getName();
072                    }
073    
074                    List<String> proxyInterfaceClassNames = new ArrayList<String>();
075    
076                    for (Class<?> interfaceClass : interfaceClasses) {
077                            proxyInterfaceClassNames.add(interfaceClass.getName());
078                    }
079    
080                    return "proxyInterfaceClassNames=" +
081                            StringUtil.merge(
082                                    proxyInterfaceClassNames, StringPool.COMMA_AND_SPACE);
083            }
084    
085            private static final Log _log = LogFactoryUtil.getLog(
086                    LiferayMethodExceptionEventHandler.class);
087    
088    }