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.util;
016    
017    import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
018    import com.liferay.portal.kernel.security.pacl.DoPrivileged;
019    import com.liferay.portal.kernel.util.ServiceBeanMethodInvocationFactory;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.spring.aop.ServiceBeanMethodInvocation;
022    
023    import java.lang.reflect.Method;
024    
025    import java.util.ArrayList;
026    import java.util.HashMap;
027    import java.util.List;
028    import java.util.Map;
029    
030    import org.aopalliance.intercept.MethodInterceptor;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Wesley Gong
035     */
036    @DoPrivileged
037    public class ServiceBeanMethodInvocationFactoryImpl
038            implements ServiceBeanMethodInvocationFactory {
039    
040            @Override
041            public Object proceed(
042                            Object target, Class<?> targetClass, Method method,
043                            Object[] arguments, String[] methodInterceptorBeanIds)
044                    throws Exception {
045    
046                    if ((methodInterceptorBeanIds == null) ||
047                            (methodInterceptorBeanIds.length == 0)) {
048    
049                            throw new IllegalArgumentException(
050                                    "Method interceptor bean IDs array is empty");
051                    }
052    
053                    ServiceBeanMethodInvocation serviceBeanMethodInvocation = create(
054                            target, targetClass, method, arguments);
055    
056                    List<MethodInterceptor> methodInterceptors = getMethodInterceptors(
057                            methodInterceptorBeanIds);
058    
059                    serviceBeanMethodInvocation.setMethodInterceptors(methodInterceptors);
060    
061                    try {
062                            return serviceBeanMethodInvocation.proceed();
063                    }
064                    catch (Throwable t) {
065                            if (t instanceof Exception) {
066                                    throw (Exception)t;
067                            }
068    
069                            throw new Exception(t);
070                    }
071            }
072    
073            protected ServiceBeanMethodInvocation create(
074                    Object target, Class<?> targetClass, Method method,
075                    Object[] arguments) {
076    
077                    return new ServiceBeanMethodInvocation(
078                            target, targetClass, method, arguments);
079            }
080    
081            protected List<MethodInterceptor> getMethodInterceptors(
082                    String... methodInterceptorBeanIds) {
083    
084                    String methodInterceptorsKey = StringUtil.merge(
085                            methodInterceptorBeanIds);
086    
087                    List<MethodInterceptor> methodInterceptors = _methodInterceptors.get(
088                            methodInterceptorsKey);
089    
090                    if (methodInterceptors != null) {
091                            return methodInterceptors;
092                    }
093    
094                    methodInterceptors = new ArrayList<MethodInterceptor>();
095    
096                    for (String methodInterceptorBeanId : methodInterceptorBeanIds) {
097                            MethodInterceptor methodInterceptor =
098                                    (MethodInterceptor)PortalBeanLocatorUtil.locate(
099                                            methodInterceptorBeanId);
100    
101                            methodInterceptors.add(methodInterceptor);
102                    }
103    
104                    _methodInterceptors.put(methodInterceptorsKey, methodInterceptors);
105    
106                    return methodInterceptors;
107            }
108    
109            private Map<String, List<MethodInterceptor>> _methodInterceptors =
110                    new HashMap<String, List<MethodInterceptor>>();
111    
112    }