001    /**
002     * Copyright (c) 2000-2013 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.spring.aop;
016    
017    import com.liferay.portal.kernel.util.ObjectValuePair;
018    import com.liferay.portal.kernel.util.ProxyUtil;
019    import com.liferay.portal.util.ClassLoaderUtil;
020    
021    import java.lang.reflect.InvocationHandler;
022    
023    import java.util.ArrayList;
024    import java.util.List;
025    
026    import org.springframework.beans.BeansException;
027    import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter;
028    import org.springframework.core.Ordered;
029    
030    /**
031     * @author Shuyang Zhou
032     */
033    public class DynamicProxyCreator
034            extends InstantiationAwareBeanPostProcessorAdapter implements Ordered {
035    
036            public static DynamicProxyCreator getDynamicProxyCreator() {
037                    return _instance;
038            }
039    
040            @Override
041            public int getOrder() {
042                    return Ordered.HIGHEST_PRECEDENCE;
043            }
044    
045            @Override
046            public Object postProcessAfterInitialization(Object bean, String beanName)
047                    throws BeansException {
048    
049                    Class<?> beanClass = bean.getClass();
050    
051                    for (ObjectValuePair<BeanMatcher, InvocationHandlerFactory>
052                                    objectValuePair : _beanMatcherInvocationHandlerFactories) {
053    
054                            BeanMatcher beanMatcher = objectValuePair.getKey();
055    
056                            if (beanMatcher.match(beanClass, beanName)) {
057                                    InvocationHandlerFactory invocationHandlerFactory =
058                                            objectValuePair.getValue();
059    
060                                    InvocationHandler invocationHandler =
061                                            invocationHandlerFactory.createInvocationHandler(bean);
062    
063                                    bean = ProxyUtil.newProxyInstance(
064                                            ClassLoaderUtil.getContextClassLoader(),
065                                            beanClass.getInterfaces(), invocationHandler);
066                            }
067                    }
068    
069                    return bean;
070            }
071    
072            public static class Register {
073    
074                    public Register(
075                            BeanMatcher beanMatcher,
076                            InvocationHandlerFactory invocationHandlerFactory) {
077    
078                            ObjectValuePair<BeanMatcher, InvocationHandlerFactory>
079                                    objectValuePair =
080                                            new ObjectValuePair<BeanMatcher, InvocationHandlerFactory>(
081                                                    beanMatcher, invocationHandlerFactory);
082    
083                            _instance._beanMatcherInvocationHandlerFactories.add(
084                                    objectValuePair);
085                    }
086    
087            }
088    
089            private static DynamicProxyCreator _instance = new DynamicProxyCreator();
090    
091            private List<ObjectValuePair<BeanMatcher, InvocationHandlerFactory>>
092                    _beanMatcherInvocationHandlerFactories =
093                            new ArrayList
094                                    <ObjectValuePair<BeanMatcher, InvocationHandlerFactory>>();
095    
096    }