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            public void clear() {
042                    _beanMatcherInvocationHandlerFactories.clear();
043            }
044    
045            @Override
046            public int getOrder() {
047                    return Ordered.HIGHEST_PRECEDENCE;
048            }
049    
050            @Override
051            public Object postProcessAfterInitialization(Object bean, String beanName)
052                    throws BeansException {
053    
054                    Class<?> beanClass = bean.getClass();
055    
056                    for (ObjectValuePair<BeanMatcher, InvocationHandlerFactory>
057                                    objectValuePair : _beanMatcherInvocationHandlerFactories) {
058    
059                            BeanMatcher beanMatcher = objectValuePair.getKey();
060    
061                            if (beanMatcher.match(beanClass, beanName)) {
062                                    InvocationHandlerFactory invocationHandlerFactory =
063                                            objectValuePair.getValue();
064    
065                                    InvocationHandler invocationHandler =
066                                            invocationHandlerFactory.createInvocationHandler(bean);
067    
068                                    bean = ProxyUtil.newProxyInstance(
069                                            ClassLoaderUtil.getContextClassLoader(),
070                                            beanClass.getInterfaces(), invocationHandler);
071                            }
072                    }
073    
074                    return bean;
075            }
076    
077            public static class Register {
078    
079                    public Register(
080                            BeanMatcher beanMatcher,
081                            InvocationHandlerFactory invocationHandlerFactory) {
082    
083                            ObjectValuePair<BeanMatcher, InvocationHandlerFactory>
084                                    objectValuePair = new ObjectValuePair<>(
085                                            beanMatcher, invocationHandlerFactory);
086    
087                            _instance._beanMatcherInvocationHandlerFactories.add(
088                                    objectValuePair);
089                    }
090    
091            }
092    
093            private static final DynamicProxyCreator _instance =
094                    new DynamicProxyCreator();
095    
096            private final List<ObjectValuePair<BeanMatcher, InvocationHandlerFactory>>
097                    _beanMatcherInvocationHandlerFactories = new ArrayList<>();
098    
099    }