001    /**
002     * Copyright (c) 2000-2012 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.bean;
016    
017    import com.liferay.portal.security.pacl.PACLBeanHandler;
018    import com.liferay.portal.util.ClassLoaderUtil;
019    
020    import java.lang.Object;
021    import java.lang.reflect.InvocationTargetException;
022    import java.lang.reflect.Method;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class VelocityBeanHandler extends PACLBeanHandler {
028    
029            public VelocityBeanHandler(Object bean, ClassLoader classLoader) {
030                    super(bean);
031    
032                    _classLoader = classLoader;
033            }
034    
035            public ClassLoader getClassLoader() {
036                    return _classLoader;
037            }
038    
039            @Override
040            public Object invoke(Object proxy, Method method, Object[] arguments)
041                    throws Throwable {
042    
043                    ClassLoader contextClassLoader =
044                            ClassLoaderUtil.getContextClassLoader();
045    
046                    try {
047                            if ((_classLoader != null) &&
048                                    (_classLoader != contextClassLoader)) {
049    
050                                    ClassLoaderUtil.setContextClassLoader(_classLoader);
051                            }
052    
053                            return super.invoke(proxy, method, arguments);
054                    }
055                    catch (InvocationTargetException ite) {
056                            return null;
057                    }
058                    finally {
059                            if ((_classLoader != null) &&
060                                    (_classLoader != contextClassLoader)) {
061    
062                                    ClassLoaderUtil.setContextClassLoader(contextClassLoader);
063                            }
064                    }
065            }
066    
067            private ClassLoader _classLoader;
068    
069    }