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 com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.io.Serializable;
022    
023    import java.lang.reflect.AccessibleObject;
024    import java.lang.reflect.InvocationTargetException;
025    import java.lang.reflect.Method;
026    
027    import java.util.List;
028    
029    import org.aopalliance.intercept.MethodInterceptor;
030    import org.aopalliance.intercept.MethodInvocation;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class ServiceBeanMethodInvocation
036            implements MethodInvocation, Serializable {
037    
038            public ServiceBeanMethodInvocation(
039                    Object target, Class<?> targetClass, Method method,
040                    Object[] arguments) {
041    
042                    _target = target;
043                    _targetClass = targetClass;
044                    _method = method;
045                    _arguments = arguments;
046    
047                    if (!_method.isAccessible()) {
048                            _method.setAccessible(true);
049                    }
050            }
051    
052            @Override
053            public boolean equals(Object obj) {
054                    if (this == obj) {
055                            return true;
056                    }
057    
058                    if (!(obj instanceof ServiceBeanMethodInvocation)) {
059                            return false;
060                    }
061    
062                    ServiceBeanMethodInvocation serviceBeanMethodInvocation =
063                            (ServiceBeanMethodInvocation)obj;
064    
065                    if (Validator.equals(_method, serviceBeanMethodInvocation._method)) {
066                            return true;
067                    }
068    
069                    return false;
070            }
071    
072            @Override
073            public Object[] getArguments() {
074                    return _arguments;
075            }
076    
077            @Override
078            public Method getMethod() {
079                    return _method;
080            }
081    
082            @Override
083            public AccessibleObject getStaticPart() {
084                    return _method;
085            }
086    
087            public Class<?> getTargetClass() {
088                    return _targetClass;
089            }
090    
091            @Override
092            public Object getThis() {
093                    return _target;
094            }
095    
096            @Override
097            public int hashCode() {
098                    if (_hashCode == 0) {
099                            _hashCode = _method.hashCode();
100                    }
101    
102                    return _hashCode;
103            }
104    
105            @Override
106            public Object proceed() throws Throwable {
107                    if (_index < _methodInterceptors.size()) {
108                            MethodInterceptor methodInterceptor = _methodInterceptors.get(
109                                    _index++);
110    
111                            return methodInterceptor.invoke(this);
112                    }
113    
114                    try {
115                            return _method.invoke(_target, _arguments);
116                    }
117                    catch (InvocationTargetException ite) {
118                            throw ite.getTargetException();
119                    }
120            }
121    
122            public void setMethodInterceptors(
123                    List<MethodInterceptor> methodInterceptors) {
124    
125                    _methodInterceptors = methodInterceptors;
126            }
127    
128            public ServiceBeanMethodInvocation toCacheKeyModel() {
129                    ServiceBeanMethodInvocation serviceBeanMethodInvocation =
130                            new ServiceBeanMethodInvocation(null, null, _method, null);
131    
132                    serviceBeanMethodInvocation._hashCode = _hashCode;
133    
134                    return serviceBeanMethodInvocation;
135            }
136    
137            @Override
138            public String toString() {
139                    if (_toString != null) {
140                            return _toString;
141                    }
142    
143                    Class<?>[] parameterTypes = _method.getParameterTypes();
144    
145                    StringBundler sb = new StringBundler(parameterTypes.length * 2 + 6);
146    
147                    Class<?> declaringClass = _method.getDeclaringClass();
148    
149                    sb.append(declaringClass.getName());
150    
151                    sb.append(StringPool.PERIOD);
152                    sb.append(_method.getName());
153                    sb.append(StringPool.OPEN_PARENTHESIS);
154    
155                    for (int i = 0; i < parameterTypes.length; i++) {
156                            Class<?> parameterType = parameterTypes[i];
157    
158                            sb.append(parameterType.getName());
159    
160                            if ((i + 1) < parameterTypes.length) {
161                                    sb.append(StringPool.COMMA);
162                            }
163                    }
164    
165                    sb.append(StringPool.CLOSE_PARENTHESIS);
166    
167                    if (_targetClass != null) {
168                            sb.append(StringPool.AT);
169                            sb.append(_targetClass.getName());
170                    }
171    
172                    _toString = sb.toString();
173    
174                    return _toString;
175            }
176    
177            private Object[] _arguments;
178            private int _hashCode;
179            private int _index;
180            private Method _method;
181            private List<MethodInterceptor> _methodInterceptors;
182            private Object _target;
183            private Class<?> _targetClass;
184            private String _toString;
185    
186    }