001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 Log _log = LogFactoryUtil.getLog(
086                    LiferayMethodExceptionEventHandler.class);
087    
088    }