001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.osgi;
016    
017    import com.liferay.portal.kernel.bean.ClassLoaderBeanHandler;
018    import com.liferay.portal.kernel.util.ProxyUtil;
019    
020    import java.lang.reflect.Field;
021    import java.lang.reflect.InvocationHandler;
022    
023    import org.springframework.aop.TargetSource;
024    import org.springframework.aop.framework.AdvisedSupport;
025    
026    /**
027     * @author Raymond Augé
028     */
029    public class ServiceWrapperUtil {
030    
031            public static AdvisedSupport getAdvisedSupport(Object serviceProxy)
032                    throws Exception {
033    
034                    InvocationHandler invocationHandler = ProxyUtil.getInvocationHandler(
035                            serviceProxy);
036    
037                    Class<?> invocationHandlerClass = invocationHandler.getClass();
038    
039                    Field advisedSupportField = invocationHandlerClass.getDeclaredField(
040                            "_advisedSupport");
041    
042                    advisedSupportField.setAccessible(true);
043    
044                    return (AdvisedSupport)advisedSupportField.get(invocationHandler);
045            }
046    
047            public static Object getTarget(AdvisedSupport advisedSupport)
048                    throws Exception {
049    
050                    TargetSource targetSource = advisedSupport.getTargetSource();
051    
052                    Object previousService = targetSource.getTarget();
053    
054                    if (!ProxyUtil.isProxyClass(previousService.getClass())) {
055                            return previousService;
056                    }
057    
058                    InvocationHandler invocationHandler = ProxyUtil.getInvocationHandler(
059                            previousService);
060    
061                    if (invocationHandler instanceof ClassLoaderBeanHandler) {
062                            ClassLoaderBeanHandler classLoaderBeanHandler =
063                                    (ClassLoaderBeanHandler)invocationHandler;
064    
065                            previousService = classLoaderBeanHandler.getBean();
066                    }
067    
068                    return previousService;
069            }
070    
071    }