001    /**
002     * Copyright (c) 2000-present 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.jsonwebservice;
016    
017    import com.liferay.portal.kernel.jsonwebservice.JSONWebServiceScannerStrategy;
018    import com.liferay.portal.kernel.util.ProxyUtil;
019    import com.liferay.portal.spring.aop.ServiceBeanAopProxy;
020    
021    import java.lang.reflect.Method;
022    
023    import java.util.ArrayList;
024    import java.util.Arrays;
025    import java.util.LinkedList;
026    import java.util.List;
027    import java.util.Queue;
028    
029    import org.springframework.aop.TargetSource;
030    import org.springframework.aop.framework.AdvisedSupport;
031    
032    /**
033     * @author Miguel Pastor
034     */
035    public class SpringJSONWebServiceScannerStrategy
036            implements JSONWebServiceScannerStrategy {
037    
038            @Override
039            public MethodDescriptor[] scan(Object service) {
040                    Class<?> clazz = null;
041    
042                    try {
043                            clazz = getTargetClass(service);
044                    }
045                    catch (Exception e) {
046                            return new MethodDescriptor[0];
047                    }
048    
049                    Method[] methods = clazz.getMethods();
050    
051                    List<MethodDescriptor> methodDescriptors = new ArrayList<>(
052                            methods.length);
053    
054                    for (Method method : methods) {
055                            Class<?> declaringClass = method.getDeclaringClass();
056    
057                            if ((declaringClass != clazz) || !isInterfaceMethod(method)) {
058                                    continue;
059                            }
060    
061                            methodDescriptors.add(new MethodDescriptor(method));
062                    }
063    
064                    return methodDescriptors.toArray(
065                            new MethodDescriptor[methodDescriptors.size()]);
066            }
067    
068            protected Class<?> getTargetClass(Object service) throws Exception {
069                    if (ProxyUtil.isProxyClass(service.getClass())) {
070                            AdvisedSupport advisedSupport =
071                                    ServiceBeanAopProxy.getAdvisedSupport(service);
072    
073                            TargetSource targetSource = advisedSupport.getTargetSource();
074    
075                            service = targetSource.getTarget();
076                    }
077    
078                    return service.getClass();
079            }
080    
081            protected boolean isInterfaceMethod(Method method) {
082                    Class<?> declaringClass = method.getDeclaringClass();
083    
084                    if (declaringClass.isInterface()) {
085                            return true;
086                    }
087    
088                    Queue<Class<?>> queue = new LinkedList<>(
089                            Arrays.asList(declaringClass.getInterfaces()));
090    
091                    Class<?> superClass = declaringClass.getSuperclass();
092    
093                    if (superClass != null) {
094                            queue.add(superClass);
095                    }
096    
097                    Class<?> clazz = null;
098    
099                    while ((clazz = queue.poll()) != null) {
100                            if (clazz.isInterface()) {
101                                    try {
102                                            clazz.getMethod(
103                                                    method.getName(), method.getParameterTypes());
104    
105                                            return true;
106                                    }
107                                    catch (ReflectiveOperationException roe) {
108                                    }
109                            }
110                            else {
111                                    queue.addAll(Arrays.asList(clazz.getInterfaces()));
112    
113                                    superClass = clazz.getSuperclass();
114    
115                                    if (superClass != null) {
116                                            queue.add(superClass);
117                                    }
118                            }
119                    }
120    
121                    return false;
122            }
123    
124    }