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