001    /**
002     * Copyright (c) 2000-2012 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.security.ac;
016    
017    import com.liferay.portal.spring.aop.AnnotationChainableMethodAdvice;
018    
019    import java.lang.annotation.Annotation;
020    import java.lang.reflect.Method;
021    
022    import org.aopalliance.intercept.MethodInvocation;
023    
024    /**
025     * @author Tomas Polesovsky
026     * @author Igor Spasic
027     * @author Michael C. Han
028     * @author Raymond Augé
029     * @author Shuyang Zhou
030     */
031    public class AccessControlAdvice extends
032            AnnotationChainableMethodAdvice<AccessControlled> {
033    
034            @Override
035            public Object before(MethodInvocation methodInvocation) throws Throwable {
036                    AccessControlled accessControlled = findAnnotation(methodInvocation);
037    
038                    if (accessControlled == _nullAccessControlled) {
039                            return null;
040                    }
041    
042                    boolean remoteAccess = AccessControlThreadLocal.isRemoteAccess();
043    
044                    if (remoteAccess) {
045                            Method targetMethod = methodInvocation.getMethod();
046    
047                            _accessControlAdvisor.accept(targetMethod, accessControlled);
048                    }
049    
050                    return null;
051            }
052    
053            @Override
054            public AccessControlled getNullAnnotation() {
055                    return _nullAccessControlled;
056            }
057    
058            public void setAccessControlAdvisor(
059                    AccessControlAdvisor accessControlAdvisor) {
060    
061                    _accessControlAdvisor = accessControlAdvisor;
062            }
063    
064            private static AccessControlled _nullAccessControlled =
065                    new AccessControlled() {
066    
067                    public Class<? extends Annotation> annotationType() {
068                            return AccessControlled.class;
069                    }
070    
071            };
072    
073            private AccessControlAdvisor _accessControlAdvisor;
074    
075    }