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            public void proceed(
041                            Object target, Class<?> targetClass, Method method,
042                            Object[] arguments, String[] methodInterceptorBeanIds)
043                    throws Exception {
044    
045                    if ((methodInterceptorBeanIds == null) ||
046                            (methodInterceptorBeanIds.length == 0)) {
047    
048                            throw new IllegalArgumentException(
049                                    "Method interceptor bean IDs array is empty");
050                    }
051    
052                    ServiceBeanMethodInvocation serviceBeanMethodInvocation = create(
053                            target, targetClass, method, arguments);
054    
055                    List<MethodInterceptor> methodInterceptors = getMethodInterceptors(
056                            methodInterceptorBeanIds);
057    
058                    serviceBeanMethodInvocation.setMethodInterceptors(methodInterceptors);
059    
060                    try {
061                            serviceBeanMethodInvocation.proceed();
062                    }
063                    catch (Throwable t) {
064                            if (t instanceof Exception) {
065                                    throw (Exception)t;
066                            }
067    
068                            throw new Exception(t);
069                    }
070            }
071    
072            protected ServiceBeanMethodInvocation create(
073                    Object target, Class<?> targetClass, Method method,
074                    Object[] arguments) {
075    
076                    return new ServiceBeanMethodInvocation(
077                            target, targetClass, method, arguments);
078            }
079    
080            protected List<MethodInterceptor> getMethodInterceptors(
081                    String... methodInterceptorBeanIds) {
082    
083                    String methodInterceptorsKey = StringUtil.merge(
084                            methodInterceptorBeanIds);
085    
086                    List<MethodInterceptor> methodInterceptors = _methodInterceptors.get(
087                            methodInterceptorsKey);
088    
089                    if (methodInterceptors != null) {
090                            return methodInterceptors;
091                    }
092    
093                    methodInterceptors = new ArrayList<MethodInterceptor>();
094    
095                    for (String methodInterceptorBeanId : methodInterceptorBeanIds) {
096                            MethodInterceptor methodInterceptor =
097                                    (MethodInterceptor)PortalBeanLocatorUtil.locate(
098                                            methodInterceptorBeanId);
099    
100                            methodInterceptors.add(methodInterceptor);
101                    }
102    
103                    _methodInterceptors.put(methodInterceptorsKey, methodInterceptors);
104    
105                    return methodInterceptors;
106            }
107    
108            private Map<String, List<MethodInterceptor>> _methodInterceptors =
109                    new HashMap<String, List<MethodInterceptor>>();
110    
111    }