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