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 java.lang.reflect.InvocationHandler;
018    import java.lang.reflect.Method;
019    
020    import java.util.List;
021    
022    import org.aopalliance.intercept.MethodInterceptor;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public class MethodInterceptorInvocationHandler implements InvocationHandler {
028    
029            public MethodInterceptorInvocationHandler(
030                    Object target, List<MethodInterceptor> methodInterceptors) {
031    
032                    if (target == null) {
033                            throw new NullPointerException("Target is null");
034                    }
035    
036                    _target = target;
037                    _targetClass = target.getClass();
038    
039                    if (methodInterceptors == null) {
040                            throw new NullPointerException("Method interceptors is null");
041                    }
042    
043                    if (methodInterceptors.isEmpty()) {
044                            throw new IllegalArgumentException("Method interceptors is empty");
045                    }
046    
047                    for (int i = 0; i < methodInterceptors.size(); i++) {
048                            if (methodInterceptors.get(i) == null) {
049                                    throw new IllegalArgumentException(
050                                            "Method interceptor " + i + " is null");
051                            }
052                    }
053    
054                    _methodInterceptors = methodInterceptors;
055            }
056    
057            @Override
058            public Object invoke(Object proxy, Method method, Object[] arguments)
059                    throws Throwable {
060    
061                    ServiceBeanMethodInvocation serviceBeanMethodInvocation =
062                            new ServiceBeanMethodInvocation(
063                                    _target, _targetClass, method, arguments);
064    
065                    serviceBeanMethodInvocation.setMethodInterceptors(_methodInterceptors);
066    
067                    return serviceBeanMethodInvocation.proceed();
068            }
069    
070            private List<MethodInterceptor> _methodInterceptors;
071            private Object _target;
072            private Class<?> _targetClass;
073    
074    }