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.deploy.hot;
016    
017    import com.liferay.portal.kernel.bean.ClassLoaderBeanHandler;
018    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
019    import com.liferay.portal.kernel.util.ProxyUtil;
020    import com.liferay.portal.service.ServiceWrapper;
021    
022    import java.lang.reflect.InvocationHandler;
023    
024    import org.springframework.aop.TargetSource;
025    import org.springframework.aop.framework.AdvisedSupport;
026    import org.springframework.aop.target.SingletonTargetSource;
027    
028    /**
029     * @author Raymond Aug??
030     */
031    public class ServiceBag<V> {
032    
033            public ServiceBag(
034                    ClassLoader classLoader, AdvisedSupport advisedSupport,
035                    Class<?> serviceTypeClass, final ServiceWrapper<V> serviceWrapper) {
036    
037                    _advisedSupport = advisedSupport;
038    
039                    Object previousService = serviceWrapper.getWrappedService();
040    
041                    if (!(previousService instanceof ServiceWrapper)) {
042                            ClassLoader portalClassLoader =
043                                    PortalClassLoaderUtil.getClassLoader();
044    
045                            previousService = ProxyUtil.newProxyInstance(
046                                    portalClassLoader, new Class<?>[] {serviceTypeClass},
047                                            new ClassLoaderBeanHandler(
048                                                    previousService, portalClassLoader));
049    
050                            serviceWrapper.setWrappedService((V)previousService);
051                    }
052    
053                    Object nextTarget = ProxyUtil.newProxyInstance(
054                            classLoader,
055                            new Class<?>[] {serviceTypeClass, ServiceWrapper.class},
056                            new ClassLoaderBeanHandler(serviceWrapper, classLoader));
057    
058                    TargetSource nextTargetSource = new SingletonTargetSource(nextTarget) {
059    
060                            @Override
061                            public Class<?> getTargetClass() {
062                                    return serviceWrapper.getClass();
063                            }
064    
065                    };
066    
067                    _advisedSupport.setTargetSource(nextTargetSource);
068    
069                    _serviceWrapper = (ServiceWrapper<?>)nextTarget;
070            }
071    
072            @SuppressWarnings("unchecked")
073            public <T> void replace() throws Exception {
074                    TargetSource targetSource = _advisedSupport.getTargetSource();
075    
076                    Object currentService = targetSource.getTarget();
077                    ServiceWrapper<T> previousService = null;
078    
079                    // Loop through services
080    
081                    while (true) {
082    
083                            // A matching service was found
084    
085                            if (currentService == _serviceWrapper) {
086                                    Object wrappedService = _serviceWrapper.getWrappedService();
087    
088                                    if (previousService == null) {
089    
090                                            // There is no previous service, so we need to unwrap the
091                                            // portal class loader bean handler and change the target
092                                            // source
093    
094                                            if (!(wrappedService instanceof ServiceWrapper) &&
095                                                    ProxyUtil.isProxyClass(wrappedService.getClass())) {
096    
097                                                    InvocationHandler invocationHandler =
098                                                            ProxyUtil.getInvocationHandler(wrappedService);
099    
100                                                    if (invocationHandler instanceof
101                                                                    ClassLoaderBeanHandler) {
102    
103                                                            ClassLoaderBeanHandler classLoaderBeanHandler =
104                                                                    (ClassLoaderBeanHandler)invocationHandler;
105    
106                                                            wrappedService = classLoaderBeanHandler.getBean();
107                                                    }
108                                            }
109    
110                                            TargetSource previousTargetSource =
111                                                    new SingletonTargetSource(wrappedService);
112    
113                                            _advisedSupport.setTargetSource(previousTargetSource);
114                                    }
115                                    else {
116    
117                                            // Take ourselves out of the chain by setting our
118                                            // wrapped service as the previous without changing the
119                                            // target source
120    
121                                            previousService.setWrappedService((T)wrappedService);
122                                    }
123    
124                                    break;
125                            }
126    
127                            // Every item in the chain is a ServiceWrapper except the original
128                            // service
129    
130                            if (!(currentService instanceof ServiceWrapper)) {
131                                    break;
132                            }
133    
134                            // Check the next service because no matching service was found
135    
136                            previousService = (ServiceWrapper<T>)currentService;
137    
138                            currentService = previousService.getWrappedService();
139                    }
140            }
141    
142            private final AdvisedSupport _advisedSupport;
143            private final ServiceWrapper<?> _serviceWrapper;
144    
145    }