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