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