001
014
015 package com.liferay.portal.resiliency.service;
016
017 import com.liferay.portal.kernel.module.framework.service.IdentifiableOSGiServiceInvokerUtil;
018 import com.liferay.portal.kernel.nio.intraband.rpc.IntrabandRPCUtil;
019 import com.liferay.portal.kernel.resiliency.spi.SPI;
020 import com.liferay.portal.kernel.resiliency.spi.SPIRegistryUtil;
021 import com.liferay.portal.kernel.security.access.control.AccessControl;
022 import com.liferay.portal.kernel.security.access.control.AccessControlThreadLocal;
023 import com.liferay.portal.kernel.security.access.control.AccessControlled;
024 import com.liferay.portal.kernel.util.ClassLoaderPool;
025 import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
026
027 import java.io.Serializable;
028
029 import java.lang.reflect.Method;
030
031 import java.util.concurrent.Future;
032
033 import org.aopalliance.intercept.MethodInvocation;
034
035
038 public class PortalResiliencyAdvice
039 extends AnnotationChainableMethodAdvice<AccessControlled> {
040
041 @Override
042 public Object before(MethodInvocation methodInvocation) throws Throwable {
043 AccessControlled accessControlled = findAnnotation(methodInvocation);
044
045 if (accessControlled == AccessControl.NULL_ACCESS_CONTROLLED) {
046 return null;
047 }
048
049 boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
050
051 if (!remoteAccess) {
052 return null;
053 }
054
055 Object targetObject = methodInvocation.getThis();
056
057 Class<?> targetClass = targetObject.getClass();
058
059 String servletContextName = ClassLoaderPool.getContextName(
060 targetClass.getClassLoader());
061
062 SPI spi = SPIRegistryUtil.getServletContextSPI(servletContextName);
063
064 if (spi == null) {
065 serviceBeanAopCacheManager.removeMethodInterceptor(
066 methodInvocation, this);
067
068 return null;
069 }
070
071 ServiceMethodProcessCallable serviceMethodProcessCallable =
072 new ServiceMethodProcessCallable(
073 IdentifiableOSGiServiceInvokerUtil.createMethodHandler(
074 methodInvocation.getThis(), methodInvocation.getMethod(),
075 methodInvocation.getArguments()));
076
077 Future<Serializable> future = IntrabandRPCUtil.execute(
078 spi.getRegistrationReference(), serviceMethodProcessCallable);
079
080 Object result = future.get();
081
082 Method method = methodInvocation.getMethod();
083
084 Class<?> returnType = method.getReturnType();
085
086 if (returnType == void.class) {
087 result = nullResult;
088 }
089
090 return result;
091 }
092
093 @Override
094 public AccessControlled getNullAnnotation() {
095 return AccessControl.NULL_ACCESS_CONTROLLED;
096 }
097
098 }