001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.tide.spring;
023
024import org.aspectj.lang.ProceedingJoinPoint;
025import org.aspectj.lang.annotation.Around;
026import org.aspectj.lang.annotation.Aspect;
027import org.granite.gravity.Gravity;
028import org.granite.tide.data.DataEnabled;
029import org.granite.tide.data.DataUpdatePostprocessor;
030import org.granite.util.ThrowableCallable;
031import org.springframework.beans.factory.InitializingBean;
032import org.springframework.beans.factory.annotation.Autowired;
033import org.springframework.core.Ordered;
034
035/**
036 * Spring AOP AspectJ aspect to handle publishing of data changes instead of relying on the default behaviour
037 * This can be used outside of a HTTP Granite context and inside the security/transaction context
038 *  
039 * @author William DRAI
040 */
041@Aspect
042public class TideDataPublishingAspect implements Ordered, InitializingBean {
043        
044        //private static final Logger log = Logger.getLogger(TideDataPublishingAspect.class);
045
046        private int order = 0;
047        private Gravity gravity;
048        private DataUpdatePostprocessor dataUpdatePostprocessor;
049
050    private TideDataPublishingWrapper tideDataPublishingWrapper = null;
051        
052        public void setGravity(Gravity gravity) {
053                this.gravity = gravity;
054        }
055
056    public void setTideDataPublishingWrapper(TideDataPublishingWrapper tideDataPublishingWrapper) {
057        this.tideDataPublishingWrapper = tideDataPublishingWrapper;
058    }
059
060        @Autowired(required=false)
061        public void setDataUpdatePostprocessor(DataUpdatePostprocessor dataUpdatePostprocessor) {
062                this.dataUpdatePostprocessor = dataUpdatePostprocessor;
063        }
064
065    public int getOrder() {
066        return order;
067    }
068    public void setOrder(int order) {
069        this.order = order;
070    }
071
072    @Override
073    public void afterPropertiesSet() throws Exception {
074        if (tideDataPublishingWrapper == null)
075            tideDataPublishingWrapper = new TideDataPublishingWrapper(gravity, dataUpdatePostprocessor);
076    }
077
078        @Around("@within(dataEnabled)")
079    public Object invoke(final ProceedingJoinPoint pjp, DataEnabled dataEnabled) throws Throwable {
080        if (dataEnabled == null || !dataEnabled.useInterceptor())
081                return pjp.proceed();
082
083        return tideDataPublishingWrapper.execute(dataEnabled, new ThrowableCallable<Object>() {
084            @Override
085            public Object call() throws Throwable {
086                return pjp.proceed();
087            }
088        });
089    }
090}