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 ((_method == serviceBeanMethodInvocation._method) &&
066                            Validator.equals(_method, serviceBeanMethodInvocation._method)) {
067    
068                            return true;
069                    }
070    
071                    return false;
072            }
073    
074            @Override
075            public Object[] getArguments() {
076                    return _arguments;
077            }
078    
079            @Override
080            public Method getMethod() {
081                    return _method;
082            }
083    
084            @Override
085            public AccessibleObject getStaticPart() {
086                    return _method;
087            }
088    
089            public Class<?> getTargetClass() {
090                    return _targetClass;
091            }
092    
093            @Override
094            public Object getThis() {
095                    return _target;
096            }
097    
098            @Override
099            public int hashCode() {
100                    if (_hashCode == 0) {
101                            _hashCode = _method.hashCode();
102                    }
103    
104                    return _hashCode;
105            }
106    
107            @Override
108            public Object proceed() throws Throwable {
109                    if (_index < _methodInterceptors.size()) {
110                            MethodInterceptor methodInterceptor = _methodInterceptors.get(
111                                    _index++);
112    
113                            return methodInterceptor.invoke(this);
114                    }
115    
116                    try {
117                            return _method.invoke(_target, _arguments);
118                    }
119                    catch (InvocationTargetException ite) {
120                            throw ite.getTargetException();
121                    }
122            }
123    
124            public void setMethodInterceptors(
125                    List<MethodInterceptor> methodInterceptors) {
126    
127                    _methodInterceptors = methodInterceptors;
128            }
129    
130            public ServiceBeanMethodInvocation toCacheKeyModel() {
131                    ServiceBeanMethodInvocation serviceBeanMethodInvocation =
132                            new ServiceBeanMethodInvocation(null, null, _method, null);
133    
134                    serviceBeanMethodInvocation._hashCode = _hashCode;
135    
136                    return serviceBeanMethodInvocation;
137            }
138    
139            @Override
140            public String toString() {
141                    if (_toString != null) {
142                            return _toString;
143                    }
144    
145                    Class<?>[] parameterTypes = _method.getParameterTypes();
146    
147                    StringBundler sb = new StringBundler(parameterTypes.length * 2 + 6);
148    
149                    Class<?> declaringClass = _method.getDeclaringClass();
150    
151                    sb.append(declaringClass.getName());
152    
153                    sb.append(StringPool.PERIOD);
154                    sb.append(_method.getName());
155                    sb.append(StringPool.OPEN_PARENTHESIS);
156    
157                    for (int i = 0; i < parameterTypes.length; i++) {
158                            Class<?> parameterType = parameterTypes[i];
159    
160                            sb.append(parameterType.getName());
161    
162                            if ((i + 1) < parameterTypes.length) {
163                                    sb.append(StringPool.COMMA);
164                            }
165                    }
166    
167                    sb.append(StringPool.CLOSE_PARENTHESIS);
168    
169                    if (_targetClass != null) {
170                            sb.append(StringPool.AT);
171                            sb.append(_targetClass.getName());
172                    }
173    
174                    _toString = sb.toString();
175    
176                    return _toString;
177            }
178    
179            private Object[] _arguments;
180            private int _hashCode;
181            private int _index;
182            private Method _method;
183            private List<MethodInterceptor> _methodInterceptors;
184            private Object _target;
185            private Class<?> _targetClass;
186            private String _toString;
187    
188    }