001    /**
002     * Copyright (c) 2000-2013 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.resiliency.service;
016    
017    import com.liferay.portal.bean.IdentifiableBeanInvokerUtil;
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.util.ClassLoaderPool;
022    import com.liferay.portal.security.ac.AccessControl;
023    import com.liferay.portal.security.ac.AccessControlThreadLocal;
024    import com.liferay.portal.security.ac.AccessControlled;
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    /**
036     * @author Shuyang Zhou
037     */
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                                    IdentifiableBeanInvokerUtil.createMethodHandler(
074                                            methodInvocation));
075    
076                    Future<Serializable> future = IntrabandRPCUtil.execute(
077                            spi.getRegistrationReference(), serviceMethodProcessCallable);
078    
079                    Object result = future.get();
080    
081                    Method method = methodInvocation.getMethod();
082    
083                    Class<?> returnType = method.getReturnType();
084    
085                    if (returnType == void.class) {
086                            result = nullResult;
087                    }
088    
089                    return result;
090            }
091    
092            @Override
093            public AccessControlled getNullAnnotation() {
094                    return AccessControl.NULL_ACCESS_CONTROLLED;
095            }
096    
097    }