001
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
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
080
081 while (true) {
082
083
084
085 if (currentService == _serviceWrapper) {
086 Object wrappedService = _serviceWrapper.getWrappedService();
087
088 if (previousService == null) {
089
090
091
092
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
118
119
120
121 previousService.setWrappedService((T)wrappedService);
122 }
123
124 break;
125 }
126
127
128
129
130 if (!(currentService instanceof ServiceWrapper)) {
131 break;
132 }
133
134
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 }