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.kernel.bean.IdentifiableBean;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.nio.intraband.rpc.IntrabandRPCUtil;
021    import com.liferay.portal.kernel.resiliency.spi.SPI;
022    import com.liferay.portal.kernel.resiliency.spi.SPIRegistryUtil;
023    import com.liferay.portal.kernel.util.ClassLoaderPool;
024    import com.liferay.portal.kernel.util.MethodHandler;
025    import com.liferay.portal.security.ac.AccessControl;
026    import com.liferay.portal.security.ac.AccessControlThreadLocal;
027    import com.liferay.portal.security.ac.AccessControlled;
028    import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
029    
030    import org.aopalliance.intercept.MethodInvocation;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class PortalResiliencyAdvice
036            extends AnnotationChainableMethodAdvice<AccessControlled> {
037    
038            @Override
039            public Object before(MethodInvocation methodInvocation) throws Throwable {
040                    AccessControlled accessControlled = findAnnotation(methodInvocation);
041    
042                    if (accessControlled == AccessControl.NULL_ACCESS_CONTROLLED) {
043                            return null;
044                    }
045    
046                    boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
047    
048                    if (!remoteAccess) {
049                            return null;
050                    }
051    
052                    Object targetObject = methodInvocation.getThis();
053    
054                    if (targetObject == null) {
055                            return null;
056                    }
057    
058                    Class<?> targetClass = targetObject.getClass();
059    
060                    String servletContextName = ClassLoaderPool.getContextName(
061                            targetClass.getClassLoader());
062    
063                    SPI spi = SPIRegistryUtil.getServletContextSPI(servletContextName);
064    
065                    if (spi == null) {
066                            serviceBeanAopCacheManager.removeMethodInterceptor(
067                                    methodInvocation, this);
068    
069                            return null;
070                    }
071    
072                    if (!(targetObject instanceof IdentifiableBean)) {
073                            Class<?> clazz = targetObject.getClass();
074    
075                            _log.error(
076                                    "Unable to bridge portal resiliency call for " +
077                                            clazz.getName() + " because it does not implement " +
078                                                    IdentifiableBean.class.getName());
079    
080                            return null;
081                    }
082    
083                    IdentifiableBean identifiableBean = (IdentifiableBean)targetObject;
084    
085                    ServiceMethodProcessCallable serviceMethodProcessCallable =
086                            new ServiceMethodProcessCallable(
087                                    servletContextName, identifiableBean.getBeanIdentifier(),
088                                    new MethodHandler(
089                                            methodInvocation.getMethod(),
090                                            methodInvocation.getArguments()));
091    
092                    return IntrabandRPCUtil.execute(
093                            spi.getRegistrationReference(), serviceMethodProcessCallable);
094            }
095    
096            @Override
097            public AccessControlled getNullAnnotation() {
098                    return AccessControl.NULL_ACCESS_CONTROLLED;
099            }
100    
101            private static Log _log = LogFactoryUtil.getLog(
102                    PortalResiliencyAdvice.class);
103    
104    }